From ad2d8910ec910809838ce9f462d50a35137ea939 Mon Sep 17 00:00:00 2001 From: irubido Date: Mon, 5 Feb 2024 16:16:58 +0100 Subject: [PATCH 01/10] feat: initLocalEnvironment --- ...ultichainHardhatRuntimeEnvironmentField.ts | 54 +++++++++++++++++-- packages/plugin/src/adapterABI.ts | 5 ++ 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts index 1e0ed5b..1711fd9 100644 --- a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts +++ b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts @@ -14,13 +14,18 @@ import { transferStatusInterval, mapNetworkArgs, } from "./utils"; -import { AdapterABI } from "./adapterABI"; +import { + AdapterABI, + AdapterBytecode, + CreateXABI, + CreateXBytecode, +} from "./adapterABI"; import { DeployOptions, DeploymentInfo, NetworkArguments } from "./types"; export class MultichainHardhatRuntimeEnvironmentField { private isInitiated: boolean = false; private domains: Domain[] = []; - private readonly web3: Web3 | null; + private readonly web3: Web3; public constructor(private readonly hre: HardhatRuntimeEnvironment) { const provider = this.hre.network.provider; @@ -48,6 +53,46 @@ export class MultichainHardhatRuntimeEnvironmentField { this.domains = config.getDomains(); this.isInitiated; + this.ADAPTER_ADDRESS = vars.get( + "ADAPTER_ADDRESS", + "0x85d62ad850b322152bf4ad9147bfbf097da42217" + ); + } + + public async initLocalEnvironment(): Promise { + const [deployer] = await this.web3.eth.getAccounts(); + + const gasMultiplier = this.hre.network.name.includes("arbi") ? 10 : 1; + const txOptionsAdapter = { gasLimit: 1900000 * gasMultiplier }; + const txOptionsCreateX = { gasLimit: 2700000 * gasMultiplier }; + + const createX = new this.web3.eth.Contract(CreateXABI); + const response = await createX + .deploy({ + data: CreateXBytecode, + }) + .send({ from: deployer, ...txOptionsCreateX }); + console.log(`CreateX locally deployed: ${response.options.address!}`); + + const deployerSalt = this.web3.utils.encodePacked( + ["bytes", "bytes", "bytes"], + [deployer, "0x00", "0x0000000000000000000000"] + ); + + const receipt = await createX.methods + .deployCreate3(deployerSalt, AdapterBytecode) + .send({ from: deployer, ...txOptionsAdapter }); + + const adapterAddress = receipt.events!.ContractCreation.returnValues + .newContract as string; + + this.ADAPTER_ADDRESS = adapterAddress; + + console.log( + `Adapter locally deployed: ${adapterAddress}` + + "\n" + + "Local environment initiated" + ); } /** @@ -80,7 +125,7 @@ export class MultichainHardhatRuntimeEnvironmentField { ): Promise<{ deploymentInfo: DeploymentInfo[]; receipt: Transaction; - } | void> { + }> { const artifact = this.hre.artifacts.readArtifactSync(contractName); return this.deployMultichainBytecode( @@ -126,9 +171,8 @@ export class MultichainHardhatRuntimeEnvironmentField { ): Promise<{ deploymentInfo: DeploymentInfo[]; receipt: Transaction; - } | void> { + }> { if (!this.isInitiated) await this.initConfig(); - if (!this.web3) return; //optional params const salt = options?.salt ?? utils.randomBytes(32); diff --git a/packages/plugin/src/adapterABI.ts b/packages/plugin/src/adapterABI.ts index bc7ca71..45f442f 100644 --- a/packages/plugin/src/adapterABI.ts +++ b/packages/plugin/src/adapterABI.ts @@ -1,3 +1,8 @@ import CrosschainDeployAdapter from "@chainsafe/hardhat-plugin-multichain-deploy-contracts/artifacts/contracts/CrosschainDeployAdapter.sol/CrosschainDeployAdapter"; +import CreateX from "@chainsafe/hardhat-plugin-multichain-deploy-contracts/artifacts/contracts/deps/CreateX.sol/CreateX"; export const AdapterABI = CrosschainDeployAdapter.abi; +export const AdapterBytecode = CrosschainDeployAdapter.bytecode; + +export const CreateXABI = CreateX.abi; +export const CreateXBytecode = CreateX.bytecode; From 58227bf231b28162c68141606d8fee3ae602f0b2 Mon Sep 17 00:00:00 2001 From: irubido Date: Mon, 5 Feb 2024 16:20:39 +0100 Subject: [PATCH 02/10] local adapter address variable --- .../plugin/src/MultichainHardhatRuntimeEnvironmentField.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts index 1711fd9..788ebe0 100644 --- a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts +++ b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts @@ -36,6 +36,7 @@ export class MultichainHardhatRuntimeEnvironmentField { "ADAPTER_ADDRESS", "0x85d62ad850b322152bf4ad9147bfbf097da42217" ); + public LOCAL_ADAPTER_ADDRESS = ""; //current Sygma hardcoded gasLimit private gasLimit = 1000000; @@ -53,10 +54,6 @@ export class MultichainHardhatRuntimeEnvironmentField { this.domains = config.getDomains(); this.isInitiated; - this.ADAPTER_ADDRESS = vars.get( - "ADAPTER_ADDRESS", - "0x85d62ad850b322152bf4ad9147bfbf097da42217" - ); } public async initLocalEnvironment(): Promise { @@ -86,7 +83,7 @@ export class MultichainHardhatRuntimeEnvironmentField { const adapterAddress = receipt.events!.ContractCreation.returnValues .newContract as string; - this.ADAPTER_ADDRESS = adapterAddress; + this.LOCAL_ADAPTER_ADDRESS = adapterAddress; console.log( `Adapter locally deployed: ${adapterAddress}` + From 7ffc20b0455a097004b367fb83afd2ee25582273 Mon Sep 17 00:00:00 2001 From: BeroBurny Date: Wed, 7 Feb 2024 14:31:41 +0100 Subject: [PATCH 03/10] small fix --- .../MultichainHardhatRuntimeEnvironmentField.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts index e560813..b96299d 100644 --- a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts +++ b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts @@ -35,7 +35,6 @@ export class MultichainHardhatRuntimeEnvironmentField { "ADAPTER_ADDRESS", "0x85d62ad850b322152bf4ad9147bfbf097da42217" ); - public LOCAL_ADAPTER_ADDRESS = ""; //current Sygma hardcoded gasLimit private gasLimit = 1000000; @@ -55,8 +54,8 @@ export class MultichainHardhatRuntimeEnvironmentField { this.isInitiated; } - public async initLocalEnvironment(): Promise { - const [deployer] = await this.web3.eth.getAccounts(); + public async initLocalEnvironment(account?: string): Promise { + if (!account) account = (await this.web3.eth.getAccounts())[0] const gasMultiplier = this.hre.network.name.includes("arbi") ? 10 : 1; const txOptionsAdapter = { gasLimit: 1900000 * gasMultiplier }; @@ -67,28 +66,28 @@ export class MultichainHardhatRuntimeEnvironmentField { .deploy({ data: CreateXBytecode, }) - .send({ from: deployer, ...txOptionsCreateX }); + .send({ from: account, ...txOptionsCreateX }); console.log(`CreateX locally deployed: ${response.options.address!}`); const deployerSalt = this.web3.utils.encodePacked( ["bytes", "bytes", "bytes"], - [deployer, "0x00", "0x0000000000000000000000"] + [account, "0x00", "0x0000000000000000000000"] ); const receipt = await createX.methods .deployCreate3(deployerSalt, AdapterBytecode) - .send({ from: deployer, ...txOptionsAdapter }); + .send({ from: account, ...txOptionsAdapter }); const adapterAddress = receipt.events!.ContractCreation.returnValues .newContract as string; - this.LOCAL_ADAPTER_ADDRESS = adapterAddress; - console.log( `Adapter locally deployed: ${adapterAddress}` + "\n" + "Local environment initiated" ); + + return adapterAddress; } /** From 154b29776f12f9ac7fe902b18d124a042afcc498 Mon Sep 17 00:00:00 2001 From: BeroBurny Date: Wed, 7 Feb 2024 17:05:28 +0100 Subject: [PATCH 04/10] fix missing parts --- ...ultichainHardhatRuntimeEnvironmentField.ts | 63 +++++++++++++++---- packages/plugin/src/adapterABI.ts | 8 +++ 2 files changed, 60 insertions(+), 11 deletions(-) diff --git a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts index b96299d..e45a434 100644 --- a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts +++ b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts @@ -14,6 +14,10 @@ import { AdapterBytecode, CreateXABI, CreateXBytecode, + MockBridgeABI, + MockBridgeBytecode, + MockFeeHandlerABI, + MockFeeHandlerBytecode, } from "./adapterABI"; import { DeployOptions, @@ -54,29 +58,66 @@ export class MultichainHardhatRuntimeEnvironmentField { this.isInitiated; } - public async initLocalEnvironment(account?: string): Promise { - if (!account) account = (await this.web3.eth.getAccounts())[0] + public async initLocalEnvironment(deployer?: string): Promise { + // Assign default values if is not provided + if (!deployer) deployer = (await this.web3.eth.getAccounts())[0]; + + const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"; + + /** Deploy Mock Sygma Bridge */ + const DOMAIN_ID = BigInt(10); + + const feeHandler = new this.web3.eth.Contract(MockFeeHandlerABI); + const feeHandlerResponse = await feeHandler + .deploy({ data: MockFeeHandlerBytecode }) + .send({ from: deployer }); + + const bridge = new this.web3.eth.Contract(MockBridgeABI); + const bridgeResponse = await bridge + .deploy({ + data: MockBridgeBytecode, + arguments: [ + deployer, + feeHandlerResponse.options.address || ZERO_ADDRESS, + DOMAIN_ID, + ], + }) + .send({ from: deployer }); + + /** Deploy Adapter */ + const RESOURCE_ID = + "0x000000000000000000000000000000000000000000000000000000000000cafe"; const gasMultiplier = this.hre.network.name.includes("arbi") ? 10 : 1; const txOptionsAdapter = { gasLimit: 1900000 * gasMultiplier }; const txOptionsCreateX = { gasLimit: 2700000 * gasMultiplier }; const createX = new this.web3.eth.Contract(CreateXABI); - const response = await createX + const createXResponse = await createX .deploy({ data: CreateXBytecode, }) - .send({ from: account, ...txOptionsCreateX }); - console.log(`CreateX locally deployed: ${response.options.address!}`); - - const deployerSalt = this.web3.utils.encodePacked( - ["bytes", "bytes", "bytes"], - [account, "0x00", "0x0000000000000000000000"] + .send({ from: deployer, ...txOptionsCreateX }); + console.log( + `CreateX locally deployed: ${createXResponse.options.address!}` ); + const adapter = new this.web3.eth.Contract(AdapterABI); + const adapterEncodedAbi = adapter + .deploy({ + data: AdapterBytecode, + arguments: [ + createXResponse.options.address || ZERO_ADDRESS, + bridgeResponse.options.address || ZERO_ADDRESS, + RESOURCE_ID, + ], + }) + .encodeABI(); + + const salt = `${deployer}000000000000000000000000`; const receipt = await createX.methods - .deployCreate3(deployerSalt, AdapterBytecode) - .send({ from: account, ...txOptionsAdapter }); + .deployCreate3(salt, adapterEncodedAbi) + .send({ from: deployer, ...txOptionsAdapter }); const adapterAddress = receipt.events!.ContractCreation.returnValues .newContract as string; diff --git a/packages/plugin/src/adapterABI.ts b/packages/plugin/src/adapterABI.ts index 45f442f..1cc54cf 100644 --- a/packages/plugin/src/adapterABI.ts +++ b/packages/plugin/src/adapterABI.ts @@ -1,8 +1,16 @@ import CrosschainDeployAdapter from "@chainsafe/hardhat-plugin-multichain-deploy-contracts/artifacts/contracts/CrosschainDeployAdapter.sol/CrosschainDeployAdapter"; import CreateX from "@chainsafe/hardhat-plugin-multichain-deploy-contracts/artifacts/contracts/deps/CreateX.sol/CreateX"; +import MockFeeHandler from "@chainsafe/hardhat-plugin-multichain-deploy-contracts/artifacts/contracts/mocks/MockFeeHandler.sol/MockFeeHandler"; +import MockBridge from "@chainsafe/hardhat-plugin-multichain-deploy-contracts/artifacts/contracts/mocks/MockBridge.sol/MockBridge"; export const AdapterABI = CrosschainDeployAdapter.abi; export const AdapterBytecode = CrosschainDeployAdapter.bytecode; export const CreateXABI = CreateX.abi; export const CreateXBytecode = CreateX.bytecode; + +export const MockFeeHandlerABI = MockFeeHandler.abi; +export const MockFeeHandlerBytecode = MockFeeHandler.bytecode; + +export const MockBridgeABI = MockBridge.abi; +export const MockBridgeBytecode = MockBridge.bytecode; From c3290a29b95f1116f2dc1dd889315571d8a3ab4a Mon Sep 17 00:00:00 2001 From: BeroBurny Date: Thu, 8 Feb 2024 10:50:46 +0100 Subject: [PATCH 05/10] mini refactor --- ...ultichainHardhatRuntimeEnvironmentField.ts | 32 ++++++++++--------- packages/plugin/src/types.ts | 7 ++++ 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts index e45a434..2f70d5b 100644 --- a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts +++ b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts @@ -23,6 +23,7 @@ import { DeployOptions, NetworkArguments, DeployMultichainResponse, + DeployedLocalEnvironmentContracts, } from "./types"; export class MultichainHardhatRuntimeEnvironmentField { @@ -58,7 +59,9 @@ export class MultichainHardhatRuntimeEnvironmentField { this.isInitiated; } - public async initLocalEnvironment(deployer?: string): Promise { + public async initLocalEnvironment( + deployer?: string + ): Promise { // Assign default values if is not provided if (!deployer) deployer = (await this.web3.eth.getAccounts())[0]; @@ -71,18 +74,17 @@ export class MultichainHardhatRuntimeEnvironmentField { const feeHandlerResponse = await feeHandler .deploy({ data: MockFeeHandlerBytecode }) .send({ from: deployer }); + const feeHandlerAddress = + feeHandlerResponse.options.address || ZERO_ADDRESS; const bridge = new this.web3.eth.Contract(MockBridgeABI); const bridgeResponse = await bridge .deploy({ data: MockBridgeBytecode, - arguments: [ - deployer, - feeHandlerResponse.options.address || ZERO_ADDRESS, - DOMAIN_ID, - ], + arguments: [deployer, feeHandlerAddress, DOMAIN_ID], }) .send({ from: deployer }); + const bridgeAddress = bridgeResponse.options.address || ZERO_ADDRESS; /** Deploy Adapter */ const RESOURCE_ID = @@ -98,19 +100,14 @@ export class MultichainHardhatRuntimeEnvironmentField { data: CreateXBytecode, }) .send({ from: deployer, ...txOptionsCreateX }); - console.log( - `CreateX locally deployed: ${createXResponse.options.address!}` - ); + const createXAddress = createXResponse.options.address || ZERO_ADDRESS; + console.log(`CreateX locally deployed: ${createXAddress}`); const adapter = new this.web3.eth.Contract(AdapterABI); const adapterEncodedAbi = adapter .deploy({ data: AdapterBytecode, - arguments: [ - createXResponse.options.address || ZERO_ADDRESS, - bridgeResponse.options.address || ZERO_ADDRESS, - RESOURCE_ID, - ], + arguments: [createXAddress, bridgeAddress, RESOURCE_ID], }) .encodeABI(); @@ -128,7 +125,12 @@ export class MultichainHardhatRuntimeEnvironmentField { "Local environment initiated" ); - return adapterAddress; + return { + adapter: adapterAddress, + createX: createXAddress, + bridge: bridgeAddress, + feeHandler: feeHandlerAddress, + }; } /** diff --git a/packages/plugin/src/types.ts b/packages/plugin/src/types.ts index 99d75f8..5cd0fa4 100644 --- a/packages/plugin/src/types.ts +++ b/packages/plugin/src/types.ts @@ -40,3 +40,10 @@ export interface DeployMultichainResponse { domainIDs: bigint[]; transactionHash: HexString; } + +export interface DeployedLocalEnvironmentContracts { + createX: string; + adapter: string; + feeHandler: string; + bridge: string; +} From 78fab11abadc532e43d523ec118f21cf67439ee2 Mon Sep 17 00:00:00 2001 From: BeroBurny Date: Thu, 8 Feb 2024 11:51:50 +0100 Subject: [PATCH 06/10] d1 --- .../MultichainHardhatRuntimeEnvironmentField.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts index 2f70d5b..90537a7 100644 --- a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts +++ b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts @@ -59,6 +59,22 @@ export class MultichainHardhatRuntimeEnvironmentField { this.isInitiated; } + /** + * Initializes the local development environment by deploying mock contract require for testing contract interaction with Sygma Bridge via Adapter contracts. + * This setup is crucial for local testing and simulation of the deployment process in a controlled environment. + * + * @param [deployer] - Optional. The address of the deployer account. If not provided, the first account from accounts is used. + * @returns A promise that resolves to an object containing the addresses of the deployed contracts: adapter, createX, bridge, and feeHandler. + * + * @example + * const { adapterAddress } = await initLocalEnvironment(); + * + * const options = { + * salt: "0xcafe00000000000000000000000000000000000000000000000000000000cafe", + * adapterAddress, + * }; + * this.hre.multichain.deployMultichain("HelloContract", networkArgs, options); + */ public async initLocalEnvironment( deployer?: string ): Promise { From 57c14eef9807d3c610910633be3577963c22aef6 Mon Sep 17 00:00:00 2001 From: BeroBurny Date: Thu, 8 Feb 2024 13:06:04 +0100 Subject: [PATCH 07/10] d2 --- .../src/MultichainHardhatRuntimeEnvironmentField.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts index 90537a7..159618a 100644 --- a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts +++ b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts @@ -60,11 +60,11 @@ export class MultichainHardhatRuntimeEnvironmentField { } /** - * Initializes the local development environment by deploying mock contract require for testing contract interaction with Sygma Bridge via Adapter contracts. - * This setup is crucial for local testing and simulation of the deployment process in a controlled environment. + * Initializes the local development environment by deploying mock contracts required for testing contract interactions with the Sygma Bridge via Adapter contracts. + * This method is crucial for setting up a local testing environment that simulates the deployment process and interactions in a controlled manner, mirroring real-world operations with the Sygma Bridge. * - * @param [deployer] - Optional. The address of the deployer account. If not provided, the first account from accounts is used. - * @returns A promise that resolves to an object containing the addresses of the deployed contracts: adapter, createX, bridge, and feeHandler. + * @param deployer - Optional. The Ethereum address of the deployer account. If not specified, the method uses the first account from the list returned by accounts. This account is responsible for deploying the mock contracts. + * @returns A `Promise` that resolves to an object containing the addresses of the deployed mock contracts: `adapter`, `createX`, `bridge`, and `feeHandler`. These addresses can be utilized in further development or testing activities to interact with the contracts. * * @example * const { adapterAddress } = await initLocalEnvironment(); @@ -73,7 +73,7 @@ export class MultichainHardhatRuntimeEnvironmentField { * salt: "0xcafe00000000000000000000000000000000000000000000000000000000cafe", * adapterAddress, * }; - * this.hre.multichain.deployMultichain("HelloContract", networkArgs, options); + * await this.hre.multichain.deployMultichain("HelloContract", networkArgs, options); */ public async initLocalEnvironment( deployer?: string From 1ad3024686f1c03d8532e7c04489c6f7c910da56 Mon Sep 17 00:00:00 2001 From: BeroBurny Date: Thu, 8 Feb 2024 13:06:17 +0100 Subject: [PATCH 08/10] micro refactor --- .../src/MultichainHardhatRuntimeEnvironmentField.ts | 8 ++++---- packages/plugin/src/types.ts | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts index 159618a..f737917 100644 --- a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts +++ b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts @@ -142,10 +142,10 @@ export class MultichainHardhatRuntimeEnvironmentField { ); return { - adapter: adapterAddress, - createX: createXAddress, - bridge: bridgeAddress, - feeHandler: feeHandlerAddress, + adapterAddress, + createXAddress, + bridgeAddress, + feeHandlerAddress, }; } diff --git a/packages/plugin/src/types.ts b/packages/plugin/src/types.ts index 5cd0fa4..a1da71d 100644 --- a/packages/plugin/src/types.ts +++ b/packages/plugin/src/types.ts @@ -42,8 +42,8 @@ export interface DeployMultichainResponse { } export interface DeployedLocalEnvironmentContracts { - createX: string; - adapter: string; - feeHandler: string; - bridge: string; + createXAddress: string; + adapterAddress: string; + feeHandlerAddress: string; + bridgeAddress: string; } From 2f2f0eec374f1af12e90e2c629b63eed650dc189 Mon Sep 17 00:00:00 2001 From: BeroBurny Date: Thu, 8 Feb 2024 13:10:52 +0100 Subject: [PATCH 09/10] d3 --- .../plugin/src/MultichainHardhatRuntimeEnvironmentField.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts index f737917..c88678a 100644 --- a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts +++ b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts @@ -60,11 +60,10 @@ export class MultichainHardhatRuntimeEnvironmentField { } /** - * Initializes the local development environment by deploying mock contracts required for testing contract interactions with the Sygma Bridge via Adapter contracts. - * This method is crucial for setting up a local testing environment that simulates the deployment process and interactions in a controlled manner, mirroring real-world operations with the Sygma Bridge. + * Initializes the local development environment by deploying mock contracts necessary for testing interactions with the Sygma Bridge through Adapter contracts. This setup is vital for developers aiming to simulate the deployment process and contract interactions within a local and controlled environment, closely replicating interactions with the Sygma Bridge in production. * - * @param deployer - Optional. The Ethereum address of the deployer account. If not specified, the method uses the first account from the list returned by accounts. This account is responsible for deploying the mock contracts. - * @returns A `Promise` that resolves to an object containing the addresses of the deployed mock contracts: `adapter`, `createX`, `bridge`, and `feeHandler`. These addresses can be utilized in further development or testing activities to interact with the contracts. + * @param deployer - Optional. The Ethereum address of the deployer account. If not provided, the method defaults to using the first available account. This account is tasked with deploying the mock contracts and is essential for setting up the local testing environment. + * @returns A `Promise` that resolves to an object containing the addresses of the deployed mock contracts. These addresses are crucial for conducting further development or testing, enabling comprehensive interaction with the contracts. * * @example * const { adapterAddress } = await initLocalEnvironment(); From 322624a2b89b7a5cae8f749600ae316f06257eab Mon Sep 17 00:00:00 2001 From: BeroBurny Date: Thu, 8 Feb 2024 14:41:44 +0100 Subject: [PATCH 10/10] tests --- ...ultichainHardhatRuntimeEnvironmentField.ts | 21 +++++-------------- .../hardhat-localhost/hardhat.config.ts | 15 +++++++++++++ packages/plugin/test/helpers.ts | 7 +++++++ packages/plugin/test/project.test.ts | 18 +++++++++++++++- 4 files changed, 44 insertions(+), 17 deletions(-) create mode 100644 packages/plugin/test/fixture-projects/hardhat-localhost/hardhat.config.ts diff --git a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts index c88678a..3cdd80b 100644 --- a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts +++ b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts @@ -36,7 +36,7 @@ export class MultichainHardhatRuntimeEnvironmentField { this.web3 = new Web3(provider); } - public ADAPTER_ADDRESS = vars.get( + private ADAPTER_ADDRESS = vars.get( "ADAPTER_ADDRESS", "0x85d62ad850b322152bf4ad9147bfbf097da42217" ); @@ -105,34 +105,23 @@ export class MultichainHardhatRuntimeEnvironmentField { const RESOURCE_ID = "0x000000000000000000000000000000000000000000000000000000000000cafe"; - const gasMultiplier = this.hre.network.name.includes("arbi") ? 10 : 1; - const txOptionsAdapter = { gasLimit: 1900000 * gasMultiplier }; - const txOptionsCreateX = { gasLimit: 2700000 * gasMultiplier }; - const createX = new this.web3.eth.Contract(CreateXABI); const createXResponse = await createX .deploy({ data: CreateXBytecode, }) - .send({ from: deployer, ...txOptionsCreateX }); + .send({ from: deployer }); const createXAddress = createXResponse.options.address || ZERO_ADDRESS; console.log(`CreateX locally deployed: ${createXAddress}`); const adapter = new this.web3.eth.Contract(AdapterABI); - const adapterEncodedAbi = adapter + const adapterResponse = await adapter .deploy({ data: AdapterBytecode, arguments: [createXAddress, bridgeAddress, RESOURCE_ID], }) - .encodeABI(); - - const salt = `${deployer}000000000000000000000000`; - const receipt = await createX.methods - .deployCreate3(salt, adapterEncodedAbi) - .send({ from: deployer, ...txOptionsAdapter }); - - const adapterAddress = receipt.events!.ContractCreation.returnValues - .newContract as string; + .send({ from: deployer }); + const adapterAddress = adapterResponse.options.address || ZERO_ADDRESS; console.log( `Adapter locally deployed: ${adapterAddress}` + diff --git a/packages/plugin/test/fixture-projects/hardhat-localhost/hardhat.config.ts b/packages/plugin/test/fixture-projects/hardhat-localhost/hardhat.config.ts new file mode 100644 index 0000000..98cb05e --- /dev/null +++ b/packages/plugin/test/fixture-projects/hardhat-localhost/hardhat.config.ts @@ -0,0 +1,15 @@ +// We load the plugin here. +import { HardhatUserConfig } from "hardhat/types"; +import { Environment } from "@buildwithsygma/sygma-sdk-core"; + +import "../../../src/index"; + +const config: HardhatUserConfig = { + solidity: "0.7.3", + defaultNetwork: "hardhat", + multichain: { + environment: Environment.TESTNET, + }, +}; + +export default config; diff --git a/packages/plugin/test/helpers.ts b/packages/plugin/test/helpers.ts index 34d44c0..79bf836 100644 --- a/packages/plugin/test/helpers.ts +++ b/packages/plugin/test/helpers.ts @@ -1,6 +1,7 @@ import path from "path"; import { resetHardhatContext } from "hardhat/plugins-testing"; import { HardhatRuntimeEnvironment } from "hardhat/types"; +import { TASK_NODE } from "hardhat/builtin-tasks/task-names"; declare module "mocha" { interface Context { @@ -19,3 +20,9 @@ export function useEnvironment(fixtureProjectName: string) { resetHardhatContext(); }); } + +export function useHardhatNode() { + beforeEach("Loading hardhat node", function () { + this.hre.run(TASK_NODE); + }); +} diff --git a/packages/plugin/test/project.test.ts b/packages/plugin/test/project.test.ts index ff1cfa7..60d38c3 100644 --- a/packages/plugin/test/project.test.ts +++ b/packages/plugin/test/project.test.ts @@ -5,7 +5,7 @@ import chaiAsPromised from "chai-as-promised"; import { Environment } from "@buildwithsygma/sygma-sdk-core"; import { MultichainHardhatRuntimeEnvironmentField } from "../src/MultichainHardhatRuntimeEnvironmentField"; -import { useEnvironment } from "./helpers"; +import { useEnvironment, useHardhatNode } from "./helpers"; use(chaiAsPromised); @@ -35,4 +35,20 @@ describe("Integration tests examples", function () { describe("Hardhat Runtime Environment extension", function () { useEnvironment("hardhat-project"); }); + + describe("Hardhat Runtime Environment extension - initLocalEnvironment", function () { + useEnvironment("hardhat-localhost"); + useHardhatNode(); + + it("Should deploy all required contracts on testnet", async function () { + const addresses = await this.hre.multichain.initLocalEnvironment(); + + assert.deepEqual(addresses, { + adapterAddress: "0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9", + createXAddress: "0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0", + bridgeAddress: "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512", + feeHandlerAddress: "0x5FbDB2315678afecb367f032d93F642f64180aa3", + }); + }); + }); });