Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve(sendTokens.ts): Add options to send txn at nonce and with overridden gas prices #2117

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions scripts/sendTokens.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { ethers, retrieveSignerFromCLIArgs, getProvider, ERC20, ZERO_ADDRESS, toBN, getGasPrice } from "../src/utils";
import {
ethers,
retrieveSignerFromCLIArgs,
getProvider,
ERC20,
ZERO_ADDRESS,
toBN,
getGasPrice,
toGWei,
} from "../src/utils";
import { askYesNoQuestion } from "./utils";
import minimist from "minimist";
const args = minimist(process.argv.slice(2), {
string: ["token", "to", "amount", "chainId"],
string: ["token", "to", "amount", "chainId", "nonce", "maxFeePerGas", "maxPriorityFeePerGas"],
});

// Example run:
Expand All @@ -13,6 +22,17 @@ const args = minimist(process.argv.slice(2), {
// \ --wallet gckms
// \ --keys bot1

// Example run to clear up a stuck nonce with a maxFeePerGas of 1 Gwei and a maxPriorityFeePerGas of 2 Gwei
// ts-node ./scripts/sendTokens.ts
// \ --token 0x
// \ --amount 0 --to <self-EOA>
// \ --chainId 1868
// \ --nonce 100
// \ --maxFeePerGas 1
// \ --maxPriorityFeePerGas 2
// \ --wallet gckms
// \ --keys bot4

export async function run(): Promise<void> {
console.log("Executing Token sender 💸");
if (!Object.keys(args).includes("token")) {
Expand All @@ -36,17 +56,25 @@ export async function run(): Promise<void> {
if (!ethers.utils.isAddress(recipient)) {
throw new Error("invalid addresses");
}

const nonce = args.nonce ? Number(args.nonce) : undefined;
const maxFeePerGas = args.maxFeePerGas ? toGWei(args.maxFeePerGas) : undefined;
const maxPriorityFeePerGas = args.maxFeePerGas ? toGWei(args.maxFeePerGas) : undefined;
const gas =
maxFeePerGas && maxPriorityFeePerGas
? { maxFeePerGas, maxPriorityFeePerGas }
: await getGasPrice(connectedSigner.provider);
console.log(
`Submitting txn withmaxFeePerGas ${gas.maxFeePerGas.toString()} and priority fee ${gas.maxPriorityFeePerGas.toString()} with overridden nonce ${nonce}`
);
// Send ETH
if (token === ZERO_ADDRESS) {
if (token === ZERO_ADDRESS || token === "0x") {
const amountFromWei = ethers.utils.formatUnits(args.amount, 18);
console.log(`Send ETH with amount ${amountFromWei} tokens to ${recipient} on chain ${args.chainId}`);
if (!(await askYesNoQuestion("\nConfirm that you want to execute this transaction?"))) {
return;
}
console.log("sending...");
const gas = await getGasPrice(connectedSigner.provider);
const tx = await connectedSigner.sendTransaction({ to: recipient, value: toBN(args.amount), ...gas });
const tx = await connectedSigner.sendTransaction({ to: recipient, value: toBN(args.amount), nonce, ...gas });
const receipt = await tx.wait();
console.log("Transaction hash:", receipt.transactionHash);
}
Expand All @@ -62,15 +90,8 @@ export async function run(): Promise<void> {
return;
}
console.log("sending...");
const tx = await erc20.transfer(recipient, args.amount, {
maxFeePerGas: 150000000000,
maxPriorityFeePerGas: 40000000000,
});
console.log(
`submitted with max fee per gas ${tx.maxFeePerGas.toString()} and priority fee ${tx.maxPriorityFeePerGas.toString()} at nonce ${
tx.nonce
}`
);
const tx = await erc20.transfer(recipient, args.amount, { nonce, ...gas });

const receipt = await tx.wait();
console.log("Transaction hash:", receipt.transactionHash);
}
Expand Down