Last active
August 1, 2019 03:53
-
-
Save suryapratap1/3659831cbf9991cf53f5cccbb2e4a5a6 to your computer and use it in GitHub Desktop.
manage transfer Action
This file contains 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
var Web3 = require('web3'); | |
var BigNumber = require('bignumber.js'); | |
let Tx = require('ethereumjs-tx'); | |
var web3 = new Web3(Web3.givenProvider || 'http://3.0.185.98:9031');//mainnet | |
var Accounts = require('web3-eth-accounts'); | |
var accounts = new Accounts('http://3.0.185.98:9031');//mainnet | |
const safeJsonStringify = require('safe-json-stringify'); | |
const numberToBN = require('number-to-bn'); | |
var CircularJSON = require('circular-json'); | |
const request = require('request'); | |
getBalance = (address, cb) => { | |
web3.eth.getBalance(address).then(amount => { | |
console.log("amount==>>", amount) | |
amount = new BigNumber(amount).dividedBy(new BigNumber(Math.pow(10, 18))) | |
console.log("amountBigNo.==>>", amount) | |
cb(null, amount.toFixed(18)) | |
}).catch(err => { | |
cb(null, err) | |
}) | |
} | |
getCurrentGasPrice = (cb) => { | |
web3.eth.getGasPrice() | |
.then((currentGasPrice) => { | |
console.log("currentGasPrice===>>", currentGasPrice) | |
return cb(currentGasPrice) | |
}) | |
} | |
estGas = (toAddr, fromAddr, value, cb) => { | |
web3.eth.estimateGas({ | |
from: fromAddr, | |
to: toAddr, | |
value: value | |
}).then((estmdGas) => { | |
console.log(" Your estmdGas is ==>>", estmdGas) | |
return cb(estmdGas) | |
}).catch(console.log) | |
} | |
getTxnCountForNonce = (addr, cb) => { | |
web3.eth.getTransactionCount(addr) | |
.then((count) => { | |
return cb(count) | |
}); | |
} | |
signTxn = (toAddr, fromAddr, value, key, cb) => { | |
estGas(toAddr, fromAddr, value, (estmdGas) => { | |
getCurrentGasPrice((currentGasPrice) => { | |
getTxnCountForNonce(fromAddr, (hardCount) => { | |
let rawTx = { | |
nonce: web3.utils.toHex(hardCount), | |
let rawTx = { | |
nonce: web3.utils.toHex(hardCount), | |
from: web3.utils.toHex(fromAddr), | |
gasPrice: web3.utils.toHex(50 * 1e9), | |
gas: web3.utils.toHex(estmdGas), | |
to: web3.utils.toHex(toAddr), | |
value: web3.utils.toHex(value) | |
} | |
var tx = new Tx(rawTx); | |
tx.sign(key); | |
let serializedTx = tx.serialize(); | |
console.log("serializedTx", serializedTx) | |
let cbData = '0x' + serializedTx.toString('hex') | |
console.log("cb Data is ", cbData) | |
cb(cbData) | |
}) | |
}) | |
}) | |
} | |
module.exports = { | |
get_wallet: (req, res) => { | |
var privateKey = web3.eth.accounts.wallet.create(1, "MyPassword") | |
var objInfo = privateKey.length - 1; | |
var result = { | |
address: privateKey[objInfo].address, | |
privateKey: privateKey[objInfo].privateKey | |
} | |
return res.send({ code: 200, Result: result }) | |
}, | |
///////Getting an payment//////// | |
get_payment: (req, res) => { | |
console.log("req.body1111======>>>", req.body) | |
if (!req.body.privateKey || !req.body.fromAddr || !req.body.toAddr || !req.body.value) { | |
return res.send({ code: 400, message: "Parameters Missing!!" }) | |
} | |
getBalance(req.body.fromAddr, (err, result) => { | |
console.log("result--->", result) | |
if (err) { | |
console.log("err34343", err) | |
return res.send({ code: 500, message: "Internal server error" }) | |
} | |
else if (result == undefined) { | |
console.log("*****************") | |
res.send({ code: 500, message: "Private key is Invalid!!" }) | |
} | |
else if (result) { | |
console.log("check balance ===>>", result) | |
privateKey = (req.body.privateKey).split('0x') | |
privateKey = privateKey[1] | |
console.log("PrivateKey=======>>>", privateKey) | |
var privateKey = new Buffer(privateKey, 'hex'); | |
console.log("=======>>>", privateKey) | |
var amount = new BigNumber(req.body.value).multipliedBy(new BigNumber(Math.pow(10, 18))); | |
signTxn(req.body.toAddr, req.body.fromAddr, amount, privateKey, (hash) => { | |
if (hash) { | |
console.log("hash=====>>", hash) | |
web3.eth.sendSignedTransaction(hash).then((receipt) => { | |
console.log('Transaction Hash---------->', receipt) | |
var transactionFee = (50 * 1e9) * receipt.gasUsed; | |
console.log("*************8", transactionFee) | |
var fee_data = new BigNumber(transactionFee).dividedBy(new BigNumber(Math.pow(10, 18))); | |
return res.send({ code: 200, txid: receipt.transactionHash, fee: fee_data }) | |
}).catch(err => { | |
return res.send({ code: 500, message: "Insufficients Funds!!" }) | |
}) | |
// }) | |
} | |
}) | |
} else { | |
return res.send({ code: 500, message: "Internal server error" }) | |
} | |
}) | |
}, | |
///////Getting an balance//////// | |
get_balance: (req, res) => { | |
if (!req.body.addresses) { | |
return res.send({ code: 400, message: "Parameters Missing!!" }) | |
} | |
getBalance(req.body.addresses, (err, result) => { | |
if (err) { | |
return res.send({ code: 500, message: "Internal Sever Error" }) | |
} else { | |
return res.send({ code: 200, balance: result }) | |
} | |
}) | |
}, | |
//Details from the Transaction Id | |
get_details: (req, res) => { | |
if (!req.query.txid) { | |
return res.send({ responseCode: 400, responseMessage: "Parameters Missing!!" }) | |
} | |
var command = { | |
"jsonrpc": "2.0", | |
"id": "1", | |
"method": "eth_getTransactionByHash", | |
"params": [ | |
req.query.txid | |
] | |
} | |
var options = { | |
url: 'http://3.0.185.98:8545/', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
form: JSON.stringify(command) | |
}; | |
request(options, function (error, response, data) { | |
const arr = [] | |
const data_result = JSON.parse(data) | |
const d_value = parseInt(data_result.result.value) | |
var amount = new BigNumber(d_value).dividedBy(new BigNumber(Math.pow(10, 18))); | |
const d_gas = parseInt(data_result.result.gas) | |
var fee = new BigNumber(d_gas).dividedBy(new BigNumber(Math.pow(10, 9))); | |
arr.push({ | |
txid: data_result.result.hash, | |
from: data_result.result.from, | |
to: data_result.result.to, | |
value: amount, | |
gas: fee | |
}) | |
if (!error && response.statusCode == 200) { | |
res.send({ responseCode: 200, responseMessage: "Success", Details: arr }) | |
} | |
else | |
res.send({ responseCode: 500, responseMessage: "Internal Sever Error" }) | |
}) | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment