SHARE
TWEET

Untitled

a guest Oct 10th, 2019 109 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async sendTransaction(contractAddress, privateKey, from, to, amount, callback) {
  2.  
  3.     let sendAmount = web3.utils.toWei(amount.toString(), 'ether')
  4.  
  5.     const consumerContract = new web3.eth.Contract(config.erc20ABI, contractAddress);
  6.     const myData = consumerContract.methods.transfer(to, sendAmount).encodeABI();
  7.  
  8.     const gasPriceGwei = 3;
  9.     const gasLimit = 3000000;
  10.  
  11.     const [chainId, nonce] = await Promise.all([web3.eth.net.getId(), web3.eth.getTransactionCount(from, 'pending')]);
  12.     console.log('chainId: ' + chainId + ', nonce: ' + nonce);
  13.  
  14.     const tx = {
  15.       from,
  16.       to: contractAddress,
  17.  
  18.       gasPrice: web3.utils.toHex(gasPriceGwei * 1e9),
  19.       gasLimit: web3.utils.toHex(gasLimit),
  20.       value: '0x0',
  21.  
  22.       gas: 0,
  23.       chainId: chainId,
  24.       nonce: nonce,
  25.       data: myData
  26.     }
  27.  
  28.     const rawTx = new Tx.Transaction(tx, { chain: 'mainnet', hardfork: 'petersburg' });
  29.     const privKey = Buffer.from(privateKey, 'hex');
  30.     rawTx.sign(privKey);
  31.     const serializedTx = rawTx.serialize();
  32.  
  33.     console.log(`Attempting to send signed tx:  ${serializedTx.toString('hex')}\n------------------------`);
  34.  
  35.     const receipt = await web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'),
  36.       function (err, hash) {
  37.         if (err) {
  38.           callback(err, null)
  39.         }
  40.         callback(null, hash.toString())
  41.       })
  42.     console.log(receipt)
  43.   },
  44. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top