Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
uraymeiviar committed Dec 23, 2015
1 parent 5aae690 commit 4a178d9
Show file tree
Hide file tree
Showing 14 changed files with 619 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Created by .ignore support plugin (hsz.mobi)
default.json
node_modules
dbnavigator.xml
workspace.xml
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/ethereum-autosend.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "ethereum-autosend",
"version": "0.0.1",
"description": "node.js application to manage ethereum account, including auto send balance from one account to another",
"license": "GPLv2",
"bin": "server.js",
"url":"https://pool.ethernet.id",
"config" : {
"serverPort" : "8080"
},
"repository" : {
"type" : "git",
"url" : "https://github.com/uraymeiviar/ethereum-autosend.git"
},
"dependencies" : {
"express": "*",
"socket.io": "*",
"async":"*",
"compression" : "*",
"chalk" : "*",
"request" : "*",
"config" : "*",
"body-parser" : "*",
"moment" : "*",
"bn.js" : "*",
"prettyjson" : "*"
}
}
52 changes: 52 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"use strict";

var express = require("express");
var compression = require('compression');
var expressApp = express();
expressApp.use(compression());
var webServer = require('./server/webserver.js');
var config = require('config');
var chalk = require('chalk');
var rpc = require('./server/ethrpc.js');
var accounting = require('./server/accounting.js');

var logError = chalk.red;
var logWarning = chalk.yellow;
var logInfo = chalk.gray;
var logSuccess = chalk.green;

console.log(logInfo("starting server.."));

var bodyParser = require('body-parser');
expressApp.use(bodyParser.json()); // to support JSON-encoded bodies
expressApp.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
expressApp.use(bodyParser.text());
expressApp.use(bodyParser.raw());

rpc.init(false, function(){
webServer.init(expressApp,function(){
accounting.init(function(){
rpc.startBlockPooler();
});
});
});

process.stdin.resume();

function exitHandler(options, err) {
if (options.cleanup) {
console.log(logInfo('clean'));
}
if (err) {
console.log(logError(err.stack));
}
if (options.exit) {
console.log(logWarning('received exit by SIGINT'));
process.exit();
}
}

process.on('exit', exitHandler.bind(null,{cleanup:true}));
process.on('SIGINT', exitHandler.bind(null, {exit:true}));
106 changes: 106 additions & 0 deletions server/accounting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"use strict";
var config = require('config');
var rpc = require('./ethrpc');
var async = require('async');
var chalk = require('chalk');

var logError = chalk.red;
var logWarning = chalk.yellow;
var logInfo = chalk.gray;
var logSuccess = chalk.green;

module.exports = {
gasPrice : 0,
processAutoSend: function(from, balance, config, done){
var self = this;
var minimal = 0;
if(config.hasOwnProperty('minimal')){
minimal = parseInt(config.minimal);
}
if( (balance > minimal) && config.hasOwnProperty('to') && (from !== config.to) ){
rpc.estimageSendTxGas(from,config.to,balance,function(result,gas){
var gasNeeded = 20000;
if(result){
gasNeeded = gas;
console.log(logInfo("gas requirement estimation : "+gasNeeded));
console.log(logInfo("estimated transaction cost : "+(gasNeeded*self.gasPrice/1000000000000000000).toFixed(6)));
}
var amount = balance - gasNeeded*self.gasPrice;
rpc.unlockAccount(from,config.password,60,function(result,reason){
if(result){
rpc.sendTransactionAdv(from, config.to, amount, gasNeeded, self.gasPrice,
function(err, res, body){
if( err !== null){
console.log(logError("failed to send "+amount+" from:"+from+" to:"+config.to));
console.log(logError("err = "+err));
}
else{
if(body.hasOwnProperty('error')){
console.log(logError("failed to send "+amount+" from:"+from+" to:"+config.to));
console.log(logError("reply = "+body.error.message));
}
else{
console.log(logSuccess("sent "+amount+" from:"+from+" to:"+config.to));
console.log(logInfo("TX = "+body.result));
}
}
done();
}
);
}
else{
console.log(logError("failed to send "+balance+" from:"+from+" to:"+config.to));
console.log(logError("reply = "+reason.error.message));
done();
}
});
});
}
else{
done();
}
},
processAccount: function (accountName, accountConfig, done) {
var self = this;
rpc.getBalance(accountConfig.accountId,function(err,res,body){
if((err===null) && (body!==undefined) && body.hasOwnProperty('result')){
var balance = parseInt(body.result,16);
var balanceEth = (balance/1000000000000000000).toFixed(6);
console.log(accountConfig.accountId+" : "+balanceEth+" "+accountName);
if(accountConfig.hasOwnProperty('autosend')){
self.processAutoSend(accountConfig.accountId,balance,accountConfig.autosend, done);
}
else{
done();
}
}
else{
done();
}
});
},

eventNewBlockHandler: function(blockNum){
var self = this;
rpc.getGasPrice(function(result,price){
if(result){
self.gasPrice = price;
console.log(logInfo("gas price = "+(price/1000000000000000000).toFixed(9)));
}
if(config.has('accounts')){
var accounts = config.get('accounts');
async.eachSeries(Object.keys(accounts), function(accountName, callback){
self.processAccount(accountName, accounts[accountName], callback);
});
}
});
},

init: function (done) {
var self = this;
rpc.eventNewBlock.push(function(blockNum){
self.eventNewBlockHandler(blockNum);
});
done();
}
};
Loading

0 comments on commit 4a178d9

Please sign in to comment.