Skip to content

Commit

Permalink
Merge branch 'contracts-deployer' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Joachim-Lebrun authored Feb 21, 2024
2 parents b345f92 + b3893f3 commit 38ed737
Show file tree
Hide file tree
Showing 9 changed files with 694 additions and 53 deletions.
2 changes: 2 additions & 0 deletions contracts/_testContracts/OIDImports.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pragma solidity 0.8.17;

import "@onchain-id/solidity/contracts/ClaimIssuer.sol";
import "@onchain-id/solidity/contracts/Identity.sol";
import "@onchain-id/solidity/contracts/factory/IdFactory.sol";
import "@onchain-id/solidity/contracts/gateway/Gateway.sol";
import "@onchain-id/solidity/contracts/proxy/ImplementationAuthority.sol";

contract OIDImports {
Expand Down
128 changes: 128 additions & 0 deletions contracts/factory/ContractsDeployer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// SPDX-License-Identifier: GPL-3.0
//
// :+#####%%%%%%%%%%%%%%+
// .-*@@@%+.:+%@@@@@%%#***%@@%=
// :=*%@@@#=. :#@@% *@@@%=
// .-+*%@%*-.:+%@@@@@@+. -*+: .=#. :%@@@%-
// :=*@@@@%%@@@@@@@@@%@@@- .=#@@@%@%= =@@@@#.
// -=+#%@@%#*=:. :%@@@@%. -*@@#*@@@@@@@#=:- *@@@@+
// =@@%=:. :=: *@@@@@%#- =%*%@@@@#+-. =+ :%@@@%-
// -@@%. .+@@@ =+=-. @@#- +@@@%- =@@@@%:
// :@@@. .+@@#%: : .=*=-::.-%@@@+*@@= +@@@@#.
// %@@: +@%%* =%@@@@@@@@@@@#. .*@%- +@@@@*.
// #@@= .+@@@@%:=*@@@@@- :%@%: .*@@@@+
// *@@* +@@@#-@@%-:%@@* +@@#. :%@@@@-
// -@@% .:-=++*##%%%@@@@@@@@@@@@*. :@+.@@@%: .#@@+ =@@@@#:
// .@@@*-+*#%%%@@@@@@@@@@@@@@@@%%#**@@%@@@. *@=*@@# :#@%= .#@@@@#-
// -%@@@@@@@@@@@@@@@*+==-:-@@@= *@# .#@*-=*@@@@%= -%@@@* =@@@@@%-
// -+%@@@#. %@%%= -@@:+@: -@@* *@@*-:: -%@@%=. .*@@@@@#
// *@@@* +@* *@@##@@- #@*@@+ -@@= . :+@@@#: .-+@@@%+-
// +@@@%*@@:..=@@@@* .@@@* .#@#. .=+- .=%@@@*. :+#@@@@*=:
// =@@@@%@@@@@@@@@@@@@@@@@@@@@@%- :+#*. :*@@@%=. .=#@@@@%+:
// .%@@= ..... .=#@@+. .#@@@*: -*%@@@@%+.
// +@@#+===---:::... .=%@@*- +@@@+. -*@@@@@%+.
// -@@@@@@@@@@@@@@@@@@@@@@%@@@@= -@@@+ -#@@@@@#=.
// ..:::---===+++***###%%%@@@#- .#@@+ -*@@@@@#=.
// @@@@@@+. +@@*. .+@@@@@%=.
// -@@@@@= =@@%: -#@@@@%+.
// +@@@@@. =@@@= .+@@@@@*:
// #@@@@#:%@@#. :*@@@@#-
// @@@@@%@@@= :#@@@@+.
// :@@@@@@@#.:#@@@%-
// +@@@@@@-.*@@@*:
// #@@@@#.=@@@+.
// @@@@+-%@%=
// :@@@#%@%=
// +@@@@%-
// :#%%=
//
/**
* NOTICE
*
* The T-REX software is licensed under a proprietary license or the GPL v.3.
* If you choose to receive it under the GPL v.3 license, the following applies:
* T-REX is a suite of smart contracts implementing the ERC-3643 standard and
* developed by Tokeny to manage and transfer financial assets on EVM blockchains
*
* Copyright (C) 2023, Tokeny sàrl.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
pragma solidity 0.8.17;

import "../roles/AgentRole.sol";

/// @notice Error thrown when a contract with the same name has already been deployed.
/// @param addr The address of the previously deployed contract.
error ContractDeployedAlready(address addr);

contract ContractsDeployer is AgentRole {

/// @notice Maps a human-readable name to the address of a deployed contract.
/// @dev Used to retrieve contract addresses deployed by this deployer.
mapping(string => address) private _deployedContracts;

/// @notice Emitted when a contract is deployed.
/// @param name The human-readable name of the deployed contract.
/// @param contractAddress The address of the deployed contract.
event ContractDeployed(string name, address contractAddress);


/**
* @dev Deploys a contract using the create2 opcode, ensuring deterministic address generation.
* @param name A human-readable name for the contract, used for referencing in the deployedContracts mapping.
* @param bytecode The bytecode of the contract to be deployed.
* @return addr The address of the deployed contract.
* @notice The function will revert with `ContractDeployedAlready` if a contract with the same name has been deployed.
*/
function deployContract(string memory name, bytes memory bytecode) external onlyAgent returns (address) {
bytes32 salt = keccak256(bytecode);
if (_deployedContracts[name] != address(0)) {
revert ContractDeployedAlready(_deployedContracts[name]);
}

address addr;
// solhint-disable-next-line no-inline-assembly
assembly {
let encoded_data := add(0x20, bytecode) // Load initialization code.
let encoded_size := mload(bytecode) // Load init code's length.
addr := create2(0, encoded_data, encoded_size, salt)
if iszero(extcodesize(addr)) {
revert(0, 0)
}
}
_deployedContracts[name] = addr;
emit ContractDeployed(name, addr);
return addr;
}

/**
* @dev Transfers the ownership of a contract to a new owner.
* @param _contract The address of the contract whose ownership is to be transferred.
* @param _owner The address of the new owner.
* @notice This function can only be called by an agent.
*/
function recoverContractOwnership(address _contract, address _owner) external onlyAgent {
Ownable(_contract).transferOwnership(_owner);
}

/**
* @dev Retrieves the address of a deployed contract by its name.
* @param name The name of the contract.
* @return The address of the deployed contract.
*/
function getContract(string calldata name) external view returns (address) {
return _deployedContracts[name];
}
}
21 changes: 20 additions & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { HardhatUserConfig } from 'hardhat/config';
import 'solidity-coverage';
import '@nomiclabs/hardhat-solhint';
import '@primitivefi/hardhat-dodoc';
import '@nomiclabs/hardhat-etherscan';
import * as dotenv from 'dotenv';

