Add token api metadata getter (first without okota)

This commit is contained in:
nolash 2021-10-08 16:04:13 +02:00
parent 6b59c87f68
commit 53e9c63698
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 90 additions and 6 deletions

View File

@ -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()
@ -19,3 +25,23 @@ def default_token(self):
'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

View File

@ -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.
#

View File

@ -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