From 7d45fd75ce69a3d1b22c10476bbef2eb6e367a52 Mon Sep 17 00:00:00 2001 From: Priya Narayanaswamy Date: Mon, 2 Dec 2024 21:05:30 +0800 Subject: [PATCH 1/7] fix: race condition of buttons being enabled too soon before contracts are deployed --- package.json | 2 +- src/index.js | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index a65b0f6a..3a877d3d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@metamask/test-dapp", - "version": "8.13.0", + "version": "8.13.1", "description": "A simple dapp used in MetaMask e2e tests.", "engines": { "node": ">= 18.0.0" diff --git a/src/index.js b/src/index.js index 40108a23..aca625e7 100644 --- a/src/index.js +++ b/src/index.js @@ -379,6 +379,8 @@ const ensInput = document.getElementById('ensInput'); const ensSubmit = document.getElementById('ensSubmit'); const ensResult = document.getElementById('ensResult'); +let contractsInitialized = false; + // Buttons that require connecting an account const allConnectedButtons = [ deployButton, @@ -918,7 +920,7 @@ let multisigContract; let erc1155Contract; // Must be called after the active provider changes -const initializeContracts = () => { +const initializeContracts = async () => { try { // We must specify the network as 'any' for ethers to allow network changes ethersProvider = new ethers.providers.Web3Provider(provider, 'any'); @@ -986,6 +988,8 @@ const initializeContracts = () => { ); } catch (error) { console.error(error); + } finally { + contractsInitialized = true; } }; @@ -1004,7 +1008,7 @@ export const updateFormElements = () => { } clearDisplayElements(); } - if (isMetaMaskConnected()) { + if (isMetaMaskConnected() && contractsInitialized) { for (const button of initialConnectedButtons) { button.disabled = false; } From 59b0c386568c41de3cc002744f65e2448666a461 Mon Sep 17 00:00:00 2001 From: Priya Narayanaswamy Date: Mon, 2 Dec 2024 21:09:11 +0800 Subject: [PATCH 2/7] chore: update contracts button when intialized --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index aca625e7..966067f9 100644 --- a/src/index.js +++ b/src/index.js @@ -1015,7 +1015,7 @@ export const updateFormElements = () => { } updateOnboardElements(); - updateContractElements(); + updateContractElements(contractsInitialized); }; const clearDisplayElements = () => { @@ -1088,7 +1088,7 @@ const updateOnboardElements = () => { }; const updateContractElements = () => { - if (deployedContractAddress) { + if (deployedContractAddress && contractsInitialized) { // Piggy bank contract contractStatus.innerHTML = 'Deployed'; depositButton.disabled = false; From 4a16933351f195fee61c975f64b8fb9b440eb346 Mon Sep 17 00:00:00 2001 From: Priya Narayanaswamy Date: Mon, 2 Dec 2024 21:11:53 +0800 Subject: [PATCH 3/7] chore: remove unnecessary parameter --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 966067f9..0ef02b69 100644 --- a/src/index.js +++ b/src/index.js @@ -1015,7 +1015,7 @@ export const updateFormElements = () => { } updateOnboardElements(); - updateContractElements(contractsInitialized); + updateContractElements(); }; const clearDisplayElements = () => { From 9b125f79b28098b1db4af17f2d0cf3014720a6dc Mon Sep 17 00:00:00 2001 From: Priya Narayanaswamy Date: Mon, 2 Dec 2024 21:14:05 +0800 Subject: [PATCH 4/7] chore: await intialize --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 0ef02b69..545929ad 100644 --- a/src/index.js +++ b/src/index.js @@ -848,7 +848,7 @@ const closeProvider = () => { // Must be called after the active provider changes // Initializes active provider and adds any listeners const initializeProvider = async () => { - initializeContracts(); + await initializeContracts(); updateFormElements(); if (isMetaMaskInstalled()) { From 52cc742e743440c35a006b34cbae4377cd3110c1 Mon Sep 17 00:00:00 2001 From: Priya Narayanaswamy Date: Mon, 2 Dec 2024 21:45:26 +0800 Subject: [PATCH 5/7] chore: update package json and logic --- package.json | 2 +- src/index.js | 29 +++++++++++++++++++---------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 3a877d3d..a65b0f6a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@metamask/test-dapp", - "version": "8.13.1", + "version": "8.13.0", "description": "A simple dapp used in MetaMask e2e tests.", "engines": { "node": ">= 18.0.0" diff --git a/src/index.js b/src/index.js index 545929ad..1a265e61 100644 --- a/src/index.js +++ b/src/index.js @@ -848,7 +848,8 @@ const closeProvider = () => { // Must be called after the active provider changes // Initializes active provider and adds any listeners const initializeProvider = async () => { - await initializeContracts(); + const initialized = await initializeContracts(); + contractsInitialized = initialized; updateFormElements(); if (isMetaMaskInstalled()) { @@ -919,7 +920,6 @@ let failingContract; let multisigContract; let erc1155Contract; -// Must be called after the active provider changes const initializeContracts = async () => { try { // We must specify the network as 'any' for ethers to allow network changes @@ -956,6 +956,7 @@ const initializeContracts = async () => { ethersProvider.getSigner(), ); } + hstFactory = new ethers.ContractFactory( hstAbi, hstBytecode, @@ -986,10 +987,11 @@ const initializeContracts = async () => { erc1155Bytecode, ethersProvider.getSigner(), ); + + return true; } catch (error) { - console.error(error); - } finally { - contractsInitialized = true; + console.error('Failed to initialize contracts:', error); + return false; } }; @@ -1001,16 +1003,19 @@ const initializeContracts = async () => { // Updates form elements content and disabled status export const updateFormElements = () => { const accountButtonsDisabled = - !isMetaMaskInstalled() || !isMetaMaskConnected(); + !isMetaMaskInstalled() || + !isMetaMaskConnected() || + (deployedContractAddress && !contractsInitialized); + if (accountButtonsDisabled) { for (const button of allConnectedButtons) { button.disabled = true; } clearDisplayElements(); } - if (isMetaMaskConnected() && contractsInitialized) { + if (isMetaMaskConnected()) { for (const button of initialConnectedButtons) { - button.disabled = false; + button.disabled = !contractsInitialized && deployedContractAddress; } } @@ -1088,7 +1093,11 @@ const updateOnboardElements = () => { }; const updateContractElements = () => { - if (deployedContractAddress && contractsInitialized) { + if (deployedContractAddress && !contractsInitialized) { + return; // Don't enable any contract elements if not initialized + } + + if (deployedContractAddress) { // Piggy bank contract contractStatus.innerHTML = 'Deployed'; depositButton.disabled = false; @@ -1798,7 +1807,7 @@ const initializeFormElements = () => { method: 'eth_signTypedData_v4', params: [ accounts[0], - `{"types":{"ERC721Order":[{"type":"uint8","name":"direction"},{"type":"address","name":"maker"},{"type":"address","name":"taker"},{"type":"uint256","name":"expiry"},{"type":"uint256","name":"nonce"},{"type":"address","name":"erc20Token"},{"type":"uint256","name":"erc20TokenAmount"},{"type":"Fee[]","name":"fees"},{"type":"address","name":"erc721Token"},{"type":"uint256","name":"erc721TokenId"},{"type":"Property[]","name":"erc721TokenProperties"}],"Fee":[{"type":"address","name":"recipient"},{"type":"uint256","name":"amount"},{"type":"bytes","name":"feeData"}],"Property":[{"type":"address","name":"propertyValidator"},{"type":"bytes","name":"propertyData"}],"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}]},"domain":{"name":"ZeroEx","version":"1.0.0","chainId":"${chainIdInt}","verifyingContract":"0xdef1c0ded9bec7f1a1670819833240f027b25eff"},"primaryType":"ERC721Order","message":{"direction":"0","maker":"${accounts[0]}","taker":"${maliciousAddress}","expiry":"2524604400","nonce":"100131415900000000000000000000000000000083840314483690155566137712510085002484","erc20Token":"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","erc20TokenAmount":"42000000000000","fees":[],"erc721Token":"0x8a90CAb2b38dba80c64b7734e58Ee1dB38B8992e","erc721TokenId":"2516","erc721TokenProperties":[]}}`, + `{"types":{"ERC721Order":[{"type":"uint8","name":"direction"},{"type":"address","name":"maker"},{"type":"address","name":"taker"},{"type":"uint256","name":"expiry"},{"type":"uint256","name":"nonce"},{"type":"address","name":"erc20Token"},{"type":"uint256","name":"erc20TokenAmount"},{"type":"Fee[]","name":"fees"},{"type":"address","name":"erc721Token"},{"type":"uint256","name":"erc721TokenId"},{"type":"Property[]","name":"erc721TokenProperties"}],"Fee":[{"type":"address","name":"recipient"},{"type":"uint256","name":"amount"},{"type":"bytes","name":"feeData"}],"Property":[{"type":"address","name":"propertyValidator"},{"type":"bytes","name":"propertyData"}],"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}]},"domain":{"name":"ZeroEx","version":"1.0.0","chainId":${chainIdInt},"verifyingContract":"0xdef1c0ded9bec7f1a1670819833240f027b25eff"},"primaryType":"ERC721Order","message":{"direction":"0","maker":"${accounts[0]}","taker":${maliciousAddress},"expiry":"2524604400","nonce":"100131415900000000000000000000000000000083840314483690155566137712510085002484","erc20Token":"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","erc20TokenAmount":"42000000000000","fees":[],"erc721Token":"0x8a90CAb2b38dba80c64b7734e58Ee1dB38B8992e","erc721TokenId":"2516","erc721TokenProperties":[]}}`, ], }); console.log(result); From 9fd60d84227855ea8732eab0d7ab9ee6abe1d93f Mon Sep 17 00:00:00 2001 From: Priya Narayanaswamy Date: Mon, 2 Dec 2024 21:50:03 +0800 Subject: [PATCH 6/7] chore: update signTypesData para --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 1a265e61..d7b66078 100644 --- a/src/index.js +++ b/src/index.js @@ -1807,7 +1807,7 @@ const initializeFormElements = () => { method: 'eth_signTypedData_v4', params: [ accounts[0], - `{"types":{"ERC721Order":[{"type":"uint8","name":"direction"},{"type":"address","name":"maker"},{"type":"address","name":"taker"},{"type":"uint256","name":"expiry"},{"type":"uint256","name":"nonce"},{"type":"address","name":"erc20Token"},{"type":"uint256","name":"erc20TokenAmount"},{"type":"Fee[]","name":"fees"},{"type":"address","name":"erc721Token"},{"type":"uint256","name":"erc721TokenId"},{"type":"Property[]","name":"erc721TokenProperties"}],"Fee":[{"type":"address","name":"recipient"},{"type":"uint256","name":"amount"},{"type":"bytes","name":"feeData"}],"Property":[{"type":"address","name":"propertyValidator"},{"type":"bytes","name":"propertyData"}],"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}]},"domain":{"name":"ZeroEx","version":"1.0.0","chainId":${chainIdInt},"verifyingContract":"0xdef1c0ded9bec7f1a1670819833240f027b25eff"},"primaryType":"ERC721Order","message":{"direction":"0","maker":"${accounts[0]}","taker":${maliciousAddress},"expiry":"2524604400","nonce":"100131415900000000000000000000000000000083840314483690155566137712510085002484","erc20Token":"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","erc20TokenAmount":"42000000000000","fees":[],"erc721Token":"0x8a90CAb2b38dba80c64b7734e58Ee1dB38B8992e","erc721TokenId":"2516","erc721TokenProperties":[]}}`, + `{"types":{"ERC721Order":[{"type":"uint8","name":"direction"},{"type":"address","name":"maker"},{"type":"address","name":"taker"},{"type":"uint256","name":"expiry"},{"type":"uint256","name":"nonce"},{"type":"address","name":"erc20Token"},{"type":"uint256","name":"erc20TokenAmount"},{"type":"Fee[]","name":"fees"},{"type":"address","name":"erc721Token"},{"type":"uint256","name":"erc721TokenId"},{"type":"Property[]","name":"erc721TokenProperties"}],"Fee":[{"type":"address","name":"recipient"},{"type":"uint256","name":"amount"},{"type":"bytes","name":"feeData"}],"Property":[{"type":"address","name":"propertyValidator"},{"type":"bytes","name":"propertyData"}],"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}]},"domain":{"name":"ZeroEx","version":"1.0.0","chainId":"${chainIdInt}","verifyingContract":"0xdef1c0ded9bec7f1a1670819833240f027b25eff"},"primaryType":"ERC721Order","message":{"direction":"0","maker":"${accounts[0]}","taker":"${maliciousAddress}","expiry":"2524604400","nonce":"100131415900000000000000000000000000000083840314483690155566137712510085002484","erc20Token":"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","erc20TokenAmount":"42000000000000","fees":[],"erc721Token":"0x8a90CAb2b38dba80c64b7734e58Ee1dB38B8992e","erc721TokenId":"2516","erc721TokenProperties":[]}}`, ], }); console.log(result); From ec7754d934dcaed797b2c5947a0ededf2894881b Mon Sep 17 00:00:00 2001 From: Priya Narayanaswamy Date: Mon, 2 Dec 2024 21:59:41 +0800 Subject: [PATCH 7/7] chore: move contractsInitialized --- src/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index d7b66078..d36bd0d5 100644 --- a/src/index.js +++ b/src/index.js @@ -379,8 +379,6 @@ const ensInput = document.getElementById('ensInput'); const ensSubmit = document.getElementById('ensSubmit'); const ensResult = document.getElementById('ensResult'); -let contractsInitialized = false; - // Buttons that require connecting an account const allConnectedButtons = [ deployButton, @@ -919,6 +917,7 @@ let nftsContract; let failingContract; let multisigContract; let erc1155Contract; +let contractsInitialized = false; const initializeContracts = async () => { try {