Skip to content

Commit

Permalink
Merge pull request #17 from lidofinance/develop
Browse files Browse the repository at this point in the history
fix: overrides printing
  • Loading branch information
avsetsin authored Oct 16, 2023
2 parents 57f8394 + fb14ec9 commit c89c6c2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
17 changes: 3 additions & 14 deletions utils/call-tx.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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<ContractTransaction, 'data' | 'to'> = {};
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);
Expand Down
10 changes: 8 additions & 2 deletions utils/print-tx.ts
Original file line number Diff line number Diff line change
@@ -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})`;
Expand All @@ -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)`));
}
};
19 changes: 19 additions & 0 deletions utils/split-args-and-overrides.ts
Original file line number Diff line number Diff line change
@@ -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<ContractTransaction, 'data' | 'to'> = {};
if (fragment.inputs.length + 1 === args.length) {
overrides = { ...(args.pop() as typeof overrides) };
}

return { args, overrides };
};

0 comments on commit c89c6c2

Please sign in to comment.