Last active
May 17, 2019 18:30
-
-
Save Chanutg/73c47815e797cb6157a21afeb6fae469 to your computer and use it in GitHub Desktop.
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
const Web3 = require('web3'); | |
const web3js = new Web3(Web3.givenProvider || 'https://ropsten.infura.io/VOTRE_ACCES_INFURA', null, {}); | |
const Tx = require('ethereumjs-tx'); | |
const express = require('express'); | |
const router = express.Router(); | |
const BigNumber = require('bignumber.js'); | |
var contractABI = CODE_ABI_DE_VOTRE_TOKEN | |
contractAddress = "ADRESSE_DU_CONTRAT_DE_VOTRE_TOKEN"; | |
let makeTransaction = (fromAdresse, toAdresse, amount) => { | |
var tokenContract = new web3js.eth.Contract(contractABI, contractAddress); | |
var myAddress = 'VOTRE_ADRESSE'; | |
var privateKey = Buffer.from('VOTRE_CLE_PRIVEE', 'hex') | |
var count; | |
return new Promise ((res, rej) => { | |
if (!fromAdresse || !toAdresse || !amount){ | |
return res({ | |
message: 'You must send all datas' | |
}); | |
} | |
return web3js.eth.getTransactionCount(myAddress) | |
.then(function(c){ | |
count = c; | |
hexamount = web3js.utils.toHex(amount * DECIMALS_DE_VOTRE_TOKEN); | |
var rawTransaction = {"from":myAddress, "gasPrice":web3js.utils.toHex(20 * 1e9),"gasLimit":web3js.utils.toHex(210000),"to":contractAddress,"value":"0x0","data":tokenContract.methods.transfer(toAdresse, hexamount).encodeABI(),"nonce":web3js.utils.toHex(count)} | |
var transaction = new Tx(rawTransaction); | |
transaction.sign(privateKey); | |
web3js.eth.sendSignedTransaction('0x'+transaction.serialize().toString('hex')) | |
return res({ | |
message: "transaction from " + myAddress + " to " + toAdresse + " of " + amount + " done !" | |
}); | |
}) | |
.catch(error => { | |
console.log('caught', error.message); | |
}); | |
}); | |
}; | |
router.post("/sendtx", (req, res) => { | |
var myAddress = 'VOTRE_ADDRESSE'; | |
var privateKey = Buffer.from('VOTRE_CLE_PRIVEE', 'hex') | |
makeTransaction(myAddress, req.body.to, req.body.amount) | |
.then(transaction => { | |
res.send({ | |
success: true, | |
message: transaction.message | |
}); | |
}) | |
.catch(err => { | |
console.log("error"); | |
res.send( | |
{ | |
success: false, | |
err: err.message | |
}); | |
}); | |
}); | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment