Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: race condition of buttons being enabled too soon before contract… #378

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
24 changes: 18 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,8 @@ const closeProvider = () => {
// Must be called after the active provider changes
// Initializes active provider and adds any listeners
const initializeProvider = async () => {
initializeContracts();
const initialized = await initializeContracts();
contractsInitialized = initialized;
updateFormElements();

if (isMetaMaskInstalled()) {
Expand Down Expand Up @@ -916,9 +917,9 @@ let nftsContract;
let failingContract;
let multisigContract;
let erc1155Contract;
let contractsInitialized = false;

// 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');
Expand Down Expand Up @@ -954,6 +955,7 @@ const initializeContracts = () => {
ethersProvider.getSigner(),
);
}

hstFactory = new ethers.ContractFactory(
hstAbi,
hstBytecode,
Expand Down Expand Up @@ -984,8 +986,11 @@ const initializeContracts = () => {
erc1155Bytecode,
ethersProvider.getSigner(),
);

return true;
} catch (error) {
console.error(error);
console.error('Failed to initialize contracts:', error);
return false;
}
};

Expand All @@ -997,7 +1002,10 @@ const initializeContracts = () => {
// 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;
Expand All @@ -1006,7 +1014,7 @@ export const updateFormElements = () => {
}
if (isMetaMaskConnected()) {
for (const button of initialConnectedButtons) {
button.disabled = false;
button.disabled = !contractsInitialized && deployedContractAddress;
}
}

Expand Down Expand Up @@ -1084,6 +1092,10 @@ const updateOnboardElements = () => {
};

const updateContractElements = () => {
if (deployedContractAddress && !contractsInitialized) {
return; // Don't enable any contract elements if not initialized
}

if (deployedContractAddress) {
// Piggy bank contract
contractStatus.innerHTML = 'Deployed';
Expand Down
Loading