Skip to content

Commit

Permalink
Adding fees & external configuration files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryptotron committed Apr 9, 2013
1 parent 95de8a1 commit 657e375
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 30 deletions.
13 changes: 13 additions & 0 deletions config/testnet.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
http_port : 80, // http interface port
daemon_poll_freq : 5000, // how often to poll daemon for status updates
http_poll_freq : 2000, // how often should http client poll server for status updates

daemon : {
host: '127.0.0.1',
port: 20332,
user: 'testnet',
pass: 'q1w2e3r4'
},

}
61 changes: 40 additions & 21 deletions testnet.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
// A miniature HTTP interface for litecoind

// Configuration:

var HTTP_SERVER_PORT = 8080;

var LITECOIN_RPC_CONFIG = {
host: '127.0.0.1',
port: 20332,
user: 'testnet',
pass: 'q1w2e3r4'
}

var DAEMON_POLL_FREQ = 5000;
var HTTP_POLL_FREQ = 2000;

// ----------------------------------------------------------

var JSON = require("json"),
fs = require('fs'),
os = require('os'),
Expand All @@ -28,6 +12,26 @@ var JSON = require("json"),

function dpc(t,fn) { if(typeof(t) == 'function') setTimeout(t,0); else setTimeout(fn,t); }

// Read config from the config folder. This function will check for 'name.hostname.cfg'
// and if not present, it will read 'name.cfg'. This allows user to create a custom
// local config file without distrupting the main config.
function get_config(name) {

var host_filename = __dirname + '/config/'+name+'.'+os.hostname()+'.cfg';
var filename = __dirname + '/config/'+name+'.cfg';
var data = undefined;
if(fs.existsSync(host_filename)) {
data = fs.readFileSync(host_filename);
console.log("Reading config:",host_filename);
}
else {
data = fs.readFileSync(filename);
console.log("Reading config:",filename);
}

return eval('('+data.toString('utf-8')+')');
}

function no_cache(res, is_json) {
res.header("Cache-Control", "no-cache, no-store, must-revalidate");
res.header("Pragma", "no-cache");
Expand All @@ -40,9 +44,11 @@ function Application() {
var self = this;
self.status = { }

var config = get_config('testnet');

console.log("Starting...".bold);

self.client = new wallet_interface.Client(LITECOIN_RPC_CONFIG);
self.client = new wallet_interface.Client(config.daemon);

var app = express();

Expand All @@ -53,7 +59,7 @@ function Application() {
});

app.get('/', function(req, res, next) {
res.render('index.ejs', { self : self, HTTP_POLL_FREQ : HTTP_POLL_FREQ });
res.render('index.ejs', { self : self, HTTP_POLL_FREQ : config.http_poll_freq });
});

app.get('/status', function(req, res) {
Expand All @@ -80,6 +86,19 @@ function Application() {
})
});

app.get('/fee', function(req, res) {
no_cache(res, true);
if(!req.query.fee)
return res.end(JSON.stringify({ error : "fee value is required" }));

self.client.setTxFee(parseFloat(req.query.fee), function(err) {
if(err)
return res.end(JSON.stringify(err));

res.end("\"OK - FEE IS SET TO "+req.query.fee+" (info should change in few sec.)\"");
})
});

app.get('/tx/:txhash', function(req, res) {
no_cache(res, true);

Expand Down Expand Up @@ -112,8 +131,8 @@ function Application() {

app.use(express.static('http/'));

console.log("HTTP server listening on ports: ",HTTP_SERVER_PORT);
http.createServer(app).listen(HTTP_SERVER_PORT);
console.log("HTTP server listening on ports: ",config.http_port);
http.createServer(app).listen(config.http_port);

function init() {
self.client.getAccountAddress('', function(err, address) {
Expand Down Expand Up @@ -150,7 +169,7 @@ function Application() {
console.error("getBalance error:", err);
self.status.balance_1 = balance_1;

dpc(DAEMON_POLL_FREQ, update_status);
dpc(config.daemon_poll_freq, update_status);

})
})
Expand Down
56 changes: 47 additions & 9 deletions views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
}
button, input { font-family: "Consolas"; }
table { border-collapse: collapse; }
td, th { padding: 8px; text-align: center; border: 1px solid #ccc; }
</style>

<script type="text/javascript">
Expand All @@ -39,24 +43,51 @@ function Application() {
success: function(response) {
dpc(<%= HTTP_POLL_FREQ %>,self.update);
$("#info").html(jt(response.info));
$("#peer_info").html(jt(response.peer_info));
$("#address").html(response.address);
$("#balance_0").html(response.balance_0);
$("#balance_1").html(response.balance_1);
var str = "";
// ---
str = "<table>"
_.each(response.info, function(v,n) {
str += "<tr><td style='text-align:left;'>"+n+"</td><td style='text-align:left;'>"+v+"</td></tr>";
})
str += "</table>";
$("#info").html(str);
// ---
str = "<table><tr><th>ADDR</th><th>SERVICES</th><th>LASTSEND</th><th>LASTRECV</th><th>CONNTIME</th><th>VERSION</th><th>SUBVER</th><th>INBOUND</th><th>RELEASE<br/>TIME</th><th>STARTING<br/>HEIGHT</th><th>BANSCORE</th></tr>";
var template =
_.each(response.peer_info, function(peer) {
str += "<tr><td>"+peer.addr+"</td><td>"+peer.services+"</td><td>"+peer.lastsend+"</td><td>"+peer.lastrecv+"</td><td>"+peer.conntime+"</td><td>"+peer.version+"</td><td>"+peer.subver+"</td><td>"+peer.inbound+"</td><td>"+peer.releasetime+"</td><td>"+peer.startingheight+"</td><td>"+peer.banscore+"</td></tr>";
});
str += "</table>";
$("#peer_info").html(str);
}
});
}
self.update();
$("#send").click(function(){
var amount = $("#send_amount").val();
var address = $("#send_address").val();
var amount = $("#send_amount").val();
var address = $("#send_address").val();
$.getJSON("/send?address="+address+"&amount="+amount, function(data) {
$("#send_response").html(jt(data));
});
$.getJSON("/send?address="+address+"&amount="+amount, function(data) {
$("#send_response").html(jt(data));
});
})
$("#set_fee").click(function(){
var fee = $("#fee_amount").val();
$.getJSON("/fee?fee="+fee, function(data) {
$("#fee_response").html(jt(data));
});
})
$("#tx_lookup").click(function(){
Expand Down Expand Up @@ -100,6 +131,13 @@ AMOUNT: <input id='send_amount' type='text' />
RESPONSE:
<span id='send_response'></span>

--- SET TX FEE ---
FEE: <input id='fee_amount' type='text' />
<button id='set_fee'>SET TX FEE</button>

RESPONSE:
<span id='fee_response'></span>

-------------------
LOOKUP TX HASH: <input id='tx_input' type='text' style='width:400px;' /><button id='tx_lookup'>LOOKUP</button>
<span id='tx_info'></span>
Expand All @@ -108,10 +146,10 @@ LOOKUP TX HASH: <input id='tx_input' type='text' style='width:400px;' /><button
LOOKUP BLOCK HASH: <input id='block_input' type='text' style='width:400px;' /><button id='block_lookup'>LOOKUP</button>
<span id='block_info'></span>

--- getInfo ---
INFO:
<span id='info'></span>

--- getPeerInfo ---
CONNECTED PEERS:
<span id='peer_info'></span>

</div>
Expand Down

0 comments on commit 657e375

Please sign in to comment.