diff --git a/bin/MasterDistiller.js b/bin/MasterDistiller.js index ff0f76f1efa24c6074c53c163568ffd7a6ef73ec..405372af81a1c4e96a276d479762dcf28b2bbdfe 100755 --- a/bin/MasterDistiller.js +++ b/bin/MasterDistiller.js @@ -2,7 +2,7 @@ "use strict" -const BigInteger = require('big-integer'); +const BigNumber = require('bignumber.js'); const Libbfa = require( process.env.BFAHOME + '/bin/libbfa.js'); const rl = require('readline').createInterface( { input: process.stdin, output: process.stdout } @@ -10,55 +10,32 @@ const rl = require('readline').createInterface( var web3; var Distillery; var bfa; -var notation = [ 15 ]; +var notation = { "potency": 15 }; function init() { - notation.push( Math.pow(10, notation[0]) ); - switch ( notation[0] ) { - case 6: - notation.push( "Mwei" ); - break; - case 9: - notation.push( "Gwei" ); - break; - case 12: - notation.push( "micro" ); - break; - case 15: - notation.push( "finney" ); - break; - case 18: - notation.push( "ether" ); - break; - case 21: - notation.push( "kether" ); - break; - case 24: - notation.push( "grand" ); - break; - case 27: - notation.push( "mether" ); - break; - case 30: - notation.push( "gether" ); - break; - case 33: - notation.push( "tether" ); - break; - default: - notation = [ 15, Math.pow(10, 15), "finney" ]; - } + var table = [ + 6, 'Mwei', + 9, 'Gwei', + 12, 'micro', + 15, 'finney', + 18, 'ether', + 21, 'kether', + 24, 'grand', + 27, 'mether', + 30, 'gether', + 33, 'tether' + ]; + for (var i = 0; table[i]; i+=2) + if ( table[i] == notation.potency ) + notation.name = table[i+1] + if ( undefined == notation.name ) + notation = { 'potency': 15, 'name': 'finney' }; + notation.num = BigNumber( 10 ).pow( notation.potency ); bfa = new Libbfa(); web3 = bfa.newweb3(); Distillery = bfa.contract( web3, 'Distillery' ); - web3.eth.getBalance( Distillery.contractaddress ).then( - function receivedOwnBalance(val) { - Distillery.contractbalance = val; - getlist(); - }, - function err(x){ bfa.fatal("Can't do that: "+x);process.exit(1); } - ); + requestBalances(); } function palletSort(a,b) @@ -72,8 +49,8 @@ function palletSort(a,b) } if ( a == undefined ) return 1; - var strA = a[0].toLowerCase(); - var strB = b[0].toLowerCase(); + var strA = a.addr.toLowerCase(); + var strB = b.addr.toLowerCase(); if ( strA < strB ) return -1; if ( strA > strB ) @@ -81,44 +58,23 @@ function palletSort(a,b) return 0; } -function requestBalances( count ) +async function requestBalances() { - var pallet = new Array; - var proms = new Array; + var distbal = await web3.eth.getBalance(Distillery.contractaddress); + var count = await Distillery.methods.numberOfBeneficiaries().call(); + var pallet = new Array; var i; + Distillery.contractbalance = new BigNumber( distbal ); // Fetch addresses from the list in the contract. for ( i=0; i<count; i++ ) { - proms.push( - Distillery.methods.atPosition(i).call( {} ) - .then( - function(res) { pallet.push(res); }, - function(err) { bfa.fatal("Fetching position data failed: "+err) } - ) - ); + var addressandsetting = await Distillery.methods.atPosition(i).call(); + var addr = addressandsetting[0]; + var setting = addressandsetting[1]; + var bal = await web3.eth.getBalance( addr ); + pallet.push( { "addr": addr, "setting": new BigNumber(setting), "balance": new BigNumber(bal) } ); } - Promise.all( proms ).then( - function(x) { - // The Well has now been filled out - // so we will ask for the balances of the found accounts - var p2 = new Array(); - for ( i=0; i<count; i++ ) - { - 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(pallet) }, - function(err) { bfa.fatal("Getting balances failed: "+err) } - ); - }, - function(err) { bfa.fatal("Getting account list failed: "+err) } - ); -} - -function setBal( arr, idx, val ) -{ - arr[idx][2]=val; + displayBalances(pallet); } function editAccount( entry, pallet ) @@ -134,7 +90,7 @@ function editAccount( entry, pallet ) var n = pallet.length; while ( i < n ) { - if (String(pallet[i][0]).toLowerCase() == String(entry).toLowerCase() ) + if (String(pallet[i].addr).toLowerCase() == String(entry).toLowerCase() ) entry = i; i++; } @@ -148,27 +104,30 @@ function editAccount( entry, pallet ) else if ( bfa.isNumeric(entry) && entry < pallet.length ) { - acct = pallet[entry][0]; - value = pallet[entry][1]; + acct = pallet[entry].addr; + value = pallet[entry].setting; } else if ( entry == "x" ) { + var rcpt; // trigger distribution - Distillery.methods.distribute().send( {"from": bfa.account, "gas": 4000000 } ) - .then( - function distOK(x) { - console.log( - "Distribute returned succesfully in block# " - + x.blockNumber - + " using " - + x.gasUsed - + " gas." - ); - getlist(); - }, - function distFail(x) { - bfa.fatal( "Distribute returned errors: " + x ); + Distillery.methods.distribute().send( {"from": bfa.account, "gas": 4000000, gasPrice: 1000000000 } ) + .on( 'error', (x) => { bfa.fatal( "Distribute returned errors: " + x ) } ) + .on( 'confirmation', (n,x) => + { + if ( undefined == rcpt ) + { + rcpt = x; + console.log( + "Distribute returned succesfully in block# " + + rcpt.blockNumber + + " using " + + rcpt.gasUsed + + " gas." + ); + requestBalances(); + } } ); return; @@ -185,7 +144,7 @@ function editAccount( entry, pallet ) } rl.question( "Adjust the " - + notation[2] + + notation.name + " fill value of " + acct + " (setting to 0 is the same as deleting)\n" @@ -193,23 +152,28 @@ function editAccount( entry, pallet ) (answer) => { if ( bfa.isNumeric(answer) ) { - var bi = BigInteger( answer ).multiply( notation[1] ); + var bi = BigNumber( answer ).multipliedBy( notation.num ); console.log("Sending update to the SC..."); + var rcpt; Distillery.methods.setEtherAllowance(acct,web3.utils.toHex(bi)) - .send( {"from": bfa.account } ) - .then( - function(a){ - console.log("Update accepted.") - getlist(); - }, - function(b){ + .send( {"from": bfa.account, gasPrice: 1000000000, gas: 100000 } ) + .on( 'error', (err) => { bfa.fatal( "\nMaybe you are not authorized:\n" - +b + +err +"\n\n\nI think you should leave now.\n" ); } ) + .on( 'confirmation', (n,x) => { + if ( undefined == rcpt ) + { + rcpt = x; + console.log("Update accepted.") + requestBalances(); + } + } + ); } else bfa.fatal( "I have no idea what to do with \""+answer+"\"." ); @@ -227,15 +191,15 @@ function displayBalances( pallet ) "The contract's account (" + Distillery.contractaddress + ") has " - + Math.floor(Distillery.contractbalance/notation[1]).toFixed(0) + + Distillery.contractbalance.div( notation.num ).toFixed() + " " - + notation[2] + + notation.name + ".\n" ); var longest = 1; for ( i=0; i<n; i++ ) { - var len = (pallet[i][1]/notation[1]).toFixed(notation[0]).length; + var len = pallet[i].setting.div(notation.num).toFixed(notation.potency).length; if ( len > longest ) longest = len; } @@ -246,19 +210,19 @@ function displayBalances( pallet ) console.log( i+": <undef>" ); else { - var numstr = (pallet[i][1]/notation[1]).toFixed(notation[0]); + var numstr = pallet[i].setting.div(notation.num).toFixed(notation.potency); while ( numstr.length < longest ) numstr = " "+numstr; console.log( i + ": " - + pallet[i][0] + + pallet[i].addr + " fills to " + numstr + " " - + notation[2] + + notation.name + " (has " - + Number(pallet[i][2]/notation[1]).toFixed(notation[0]) + + pallet[i].balance.div(notation.num).toFixed(notation.potency) + ")." ); } @@ -278,14 +242,4 @@ function displayBalances( pallet ) ) } -function getlist() -{ - Distillery.methods.numberOfBeneficiaries() - .call() - .then( - requestBalances, - function beneficiaryFail(x){bfa.fatal(x)} - ); -} - init(); diff --git a/package-lock.json b/package-lock.json index d1f07214a1e6e77ae0bbd9e73a126ad808b7fcec..c79fa7d996f141c69d7fa24c75c73d29c05be7db 100644 --- a/package-lock.json +++ b/package-lock.json @@ -88,9 +88,14 @@ } }, "big-integer": { - "version": "1.6.43", - "resolved": "https://registry.npmjs.org/big-integer/-/big-integer-1.6.43.tgz", - "integrity": "sha512-9dULc9jsKmXl0Aeunug8wbF+58n+hQoFjqClN7WeZwGLh0XJUWyJJ9Ee+Ep+Ql/J9fRsTVaeThp8MhiCCrY0Jg==" + "version": "1.6.44", + "resolved": "https://registry.npmjs.org/big-integer/-/big-integer-1.6.44.tgz", + "integrity": "sha512-7MzElZPTyJ2fNvBkPxtFQ2fWIkVmuzw41+BZHSzpEq3ymB2MfeKp1+yXl/tS75xCx+WnyV+yb0kp+K1C3UNwmQ==" + }, + "bignumber.js": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz", + "integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A==" }, "bindings": { "version": "1.5.0", diff --git a/package.json b/package.json index e8819647f4d34c41f2fd61540a8e7ed598229068..6851a64b7ce534f7cea965c1bca8efd3b6d1b1ed 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,8 @@ "version": "1.0.0", "description": "Blockchain Federal Argentina", "dependencies": { - "big-integer": "^1.6.43", + "big-integer": "^1.6.44", + "bignumber.js": "^9.0.0", "request": "^2.88.0", "require": "^2.4.20", "web3": "^1.0.0-beta.55",