dotenv.config();

const config: HardhatUserConfig = {
solidity: {
Expand All @@ -21,9 +25,24 @@ const config: HardhatUserConfig = {
dodoc: {
runOnCompile: false,
debugMode: true,
outputDir: "./docgen",
outputDir: './docgen',
freshOutput: true,
},
networks: {
mumbai: {
url: process.env.MUMBAI_RPC_URL,
accounts: [`0x${process.env.DEPLOYER_PRIVATE_KEY}`],
},
polygon: {
url: process.env.POLYGON_RPC_URL,
accounts: [`0x${process.env.DEPLOYER_PRIVATE_KEY}`],
},
},
etherscan: {
apiKey: {
polygonMumbai: process.env.POLYGONSCAN_API_KEY,
},
},
};

export default config;
5 changes: 4 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type ContractJSON = {
bytecode: string;
deployedBytecode: string;
linkReferences: any;
}
};

export namespace contracts {
// Token
Expand Down Expand Up @@ -48,6 +48,9 @@ export namespace contracts {
export const TREXFactory: ContractJSON;
// gateway
export const TREXGateway: ContractJSON;
// contractsDeployer

export const ContractsDeployer: ContractJSON;
// DVD
export const DVDTransferManager: ContractJSON;
// DVA
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const TREXFactory = require('./artifacts/contracts/factory/TREXFactory.sol/TREXF
// gateway
const ITREXGateway = require('./artifacts/contracts/factory/ITREXGateway.sol/ITREXGateway.json');
const TREXGateway = require('./artifacts/contracts/factory/TREXGateway.sol/TREXGateway.json');
// contractsDeployer
const ContractsDeployer = require('./artifacts/contracts/factory/ContractsDeployer.sol/ContractsDeployer.json');
// DVD
const DVDTransferManager = require('./artifacts/contracts/DVD/DVDTransferManager.sol/DVDTransferManager.json');
// DVA
Expand Down Expand Up @@ -112,6 +114,8 @@ module.exports = {
TREXFactory,
// gateway
TREXGateway,
// contractsDeployer
ContractsDeployer,
// DVD
DVDTransferManager,
// DVA
Expand Down
44 changes: 23 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 38ed737

Please sign in to comment.