Skip to content

Instantly share code, notes, and snippets.

@raineorshine
Last active December 3, 2022 18:02
Show Gist options
  • Select an option

  • Save raineorshine/c8b30db96d7532e15f85fcfe72ac719c to your computer and use it in GitHub Desktop.

Select an option

Save raineorshine/c8b30db96d7532e15f85fcfe72ac719c to your computer and use it in GitHub Desktop.

Revisions

  1. raineorshine revised this gist Apr 10, 2020. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions sendRawTransaction.js
    Original file line number Diff line number Diff line change
    @@ -19,9 +19,10 @@ const txData = {
    gasPrice: web3.utils.toHex(10e9), // 10 Gwei
    to: addressTo,
    from: addressFrom,
    value: web3.utils.toHex(web3.utils.toWei('123', 'wei')) // Thanks @abel30567
    // if you want to send just raw data (e.g. contract execution) rather than sending tokens,
    value: web3.utils.toHex(web3.utils.toWei('123', 'wei')) // thanks @abel30567
    // if you want to send raw data (e.g. contract execution) rather than sending tokens,
    // use 'data' instead of 'value' (thanks @AlecZadikian9001)
    // e.g. myContract.methods.myMethod(123).encodeABI() (thanks @NguyenHoangSon96)
    }

    /** Signs the given transaction data and sends it. Abstracts some of the details of
  2. raineorshine revised this gist Feb 3, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion sendRawTransaction.js
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ const txData = {
    * buffering and serializing the transaction for web3.
    * @returns A promise of an object that emits events: transactionHash, receipt, confirmaton, error
    */
    function sendRawTransaction(txData) =>
    const sendRawTransaction = txData =>
    // get the number of transactions sent so far so we can create a fresh nonce
    web3.eth.getTransactionCount(addressFrom).then(txCount => {
    const newNonce = web3.utils.toHex(txCount)
  3. raineorshine revised this gist Nov 1, 2019. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions sendRawTransaction.js
    Original file line number Diff line number Diff line change
    @@ -19,9 +19,9 @@ const txData = {
    gasPrice: web3.utils.toHex(10e9), // 10 Gwei
    to: addressTo,
    from: addressFrom,
    value: web3.utils.toHex(web3.utils.toWei(123, 'wei'))
    value: web3.utils.toHex(web3.utils.toWei('123', 'wei')) // Thanks @abel30567
    // if you want to send just raw data (e.g. contract execution) rather than sending tokens,
    // use 'data' instead of 'value'
    // use 'data' instead of 'value' (thanks @AlecZadikian9001)
    }

    /** Signs the given transaction data and sends it. Abstracts some of the details of
    @@ -40,6 +40,7 @@ function sendRawTransaction(txData) =>


    // fire away!
    // (thanks @AndreiD)
    sendRawTransaction(txData).then(result =>
    result
    .on('transactionHash', txHash => {
  4. raineorshine revised this gist Nov 1, 2019. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions sendRawTransaction.js
    Original file line number Diff line number Diff line change
    @@ -20,6 +20,8 @@ const txData = {
    to: addressTo,
    from: addressFrom,
    value: web3.utils.toHex(web3.utils.toWei(123, 'wei'))
    // if you want to send just raw data (e.g. contract execution) rather than sending tokens,
    // use 'data' instead of 'value'
    }

    /** Signs the given transaction data and sends it. Abstracts some of the details of
  5. raineorshine revised this gist Nov 1, 2019. 1 changed file with 41 additions and 27 deletions.
    68 changes: 41 additions & 27 deletions sendRawTransaction.js
    Original file line number Diff line number Diff line change
    @@ -6,38 +6,52 @@ const web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io

    // the address that will send the test transaction
    const addressFrom = '0x1889EF49cDBaad420EB4D6f04066CA4093088Bbd'
    const privKey = 'PRIVATE_KEY'
    const privateKey = new Buffer('PRIVATE_KEY', 'hex')

    // 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)
    // construct the transaction data
    // NOTE: property 'nonce' must be merged in from web3.eth.getTransactionCount
    // before the transaction data is passed to new Tx(); see sendRawTransaction below.
    const txData = {
    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'))
    }

    // 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)
    /** Signs the given transaction data and sends it. Abstracts some of the details of
    * buffering and serializing the transaction for web3.
    * @returns A promise of an object that emits events: transactionHash, receipt, confirmaton, error
    */
    function sendRawTransaction(txData) =>
    // get the number of transactions sent so far so we can create a fresh nonce
    web3.eth.getTransactionCount(addressFrom).then(txCount => {
    const newNonce = web3.utils.toHex(txCount)
    const transaction = new Tx({ ...txData, nonce: newNonce }, { chain: 'mainnet' }) // or 'rinkeby'
    transaction.sign(privateKey)
    const serializedTx = transaction.serialize().toString('hex')
    return web3.eth.sendSignedTransaction('0x' + serializedTx)
    })

    })

    // fire away!
    sendRawTransaction(txData).then(result =>
    result
    .on('transactionHash', txHash => {
    console.log('transactionHash:', txHash)
    })
    .on('receipt', receipt => {
    console.log('receipt:', receipt)
    })
    .on('confirmation', (confirmationNumber, receipt) => {
    if (confirmationNumber >= 1) {
    console.log('confirmations:', confirmationNumber, receipt)
    }
    })
    .on('error:', error => {
    console.error(error)
    })
    )
  6. raineorshine revised this gist Aug 1, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion sendRawTransaction.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    const Web3 = require('web3')
    const Tx = require('ethereumjs-tx')
    const Tx = require('ethereumjs-tx').Transaction

    // connect to Infura node
    const web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/INFURA_KEY'))
  7. raineorshine revised this gist Sep 19, 2017. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions sendRawTransaction.js
    Original file line number Diff line number Diff line change
    @@ -24,10 +24,10 @@ function sendSigned(txData, cb) {
    // get the number of transactions sent so far so we can create a fresh nonce
    web3.eth.getTransactionCount(addressFrom).then(txCount => {

    // contstruct the transaction data
    // construct the transaction data
    const txData = {
    nonce: web3.utils.toHex(txCount),
    gasLimit: web3.utils.toHex(21000),
    gasLimit: web3.utils.toHex(25000),
    gasPrice: web3.utils.toHex(10e9), // 10 Gwei
    to: addressTo,
    from: addressFrom,
    @@ -37,7 +37,7 @@ web3.eth.getTransactionCount(addressFrom).then(txCount => {
    // fire away!
    sendSigned(txData, function(err, result) {
    if (err) return console.log('error', err)
    console.log('success', result)
    console.log('sent', result)
    })

    })
  8. raineorshine created this gist Sep 19, 2017.
    43 changes: 43 additions & 0 deletions sendRawTransaction.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    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 => {

    // contstruct the transaction data
    const txData = {
    nonce: web3.utils.toHex(txCount),
    gasLimit: web3.utils.toHex(21000),
    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('success', result)
    })

    })