diff --git a/utils/call-tx.ts b/utils/call-tx.ts index 6567ef7..cf98156 100644 --- a/utils/call-tx.ts +++ b/utils/call-tx.ts @@ -1,7 +1,8 @@ -import { Contract, ContractTransaction, ContractTransactionResponse } from 'ethers'; +import { Contract, ContractTransactionResponse } from 'ethers'; import { confirmTx } from './confirm-tx'; import { printTx } from './print-tx'; import { logger } from './logger'; +import { splitArgsAndOverrides } from './split-args-and-overrides'; export const contractCallTxWithConfirm = async (contract: Contract, method: string, args: unknown[]) => { await printTx(contract, method, args); @@ -25,19 +26,7 @@ export const contractStaticCallTx = async (contract: Contract, method: string, a }; export const populateGasLimit = async (contract: Contract, method: string, argsWithOverrides: unknown[]) => { - const fragment = contract.interface.getFunction(method, argsWithOverrides); - - if (!fragment) { - throw new Error(`Method ${method} not found`); - } - - const args = [...argsWithOverrides]; - - // If an overrides was passed in, copy it - let overrides: Omit = {}; - if (fragment.inputs.length + 1 === args.length) { - overrides = { ...(args.pop() as typeof overrides) }; - } + const { args, overrides } = splitArgsAndOverrides(contract, method, argsWithOverrides); if (!overrides.gasLimit) { const gasLimit = await contract[method].estimateGas(...args, overrides); diff --git a/utils/print-tx.ts b/utils/print-tx.ts index 055656f..01f467c 100644 --- a/utils/print-tx.ts +++ b/utils/print-tx.ts @@ -1,19 +1,21 @@ import chalk from 'chalk'; -import { Contract } from 'ethers'; +import { Contract, formatEther } from 'ethers'; import { stringify } from './stringify'; import { getProvider, getSignerAddress } from './contract'; import { logger } from './logger'; +import { splitArgsAndOverrides } from './split-args-and-overrides'; const title = chalk.gray; const chain = chalk.green.bold; const value = chalk.blue.bold; -export const printTx = async (contract: Contract, method: string, args: unknown[] = []) => { +export const printTx = async (contract: Contract, method: string, argsWithOverrides: unknown[] = []) => { const provider = getProvider(contract); const from = await getSignerAddress(contract); const network = await provider.getNetwork(); const to = await contract.getAddress(); + const { args, overrides } = splitArgsAndOverrides(contract, method, argsWithOverrides); const parsedArgs = args.map((arg) => stringify(arg)); const call = `${method}(${parsedArgs})`; @@ -24,4 +26,8 @@ export const printTx = async (contract: Contract, method: string, args: unknown[ logger.log(title(' To:'), value(to)); logger.log(title(' Call:'), value(call)); logger.log(title(' Data:'), value(data)); + + if (overrides.value) { + logger.log(title('Value:'), value(`${overrides.value.toString()} (${formatEther(overrides.value)} ETH)`)); + } }; diff --git a/utils/split-args-and-overrides.ts b/utils/split-args-and-overrides.ts new file mode 100644 index 0000000..d4f04e4 --- /dev/null +++ b/utils/split-args-and-overrides.ts @@ -0,0 +1,19 @@ +import { Contract, ContractTransaction } from 'ethers'; + +export const splitArgsAndOverrides = (contract: Contract, method: string, argsWithOverrides: unknown[]) => { + const fragment = contract.interface.getFunction(method, argsWithOverrides); + + if (!fragment) { + throw new Error(`Method ${method} not found`); + } + + const args = [...argsWithOverrides]; + + // If an overrides was passed in, copy it + let overrides: Omit = {}; + if (fragment.inputs.length + 1 === args.length) { + overrides = { ...(args.pop() as typeof overrides) }; + } + + return { args, overrides }; +};