Sending raw transaction

  1. Specify the gas price and gas limit
  2. Set the txOptions and (for bsc) custom chain info
  3. Create the transaction object
  4. Convert the private key to buffer array
  5. Sign the transaction with the private key
  6. Serialize the transaction to become raw transaction
  7. Send the raw transaction using web3 sendSignedTransaction
privateKey.substring(2,66)
// sending a raw transaction
let gasPrice = web3.utils.toHex(web3.utils.toWei(‘100’, ‘gwei’))
var gasLimit = 21000

var txOptions = {
“nonce”: web3.utils.toHex(txCount),
“from”: senderAddr,
“to”: payeeAddr,
“gasPrice”: gasPrice,
“gasLimit”: web3.utils.toHex(gasLimit),
“data”: hexData,
}

const customCommon = Common.forCustomChain(‘mainnet’, {
name: ‘bnb’,
networkId: 56,
chainId: 56
}, ‘petersburg’)

const tx = new Transaction(txOptions, {common: customCommon})
const privateKey = pk

// convert the private key to buffer array
const privateKeyBuff = new Buffer(
privateKey.substring(2,66),
// example private key without the 0x
’e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109',
‘hex’,
)
console.log(privateKeyBuff)
tx.sign(privateKeyBuff)

const serializedTrans = tx.serialize()
const raw = ‘0x’ + serializedTrans.toString(‘hex’)
console.log(raw)

const txHash = await web3.eth.sendSignedTransaction(raw)
console.log('txHash:', txHash)
// end of sending a raw transaction

--

--

--

Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Modern JavaScript in Django templates

AutoComplete With JQuery

React : Let’s Get Started…

The Biggest Problem With 코인카지노, And How You Can Fix It

The Ship Of Theseus — Next.Js

Website image.

“Class-fields-proposal” or “what went wrong in tc39 committee”

Building OTP authentication with ReactJS and AWS Amplify

CSS Lifehack: Avatar with Dynamic Text Wrapping

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Victor Yeo

Victor Yeo

Engineer

More from Medium

Creating Gashapon GameCenter — Solidity & Remix

Steps to transfer ERC20 tokens through Polygon PoS Bridge

Ethereum : Contract Development with Ganache and Remix

The GRAPH and Uniswap v3 Queries