diff --git a/packages/extension/src/background/devnet/declareAccounts.ts b/packages/extension/src/background/devnet/declareAccounts.ts index 7ebefc9a2..d55af0a75 100644 --- a/packages/extension/src/background/devnet/declareAccounts.ts +++ b/packages/extension/src/background/devnet/declareAccounts.ts @@ -1,5 +1,6 @@ import { memoize } from "lodash-es" import { Account, AccountInterface, ec } from "starknet" +import { hash } from "starknet5" import urlJoin from "url-join" import { Network, getProvider } from "../../shared/network" @@ -44,20 +45,65 @@ export const declareContracts = memoize( _loadContracts: LoadContracts, ) => { const [proxyContract, accountContract] = await _loadContracts() - const proxy = await deployAccount.declare({ - classHash: PROXY_CONTRACT_CLASS_HASHES[0], - contract: proxyContract, - }) - const account = await deployAccount.declare({ - classHash: ARGENT_ACCOUNT_CONTRACT_CLASS_HASHES[0], - contract: accountContract, - }) + let proxyClassHash: string | undefined + let accountClassHash: string | undefined - await deployAccount.waitForTransaction(account.transaction_hash, 1e3) - await deployAccount.waitForTransaction(proxy.transaction_hash, 1e3) + const computedProxyClassHash = hash.computeContractClassHash(proxyContract) + const computedAccountClassHash = + hash.computeContractClassHash(accountContract) - return { proxy, account } + const isProxyClassDeclared = await checkIfClassIsDeclared( + deployAccount, + computedProxyClassHash, + ) + + const isAccountClassDeclared = await checkIfClassIsDeclared( + deployAccount, + computedAccountClassHash, + ) + + if (!isProxyClassDeclared) { + const proxy = await deployAccount.declare({ + classHash: PROXY_CONTRACT_CLASS_HASHES[0], + contract: proxyContract, + }) + + await deployAccount.waitForTransaction(proxy.transaction_hash, 1e3) + + proxyClassHash = proxy.class_hash + } + + if (!isAccountClassDeclared) { + const account = await deployAccount.declare({ + classHash: ARGENT_ACCOUNT_CONTRACT_CLASS_HASHES[0], + contract: accountContract, + }) + + await deployAccount.waitForTransaction(account.transaction_hash, 1e3) + + accountClassHash = account.class_hash + } + + return { + proxyClassHash: proxyClassHash ?? computedProxyClassHash, + accountClassHash: accountClassHash ?? computedAccountClassHash, + } }, (network) => `${network.baseUrl}`, ) + +export const checkIfClassIsDeclared = async ( + account: AccountInterface, + classHash: string, +) => { + try { + const contract = await account.getClassByHash(classHash) + + console.log("Contract already declared", classHash) + return Boolean(contract) + } catch (error) { + console.warn("Contract not declared", classHash) + return false + } +} diff --git a/packages/extension/src/background/wallet.ts b/packages/extension/src/background/wallet.ts index 64a24b253..bef24600e 100644 --- a/packages/extension/src/background/wallet.ts +++ b/packages/extension/src/background/wallet.ts @@ -237,13 +237,13 @@ export class Wallet { const deployerAccount = await getPreDeployedAccount(network) if (deployerAccount) { - const { account } = await declareContracts( + const { accountClassHash } = await declareContracts( network, deployerAccount, this.loadContracts, ) - return account.class_hash + return accountClassHash } return ARGENT_ACCOUNT_CONTRACT_CLASS_HASHES[0]