eth-erc20

ERC20 interface and example giftable token contract
Info | Log | Files | Refs | LICENSE

transfer.py (3700B)


      1 #!python3
      2 
      3 """Token transfer script
      4 
      5 .. moduleauthor:: Louis Holbrook <dev@holbrook.no>
      6 .. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746 
      7 
      8 """
      9 
     10 # SPDX-License-Identifier: GPL-3.0-or-later
     11 
     12 # standard imports
     13 import os
     14 import io
     15 import json
     16 import argparse
     17 import logging
     18 
     19 # external imports
     20 from hexathon import (
     21         add_0x,
     22         strip_0x,
     23         )
     24 import chainlib.eth.cli
     25 from chainlib.eth.cli.log import process_log
     26 from chainlib.eth.settings import process_settings
     27 from chainlib.settings import ChainSettings
     28 from chainlib.eth.cli.arg import (
     29         Arg,
     30         ArgFlag,
     31         process_args,
     32         )
     33 from chainlib.eth.cli.config import (
     34         Config,
     35         process_config,
     36         )
     37 
     38 # local imports
     39 from eth_erc20 import ERC20
     40 
     41 logg = logging.getLogger()
     42 
     43 
     44 def process_config_local(config, arg, args, flags):
     45     config.add(config.get('_POSARG'), '_VALUE', False)
     46     return config
     47 
     48 
     49 arg_flags = ArgFlag()
     50 arg = Arg(arg_flags)
     51 flags = arg_flags.STD_WRITE | arg_flags.EXEC | arg_flags.WALLET
     52 
     53 argparser = chainlib.eth.cli.ArgumentParser()
     54 argparser = process_args(argparser, arg, flags)
     55 argparser.add_argument('value', type=str, help='Token value to send')
     56 args = argparser.parse_args()
     57 
     58 logg = process_log(args, logg)
     59 
     60 config = Config()
     61 config = process_config(config, arg, args, flags, positional_name='value')
     62 config = process_config_local(config, arg, args, flags)
     63 logg.debug('config loaded:\n{}'.format(config))
     64 
     65 settings = ChainSettings()
     66 settings = process_settings(settings, config)
     67 logg.debug('settings loaded:\n{}'.format(settings))
     68 
     69 
     70 def balance(conn, generator, token_address, address, id_generator=None):
     71     o = generator.balance(token_address, address, id_generator=id_generator)
     72     r = conn.do(o)
     73     token_balance = generator.parse_balance(r)
     74     return token_balance
     75 
     76 
     77 def main():
     78     token_address = settings.get('EXEC')
     79     signer_address = settings.get('SENDER_ADDRESS')
     80     recipient = settings.get('RECIPIENT')
     81     value = settings.get('VALUE')
     82     conn = settings.get('CONN')
     83     g = ERC20(
     84             settings.get('CHAIN_SPEC'),
     85             signer=settings.get('SIGNER'),
     86             gas_oracle=settings.get('GAS_ORACLE'),
     87             nonce_oracle=settings.get('NONCE_ORACLE'),
     88             )
     89     if logg.isEnabledFor(logging.DEBUG):
     90         sender_balance = balance(conn, g, token_address, signer_address, id_generator=settings.get('RPC_ID_GENERATOR'))
     91         recipient_balance = balance(conn, g, token_address, recipient, id_generator=settings.get('RPC_ID_GENERATOR'))
     92         logg.debug('sender {} balance before: {}'.format(signer_address, sender_balance))
     93         logg.debug('recipient {} balance before: {}'.format(recipient, recipient_balance))
     94 
     95     (tx_hash_hex, o) = g.transfer(token_address, signer_address, recipient, value, id_generator=settings.get('RPC_ID_GENERATOR'))
     96 
     97     if settings.get('RPC_SEND'):
     98         conn.do(o)
     99         if settings.get('WAIT'):
    100             r = conn.wait(tx_hash_hex)
    101             if logg.isEnabledFor(logging.DEBUG):
    102                 sender_balance = balance(conn, g, token_address, signer_address, id_generator=settings.get('RPC_ID_GENERATOR'))
    103                 recipient_balance = balance(conn, g, token_address, recipient, id_generator=settings.get('RPC_ID_GENERATOR'))
    104                 logg.debug('sender {} balance after: {}'.format(signer_address, sender_balance))
    105                 logg.debug('recipient {} balance after: {}'.format(recipient, recipient_balance))
    106             if r['status'] == 0:
    107                 logg.critical('VM revert. Wish I could tell you more')
    108                 sys.exit(1)
    109         print(tx_hash_hex)
    110 
    111     else:
    112         print(o['params'][0])
    113 
    114 
    115 if __name__ == '__main__':
    116     main()