Skip to content

Instantly share code, notes, and snippets.

@ch3ll0v3k
Forked from raineorshine/sendRawTransaction.js
Created March 25, 2018 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ch3ll0v3k/88d8195fe63f33035159b4e7b0680acd to your computer and use it in GitHub Desktop.
Save ch3ll0v3k/88d8195fe63f33035159b4e7b0680acd to your computer and use it in GitHub Desktop.
Sends a raw transaction with web3 v1.0 and Infura
const Web3 = require('web3')
const Tx = require('ethereumjs-tx')
// connect to Infura node
const web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/INFURA_KEY'))
// the address that will send the test transaction
const addressFrom = '0x1889EF49cDBaad420EB4D6f04066CA4093088Bbd'
const privKey = 'PRIVATE_KEY'
// the destination address
const addressTo = '0x1463500476a3ADDa33ef1dF530063fE126203186'
// Signs the given transaction data and sends it. Abstracts some of the details
// of buffering and serializing the transaction for web3.
function sendSigned(txData, cb) {
const privateKey = new Buffer(config.privKey, 'hex')
const transaction = new Tx(txData)
transaction.sign(privateKey)
const serializedTx = transaction.serialize().toString('hex')
web3.eth.sendSignedTransaction('0x' + serializedTx, cb)
}
// get the number of transactions sent so far so we can create a fresh nonce
web3.eth.getTransactionCount(addressFrom).then(txCount => {
// construct the transaction data
const txData = {
nonce: web3.utils.toHex(txCount),
gasLimit: web3.utils.toHex(25000),
gasPrice: web3.utils.toHex(10e9), // 10 Gwei
to: addressTo,
from: addressFrom,
value: web3.utils.toHex(web3.utils.toWei(123, 'wei'))
}
// fire away!
sendSigned(txData, function(err, result) {
if (err) return console.log('error', err)
console.log('sent', result)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment