|
| 1 | +import ora from "ora"; |
| 2 | +import { |
| 3 | + Account as SNAccount, |
| 4 | + ec, |
| 5 | + SequencerProvider, |
| 6 | + stark, |
| 7 | + hash, |
| 8 | +} from "starknet-490"; |
| 9 | +import { PROXY_CONTRACT_CLASS_HASHES } from "../../getAccounts"; |
| 10 | +import { getVersion } from "../../getVersion"; |
| 11 | +import { oraLog } from "../../oraLog"; |
| 12 | +import { Account } from "../../ui/pickAccounts"; |
| 13 | + |
| 14 | +export const fix = async ( |
| 15 | + accounts: Account[], |
| 16 | + network: "mainnet-alpha" | "goerli-alpha", |
| 17 | + accountsToRecover: string[] |
| 18 | +): Promise<void> => { |
| 19 | + const spinner = ora(`Deploying accounts (this may take some time)`).start(); |
| 20 | + const provider = new SequencerProvider({ network }); |
| 21 | + |
| 22 | + for (const address of accountsToRecover) { |
| 23 | + const account = accounts.find((a) => a.address === address); |
| 24 | + if (!account) { |
| 25 | + throw new Error(`Account ${address} not found`); |
| 26 | + } |
| 27 | + if (!account.deployImplementation) { |
| 28 | + throw new Error(`Account ${address} has no deployImplementation`); |
| 29 | + } |
| 30 | + const keyPair = ec.getKeyPair(account.privateKey); |
| 31 | + const starkKey = ec.getStarkKey(keyPair); |
| 32 | + const snAccount = new SNAccount(provider, account.address, keyPair); |
| 33 | + |
| 34 | + const constructorCallData = { |
| 35 | + implementation: account.deployImplementation, |
| 36 | + selector: hash.getSelectorFromName("initialize"), |
| 37 | + calldata: stark.compileCalldata({ signer: starkKey, guardian: "0" }), |
| 38 | + }; |
| 39 | + |
| 40 | + const deployAccountPayload = { |
| 41 | + classHash: PROXY_CONTRACT_CLASS_HASHES[0], |
| 42 | + contractAddress: account.address, |
| 43 | + constructorCalldata: stark.compileCalldata(constructorCallData), |
| 44 | + addressSalt: starkKey, |
| 45 | + }; |
| 46 | + |
| 47 | + const transaction = await snAccount.deployAccount(deployAccountPayload); |
| 48 | + oraLog(spinner, `Transaction ${transaction.transaction_hash} created`); |
| 49 | + await provider.waitForTransaction(transaction.transaction_hash); |
| 50 | + |
| 51 | + if (account) { |
| 52 | + account.signer = starkKey; |
| 53 | + account.implementation = account.deployImplementation; |
| 54 | + const [newVersion] = await getVersion( |
| 55 | + [account.address], |
| 56 | + account.networkId as any |
| 57 | + ); |
| 58 | + account.version = newVersion; |
| 59 | + } |
| 60 | + |
| 61 | + // wait 1 minute extra to make sure the transaction is mined |
| 62 | + await new Promise((resolve) => setTimeout(resolve, 60000)); |
| 63 | + } |
| 64 | + spinner.succeed(`Deployed accounts`); |
| 65 | +}; |
0 commit comments