From 53e9c636982d74b9b147449504c8241cfbbbc586 Mon Sep 17 00:00:00 2001 From: nolash Date: Fri, 8 Oct 2021 16:04:13 +0200 Subject: [PATCH] Add token api metadata getter (first without okota) --- apps/cic-eth/cic_eth/admin/token.py | 38 ++++++++++++++++--- apps/cic-eth/cic_eth/api/api_task.py | 30 +++++++++++++++ .../tests/task/api/test_app_noncritical.py | 28 ++++++++++++++ 3 files changed, 90 insertions(+), 6 deletions(-) diff --git a/apps/cic-eth/cic_eth/admin/token.py b/apps/cic-eth/cic_eth/admin/token.py index 985a5f2c..ed42c0bb 100644 --- a/apps/cic-eth/cic_eth/admin/token.py +++ b/apps/cic-eth/cic_eth/admin/token.py @@ -3,9 +3,15 @@ import logging # external imports import celery +from chainlib.connection import RPCConnection +from chainlib.chain import ChainSpec +from cic_eth_registry.erc20 import ERC20Token +from hexathon import add_0x # local imports -from cic_eth.task import BaseTask +from cic_eth.task import ( + BaseTask, + ) celery_app = celery.current_app logg = logging.getLogger() @@ -14,8 +20,28 @@ logg = logging.getLogger() @celery_app.task(bind=True, base=BaseTask) def default_token(self): return { - 'symbol': self.default_token_symbol, - 'address': self.default_token_address, - 'name': self.default_token_name, - 'decimals': self.default_token_decimals, - } + 'symbol': self.default_token_symbol, + 'address': self.default_token_address, + 'name': self.default_token_name, + 'decimals': self.default_token_decimals, + } + + +@celery_app.task(bind=True, base=BaseTask) +def token(self, tokens, chain_spec_dict): + chain_spec = ChainSpec.from_dict(chain_spec_dict) + rpc = RPCConnection.connect(chain_spec, 'default') + + r = [] + for token in tokens: + token_chain_object = ERC20Token(chain_spec, rpc, add_0x(token['address'])) + token_chain_object.load(rpc) + token_data = { + 'decimals': token_chain_object.decimals, + 'name': token_chain_object.name, + 'symbol': token_chain_object.symbol, + 'address': token_chain_object.address, + } + r.append(token_data) + + return r diff --git a/apps/cic-eth/cic_eth/api/api_task.py b/apps/cic-eth/cic_eth/api/api_task.py index 1ae14123..b6a0e3f7 100644 --- a/apps/cic-eth/cic_eth/api/api_task.py +++ b/apps/cic-eth/cic_eth/api/api_task.py @@ -35,6 +35,36 @@ class Api(ApiBase): return s_token.apply_async() + def token(self, token_symbol): + return self.tokens([token_symbol]) + + + def tokens(self, token_symbols): + chain_spec_dict = self.chain_spec.asdict() + s_token_resolve = celery.signature( + 'cic_eth.eth.erc20.resolve_tokens_by_symbol', + [ + token_symbols, + chain_spec_dict, + ], + queue=self.queue, + ) + + s_token = celery.signature( + 'cic_eth.admin.token.token', + [ + chain_spec_dict, + ], + queue=self.queue, + ) + + s_token_resolve.link(s_token) + if self.callback_param != None: + s_token.link(self.callback_success) + + return s_token_resolve.apply_async() + + # def convert_transfer(self, from_address, to_address, target_return, minimum_return, from_token_symbol, to_token_symbol): # """Executes a chain of celery tasks that performs conversion between two ERC20 tokens, and transfers to a specified receipient after convert has completed. # diff --git a/apps/cic-eth/tests/task/api/test_app_noncritical.py b/apps/cic-eth/tests/task/api/test_app_noncritical.py index 7ebb8700..702bd226 100644 --- a/apps/cic-eth/tests/task/api/test_app_noncritical.py +++ b/apps/cic-eth/tests/task/api/test_app_noncritical.py @@ -17,3 +17,31 @@ def test_default_token( t = api.default_token() r = t.get_leaf() assert r['address'] == foo_token + + +def test_tokens( + default_chain_spec, + foo_token, + bar_token, + token_registry, + register_tokens, + register_lookups, + cic_registry, + init_database, + init_celery_tasks, + custodial_roles, + celery_worker, + ): + + api = Api(str(default_chain_spec), queue=None) + + t = api.token('FOO') + r = t.get_leaf() + assert len(r) == 1 + assert r[0]['address'] == foo_token + + t = api.tokens(['BAR', 'FOO']) + r = t.get_leaf() + assert len(r) == 2 + assert r[1]['address'] == foo_token + assert r[0]['address'] == bar_token