commit b983938d8031a916d98b178a26e15ca5ef36487e
parent 0e0dbf180ebeaed0cb5e9b3e6c33de0dd03070b7
Author: nolash <dev@holbrook.no>
Date: Wed, 6 Oct 2021 07:52:19 +0200
Move all encoding steps to cliencoder
Diffstat:
2 files changed, 25 insertions(+), 17 deletions(-)
diff --git a/chainlib/eth/cli/encode.py b/chainlib/eth/cli/encode.py
@@ -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)
diff --git a/chainlib/eth/runnable/encode.py b/chainlib/eth/runnable/encode.py
@@ -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))