Implement custom token args types and positioning

This commit is contained in:
nolash 2021-10-10 19:18:37 +02:00
parent a4443bd00e
commit 6a5b0388ec
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746

View File

@ -9,7 +9,11 @@ from chainlib.eth.tx import (
TxFactory, TxFactory,
) )
from chainlib.eth.connection import RPCConnection from chainlib.eth.connection import RPCConnection
from chainlib.eth.contract import ABIContractEncoder from chainlib.eth.contract import (
ABIContractEncoder,
ABIContractType
)
from chainlib.eth.address import is_address
from eth_token_index import TokenUniqueSymbolIndex from eth_token_index import TokenUniqueSymbolIndex
from eth_address_declarator import Declarator from eth_address_declarator import Declarator
@ -39,24 +43,88 @@ class CICEth:
self.tx_format = TxFormat.RLP_SIGNED self.tx_format = TxFormat.RLP_SIGNED
def prepare_token(self, name, symbol, precision, code, extra={}, positions=None): def prepare_token(self, name, symbol, precision, code, extra=[], extra_types=[], positions=None):
self.token_details = { self.token_details = {
'name': name, 'name': name,
'symbol': symbol, 'symbol': symbol,
'precision': precision, 'precision': precision,
'code': code, 'code': code,
'extra': extra, 'extra': extra,
'extra_types': extra_types,
'positions': positions, 'positions': positions,
} }
def process_token(self): def __detect_arg_type(self, v):
enc = ABIContractEncoder() typ = None
enc.string(self.token_details['name']) try:
enc.string(self.token_details['symbol']) int(v, 10)
enc.uint256(self.token_details['precision']) typ = ABIContractType.UINT256
code = self.token_details['code'] + enc.get() except TypeError:
pass
if typ == None:
try:
vv = strip_0x(v)
if is_address(vv):
typ = ABIContractType.ADDRESS
else:
typ = ABIContractType.BYTES32
except ValueError:
pass
if typ == None:
try:
v.encode('utf-8')
typ = ABIContractType.STRING
except ValueError:
pass
if typ == None:
raise ValueError('cannot automatically determine type for value {}'.format(v))
logg.info('argument {} parsed as abi contract type {}'.format(typ.value))
return typ
def __order_args(self):
args = [
self.token_details['name'],
self.token_details['symbol'],
self.token_details['precision'],
]
args_types = [
ABIContractType.STRING.value,
ABIContractType.STRING.value,
ABIContractType.UINT256.value,
]
for i, x in enumerate(self.token_details['extra']):
args.append(x)
typ = None
if self.token_details['extra_types'] != None:
typ = self.token_details['extra_types'][i]
else:
typ = self.__detect_arg_type(x)
arg_types.append(typ)
positions = self.token_details['positions']
if positions == None:
positions = list(range(len(args)))
return (args, args_types, positions)
def process_token(self):
(args, args_types, positions) = self.__order_args()
enc = ABIContractEncoder()
for i in positions:
getattr(enc, args_types[i])(args[i])
code = self.token_details['code'] + enc.get()
signer_address = self.resources['token']['key_address'] signer_address = self.resources['token']['key_address']
c = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=self.nonce_oracle, gas_oracle=self.fee_oracle) c = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=self.nonce_oracle, gas_oracle=self.fee_oracle)
@ -76,6 +144,7 @@ class CICEth:
self.outputs.append(r) self.outputs.append(r)
return r return r
def process_token_index(self): def process_token_index(self):
c = TokenUniqueSymbolIndex(self.chain_spec, signer=self.signer) c = TokenUniqueSymbolIndex(self.chain_spec, signer=self.signer)