eth-erc20/python/eth_erc20/runnable/transfer.py

117 lines
3.6 KiB
Python
Raw Normal View History

2021-05-08 06:56:02 +02:00
#!python3
"""Token transfer script
.. moduleauthor:: Louis Holbrook <dev@holbrook.no>
.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
"""
# SPDX-License-Identifier: GPL-3.0-or-later
# standard imports
import os
import io
import json
import argparse
import logging
# external imports
2021-05-08 06:56:02 +02:00
from hexathon import (
add_0x,
strip_0x,
)
import chainlib.eth.cli
from chainlib.eth.cli.log import process_log
from chainlib.eth.settings import process_settings
from chainlib.settings import ChainSettings
from chainlib.eth.cli.arg import (
Arg,
ArgFlag,
process_args,
)
from chainlib.eth.cli.config import (
Config,
process_config,
)
2021-05-08 06:56:02 +02:00
# local imports
from eth_erc20 import ERC20
logg = logging.getLogger()
def process_config_local(config, arg, args, flags):
2022-11-06 11:29:58 +01:00
config.add(config.get('_POSARG'), '_VALUE', False)
return config
2021-05-08 06:56:02 +02:00
2021-06-26 10:30:17 +02:00
arg_flags = ArgFlag()
arg = Arg(arg_flags)
flags = arg_flags.STD_WRITE | arg_flags.EXEC | arg_flags.WALLET
argparser = chainlib.eth.cli.ArgumentParser()
argparser = process_args(argparser, arg, flags)
2022-11-06 11:29:58 +01:00
argparser.add_argument('value', type=str, help='Token value to send')
args = argparser.parse_args()
2021-05-08 06:56:02 +02:00
logg = process_log(args, logg)
2021-05-08 06:56:02 +02:00
config = Config()
2022-11-06 11:29:58 +01:00
config = process_config(config, arg, args, flags, positional_name='value')
config = process_config_local(config, arg, args, flags)
logg.debug('config loaded:\n{}'.format(config))
2021-05-08 06:56:02 +02:00
settings = ChainSettings()
settings = process_settings(settings, config)
logg.debug('settings loaded:\n{}'.format(settings))
2021-05-08 06:56:02 +02:00
def balance(conn, generator, token_address, address, id_generator=None):
o = generator.balance(token_address, address, id_generator=id_generator)
r = conn.do(o)
token_balance = generator.parse_balance(r)
return token_balance
2021-05-08 06:56:02 +02:00
def main():
token_address = settings.get('EXEC')
signer_address = settings.get('SENDER_ADDRESS')
recipient = settings.get('RECIPIENT')
value = settings.get('VALUE')
conn = settings.get('CONN')
g = ERC20(
settings.get('CHAIN_SPEC'),
signer=settings.get('SIGNER'),
gas_oracle=settings.get('GAS_ORACLE'),
nonce_oracle=settings.get('NONCE_ORACLE'),
)
2021-05-08 06:56:02 +02:00
if logg.isEnabledFor(logging.DEBUG):
sender_balance = balance(conn, g, token_address, signer_address, id_generator=settings.get('RPC_ID_GENERATOR'))
recipient_balance = balance(conn, g, token_address, recipient, id_generator=settings.get('RPC_ID_GENERATOR'))
logg.debug('sender {} balance before: {}'.format(signer_address, sender_balance))
logg.debug('recipient {} balance before: {}'.format(recipient, recipient_balance))
2021-05-08 06:56:02 +02:00
(tx_hash_hex, o) = g.transfer(token_address, signer_address, recipient, value, id_generator=settings.get('RPC_ID_GENERATOR'))
2021-05-08 06:56:02 +02:00
if settings.get('RPC_SEND'):
2021-05-08 06:56:02 +02:00
conn.do(o)
if settings.get('WAIT'):
2021-05-08 06:56:02 +02:00
r = conn.wait(tx_hash_hex)
if logg.isEnabledFor(logging.DEBUG):
sender_balance = balance(conn, g, token_address, signer_address, id_generator=settings.get('RPC_ID_GENERATOR'))
recipient_balance = balance(conn, g, token_address, recipient, id_generator=settings.get('RPC_ID_GENERATOR'))
2021-06-26 10:30:17 +02:00
logg.debug('sender {} balance after: {}'.format(signer_address, sender_balance))
logg.debug('recipient {} balance after: {}'.format(recipient, recipient_balance))
2021-05-08 06:56:02 +02:00
if r['status'] == 0:
logg.critical('VM revert. Wish I could tell you more')
sys.exit(1)
print(tx_hash_hex)
else:
print(o['params'][0])
2021-05-08 06:56:02 +02:00
if __name__ == '__main__':
main()