Skip to content

Instantly share code, notes, and snippets.

@m-bo-one
Last active October 27, 2021 09:22
Show Gist options
  • Save m-bo-one/2f940d6c380cc9d5c61e6400fffc4ba0 to your computer and use it in GitHub Desktop.
Save m-bo-one/2f940d6c380cc9d5c61e6400fffc4ba0 to your computer and use it in GitHub Desktop.
Ethers + accessList estimation
async function sendTx(contract, method, params = []) {
console.log(`Sending tx for contracts "${contract.address}" with method name "${method}" and params: "${params}"`);
const gasEstimation = await contract.estimateGas[method](...params);
const unsignedTx = await contract.populateTransaction[method](...params);
const payload = {};
const { accessList, gasUsed: gasUsedHex } = await ethers.provider.send("eth_createAccessList", [unsignedTx]);
const gasUsed = ethers.BigNumber.from(gasUsedHex);
if (gasUsed >= gasEstimation) {
payload.gasLimit = gasEstimation;
console.log(`Using accessList failed to estimate, skipping it (gasLimit: "${payload.gasLimit}")`);
} else {
payload.gasLimit = gasUsed;
payload.accessList = accessList;
console.log(`Using accessList as it give gas save (saved: "${gasEstimation.sub(payload.gasLimit).toString()}", gasLimit: "${payload.gasLimit}")`);
}
const receipt = await contract[method](...params, payload).then(tx => tx.wait());
console.log(`Transaction "${receipt.transactionHash}" completed (gas used: ${receipt.gasUsed})`);
return receipt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment