Since 2009, we have been utilizing our extensive expertise in blockchain technologies to help businesses, both large and small, maximize their efficiency.
Explore More
With more than 400+ experts, Oodles comprises a fantastic resource of business knowledge that spans multiple industries. Whatever the circumstances, we keep to our obligations.
Explore More
At Oodles, we help our clients work with a human understanding but at superhuman speed something that others can't. They thus advance and maintain their lead
2nd November 2020
15 min read
Sr. Lead Development
The most basic example for sending an ethereum transaction is through the web3js library itself using the following code.
// using the promise
web3.eth.sendTransaction({
from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe',
to: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
value: '1000000000000000'
})
.then(function(receipt){
...
});
This requires the 'from' account to be present on the Ethereum node you are interacting with, and then the account should be unlocked to submit the transaction.
But sometimes you may not have the account existing on the node itself and may want to transact through the private key itself so as to not check for unlocking the transactions every time you do a transaction.
The ethereumjs-tx library allows us to do the same.
Here are the steps involved to use the above-mentioned library, from installation to transaction signing and then broadcasting the signed tx with web3
npm install ethereumjs-tx
const EthereumTx = require('ethereumjs-tx').Transaction
const privateKey = config.get("blockchain.privateKey")
const fromAddress = config.get("blockchain.address")
If it is one of the major forks then the following changes may be not be required, in that case, only the chain name and hardfork details are needed only.
But for a fork of Ethereum, you will need to provide the details through an ethereumjs-common instance, using the following
const Common = require("ethereumjs-common")
const customCommon = Common.default.forCustomChain(
'mainnet',
{
name : "yourNetwork",
chainId: config.get("blockchain.networkId"),
networkId : config.get("blockchain.networkId")
},
"petersburg",
)
The above-mentioned values are required in the Transaction constructor.
This involves, the sendContractTx which needs the contract address passed as toAddress, a contract instance created through web3 and the parameter to the function you need to execute on the contract whose address is given by toAddress.
This function calls the signTx method which actually uses the ethereumjs-tx library to sign the transaction via our private key
async function sendContractTx(toAddress, candidateContract, param){
let nonce = await web3HttpInstance.eth.getTransactionCount(fromAddress,'pending')
let extraData = await candidateContract.methods.contractMethod(param)
extraData = extraData.encodeABI()
let gas = await web3HttpInstance.eth.estimateGas({
to : toAddress,
data : extraData
})
let gasPrice= await web3HttpInstance.eth.getGasPrice();
logger.debug("DistributeReward Tx details gas %s gasPrice %s nonce %d , txPrice is %s", gas, gasPrice, nonce,
web3HttpInstance.utils.fromWei((parseInt(gas)*parseInt(gasPrice)).toString(10)))
txObj = {
from: fromAddress,
to: toAddress,
data : extraData,
value: '0',
gas: gas,
gasPrice: gasPrice,
privKey: privateKey,
nonce
}
let signedTx = await signTx(txObj)
signedTx = "0x" + signedTx.serialize().toString('hex')
logger.debug("signed tx is %O", signedTx)
return signedTx
}
In the above function, the signTx function is called which makes use of the ethereumjs-tx library, creates a new EthereumTx instance with the transaction parameters and the network config object.
Here privKey as also used in the function above corresponds to the private key and is required for the sign method on the tx object.
async function signTx(payload) {
let { from, to, data, value, gas, gasPrice, privKey, nonce } = payload
let txParams = {
to,
data,
value: web3HttpInstance.utils.toHex(value),
gasPrice: web3HttpInstance.utils.toHex(gasPrice),
gas: web3HttpInstance.utils.toHex(gas),
nonce: web3HttpInstance.utils.toHex(nonce)
}
var tx = new EthereumTx(txParams,{common:customCommon})
privKey = new Buffer(privKey, 'hex')
tx.sign(privKey)
return tx
}
For submitting the transaction we are using web3.eth.sendSignedTransaction method, passing in the signed transaction created above as the parameter, and then we listen to the "receipt" event for the transaction receipt or the "error" event in case of any errors
let signedTx = await sendContractTx(candidate.hash,candidateContract,stakerAddress);
let signedTxObject = web3HttpInstance.eth.sendSignedTransaction(signedTx)
//On fetching receipt of the transaction
signedTxObject.on("receipt",async function(receipt){})
//On any error with the transaction
signedTxObject.on("error",function(error,receipt){})
Amit Kumar
Amit is an seasoned Backend Developer with expertise in Java, particularly the Spring framework as well as Javascript and related frameworks such as Node.js and Express. He possesses a solid understanding of blockchain fundamentals and has hands-on experience integrating major blockchains like Ethereum and Bitcoin into both centralized and decentralized applications. He is proficient in working with relational and non-relational databases, enabling him to handle various data storage requirements effectively. He has successfully contributed to a range of projects, including Belfrics, a cryptocurrency exchange platform, Daxxcoin, an Ethereum Proof-Of-Work fork, and Wethio blockchain, an Ethereum-based Proof-Of-Stake fork. His strong technical skills and practical experience make him a valuable asset, particularly in the blockchain domain. Amit's ability to integrate blockchain technologies into applications showcases his versatility and innovation.
Sr. Lead Development
By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.
We would love to
hear from you!
Innovate with confidence!