Guest User

Untitled

a guest
May 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. var fromaddress = req.body._fromaddress;
  2. var amount = req.body._amount;
  3. var privatekey = req.body._privatekey;
  4. var toAddress = req.body._toAddress;
  5.  
  6. // Step 1
  7. var payloadData = web3.toHex(web3.toWei(amount, 'ether'));
  8. var transactionObject = {
  9. 'from': fromaddress,
  10. 'to': toAddress,
  11. 'value': payloadData
  12. }
  13. gasLimit = web3.eth.estimateGas(transactionObject);
  14.  
  15. // Step 2
  16. gasPrice = web3.eth.gasPrice;
  17. totalGas = gasPrice * gasLimit;
  18.  
  19. var amountToSend = web3.toWei(amount, 'ether') - totalGas;
  20.  
  21. payloadData = web3.toHex(amountToSend);
  22. gasPriceHex = web3.toHex(gasPrice);
  23. gasLimitHex = web3.toHex(gasLimit);
  24.  
  25.  
  26. nonce = web3.eth.getTransactionCount(fromaddress, "pending");
  27. nonceHex = web3.toHex(nonce);
  28.  
  29. var rawTx = {
  30. nonce: nonceHex,
  31. gasPrice: gasPriceHex,
  32. gasLimit: gasLimitHex,
  33. to: toAddress,
  34. from: fromaddress,
  35. value: payloadData,
  36. data: '0x00'
  37. };
  38.  
  39. // Step 3
  40. var key = Buffer.from(privatekey, 'hex');
  41. var tx = new Tx(rawTx);
  42. tx.sign(key);
  43.  
  44. var serializedTx = tx.serialize();
  45.  
  46. web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function (err, hash) {
  47. if (err) {
  48. res.status(401).json("" + err);
  49. }
  50. else {
  51. res.json({"status": true, "hash": hash});
  52. }
  53. });
Add Comment
Please, Sign In to add comment