|
| 1 | +import { Command } from "@commander-js/extra-typings"; |
| 2 | +import chalk from "chalk"; |
| 3 | +import ora from "ora"; |
| 4 | +import { |
| 5 | + type Address, |
| 6 | + type PublicClient, |
| 7 | + erc20Abi, |
| 8 | + formatUnits, |
| 9 | + getAddress, |
| 10 | + isAddress, |
| 11 | + isHex, |
| 12 | + parseUnits, |
| 13 | +} from "viem"; |
| 14 | +import { getProjectName } from "../../base.js"; |
| 15 | +import { |
| 16 | + erc20PortalAbi, |
| 17 | + erc20PortalAddress, |
| 18 | + testTokenAddress, |
| 19 | +} from "../../contracts.js"; |
| 20 | +import { |
| 21 | + addressInput, |
| 22 | + bigintInput, |
| 23 | + getInputApplicationAddress, |
| 24 | +} from "../../prompts.js"; |
| 25 | +import { connect } from "../../wallet.js"; |
| 26 | +import type { DepositCommandOpts } from "../deposit.js"; |
| 27 | + |
| 28 | +type ERC20Token = { |
| 29 | + address: Address; |
| 30 | + name: string; |
| 31 | + symbol: string; |
| 32 | + decimals: number; |
| 33 | +}; |
| 34 | + |
| 35 | +const readToken = async ( |
| 36 | + publicClient: PublicClient, |
| 37 | + address: Address, |
| 38 | +): Promise<ERC20Token> => { |
| 39 | + const args = { abi: erc20Abi, address }; |
| 40 | + const symbol = await publicClient.readContract({ |
| 41 | + ...args, |
| 42 | + functionName: "symbol", |
| 43 | + }); |
| 44 | + const name = await publicClient.readContract({ |
| 45 | + ...args, |
| 46 | + functionName: "name", |
| 47 | + }); |
| 48 | + const decimals = await publicClient.readContract({ |
| 49 | + ...args, |
| 50 | + functionName: "decimals", |
| 51 | + }); |
| 52 | + return { |
| 53 | + address, |
| 54 | + name, |
| 55 | + symbol, |
| 56 | + decimals, |
| 57 | + }; |
| 58 | +}; |
| 59 | + |
| 60 | +const parseToken = async (options: { |
| 61 | + testClient: PublicClient; |
| 62 | + token?: string; |
| 63 | +}): Promise<ERC20Token> => { |
| 64 | + const { testClient } = options; |
| 65 | + |
| 66 | + const address = |
| 67 | + options.token && isAddress(options.token) |
| 68 | + ? getAddress(options.token) |
| 69 | + : await addressInput({ |
| 70 | + message: "Token address", |
| 71 | + default: testTokenAddress, |
| 72 | + }); |
| 73 | + |
| 74 | + return readToken(testClient, address); |
| 75 | +}; |
| 76 | + |
| 77 | +export const createErc20Command = () => { |
| 78 | + // biome-ignore lint/complexity/noBannedTypes: commander pattern |
| 79 | + return new Command<[], {}, DepositCommandOpts>("erc20") |
| 80 | + .description("Deposit ERC-20 to the application") |
| 81 | + .configureHelp({ showGlobalOptions: true }) |
| 82 | + .option("--token <address>", "token address") |
| 83 | + .option("--amount <number>", "amount to send") |
| 84 | + .option("--exec-layer-data <hex>", "exec layer data", "0x") |
| 85 | + .action(async (options, command) => { |
| 86 | + const { from } = command.optsWithGlobals(); |
| 87 | + |
| 88 | + const projectName = getProjectName(command.optsWithGlobals()); |
| 89 | + |
| 90 | + // connect to anvil |
| 91 | + const testClient = await connect(command.optsWithGlobals()); |
| 92 | + |
| 93 | + // the input sender, impersonated |
| 94 | + const account = |
| 95 | + from && isAddress(from) |
| 96 | + ? getAddress(from) |
| 97 | + : (await testClient.getAddresses())[0]; |
| 98 | + |
| 99 | + const token = await parseToken({ |
| 100 | + testClient, |
| 101 | + token: options.token, |
| 102 | + }); |
| 103 | + |
| 104 | + // get dapp address from local node, or ask |
| 105 | + const application = await getInputApplicationAddress({ |
| 106 | + ...command.optsWithGlobals(), |
| 107 | + projectName, |
| 108 | + }); |
| 109 | + |
| 110 | + const { decimals, symbol } = token; |
| 111 | + const amount = options.amount |
| 112 | + ? parseUnits(options.amount, decimals) |
| 113 | + : await bigintInput({ |
| 114 | + message: `Amount (${symbol})`, |
| 115 | + decimals, |
| 116 | + }); |
| 117 | + |
| 118 | + const execLayerData = isHex(options.execLayerData) |
| 119 | + ? options.execLayerData |
| 120 | + : "0x"; |
| 121 | + |
| 122 | + // progress spinner |
| 123 | + const progress = ora(); |
| 124 | + |
| 125 | + // check balance |
| 126 | + const balance = await testClient.readContract({ |
| 127 | + abi: erc20Abi, |
| 128 | + address: token.address, |
| 129 | + functionName: "balanceOf", |
| 130 | + args: [account], |
| 131 | + }); |
| 132 | + if (balance < amount) { |
| 133 | + progress.fail("Insufficient balance"); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + // check allowance |
| 138 | + const allowance = await testClient.readContract({ |
| 139 | + abi: erc20Abi, |
| 140 | + address: token.address, |
| 141 | + functionName: "allowance", |
| 142 | + args: [account, erc20PortalAddress], |
| 143 | + }); |
| 144 | + |
| 145 | + // for messages |
| 146 | + const amountStr = `${chalk.cyan(formatUnits(amount, decimals))} ${symbol}`; |
| 147 | + |
| 148 | + // approve if needed |
| 149 | + if (allowance < amount) { |
| 150 | + progress.start(`Approving ${amountStr}...`); |
| 151 | + const { request } = await testClient.simulateContract({ |
| 152 | + abi: erc20Abi, |
| 153 | + account, |
| 154 | + address: token.address, |
| 155 | + functionName: "approve", |
| 156 | + args: [erc20PortalAddress, amount], |
| 157 | + }); |
| 158 | + const hash = await testClient.writeContract(request); |
| 159 | + await testClient.waitForTransactionReceipt({ hash }); |
| 160 | + progress.succeed(`Approved ${amountStr}`); |
| 161 | + } |
| 162 | + |
| 163 | + // simulate deposit call |
| 164 | + const { request } = await testClient.simulateContract({ |
| 165 | + abi: erc20PortalAbi, |
| 166 | + account, |
| 167 | + address: erc20PortalAddress, |
| 168 | + functionName: "depositERC20Tokens", |
| 169 | + args: [token.address, application, amount, execLayerData], |
| 170 | + }); |
| 171 | + |
| 172 | + // send deposit |
| 173 | + progress.start( |
| 174 | + `Depositing ${amountStr} to ${chalk.cyan(application)}...`, |
| 175 | + ); |
| 176 | + const hash = await testClient.writeContract(request); |
| 177 | + await testClient.waitForTransactionReceipt({ hash }); |
| 178 | + progress.succeed( |
| 179 | + `Deposited ${amountStr} to ${chalk.cyan(application)}`, |
| 180 | + ); |
| 181 | + }); |
| 182 | +}; |
0 commit comments