cic-internal-integration/apps/cic-eth/cic_eth/server/controllers/account_controller.py

188 lines
4.8 KiB
Python

import json
import logging
import sys
import uuid
import connexion
import redis
import six
from cic_eth.api.api_task import Api
from cic_eth.graphql.config import config
from cic_eth.server import util
from cic_eth.server.models.token import Token # noqa: E501
from cic_eth.server.models.token_balance import TokenBalance # noqa: E501
log = logging.getLogger(__name__)
chain_spec = config.get('CHAIN_SPEC')
celery_queue = config.get('CELERY_QUEUE')
api = Api(
chain_spec,
queue=celery_queue,
)
redis_host = config.get('REDIS_HOST')
redis_port = config.get('REDIS_PORT')
redis_db = config.get('REDIS_DB')
logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger(__name__)
def call(method, *args):
redis_channel = str(uuid.uuid4())
r = redis.Redis(redis_host, redis_port, redis_db)
ps = r.pubsub()
ps.subscribe(redis_channel)
log.debug(f"message 1: {ps.get_message()}") # Subscription Object
print(f"Channel init {redis_channel}")
print(args)
api = Api(
chain_spec,
queue=celery_queue,
callback_param='{}:{}:{}:{}'.format(
redis_host, redis_port, redis_db, redis_channel),
callback_task='cic_eth.callbacks.redis.redis',
callback_queue=celery_queue,
)
getattr(api, method)(*args)
log.debug(f"message 2: {ps.get_message()}") # returns None !?
try:
o = ps.get_message(timeout=config.get('REDIS_TIMEOUT'))
log.debug(f"message 3: {o}")
except TimeoutError as e:
sys.stderr.write(
'got no new address from cic-eth before timeout: {}\n'.format(e))
sys.exit(1)
ps.unsubscribe()
print(o)
m = json.loads(o['data'])
return m["result"]
def account_balance(address, token_symbol, include_pending=True): # noqa: E501
"""account_balance
Retrieve Address Balance # noqa: E501
:param address:
:type address: dict | bytes
:param token_symbol:
:type token_symbol: str
:param include_pending:
:type include_pending: bool
:rtype: TokenBalance
"""
task = api.balance(address=address, token_symbol=token_symbol,
include_pending=include_pending)
data = task.get() # api call('balance', address, token_symbol, include_pending)
log.debug(data)
#[{'address': '3ff776b6f888980def9d4220858803f9dc5e341e', 'converters': [], 'balance_network': 0}]
return list(map(lambda b : TokenBalance(**b), data))
def create_account_post(password, register): # noqa: E501
"""create_account_post
Creates a new blockchain address # noqa: E501
:param password:
:type password: str
:param register:
:type register: bool
:rtype: Model0xAddress
"""
data = call("create_account", password, register)
return data
def list_transactions(address, limit=10): # noqa: E501
"""list_transactions
Retrieve Address Balance # noqa: E501
:param address:
:type address: dict | bytes
:param limit:
:type limit: int
:rtype: Token
"""
api = Api(
chain_spec,
queue=celery_queue,
)
data = call('list', address, limit)
# data = task.get()
return data
def transfer(from_address, to_address, value, token_symbol): # noqa: E501
"""transfer
Performs a transfer of ERC20 tokens from one address to another. # noqa: E501
:param from_address:
:type from_address: dict | bytes
:param to_address:
:type to_address: dict | bytes
:param value:
:type value: int
:param token_symbol:
:type token_symbol: str
:rtype: Token
"""
api = Api(
chain_spec,
queue=celery_queue,
)
t = api.transfer(from_address, to_address,
int(value * (10**6)), token_symbol)
log.debug(f"t {t}")
log.debug(f"transfer {t.get_leaf()}")
log.debug(f"transfer {t.successful()}")
return t.get()
def transfer_from(from_address, to_address, value, token_symbol, spender_address): # noqa: E501
"""transfer_from
Performs a transfer of ERC20 tokens by one address on behalf of another address to a third party. # noqa: E501
:param from_address: Ethereum address of sender
:type from_address: dict | bytes
:param to_address: Ethereum address of recipient
:type to_address: dict | bytes
:param value: Estimated return from conversion
:type value: int
:param token_symbol: ERC20 token symbol of token to send
:type token_symbol: str
:param spender_address: Ethereum address of recipient
:type spender_address: dict | bytes
:rtype: Token
"""
api = Api(
chain_spec,
queue=celery_queue,
)
t = api.transfer_from(from_address, to_address,
int(value * (10**6)), token_symbol, spender_address)
log.debug(f"t {t}")
log.debug(f"transfer {t.get_leaf()}")
log.debug(f"transfer {t.successful()}")
return t.get()