Advertisement
Guest User

Untitled

a guest
Aug 10th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.                     let tx = {
  2.                         to: destination,
  3.                         value: web3.utils.numberToHex(web3.utils.toWei(amount, 'ether'))
  4.                     }
  5.                     let privateKey = undefined
  6.                    
  7.                     // Promises whoop whoop
  8.                     return web3.eth.getGasPrice()
  9.                         .then(gasPrice => {                            
  10.                             tx.gasPrice = web3.utils.toHex(gasPrice)
  11.                             tx.gasLimit = web3.utils.toHex(42000)
  12.                            
  13.                             return web3.eth.net.getId()
  14.                         })                        
  15.                         .then(chainId => {
  16.                             tx.chainId = parseInt(chainId)                            
  17.  
  18.                             return Knex("wallet").where({
  19.                                 user: id,
  20.                                 currency: "ETH"
  21.                             }).select()
  22.                         })
  23.                         .then((results) => {                            
  24.                             if (!results || results.length === 0) {
  25.                                 return {
  26.                                     message: "User not found"
  27.                                 }
  28.                             }                            
  29.                            
  30.                             // Sign transaction
  31.                             tx.from = results[0].publicKey
  32.                             privateKey = results[0].privateKey                            
  33.                                                        
  34.                             // Send transaction                            
  35.                             return web3.eth.getTransactionCount(tx.from)
  36.                         }).then((nonce) => {
  37.                             tx.nonce = web3.utils.numberToHex(nonce)
  38.  
  39.                             const ethtx = new EthTx(tx)                            
  40.                             ethtx.sign(new Buffer(privateKey.slice(2), 'hex'))
  41.                             const serializedTx = ethtx.serialize().toString('hex')
  42.  
  43.                             // return { hash: ethtx.serialize().toString('hex') }
  44.                             // Broadcasts the transaction
  45.                             try{
  46.                                 web3.eth.sendSignedTransaction('0x' + serializedTx)
  47.  
  48.                                 return {
  49.                                     txhash: '0x' + new EthTx(serializedTx).hash().toString('hex')
  50.                                 }
  51.                             } catch (e) {
  52.                                 return {
  53.                                     err: e,
  54.                                     message: 'Unknown error occured'
  55.                                 }
  56.                             }
  57.                         }).catch(err => {
  58.                             return {
  59.                                 err,
  60.                                 message: "An error occured"
  61.                             }
  62. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement