From 113e0725c022dc555192a351eb97f9c854c25987 Mon Sep 17 00:00:00 2001
From: Robert Martin-Legene <robert@nic.ar>
Date: Fri, 24 Aug 2018 11:00:37 -0300
Subject: [PATCH] Distribution works too now.

---
 bin/GasAdmin.js | 51 ++++++++++++++++++++++++-------------------------
 1 file changed, 25 insertions(+), 26 deletions(-)

diff --git a/bin/GasAdmin.js b/bin/GasAdmin.js
index 3cf94f4..048d338 100755
--- a/bin/GasAdmin.js
+++ b/bin/GasAdmin.js
@@ -2,7 +2,7 @@
 
 var     port        =   14349;      // cloud
 port                =   16437;      // bc
-const   contractaddr=   '0x283bc55557c08dbc4f902b2b8479a2201a47e1c1';
+const   networkdir  =   'network5445';
 const   readline    =   require('readline');
 const   rl          =   readline.createInterface( { input: process.stdin, output: process.stdout } );
 const   Web3        =   require('web3');
@@ -11,19 +11,28 @@ const   Personal    =   require('web3-eth-personal');
 var     web3;
 var     GasWell;
 var     from;
+var     contractbalance;
+var     contractaddr;
+
+function    fatal( txt )
+{
+    console.log( txt );
+    process.exit( 1 );
+}
 
 function    init()
 {
     if ( process.env.BFAHOME == undefined )
-    {
-        console.log( "$BFAHOME not set. Did you source bfa/bin/env ?" );
-        process.exit( 1 );
-    }
+        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     =   "";
-    var     abifile =   fs.createReadStream(
-        process.env.BFAHOME+"/network5445/contracts/"+contractaddr+"/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())
@@ -31,6 +40,7 @@ function    init()
             })
         .on('end',     ()           =>  {
             GasWell     =   new web3.eth.Contract( JSON.parse(abi), contractaddr );
+            web3.eth.getBalance( contractaddr ).then( function(v){contractbalance=v} );
             getlist();
             })
         ;
@@ -52,12 +62,11 @@ function    getAccount()
     personal.getAccounts().then(
         function gotPersonalAccounts(obj) {
             console.log("Setting default account to "+ obj[0]);
-            from    =   obj[0];
+            from        =   obj[0];
             web3.eth.defaultAccount = obj[0];
         },
         function failedGettingPersonalAccounts(err){
-            console.log("Did you remember to allow 'personal' via the RPC port? " + err)
-            process.exit( 1 );
+            fatal ("Did you remember to allow 'personal' via the RPC port? " + err)
         }
     );
 }
@@ -190,10 +199,7 @@ function    editAccount( entry, well )
         return;
     }
     else
-    {
-        console.log("I don't know what to do with \""+entry+"\"." );
-        process.exit( 1 );
-    }
+        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) => {
         if ( isNumeric(answer) )
         {
@@ -206,17 +212,13 @@ function    editAccount( entry, well )
                     getlist();
                 },
                 function(b){
-                    console.log("\nMaybe you are not authorized:\n"+b+"\n\n\nI think you should leave now.\n");
-                    process.exit( 1 );
+                    fatal("\nMaybe you are not authorized:\n"+b+"\n\n\nI think you should leave now.\n");
                 }
             )
             ;
         }
         else
-        {
-            console.log( "I have no idea what to do with \""+answer+"\"." );
-            process.exit( 1 );
-        }
+            fatal( "I have no idea what to do with \""+answer+"\"." );
         rl.close;
     });
 }
@@ -226,12 +228,9 @@ function    displayBalances( well )
     var     n       =   well.length;
     var     i;
     if ( well == undefined )
-    {
-        console.log( "Bank is not defined." );
-        process.exit( 1 );
-    }
+        fatal( "Bank is not defined." );
     well.sort(wellSort);
-    console.log("");
+    console.log("The contract's account ("+contractaddr+") has "+Math.floor(contractbalance/1000000)+" Mwei.\n");
     var     longestbefore   =   1;
     var     longestafter    =   9;
     for ( i=0; i<n; i++ )
@@ -266,7 +265,7 @@ function    displayBalances( well )
             console.log( i+": "+well[i][0]+" fills to "+numstr+" Mwei (has "+has+")." );
         }
     }
-    console.log("");
+    console.log("\n[ Q=quit x=distribute ]");
     rl.question("Which account to edit (enter index number of full account number)?: ",
         (answer) => {
             if ( answer != undefined )
-- 
GitLab