From 9bdf9db32a3dcc4add7678741a1fb8e7d5123a81 Mon Sep 17 00:00:00 2001 From: nolash Date: Sun, 10 Oct 2021 12:50:18 +0200 Subject: [PATCH] Add render token index test --- cic/ext/eth/__init__.py | 43 ++++++++++++++++++++++++++---- cic/keystore.py | 1 + tests/eth/test_token_index.py | 49 +++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 tests/eth/test_token_index.py diff --git a/cic/ext/eth/__init__.py b/cic/ext/eth/__init__.py index 940b6ab..9c21b72 100644 --- a/cic/ext/eth/__init__.py +++ b/cic/ext/eth/__init__.py @@ -2,22 +2,55 @@ import logging # external imports +from chainlib.chain import ChainSpec +from chainlib.eth.tx import TxFormat from chainlib.eth.connection import RPCConnection +from eth_token_index import TokenUniqueSymbolIndex logg = logging.getLogger(__name__) class CICEth: - def __init__(self, resources, proofs, signer_hint=None): + def __init__(self, chain_spec, resources, proofs, signer=None, rpc=None, nonce_oracle=None, fee_oracle=None): self.resources = resources self.proofs = proofs - self.signer_hint = signer_hint + self.chain_spec = chain_spec + self.signer = signer + self.rpc = rpc + self.nonce_oracle = nonce_oracle + self.fee_oracle = fee_oracle + self.token_address = None + self.outputs = {} + for k in resources.keys(): + self.outputs[k] = None - def token_index(self): - pass + + def process_token(self): + raise NotImplementedError + + + def process_token_index(self): + c = TokenUniqueSymbolIndex(self.chain_spec) # tx_format = None + if self.signer == None: + tx_format = TxFormat.RAW_ARGS + + contract_address = self.resources['token_index']['reference'] + signer_address = self.resources['token_index']['key_address'] + o = c.register(contract_address, signer_address, self.token_address, tx_format=tx_format) + if self.rpc == None: + self.outputs['token_index'] = o + + + def process(self): + self.token_address = self.resources['token']['reference'] + if self.token_address == None: + tasks.append('token') + tasks.append('token_index') + for task in tasks: + getattr(self, 'process_̈́ ' + task)() def new(resources, proofs, signer_hint=None): - return CICEth(resources, proofs, signer_hint=None) + return CICEth(resources, proofs, signer=None) diff --git a/cic/keystore.py b/cic/keystore.py index 5ea9a70..24cce56 100644 --- a/cic/keystore.py +++ b/cic/keystore.py @@ -12,6 +12,7 @@ from funga.keystore import Keystore logg = logging.getLogger(__name__) + class KeystoreDirectory(Keystore): def process_dir(self, path, password_retriever=None, default_password=''): diff --git a/tests/eth/test_token_index.py b/tests/eth/test_token_index.py new file mode 100644 index 0000000..ca74780 --- /dev/null +++ b/tests/eth/test_token_index.py @@ -0,0 +1,49 @@ +# standard imports +import unittest +import logging +import random + +# external imports +from chainlib.chain import ChainSpec +from hexathon import ( + add_0x, + strip_0x, + ) + +# local imports +from cic.ext.eth import CICEth + +logg = logging.getLogger(__name__) + + +class TestCICEth(unittest.TestCase): + + def setUp(self): + random.seed(42) + self.chain_spec = ChainSpec.from_chain_str('evm:foo:42') + self.token_address = add_0x(random.randbytes(20).hex()) + self.token_index_address = add_0x(random.randbytes(20).hex()) + self.resources = { + 'token': { + 'reference': self.token_address, + 'key_address': None, + }, + 'token_index': { + 'reference': self.token_index_address, + 'key_address': None, + }, + } + self.proofs = [] + for i in range(3): + self.proofs.append(random.randbytes(32).hex()) + self.adapter = CICEth(self.chain_spec, self.resources, self.proofs) + + + def test_offline_token_index(self): + self.adapter.token_address = self.token_address + v = self.adapter.process_token_index() + self.assertEqual(self.adapter.outputs['token_index'][:8], '4420e486') + + +if __name__ == '__main__': + unittest.main()