Created
June 28, 2019 09:16
-
-
Save samjoshuva/a0838d99567c0788e1e10ddbdbd25603 to your computer and use it in GitHub Desktop.
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 characters
| async function sendSignedTransactionsForContracts(data) { | |
| let txObject = {}; | |
| let nonce = null; | |
| let gasPrice = null; | |
| return new Promise(async (resolve, reject) => { | |
| await Promise.all([web3.eth.getTransactionCount(senderAddress), web3.eth.getGasPrice()]) | |
| .then(result => { | |
| nonce = result[0]; | |
| gasPrice = result[1]; | |
| gasPrice = (parseInt(gasPrice) + parseInt(gasPrice / 2)).toString(); | |
| txObject = { | |
| from: senderAddress, | |
| nonce: web3.utils.toHex(nonce), | |
| data: data | |
| }; | |
| // console.log(txObject) | |
| return web3.eth.estimateGas(txObject); | |
| }) | |
| .then(estimateGas => { | |
| // console.log(estimateGas) | |
| txObject.gasPrice = web3.utils.toHex(gasPrice); | |
| txObject.gas = web3.utils.toHex(estimateGas); | |
| const tx = new Tx(txObject); | |
| const privateKey = Buffer.from(senderPrivateKey, 'hex'); | |
| tx.sign(privateKey); | |
| const serializedTx = tx.serialize(); | |
| const raw = '0x' + serializedTx.toString('hex'); | |
| // console.log({ "raw .....": raw }) | |
| web3.eth.sendSignedTransaction(raw, async (err, txHash) => { | |
| console.log('err:', err, 'txHash:', txHash) | |
| // Use this txHash to find the contract on Etherscan! | |
| let getTransaction = setInterval(async () => { | |
| web3.eth.getTransactionReceipt(txHash, async function (error, result) { | |
| if (error) { | |
| clearInterval(getTransaction) | |
| // console.log({ 'errrr': error }) | |
| reject(new Error(error)) | |
| } | |
| if (result) { | |
| clearInterval(getTransaction) | |
| // console.log("Transaction result ....", result) | |
| // console.log({ "address": result.contractAddress + '', "transaction_hash": txHash + '' }) | |
| resolve(result.contractAddress) | |
| } | |
| }); | |
| }, 600) | |
| }) | |
| }) | |
| }) | |
| } | |
| async function sendSignedTransactionsForMethods(data, contract_address) { | |
| return new Promise(async (resolve, reject) => { | |
| Promise.all([web3.eth.getTransactionCount(senderAddress), web3.eth.getGasPrice()]) | |
| .then(result => { | |
| nonce = result[0]; | |
| const tx = { | |
| nonce: web3.utils.toHex(nonce), | |
| from: senderAddress, | |
| to: contract_address, | |
| gas: 2000000, | |
| data: data, | |
| }; | |
| web3.eth.accounts.signTransaction(tx, senderPrivateKey).then(signed => { | |
| const tran = web3.eth | |
| .sendSignedTransaction(signed.rawTransaction, async (err, txHash) => { | |
| console.log('err:', err, 'txHash:', txHash) | |
| // Use this txHash to find the contract on Etherscan! | |
| let getTransaction = setInterval(async () => { | |
| await web3.eth.getTransactionReceipt(txHash, async function (error, result) { | |
| if (error) { | |
| clearInterval(getTransaction) | |
| // console.log({ 'errrr': error }) | |
| reject(new Error(error)) | |
| } | |
| if (result) { | |
| clearInterval(getTransaction) | |
| // console.log({ "address": result.contractAddress + '', "transaction_hash": txHash + '' }) | |
| resolve(result) | |
| } | |
| }); | |
| }, 600) | |
| }); | |
| }); | |
| }) | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment