Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Change sendTransaction to sendRawTransaction, get nonce and minGasPrice

  • Loading branch information
Bernardo Codesido
Bernardo Codesido committed Jun 16, 2017
1 parent aef60ea commit 42424f528045f54961c69555457ff019ac8b08c5
Showing with 39 additions and 45 deletions.
  1. +3 −4 example-config.json
  2. +2 −1 package.json
  3. +34 −40 rsk-faucet.js
@@ -3,8 +3,7 @@
"rskNode": "rskNodeAddress",
"faucetAddress": "rskAddressForFaucet",
"faucetPrivateKey": "rskAddressForFaucetPrivateKey",
"valueToSend": "valueToSend",
"valueToSend": "valueToSendinhex",
"captchaSecret": "someSecret",
"gas": "gas",
"gasPrice": "gasPrice"
}
"gas": "gasinhex"
}
@@ -18,7 +18,8 @@
"cron": "latest",
"captcha": "0.1.3",
"cookie-parser": "^1.4.3",
"session-file-store": "1.0.0"
"session-file-store": "1.0.0",
"ethereumjs-tx": "latest"
},
"devDependencies": {
"simpleunit": "0.0.7"
@@ -8,10 +8,10 @@ var app = express();

var fs = require('fs');
var Web3 = require('web3');
var Tx = require('ethereumjs-tx');
var CronJob = require('cron').CronJob;
const cookieParser = require('cookie-parser')


// compress all responses
app.use(compression({filter: shouldCompress}))

@@ -25,7 +25,6 @@ function shouldCompress (req, res) {
return compression.filter(req, res)
}


app.use('/css', express.static('css'));
app.use('/img', express.static('img'));
app.use('/lib', express.static('lib'));
@@ -90,7 +89,6 @@ var job = new CronJob({
}, start: false, timeZone: 'America/Los_Angeles'});
job.start();


function getWeb3() {
if (web3)
return web3;
@@ -101,15 +99,17 @@ function getWeb3() {

return web3;
}

var web3;
getWeb3();

extendWeb3();
getWeb3();

function executeTransfer(destinationAddress) {
loadPk();
var result = web3.eth.sendTransaction({from: faucetAddress, to: destinationAddress.toLowerCase(), gasPrice: gasPrice, gas: gas, value: valueToSend});
console.log('transaction hash', result);
var rawTx = buildTx(destinationAddress, getNonce(faucetAddress), getGasPrice());
var result = web3.eth.sendRawTransaction(rawTx.toString('hex'), function(err, hash){
if (!err)
console.log('transaction hash', hash);
});
}

function readConfig(){
@@ -120,39 +120,37 @@ function readConfig(){
faucetPrivateKey = obj.faucetPrivateKey;
valueToSend = obj.valueToSend;
captchaSecret = obj.captchaSecret;
gasPrice = obj.gasPrice;
gas = obj.gas;
}

function extendWeb3() {
web3._extend({
property: 'personal',
methods: [new web3._extend.Method({
name: 'importRawKey',
call: 'personal_importRawKey',
params: 2,
inputFormatter: [function (value) { return value; }, function (value) { return value; }],
outputFormatter: null
})]
});
function buildTx(account, nonce, gasPrice) {
var rawTx = {
nonce: nonce,
gasPrice: gasPrice,
gas: gas,
value: valueToSend,
to: account
}

var tx = new Tx(rawTx);
tx.sign(faucetPrivateKey);
var serializedTx = tx.serialize();
return serializedTx;
}

web3._extend({
property: 'personal',
methods: [new web3._extend.Method({
name: 'unlockAccount',
call: 'personal_unlockAccount',
params: 3,
inputFormatter: [function (value) { return value; }, function (value) { return value; }, function (value) { return value; }],
outputFormatter: null
})]
});
function getNonce(){
var result = web3.eth.getTransactionCount(faucetAddress, "pending");
return result;
}

function loadPk() {
console.log('Loding PK to node');
var result = web3.personal.importRawKey(faucetPrivateKey, "passPhraseToEncryptPrivKey");
var result = web3.personal.unlockAccount(faucetAddress, "passPhraseToEncryptPrivKey", "0xE10");
console.log('PKs loaded to the node');
function getGasPrice(){
var block = web3.eth.getBlock("latest")
console.log(block.minimumGasPrice);
if (block.minimumGasPrice <= 21000) {
return 21000;
} else {
return block.minimumGasPrice;
}
}

function accountAlreadyUsed(account) {
@@ -175,7 +173,6 @@ app.get('/*', function (req, res, next) {
next();
});


app.get(captchaUrl, captcha.image());

app.get('/balance', function (req, res) {
@@ -197,9 +194,6 @@ app.post('/', function (req, res) {
return res.status(400).send('Address already used today.');
}


//
//
if(req.body[captchaFieldName] === undefined || req.body[captchaFieldName] === '' || req.body[captchaFieldName] === null) {
console.log('No req.body.' + captchaFieldName);
return res.status(400).send("Please complete captcha.");
@@ -226,4 +220,4 @@ app.post('/', function (req, res) {

app.listen(port, function () {
console.log('RSK Faucet started on port ' + port);
});
});

0 comments on commit 42424f5

Please sign in to comment.
You can’t perform that action at this time.