Last active
December 3, 2022 18:02
-
-
Save raineorshine/c8b30db96d7532e15f85fcfe72ac719c to your computer and use it in GitHub Desktop.
Revisions
-
raineorshine revised this gist
Apr 10, 2020 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 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 -
raineorshine revised this gist
Feb 3, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 */ 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) -
raineorshine revised this gist
Nov 1, 2019 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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')) // Thanks @abel30567 // if you want to send just raw data (e.g. contract execution) rather than sending tokens, // 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 => { -
raineorshine revised this gist
Nov 1, 2019 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 -
raineorshine revised this gist
Nov 1, 2019 . 1 changed file with 41 additions and 27 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 privateKey = new Buffer('PRIVATE_KEY', 'hex') // the destination address const addressTo = '0x1463500476a3ADDa33ef1dF530063fE126203186' // 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')) } /** 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) }) ) -
raineorshine revised this gist
Aug 1, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ const Web3 = require('web3') const Tx = require('ethereumjs-tx').Transaction // connect to Infura node const web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/INFURA_KEY')) -
raineorshine revised this gist
Sep 19, 2017 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 => { // 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, @@ -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('sent', result) }) }) -
raineorshine created this gist
Sep 19, 2017 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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) }) })