commit 674b5b9ae0d97ef81575fc362d31ae548bd23d37
parent de201bc199b1efa4d38f3fae6b5d212d398aa05a
Author: nolash <dev@holbrook.no>
Date:   Thu, 28 Oct 2021 13:35:51 +0200
Allow separate settings for sender and recipient
Diffstat:
1 file changed, 37 insertions(+), 10 deletions(-)
diff --git a/eth_gas_sum/runnable/sum.py b/eth_gas_sum/runnable/sum.py
@@ -33,13 +33,17 @@ argparser.add_argument('--start', type=int, help='start at block')
 argparser.add_argument('--end', type=int, help='end block (not inclusive)') 
 argparser.add_argument('--interval', type=int, default=5, help='syncer poll interval for new blocks')
 argparser.add_argument('-d', type=str, required=True, help='output directory')
-argparser.add_positional('address', type=str, append=True, help='address sender to monitor')
+argparser.add_argument('--sender', type=str, action='append', default=[], help='sender address sender to monitor')
+argparser.add_argument('--recipient', type=str, action='append', default=[], help='recipient address sender to monitor')
+argparser.add_argument('--address', type=str, action='append', default=[], help='sender or recipient address to monitor')
 args = argparser.parse_args()
 
 extra_args = {
     'start': None,
     'end': None,
     'address': None,
+    'sender': None,
+    'recipient': None,
     'd': '_OUTPUT_DIR',
     'interval': 'SYNCER_LOOP_INTERVAL',
         }
@@ -65,22 +69,25 @@ class EthChainInterface(ChainInterface):
 
 class GasAddFilter:
 
-    def __init__(self, chain_spec, addresses):
-        self.addresses = []
-        for address in addresses:
-            clean_address = hex_uniform(strip_0x(address))
-            self.addresses.append(clean_address)
-            logg.debug('added {} to gas sum filter'.format(clean_address))
+    def __init__(self, chain_spec, senders, recipients):
+        self.senders = senders
+        self.recipients = recipients
         self.tx_gas = {}
         self.gas_sum = 0
        
 
     def filter(self, conn, block, tx, db_session):
         sender = hex_uniform(strip_0x(tx.outputs[0]))
-        if sender in self.addresses:
+        recipient = hex_uniform(strip_0x(tx.inputs[0]))
+        if sender in self.senders:
             self.gas_sum += tx.gas_used
             self.tx_gas[tx.hash] = tx.gas_used
-            logg.info('sender {} tx {} gas {} new sum {}'.format(sender, tx.hash, tx.gas_used, self.gas_sum))
+            logg.info('sender {} tx {} ({}/{}) gas {} new sum {}'.format(sender, tx.hash, tx.block.number, tx.index, tx.gas_used, self.gas_sum))
+        elif recipient in self.recipients:
+            self.gas_sum += tx.gas_used
+            self.tx_gas[tx.hash] = tx.gas_used
+            logg.info('recipient {} tx {} ({}/{}) gas {} new sum {}'.format(recipient, tx.hash, tx.block.number, tx.index, tx.gas_used, self.gas_sum))
+
 
 
     def sum(self):
@@ -106,7 +113,27 @@ def main():
         backend = FileBackend.live(chain_spec, start, base_dir=config.get('_OUTPUT_DIR'))
         syncer = HeadSyncer(backend, chain_interface)
 
-    gas_filter = GasAddFilter(chain_spec, config.get('_ADDRESS'))
+    senders = []
+    recipients = []
+
+    for address in config.get('_SENDER'):
+        clean_address = hex_uniform(strip_0x(address))
+        senders.append(clean_address)
+        logg.debug('monitoring sender {}'.format(clean_address))
+    for address in config.get('_RECIPIENT'):
+        clean_address = hex_uniform(strip_0x(address))
+        recipients.append(clean_address)
+        logg.debug('monitoring recipient {}'.format(clean_address))
+    for address in config.get('_ADDRESS'):
+        clean_address = hex_uniform(strip_0x(address))
+        if address not in senders:
+            senders.append(clean_address)
+            logg.debug('monitoring sender {}'.format(clean_address))
+        if address not in recipients:
+            recipients.append(clean_address)
+            logg.debug('monitoring recipient {}'.format(clean_address))
+
+    gas_filter = GasAddFilter(chain_spec, senders, recipients)
     syncer.add_filter(gas_filter)
 
     r = syncer.loop(config.get('SYNCER_LOOP_INTERVAL'), conn)