Add send cli tool, make token resolver pluggable
This commit is contained in:
29
chaind/eth/cli/csv.py
Normal file
29
chaind/eth/cli/csv.py
Normal file
@@ -0,0 +1,29 @@
|
||||
# standard imports
|
||||
import logging
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class CSVProcessor:
|
||||
|
||||
def load(self, s):
|
||||
contents = []
|
||||
f = None
|
||||
try:
|
||||
f = open(s, 'r')
|
||||
except FileNotFoundError:
|
||||
return None
|
||||
|
||||
import csv # only import if needed
|
||||
fr = csv.reader(f)
|
||||
|
||||
for r in fr:
|
||||
contents.append(r)
|
||||
f.close()
|
||||
l = len(contents)
|
||||
logg.info('successfully parsed source as csv, found {} records'.format(l))
|
||||
return contents
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return 'csv processor'
|
||||
34
chaind/eth/cli/output.py
Normal file
34
chaind/eth/cli/output.py
Normal file
@@ -0,0 +1,34 @@
|
||||
# standard imports
|
||||
import logging
|
||||
import socket
|
||||
import enum
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class OpMode(enum.Enum):
|
||||
STDOUT = 'standard_output'
|
||||
UNIX = 'unix_socket'
|
||||
|
||||
class Outputter:
|
||||
|
||||
def __init__(self, mode):
|
||||
self.out = getattr(self, 'do_' + mode.value)
|
||||
|
||||
|
||||
def do(self, hx, *args, **kwargs):
|
||||
return self.out(hx, *args, **kwargs)
|
||||
|
||||
|
||||
def do_standard_output(self, hx, *args, **kwargs):
|
||||
return hx
|
||||
|
||||
|
||||
def do_unix_socket(self, hx, *args, **kwargs):
|
||||
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
s.connect(kwargs['socket'])
|
||||
s.send(hx.encode('utf-8'))
|
||||
r = s.recv(64+4)
|
||||
logg.debug('r {}'.format(r))
|
||||
s.close()
|
||||
return r[4:].decode('utf-8')
|
||||
119
chaind/eth/runnable/send.py
Normal file
119
chaind/eth/runnable/send.py
Normal file
@@ -0,0 +1,119 @@
|
||||
# standard imports
|
||||
import os
|
||||
import logging
|
||||
import sys
|
||||
import datetime
|
||||
import enum
|
||||
import re
|
||||
import stat
|
||||
import socket
|
||||
|
||||
# external imports
|
||||
import chainlib.eth.cli
|
||||
from chaind.setup import Environment
|
||||
from chainlib.eth.gas import price
|
||||
from chainlib.chain import ChainSpec
|
||||
from hexathon import strip_0x
|
||||
|
||||
# local imports
|
||||
from chaind.error import TxSourceError
|
||||
from chaind.eth.token.process import Processor
|
||||
from chaind.eth.token.gas import GasTokenResolver
|
||||
from chaind.eth.cli.csv import CSVProcessor
|
||||
from chaind.eth.cli.output import (
|
||||
Outputter,
|
||||
OpMode,
|
||||
)
|
||||
|
||||
logging.basicConfig(level=logging.WARNING)
|
||||
logg = logging.getLogger()
|
||||
|
||||
script_dir = os.path.dirname(os.path.realpath(__file__))
|
||||
config_dir = os.path.join(script_dir, '..', 'data', 'config')
|
||||
|
||||
|
||||
arg_flags = chainlib.eth.cli.argflag_std_write
|
||||
argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
|
||||
argparser.add_argument('--socket', dest='socket', type=str, help='Socket to send transactions to')
|
||||
argparser.add_argument('--token-module', dest='token_module', type=str, help='Python module path to resolve tokens from identifiers')
|
||||
argparser.add_positional('source', required=False, type=str, help='Transaction source file')
|
||||
args = argparser.parse_args()
|
||||
|
||||
extra_args = {
|
||||
'socket': None,
|
||||
'source': None,
|
||||
}
|
||||
env = Environment(domain='eth', env=os.environ)
|
||||
config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, base_config_dir=config_dir)
|
||||
config.add(args.token_module, 'TOKEN_MODULE', True)
|
||||
|
||||
wallet = chainlib.eth.cli.Wallet()
|
||||
wallet.from_config(config)
|
||||
|
||||
rpc = chainlib.eth.cli.Rpc(wallet=wallet)
|
||||
conn = rpc.connect_by_config(config)
|
||||
|
||||
chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
|
||||
|
||||
mode = OpMode.STDOUT
|
||||
|
||||
re_unix = r'^ipc://(/.+)'
|
||||
m = re.match(re_unix, config.get('_SOCKET', ''))
|
||||
if m != None:
|
||||
config.add(m.group(1), '_SOCKET', exists_ok=True)
|
||||
r = 0
|
||||
try:
|
||||
stat_info = os.stat(config.get('_SOCKET'))
|
||||
if not stat.S_ISSOCK(stat_info.st_mode):
|
||||
r = 1
|
||||
except FileNotFoundError:
|
||||
r = 1
|
||||
|
||||
if r > 0:
|
||||
sys.stderr.write('{} is not a socket\n'.format(config.get('_SOCKET')))
|
||||
sys.exit(1)
|
||||
|
||||
mode = OpMode.UNIX
|
||||
|
||||
logg.info('using mode {}'.format(mode.value))
|
||||
|
||||
if config.get('_SOURCE') == None:
|
||||
sys.stderr.write('source data missing\n')
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def main():
|
||||
token_resolver = None
|
||||
if config.get('TOKEN_MODULE') != None:
|
||||
import importlib
|
||||
m = importlib.import_module(config.get('TOKEN_MODULE'))
|
||||
m = m.TokenResolver
|
||||
else:
|
||||
from chaind.eth.token.gas import GasTokenResolver
|
||||
m = GasTokenResolver
|
||||
token_resolver = m(chain_spec, rpc.get_sender_address(), rpc.get_signer(), rpc.get_gas_oracle(), rpc.get_nonce_oracle())
|
||||
|
||||
processor = Processor(token_resolver, config.get('_SOURCE'))
|
||||
processor.add_processor(CSVProcessor())
|
||||
|
||||
sends = None
|
||||
try:
|
||||
sends = processor.load()
|
||||
except TxSourceError as e:
|
||||
sys.stderr.write('processing error: {}. processors: {}\n'.format(str(e), str(processor)))
|
||||
sys.exit(1)
|
||||
|
||||
tx_iter = iter(processor)
|
||||
out = Outputter(mode)
|
||||
while True:
|
||||
tx = None
|
||||
try:
|
||||
tx_bytes = next(tx_iter)
|
||||
except StopIteration:
|
||||
break
|
||||
tx_hex = tx_bytes.hex()
|
||||
print(out.do(tx_hex))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
1
chaind/eth/token/__init__.py
Normal file
1
chaind/eth/token/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .base import *
|
||||
52
chaind/eth/token/base.py
Normal file
52
chaind/eth/token/base.py
Normal file
@@ -0,0 +1,52 @@
|
||||
# standard imports
|
||||
import logging
|
||||
|
||||
# external imports
|
||||
from funga.eth.transaction import EIP155Transaction
|
||||
from hexathon import strip_0x
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
class BaseTokenResolver:
|
||||
|
||||
def __init__(self, chain_spec, sender, signer, gas_oracle, nonce_oracle):
|
||||
self.chain_spec = chain_spec
|
||||
self.chain_id = chain_spec.chain_id()
|
||||
self.signer = signer
|
||||
self.sender = sender
|
||||
self.gas_oracle = gas_oracle
|
||||
self.nonce_oracle = nonce_oracle
|
||||
self.factory = None
|
||||
self.gas_limit_start = None
|
||||
self.gas_price_start = None
|
||||
|
||||
|
||||
def reset(self):
|
||||
gas_data = self.gas_oracle.get_gas()
|
||||
self.gas_price_start = gas_data[0]
|
||||
self.gas_limit_start = gas_data[1]
|
||||
|
||||
|
||||
def get_values(self, gas_value, value, executable_address=None):
|
||||
if executable_address == None:
|
||||
return (value, 0)
|
||||
|
||||
try:
|
||||
value = int(value)
|
||||
except ValueError:
|
||||
value = int(strip_0x(value), 16)
|
||||
|
||||
try:
|
||||
gas_value = int(gas_value)
|
||||
except ValueError:
|
||||
gas_value = int(strip_0x(gas_value), 16)
|
||||
|
||||
nonce = self.nonce_oracle.next_nonce()
|
||||
|
||||
return (gas_value, value, nonce,)
|
||||
|
||||
|
||||
def sign(self, tx):
|
||||
tx_o = EIP155Transaction(tx, tx['nonce'], self.chain_id)
|
||||
tx_bytes = self.signer.sign_transaction_to_wire(tx_o)
|
||||
return tx_bytes
|
||||
26
chaind/eth/token/erc20.py
Normal file
26
chaind/eth/token/erc20.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# external imports
|
||||
from eth_erc20 import ERC20
|
||||
from chainlib.eth.tx import TxFormat
|
||||
|
||||
# local imports
|
||||
from chaind.eth.token import BaseTokenResolver
|
||||
|
||||
|
||||
class TokenResolver(BaseTokenResolver):
|
||||
|
||||
def __init__(self, chain_spec, sender, signer, gas_oracle, nonce_oracle):
|
||||
super(TokenResolver, self).__init__(chain_spec, sender, signer, gas_oracle, nonce_oracle)
|
||||
self.factory = ERC20(self.chain_spec, signer=self.signer, gas_oracle=self.gas_oracle, nonce_oracle=self.nonce_oracle)
|
||||
|
||||
|
||||
def create(self, recipient, gas_value, data=None, token_value=0, executable_address=None, passphrase=None):
|
||||
|
||||
if executable_address == None:
|
||||
raise ValueError('executable address required')
|
||||
|
||||
(gas_value, token_value, nonce) = self.get_values(gas_value, token_value, executable_address=executable_address)
|
||||
|
||||
tx = self.factory.transfer(executable_address, self.sender, recipient, token_value, tx_format=TxFormat.DICT)
|
||||
tx['value'] = gas_value
|
||||
|
||||
return tx
|
||||
30
chaind/eth/token/gas.py
Normal file
30
chaind/eth/token/gas.py
Normal file
@@ -0,0 +1,30 @@
|
||||
# external imports
|
||||
from chainlib.eth.gas import Gas
|
||||
from hexathon import strip_0x
|
||||
|
||||
# local imports
|
||||
from chaind.eth.token import BaseTokenResolver
|
||||
|
||||
|
||||
class GasTokenResolver(BaseTokenResolver):
|
||||
|
||||
def __init__(self, chain_spec, sender, signer, gas_oracle, nonce_oracle):
|
||||
super(GasTokenResolver, self).__init__(chain_spec, sender, signer, gas_oracle, nonce_oracle)
|
||||
self.factory = Gas(self.chain_spec, signer=self.signer, gas_oracle=self.gas_oracle, nonce_oracle=self.nonce_oracle)
|
||||
|
||||
|
||||
def create(self, recipient, gas_value, data=None, token_value=0, executable_address=None, passphrase=None):
|
||||
|
||||
(gas_value, token_value, nonce) = self.get_values(gas_value, token_value, executable_address=executable_address)
|
||||
|
||||
tx = {
|
||||
'from': self.sender,
|
||||
'to': recipient,
|
||||
'value': gas_value,
|
||||
'data': data,
|
||||
'nonce': nonce,
|
||||
'gasPrice': self.gas_price_start,
|
||||
'gas': self.gas_limit_start,
|
||||
}
|
||||
|
||||
return tx
|
||||
98
chaind/eth/token/process.py
Normal file
98
chaind/eth/token/process.py
Normal file
@@ -0,0 +1,98 @@
|
||||
# standard imports
|
||||
import logging
|
||||
|
||||
# external imports
|
||||
from chaind.error import TxSourceError
|
||||
from chainlib.eth.address import is_checksum_address
|
||||
from chainlib.eth.tx import unpack
|
||||
from chainlib.eth.gas import Gas
|
||||
from hexathon import (
|
||||
add_0x,
|
||||
strip_0x,
|
||||
)
|
||||
from funga.eth.transaction import EIP155Transaction
|
||||
#from eth_erc20 import ERC20
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Processor:
|
||||
|
||||
def __init__(self, resolver, source):
|
||||
self.resolver = resolver
|
||||
self.source = source
|
||||
self.processor = []
|
||||
|
||||
|
||||
def add_processor(self, processor):
|
||||
self.processor.append(processor)
|
||||
|
||||
|
||||
def load(self, process=True):
|
||||
for processor in self.processor:
|
||||
self.content = processor.load(self.source)
|
||||
if self.content != None:
|
||||
if process:
|
||||
try:
|
||||
self.process()
|
||||
except Exception as e:
|
||||
raise TxSourceError('invalid source contents: {}'.format(str(e)))
|
||||
return self.content
|
||||
raise TxSourceError('unparseable source')
|
||||
|
||||
|
||||
# 0: recipient
|
||||
# 1: amount
|
||||
# 2: token identifier (optional, when not specified network gas token will be used)
|
||||
# 3: gas amount (optional)
|
||||
def process(self):
|
||||
txs = []
|
||||
for i, r in enumerate(self.content):
|
||||
logg.debug('processing {}'.format(r))
|
||||
if not is_checksum_address(r[0]):
|
||||
raise ValueError('invalid checksum address {} in record {}'.format(r[0], i))
|
||||
self.content[i][0] = add_0x(r[0])
|
||||
try:
|
||||
self.content[i][1] = int(r[1])
|
||||
except ValueError:
|
||||
self.content[i][1] = int(strip_0x(r[1]), 16)
|
||||
native_token_value = 0
|
||||
|
||||
if len(self.content[i]) == 3:
|
||||
self.content[i].append(native_token_value)
|
||||
|
||||
|
||||
def __iter__(self):
|
||||
self.resolver.reset()
|
||||
self.cursor = 0
|
||||
return self
|
||||
|
||||
|
||||
def __next__(self):
|
||||
if self.cursor == len(self.content):
|
||||
raise StopIteration()
|
||||
|
||||
r = self.content[self.cursor]
|
||||
|
||||
value = r[1]
|
||||
gas_value = 0
|
||||
try:
|
||||
gas_value = r[3]
|
||||
except IndexError:
|
||||
pass
|
||||
logg.debug('gasvalue {}'.format(gas_value))
|
||||
data = '0x'
|
||||
|
||||
tx = self.resolver.create(r[0], gas_value, data=data, token_value=value, executable_address=r[2])
|
||||
v = self.resolver.sign(tx)
|
||||
|
||||
self.cursor += 1
|
||||
|
||||
return v
|
||||
|
||||
|
||||
def __str__(self):
|
||||
names = []
|
||||
for s in self.processor:
|
||||
names.append(str(s))
|
||||
return ','.join(names)
|
||||
Reference in New Issue
Block a user