Skip to content

Instantly share code, notes, and snippets.

@caffeinum
Last active September 20, 2022 17:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caffeinum/2ee879df8e828e04126fc0f72ae12b9e to your computer and use it in GitHub Desktop.
Save caffeinum/2ee879df8e828e04126fc0f72ae12b9e to your computer and use it in GitHub Desktop.
How to use Truffle to replace pending transaction when you deploy new contract
const Greeter = artifacts.require("Greeter");
module.exports = async function (deployer, network, accounts) {
// await deployer.deploy(Greeter);
// replace with:
const [ owner ] = accounts;
const lastNonce = await web3.eth.getTransactionCount(owner);
// this DOESN'T include pending transactions, so lastNonce + 1 always replaces pending
console.log('Account \t', owner);
console.log('Last nonce \t', lastNonce);
console.log('Using nonce \t', lastNonce + 1);
const tx = Greeter.new({ nonce: lastNonce + 1, gasPrice: 100e9 });
tx.on('transactionHash', hash => console.log('Transaction \t', `https://etherscan.io/tx/${hash}`));
tx.on('receipt', receipt => console.log('Receipt \t', receipt));
tx.on('receipt', receipt => console.log('Gas usage \t', receipt.gasUsed, 'used', receipt.gasPrice, 'wei'));
const deployedContract = await tx;
console.log('Deployed contract address', deployedContract.address);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment