2021-06-28 07:48:36 +02:00
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
|
|
# standard imports
|
|
|
|
|
import io
|
|
|
|
|
import sys
|
|
|
|
|
import os
|
|
|
|
|
import json
|
|
|
|
|
import argparse
|
|
|
|
|
import logging
|
|
|
|
|
import urllib
|
|
|
|
|
|
|
|
|
|
# external imports
|
|
|
|
|
from hexathon import (
|
|
|
|
|
add_0x,
|
|
|
|
|
strip_0x,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# local imports
|
2021-08-21 09:27:40 +02:00
|
|
|
|
from chainlib.eth.address import to_checksum_address
|
2021-06-28 07:48:36 +02:00
|
|
|
|
from chainlib.eth.connection import EthHTTPConnection
|
|
|
|
|
from chainlib.jsonrpc import (
|
|
|
|
|
JSONRPCRequest,
|
|
|
|
|
IntSequenceGenerator,
|
|
|
|
|
)
|
2021-08-21 09:27:40 +02:00
|
|
|
|
from chainlib.eth.gas import Gas
|
2021-06-28 07:48:36 +02:00
|
|
|
|
from chainlib.eth.gas import balance as gas_balance
|
|
|
|
|
from chainlib.chain import ChainSpec
|
|
|
|
|
from chainlib.eth.runnable.util import decode_for_puny_humans
|
2021-10-30 16:49:36 +02:00
|
|
|
|
from chainlib.eth.address import (
|
|
|
|
|
is_same_address,
|
|
|
|
|
is_checksum_address,
|
|
|
|
|
)
|
2021-08-21 09:27:40 +02:00
|
|
|
|
import chainlib.eth.cli
|
2021-06-28 07:48:36 +02:00
|
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.WARNING)
|
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
|
2021-08-21 09:27:40 +02:00
|
|
|
|
arg_flags = chainlib.eth.cli.argflag_std_write | chainlib.eth.cli.Flag.WALLET
|
|
|
|
|
argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
|
|
|
|
|
argparser.add_argument('--data', type=str, help='Transaction data')
|
|
|
|
|
argparser.add_positional('amount', type=int, help='Token amount to send')
|
2021-06-28 07:48:36 +02:00
|
|
|
|
args = argparser.parse_args()
|
2021-08-21 09:27:40 +02:00
|
|
|
|
extra_args = {
|
|
|
|
|
'data': None,
|
|
|
|
|
'amount': None,
|
|
|
|
|
}
|
|
|
|
|
#config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_config_dir=config_dir)
|
|
|
|
|
config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args)
|
2021-06-28 07:48:36 +02:00
|
|
|
|
|
|
|
|
|
block_all = args.ww
|
|
|
|
|
block_last = args.w or block_all
|
|
|
|
|
|
2021-08-21 09:27:40 +02:00
|
|
|
|
wallet = chainlib.eth.cli.Wallet()
|
|
|
|
|
wallet.from_config(config)
|
2021-06-28 07:48:36 +02:00
|
|
|
|
|
2021-08-21 09:27:40 +02:00
|
|
|
|
rpc = chainlib.eth.cli.Rpc(wallet=wallet)
|
|
|
|
|
conn = rpc.connect_by_config(config)
|
2021-06-28 07:48:36 +02:00
|
|
|
|
|
2021-08-21 09:27:40 +02:00
|
|
|
|
chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
|
2021-06-28 07:48:36 +02:00
|
|
|
|
|
2021-08-21 09:27:40 +02:00
|
|
|
|
value = config.get('_AMOUNT')
|
2021-06-28 07:48:36 +02:00
|
|
|
|
|
2021-08-21 09:27:40 +02:00
|
|
|
|
send = config.true('_RPC_SEND')
|
2021-06-28 07:48:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def balance(address, id_generator):
|
|
|
|
|
o = gas_balance(address, id_generator=id_generator)
|
|
|
|
|
r = conn.do(o)
|
2021-10-28 12:17:32 +02:00
|
|
|
|
try:
|
|
|
|
|
balance = int(r)
|
|
|
|
|
except ValueError:
|
|
|
|
|
balance = strip_0x(r)
|
|
|
|
|
balance = int(balance, 16)
|
|
|
|
|
return balance
|
2021-06-28 07:48:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2021-08-21 09:27:40 +02:00
|
|
|
|
signer = rpc.get_signer()
|
|
|
|
|
signer_address = rpc.get_sender_address()
|
|
|
|
|
|
|
|
|
|
g = Gas(chain_spec, signer=signer, gas_oracle=rpc.get_gas_oracle(), nonce_oracle=rpc.get_nonce_oracle())
|
|
|
|
|
|
|
|
|
|
recipient = to_checksum_address(config.get('_RECIPIENT'))
|
2021-12-10 02:35:38 +01:00
|
|
|
|
if not config.true('_UNSAFE') and not is_checksum_address(recipient):
|
2021-06-28 07:48:36 +02:00
|
|
|
|
raise ValueError('invalid checksum address')
|
|
|
|
|
|
|
|
|
|
logg.info('gas transfer from {} to {} value {}'.format(signer_address, recipient, value))
|
|
|
|
|
if logg.isEnabledFor(logging.DEBUG):
|
|
|
|
|
try:
|
2021-10-18 12:49:40 +02:00
|
|
|
|
sender_balance = balance(add_0x(signer_address), rpc.id_generator)
|
2021-10-18 12:18:20 +02:00
|
|
|
|
recipient_balance = balance(add_0x(recipient), rpc.id_generator)
|
2021-06-28 07:48:36 +02:00
|
|
|
|
logg.debug('sender {} balance before: {}'.format(signer_address, sender_balance))
|
|
|
|
|
logg.debug('recipient {} balance before: {}'.format(recipient, recipient_balance))
|
|
|
|
|
except urllib.error.URLError:
|
|
|
|
|
pass
|
|
|
|
|
|
2021-10-18 12:49:40 +02:00
|
|
|
|
(tx_hash_hex, o) = g.create(signer_address, add_0x(recipient), value, data=config.get('_DATA'), id_generator=rpc.id_generator)
|
2021-06-28 07:48:36 +02:00
|
|
|
|
|
|
|
|
|
if send:
|
|
|
|
|
conn.do(o)
|
|
|
|
|
if block_last:
|
|
|
|
|
r = conn.wait(tx_hash_hex)
|
|
|
|
|
if logg.isEnabledFor(logging.DEBUG):
|
2021-10-18 12:49:40 +02:00
|
|
|
|
sender_balance = balance(add_0x(signer_address), rpc.id_generator)
|
2021-10-18 12:18:20 +02:00
|
|
|
|
recipient_balance = balance(add_0x(recipient), rpc.id_generator)
|
2021-06-28 07:48:36 +02:00
|
|
|
|
logg.debug('sender {} balance after: {}'.format(signer_address, sender_balance))
|
|
|
|
|
logg.debug('recipient {} balance after: {}'.format(recipient, recipient_balance))
|
|
|
|
|
if r['status'] == 0:
|
2022-01-09 13:09:23 +01:00
|
|
|
|
logg.critical('VM revert for {}. Wish I could tell you more'.format(tx_hash_hex))
|
2021-06-28 07:48:36 +02:00
|
|
|
|
sys.exit(1)
|
|
|
|
|
print(tx_hash_hex)
|
|
|
|
|
else:
|
2021-08-21 09:27:40 +02:00
|
|
|
|
#if logg.isEnabledFor(logging.INFO):
|
|
|
|
|
if config.true('_RAW'):
|
|
|
|
|
print(o['params'][0])
|
|
|
|
|
else:
|
2021-06-28 07:48:36 +02:00
|
|
|
|
io_str = io.StringIO()
|
|
|
|
|
decode_for_puny_humans(o['params'][0], chain_spec, io_str)
|
|
|
|
|
print(io_str.getvalue())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|