Skip to content
Snippets Groups Projects
Commit 19aaa317 authored by Robert Martin-Legene's avatar Robert Martin-Legene
Browse files

MasterDistiller is the CLI for managing the beneficiaries on the Distillery.

parent 42053f6d
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/node
var port = 14349; // cloud
port = 16437; // bc
const networkdir = 'network5445';
const readline = require('readline');
const rl = readline.createInterface( { input: process.stdin, output: process.stdout } );
const Web3 = require('web3');
const fs = require('fs');
const Personal = require('web3-eth-personal');
const Libbfa = require( process.env.BFAHOME + '/bin/libbfa.js');
const rl = require('readline').createInterface( { input: process.stdin, output: process.stdout } );
var web3;
var GasWell;
var from;
var contractbalance;
var contractaddr;
var Distillery;
var bfa;
var notation = [ 6 ];
function fatal( txt )
{
......@@ -22,29 +15,27 @@ function fatal( txt )
function init()
{
if ( process.env.BFAHOME == undefined )
fatal( "$BFAHOME not set. Did you source bfa/bin/env ?" );
web3 = new Web3( "http://127.0.0.1:"+port );
//accounts = new Accounts( web3 );
var abi = "";
contractaddr = fs.realpathSync([ process.env.BFAHOME, networkdir, 'contracts', 'GasWell' ].join('/'));
if ( contractaddr == undefined )
fatal( "I can't seem to find the contract directory containing the Gas Well's ABI" );
contractaddr = contractaddr.replace(/^.*\//, '');
var abipath = [ process.env.BFAHOME, networkdir, 'contracts', contractaddr, 'abi' ].join('/');
var abifile = fs.createReadStream( abipath )
.on('readable', () => {
var data;
while (data = abifile.read())
abi += data;
})
.on('end', () => {
GasWell = new web3.eth.Contract( JSON.parse(abi), contractaddr );
web3.eth.getBalance( contractaddr ).then( function(v){contractbalance=v} );
notation.push( 10**notation[0] );
switch ( notation[0] ) {
case 6:
notation.push( "Mwei" );
break;
case 9:
notation.push( "Gwei" );
break;
default:
notation = [ 6, 10**6, "Mwei" ];
}
bfa = new Libbfa();
web3 = bfa.newweb3();
Distillery = bfa.contract( web3, 'Distillery' );
web3.eth.getBalance( Distillery.contractaddress ).then(
function receivedOwnBalance(val) {
Distillery.contractbalance = val;
getlist();
})
;
getAccount();
},
function err(x){ console.log("Can't do that: "+x);process.exit(1); }
);
}
function isNumeric(n) {
......@@ -55,41 +46,7 @@ function isAddr(n) {
return n.length == 42 && n.substring(0,2) == "0x";
}
function getAccount()
{
var personal = new Personal( web3 );
personal.getAccounts().then(
function gotPersonalAccounts(obj) {
console.log("Setting default account to "+ obj[0]);
from = obj[0];
web3.eth.defaultAccount = obj[0];
},
function failedGettingPersonalAccounts(err){
fatal ("Did you remember to allow 'personal' via the RPC port? " + err)
}
);
}
function getpos(i)
{
var arr;
var p = GasWell.methods.atPosition(i).call()
.then(
function gotOneAccount(result) {
console.log( "CC: "+result);
console.log( "C1: "+result[0]);
arr = result;
},
function gotOneAccountError(err) {
console.log( "DD: "+err);
console.log(err);
}
);
return arr;
}
function wellSort(a,b)
function palletSort(a,b)
{
if ( b == undefined )
{
......@@ -111,16 +68,17 @@ function wellSort(a,b)
function requestBalances( count )
{
var well = new Array;
var pallet = new Array;
var proms = new Array;
var i;
// Fetch addresses from the list in the contract.
for ( i=0; i<count; i++ )
{
proms.push(
GasWell.methods.atPosition(i).call( {} )
Distillery.methods.atPosition(i).call( {} )
.then(
function(res){
well.push(res);
pallet.push(res);
},
function(err) { console.log("Fetching position data failed: "+err) }
)
......@@ -133,11 +91,11 @@ function requestBalances( count )
var p2 = new Array();
for ( i=0; i<count; i++ )
{
var cb = setBal.bind(null,well,i);
p2.push( web3.eth.getBalance( well[i][0] ).then(cb) );
var cb = setBal.bind(null,pallet,i);
p2.push( web3.eth.getBalance( pallet[i][0] ).then(cb) );
}
Promise.all( p2 ).then(
function allbalances(a) { displayBalances( well ) },
function allbalances(a) { displayBalances( pallet ) },
function(err) { console.log("Getting balances failed: "+err) }
);
},
......@@ -150,7 +108,7 @@ function setBal( arr, idx, val )
arr[idx][2]=val;
}
function editAccount( entry, well )
function editAccount( entry, pallet )
{
if ( entry == undefined )
return;
......@@ -160,10 +118,10 @@ function editAccount( entry, well )
if ( isAddr(entry) )
{
var i = 0;
var n = well.length;
var n = pallet.length;
while ( i < n )
{
if ( (""+well[i][0]).toLowerCase() == (""+entry).toLowerCase() )
if ( (""+pallet[i][0]).toLowerCase() == (""+entry).toLowerCase() )
entry = i;
i++;
}
......@@ -175,23 +133,35 @@ function editAccount( entry, well )
value = 0;
}
else
if ( isNumeric(entry) && entry < well.length )
if ( isNumeric(entry) && entry < pallet.length )
{
acct = well[entry][0];
value = well[entry][1];
acct = pallet[entry][0];
value = pallet[entry][1];
}
else
if ( entry == "x" )
{
// trigger Gas Well distribution
GasWell.methods.distribute().send( {"from": from, "gas": 1000000 } )
// trigger distribution
Distillery.methods.distribute().send( {"from": bfa.account, "gas": 1000000 } )
.then(
function distOK(x) {
console.log("Distribute returned succesfully in block# "+x.blockNumber+" using "+x.gasUsed+" gas.");
console.log(
"Distribute returned succesfully in block# "
+ x.blockNumber
+ " using "
+ x.gasUsed
+ " gas."
);
getlist();
},
function distFail(x) {
console.log("Distribute returned errors in block# "+x.blockNumber+" using "+x.gasUsed+" gas.");
console.log(
"Distribute returned errors in block# "
+ x.blockNumber
+ " using "
+ x.gasUsed
+ " gas."
);
console.log(x);
process.exit( 1 );
}
......@@ -200,12 +170,12 @@ function editAccount( entry, well )
}
else
fatal("I don't know what to do with \""+entry+"\"." );
rl.question("Adjust the Mwei fill value of "+acct+" (setting to 0 is the same as deleting)\nAmount?: ", (answer) => {
rl.question("Adjust the "+notation[2]+" fill value of "+acct+" (setting to 0 is the same as deleting)\nAmount?: ", (answer) => {
if ( isNumeric(answer) )
{
answer *= 1000000;
console.log("Sending update to the Gas Well...");
GasWell.methods.setEtherAllowance(acct,answer).send( {"from": from } )
answer *= notation[1];
console.log("Sending update to the SC...");
Distillery.methods.setEtherAllowance(acct,answer).send( {"from": bfa.account } )
.then(
function(a){
console.log("Update accepted.")
......@@ -223,46 +193,51 @@ function editAccount( entry, well )
});
}
function displayBalances( well )
function displayBalances( pallet )
{
var n = well.length;
var n = pallet.length;
var i;
if ( well == undefined )
if ( pallet == undefined )
fatal( "Bank is not defined." );
well.sort(wellSort);
console.log("The contract's account ("+contractaddr+") has "+Math.floor(contractbalance/1000000)+" Mwei.\n");
var longestbefore = 1;
var longestafter = 9;
pallet.sort(palletSort);
console.log(
"The contract's account ("
+ Distillery.contractaddress
+ ") has "
+ Math.floor(Distillery.contractbalance/notation[1]).toFixed(notation[0])
+ " "
+ notation[2]
+ ".\n"
);
var longest = 1;
for ( i=0; i<n; i++ )
{
var len = Math.floor(well[i][1]/1000000).toString().length;
if ( len > longestbefore )
longestbefore = len;
var len = (pallet[i][1]/notation[1]).toFixed(notation[0]).length;
if ( len > longest )
longest = len;
}
for ( i=0; i<n; i++ )
{
var entry = well[i];
var entry = pallet[i];
if ( entry == undefined )
console.log( i+": <undef>" );
else
{
var numstr = ""+well[i][1]/1000000;
var before = numstr;
var after = "";
if ( numstr.search(/\./) > -1 )
{
before = numstr.replace(/\.[0-9]+$/, '');
after = numstr.replace(/^[0-9]*\./, '');
}
while ( before.length < longestbefore )
before = " "+before;
while ( after.length < longestafter )
after += "0";
numstr = before+"."+after;
var has = well[i][2].toString();
if ( has.length > 9 )
has = has.replace(/[0-9]{9}$/, ".$&");
console.log( i+": "+well[i][0]+" fills to "+numstr+" Mwei (has "+has+")." );
var numstr = (pallet[i][1]/notation[1]).toFixed(notation[0]);
while ( numstr.length < longest )
numstr = " "+numstr;
console.log(
i
+ ": "
+ pallet[i][0]
+ " fills to "
+ numstr
+ " "
+ notation[2]
+ " (has "
+ Number(pallet[i][2]/notation[1]).toFixed(notation[0])
+ ")."
);
}
}
console.log("\n[ Q=quit x=distribute ]");
......@@ -273,7 +248,7 @@ function displayBalances( well )
if ( (""+answer).toUpperCase() == 'Q' )
process.exit( 0 );
else
editAccount(answer, well);
editAccount(answer, pallet);
}
rl.close;
}
......@@ -282,7 +257,7 @@ function displayBalances( well )
function getlist()
{
GasWell.methods.numberOfBeneficiaries()
Distillery.methods.numberOfBeneficiaries()
.call()
.then(
requestBalances,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment