Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let tx = {
- to: destination,
- value: web3.utils.numberToHex(web3.utils.toWei(amount, 'ether'))
- }
- let privateKey = undefined
- // Promises whoop whoop
- return web3.eth.getGasPrice()
- .then(gasPrice => {
- tx.gasPrice = web3.utils.toHex(gasPrice)
- tx.gasLimit = web3.utils.toHex(42000)
- return web3.eth.net.getId()
- })
- .then(chainId => {
- tx.chainId = parseInt(chainId)
- return Knex("wallet").where({
- user: id,
- currency: "ETH"
- }).select()
- })
- .then((results) => {
- if (!results || results.length === 0) {
- return {
- message: "User not found"
- }
- }
- // Sign transaction
- tx.from = results[0].publicKey
- privateKey = results[0].privateKey
- // Send transaction
- return web3.eth.getTransactionCount(tx.from)
- }).then((nonce) => {
- tx.nonce = web3.utils.numberToHex(nonce)
- const ethtx = new EthTx(tx)
- ethtx.sign(new Buffer(privateKey.slice(2), 'hex'))
- const serializedTx = ethtx.serialize().toString('hex')
- // return { hash: ethtx.serialize().toString('hex') }
- // Broadcasts the transaction
- try{
- web3.eth.sendSignedTransaction('0x' + serializedTx)
- return {
- txhash: '0x' + new EthTx(serializedTx).hash().toString('hex')
- }
- } catch (e) {
- return {
- err: e,
- message: 'Unknown error occured'
- }
- }
- }).catch(err => {
- return {
- err,
- message: "An error occured"
- }
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement