Skip to content

Instantly share code, notes, and snippets.

@Jaxkr
Last active March 31, 2018 17:54
Show Gist options
  • Save Jaxkr/6988931607c2cff36d7f4c8551783405 to your computer and use it in GitHub Desktop.
Save Jaxkr/6988931607c2cff36d7f4c8551783405 to your computer and use it in GitHub Desktop.
'''
USAGE:
Use python3
Place addresses to track, one per line, in addresses.txt
Place assetchains to track, one per line, in assets.txt
Adjust balance_threshold and send_amount as needed
If you need to specify an alternative komodo_cli location, either pass it as the first argument
when invoking the script, or change the komodo_cli variable.
'''
import subprocess
import sys
import json
addresses = []
asset_names = []
komodo_cli = '/home/komodo/komodo/src/komodo-cli'
addresses_filename = 'addresses.txt'
assetchains_filename = 'assets.txt'
balance_threshold = 0.5
send_amount = .1
DEBUG = False
def run_command(command):
#print(command)
if (not DEBUG):
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = process.communicate()
#print(output)
return output.decode("utf-8").strip()
def kmd_run(command, returns_json=True):
command_output = run_command([komodo_cli] + command)
if (returns_json):
command_parsed = json.loads(command_output)
return command_parsed
def get_address_balance(address, asset_name):
command = ['-ac_name=' + asset_name, 'listunspent', '1', '9999999', '["{}"]'.format(address)]
unspent = kmd_run(command)
address_sum = 0
for output in unspent:
address_sum += output['amount']
return address_sum
def load_assets():
global asset_names
f = open(assetchains_filename, 'r')
for asset_name in f:
asset_names.append(asset_name.rstrip())
# imports the addresses from the addresses file
def load_addresses():
global addresses
f = open(addresses_filename, 'r')
for asset_name in asset_names:
for address in f:
a = address.rstrip()
addresses.append(a)
v = kmd_run(['-ac_name=' + asset_name, 'validateaddress', a])
if (v['ismine'] == False and v['iswatchonly'] == False):
print('Address ' + a + ' is not imported. Importing...')
kmd_run(['-ac_name=' + asset_name, 'importaddress', a], False)
else:
print("Successfully loaded address " + a)
# prevents the script from running if imported
if __name__ == "__main__":
if (len(sys.argv) > 1):
komodo_cli = sys.argv[1]
print('Using komodo-cli at ' + komodo_cli)
load_assets()
print(asset_names)
load_addresses()
for asset_name in asset_names:
for address in addresses:
bal = get_address_balance(address, asset_name)
if (bal < balance_threshold):
if (not DEBUG):
kmd_run(['-ac_name=' + asset_name, 'sendtoaddress', address, send_amount])
print('Sent to ' + address)
else:
print(address + ' has a balance above threshold.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment