Skip to content

Instantly share code, notes, and snippets.

@grsLammy
Last active October 18, 2023 10:51
Show Gist options
  • Save grsLammy/a2eb6afeda700626da8d94664936852e to your computer and use it in GitHub Desktop.
Save grsLammy/a2eb6afeda700626da8d94664936852e to your computer and use it in GitHub Desktop.
Best practices for transaction management
const erc20TransferFrom = async () => {
try {
/* ---------------------------- SETUP ------------------------------ */
/*
USING INFURA PROVIDER
*/
const provider: ethers.providers.InfuraProvider =
new ethers.providers.InfuraProvider('maticmum', infuraProjectID);
/*
INITIALIZE SIGNER
*/
const signer: ethers.Wallet = new ethers.Wallet(privateKey, provider);
/*
GET SIGNER NONCE
*/
const nonce: number = await provider.getTransactionCount(signer.address);
/*
FETCH THE FAST LATEST GAS PRICE DATA FROM THE POLYGON V2 gas station API
*/
const {maxFee, maxPriorityFee}: GasData = await fetchGasPrice();
/* ---------------------------- FUNCTION CALL ---------------------------- */
/*
INITIALIZE TESTERC20 INSTANCE AND CONNECT WITH SIGNER
*/
const testERC20Instance: ethers.Contract = new ethers.Contract(
address,
abi,
provider
);
const testERC721: ethers.Contract = testERC20Instance.connect(signer);
/*
ESTIMATE GAS
*/
const estimatedGasLimit: ethers.BigNumber =
await testERC20.estimateGas.transferFrom(fromAddress, toAddress, amount, {
gasLimit: 14_999_999,
nonce: nonce,
maxFeePerGas: maxFee,
maxPriorityFeePerGas: maxPriorityFee,
});
/*
TRANSFERFROM
*/
const response: ethers.ContractTransaction =
await testERC20.transferFrom(fromAddress, toAddress, amount, {
gasLimit: estimatedGasLimit,
nonce: nonce,
maxFeePerGas: maxFee,
maxPriorityFeePerGas: maxPriorityFee,
});
await response.wait(15); // wait for 15 block confirmation
const txReceipt: ethers.providers.TransactionReceipt =
await provider.getTransactionReceipt(response.hash);
}
} catch (error) {
console.log('Error in erc20TransferFrom', error);
process.exit(1);
}
}
issueToken()
.then(() => {
console.log('\n\n---------- ENDING ALL PROCESS ----------\n\n');
process.exit(0);
})
.catch((error) => {
console.error('error', error);
process.exit(1);
});
export async function fetchGasPrice(): Promise<GasData> {
try {
const response: Response = await fetch(
'https://gasstation-testnet.polygon.technology/v2'
);
const gasData: GasApiResponse = await response.json(); // Extract JSON data from the response
// Get the maxFee and maxPriorityFee for fast
const maxFeeInGWEI = gasData.fast.maxFee;
const maxPriorityFeeInGWEI = gasData.fast.maxPriorityFee;
/* Convert the fetched GWEI gas price to WEI after converting ignore the decimal value
* as the transaction payload only accepts whole number
*/
const maxFee = Math.trunc(maxFeeInGWEI * 10 ** 9);
const maxPriorityFee = Math.trunc(maxPriorityFeeInGWEI * 10 ** 9);
return {maxFee, maxPriorityFee};
} catch (error) {
console.log(`Error in fetchGasPrice: ${error}`);
process.exit(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment