OSDN Git Service

change for loop to merge utxos and add -f argument
authorsuccessli <successli@outlook.com>
Tue, 26 Jun 2018 03:00:00 +0000 (11:00 +0800)
committersuccessli <successli@outlook.com>
Tue, 26 Jun 2018 03:00:00 +0000 (11:00 +0800)
utxomerger/README.md
utxomerger/merge_utxo.py

index b362d9c..efb2a16 100644 (file)
@@ -19,8 +19,10 @@ Usage:
 
 Options:
   ```shell
-  usage: python btmspanner.py utxomerger [-h] [-o URL] [-a ACCOUNT_ALIAS] [-p PASSWORD]
-                     [-x MAX_AMOUNT] [-s MIN_AMOUNT] [-l] [-m MERGE_LIST] [-y]
+  $ python btmspanner.py utxomerger -h
+usage: btmspanner.py [-h] [-o URL] [-a ACCOUNT_ALIAS] [-p PASSWORD]
+                     [-x MAX_AMOUNT] [-s MIN_AMOUNT] [-l] [-m MERGE_LIST]
+                     [-f FOR_LOOP] [-y]
 
 Bytom merge utxo tool
 
@@ -38,7 +40,10 @@ optional arguments:
   -l, --list            Show UTXO list without merge
   -m MERGE_LIST, --merge MERGE_LIST
                         UTXO to merge
+  -f FOR_LOOP, --forloop FOR_LOOP
+                        size for loop of UTXO to merge
   -y, --yes             confirm transfer
+  
   ```
 
 Example:
index 30f2e9f..5c35ced 100644 (file)
@@ -3,7 +3,6 @@ import getpass
 import os
 import time
 
-
 from .Account import Account
 from .Transaction import Action, Transaction
 from .UnspentOutputs import UnspentOutputs
@@ -16,7 +15,8 @@ parser.add_argument('-p', '--pass', default=None, dest='password', help='account
 parser.add_argument('-x', '--max', default=41250000000, type=int, dest='max_amount', help='range lower than max_amount')
 parser.add_argument('-s', '--min', default=1, type=int, dest='min_amount', help='range higher than min_amount')
 parser.add_argument('-l', '--list', action='store_true', dest='only_list', help='Show UTXO list without merge')
-parser.add_argument('-m', '--merge', default=None, type=int, dest='merge_list', help='UTXO to merge')
+parser.add_argument('-m', '--merge', default=20, type=int, dest='merge_list', help='UTXO to merge')
+parser.add_argument('-f', '--forloop', default=1, type=int, dest='for_loop', help='size for loop of UTXO to merge')
 parser.add_argument('-y', '--yes', action='store_true', default=None, dest='confirm', help='confirm transfer')
 
 
@@ -85,48 +85,76 @@ def send_tx(connection, utxo_list, to_address, password):
 
 def main():
     options = parser.parse_args()
+    utxo_total = []
     utxolist = list_utxo(options.url, options.account_alias, options.min_amount, options.max_amount)
+
     for i, utxo in enumerate(utxolist):
         print('{:4}. {:13.8f} BTM {}{}'.format(i, utxo['amount'] / 1e8, utxo['id'], ' (mature)'))
-        if i >= 21:
+        if i >= options.merge_list * options.for_loop:
             break
+        utxo_total.append(utxo)
+
     print("total size of available utxos is {}".format(len(utxolist)))
 
     if options.only_list:
         return
 
+    print('To merge {} UTXOs with {:13.8f} BTM totally.\n'.format(len(utxo_total),
+                                                       sum(utxo['amount'] for utxo in utxo_total) / 1e8))
+
     merge_size = options.merge_list or input('Merge size of UTXOs (5, 13 or 20): ')
-    utxo_mergelist = []
+    for_loop = options.for_loop or input('for loop size (1, 10 or 50): ')
 
-    for i in range(merge_size if merge_size <= len(utxolist) else len(utxolist)):
-        utxo_mergelist.append(utxolist[i])
 
-    if len(utxo_mergelist) < 2:
-        print('Not Merge UTXOs, Exit...')
-        return
 
-    print('To merge {} UTXOs with {:13.8f} BTM'.format(len(utxo_mergelist),
-                                                       sum(utxo['amount'] for utxo in utxo_mergelist) / 1e8))
+    print(
+        'One last disclaimer: the code we are about to go over is in no way intended to be used as an example of a robust solution. ')
+    print('You will transfer BTM to an address, please check this python code and DO IT later.\n')
 
-    if not options.account_alias:
-        options.account_alias = input('Transfer account alias: ')
+    for loops in range(for_loop):
 
-    if not options.password:
-        options.password = getpass.getpass('Bytom Account Password: ')
+        utxo_mergelist = []
 
-    print(
-        'One last disclaimer: the code we are about to go over is in no way intended to be used as an example of a robust solution. ')
-    print('You will transfer BTM to an address, please check this python code and DO IT later.')
+        # for i in range(merge_size if merge_size <= len(utxolist) else len(utxolist)):
+            # utxo_mergelist.append(utxolist[i])
+        for i in range(loops*merge_size, ((loops+1)*merge_size) if (loops*merge_size) < len(utxolist) else len(utxolist)):
+            utxo_mergelist.append(utxolist[i])
 
-    if not (options.confirm or input('Confirm [y/N] ').lower() == 'y'):
-        print('Not Merge UTXOs, Exit...')
-        return
+        # print(loops*merge_size, ", ", ((loops+1)*merge_size) if (loops*merge_size) < len(utxolist) else len(utxolist))
+        print('this is the {} times to merge utxos. -----begin'.format(loops+1))
+
+        for i, utxo in enumerate(utxo_mergelist):
+            print('{:4}. {:13.8f} BTM {}{}'.format(loops*merge_size+i, utxo['amount'] / 1e8, utxo['id'], ' (mature)'))
+
+        print("total size of available utxos is {}".format(len(utxo_mergelist)))
+
+        if len(utxo_mergelist) < 2:
+            print('Not Merge UTXOs, Exit...')
+            return
+
+        print('To merge {} UTXOs with {:13.8f} BTM'.format(len(utxo_mergelist),
+                                                           sum(utxo['amount'] for utxo in utxo_mergelist) / 1e8))
+
+        if not options.account_alias:
+            options.account_alias = input('Transfer account alias: ')
+
+        if not options.password:
+            options.password = getpass.getpass('Bytom Account Password: ')
+
+        if not (options.confirm or input('Confirm [y/N] ').lower() == 'y'):
+            print('Not Merge UTXOs, Exit...')
+            return
+
+        to_address = Account.find_address_by_alias(Connection(options.url), options.account_alias)
+        if not to_address:
+            to_address = input('Transfer address: ')
+
+        print('tx_id:', send_tx(Connection(options.url), utxo_mergelist, to_address, options.password))
+        print('this is the {} times to merge utxos. -----end\n'.format(loops+1))
 
-    to_address = Account.find_address_by_alias(Connection(options.url), options.account_alias)
-    if not to_address:
-        to_address = input('Transfer address: ')
 
-    print('tx_id:', send_tx(Connection(options.url), utxo_mergelist, to_address, options.password))
+        time.sleep(2)
+        pass
 
 
 if __name__ == '__main__':