Created
October 18, 2021 20:48
-
-
Save flashgrimmie/165075e13436026a707d1d3c92d9f4e9 to your computer and use it in GitHub Desktop.
Send transaction with web3 in nodejs
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 Web3 = require('web3'); | |
const Provider = require('@truffle/hdwallet-provider'); | |
const MyContract = require('./build/contracts/MyContract.json'); | |
const address = ''; | |
const privateKey = ''; | |
const infuraUrl = ''; | |
// Very hard way (web3#signTransaction() + web3#sendSignedTransaction()) | |
const init1 = async () => { | |
const web3 = new Web3(infuraUrl); | |
const networkId = await web3.eth.net.getId(); | |
const myContract = new web3.eth.Contract( | |
MyContract.abi, | |
MyContract.networks[networkId].address | |
); | |
const tx = myContract.methods.setData(1); | |
const gas = await tx.estimateGas({from: address}); | |
const gasPrice = await web3.eth.getGasPrice(); | |
const data = tx.encodeABI(); | |
const nonce = await web3.eth.getTransactionCount(address); | |
const signedTx = await web3.eth.accounts.signTransaction( | |
{ | |
to: myContract.options.address, | |
data, | |
gas, | |
gasPrice, | |
nonce, | |
chainId: networkId | |
}, | |
privateKey | |
); | |
console.log(`Old data value: ${await myContract.methods.data().call()}`); | |
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction); | |
console.log(`Transaction hash: ${receipt.transactionHash}`); | |
console.log(`New data value: ${await myContract.methods.data().call()}`); | |
} | |
// Hard way (web3#sendTransaction()) | |
const init2 = async () => { | |
const web3 = new Web3(infuraUrl); | |
const networkId = await web3.eth.net.getId(); | |
const myContract = new web3.eth.Contract( | |
MyContract.abi, | |
MyContract.networks[networkId].address | |
); | |
web3.eth.accounts.wallet.add(privateKey); | |
const tx = myContract.methods.setData(2); | |
const gas = await tx.estimateGas({from: address}); | |
const gasPrice = await web3.eth.getGasPrice(); | |
const data = tx.encodeABI(); | |
const nonce = await web3.eth.getTransactionCount(address); | |
const txData = { | |
from: address, | |
to: myContract.options.address, | |
data: data, | |
gas, | |
gasPrice, | |
nonce, | |
chain: 'rinkeby', | |
hardfork: 'istanbul' | |
}; | |
console.log(`Old data value: ${await myContract.methods.data().call()}`); | |
const receipt = await web3.eth.sendTransaction(txData); | |
console.log(`Transaction hash: ${receipt.transactionHash}`); | |
console.log(`New data value: ${await myContract.methods.data().call()}`); | |
} | |
//Easy way (Web3 + @truffle/hdwallet-provider) | |
const init3 = async () => { | |
const provider = new Provider(privateKey, 'https://rinkeby.infura.io/v3/74aa9a15e2524f6980edb8a377301f3c'); | |
const web3 = new Web3(provider); | |
const networkId = await web3.eth.net.getId(); | |
const myContract = new web3.eth.Contract( | |
MyContract.abi, | |
MyContract.networks[networkId].address | |
); | |
console.log(await myContract.methods.data().call()); | |
console.log(`Old data value: ${await myContract.methods.data().call()}`); | |
const receipt = await myContract.methods.setData(3).send({ from: address }); | |
console.log(`Transaction hash: ${receipt.transactionHash}`); | |
console.log(`New data value: ${await myContract.methods.data().call()}`); | |
} | |
init3(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment