Add send cli tool, make token resolver pluggable

This commit is contained in:
lash
2022-04-10 19:03:23 +00:00
parent dab50dadd1
commit 2a7fd70f4e
23 changed files with 234 additions and 1014 deletions

View File

@@ -0,0 +1 @@
from .base import *

52
chaind/eth/token/base.py Normal file
View 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
View 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
View 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

View 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)