Last active
September 20, 2022 17:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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