Move all encoding steps to cliencoder

This commit is contained in:
nolash 2021-10-06 07:52:19 +02:00
parent 0e0dbf180e
commit b983938d80
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
2 changed files with 25 additions and 17 deletions

View File

@ -3,12 +3,15 @@ import re
import logging
# external imports
from chainlib.eth.contract import ABIContractType
from chainlib.eth.contract import (
ABIContractType,
ABIContractEncoder,
)
logg = logging.getLogger(__name__)
class CLIEncoder:
class CLIEncoder(ABIContractEncoder):
__re_uint = r'^([uU])[int]*([0-9]+)?$'
__re_bytes = r'^([bB])[ytes]*([0-9]+)?$'
@ -19,6 +22,12 @@ class CLIEncoder:
'to_string',
]
def __init__(self, signature=None):
super(CLIEncoder, self).__init__()
self.signature = signature
if signature != None:
self.method(signature)
def to_uint(self, typ):
s = None
a = None
@ -68,3 +77,13 @@ class CLIEncoder:
raise ValueError('no translation for type {}'.format(typ))
logg.debug('type {} translated to {}'.format(typ, r[0]))
return r[1]
def add_from(self, arg):
logg.debug('arg {}'.format(arg))
(typ, val) = arg.split(':', maxsplit=1)
real_typ = self.translate_type(typ)
if self.signature != None:
self.typ(real_typ)
fn = getattr(self, real_typ.value)
fn(val)

View File

@ -45,7 +45,6 @@ from chainlib.error import SignerMissingException
from chainlib.chain import ChainSpec
from chainlib.eth.runnable.util import decode_for_puny_humans
from chainlib.eth.jsonrpc import to_blockheight_param
from chainlib.eth.contract import ABIContractEncoder
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
@ -96,22 +95,12 @@ def main():
pass
code = '0x'
cli_encoder = CLIEncoder()
contract_encoder = ABIContractEncoder()
if config.get('_SIGNATURE'):
contract_encoder.method(args.signature)
cli_encoder = CLIEncoder(signature=config.get('_SIGNATURE'))
for arg in config.get('_CONTRACT_ARGS'):
logg.debug('arg {}'.format(arg))
(typ, val) = arg.split(':', maxsplit=1)
real_typ = cli_encoder.translate_type(typ)
if config.get('_SIGNATURE'):
contract_encoder.typ(real_typ)
fn = getattr(contract_encoder, real_typ.value)
fn(val)
code += contract_encoder.get()
cli_encoder.add_from(arg)
code += cli_encoder.get()
if not config.get('_SIGNATURE'):
print(strip_0x(code))