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

113 lines
2.9 KiB
Python
Raw Normal View History

2021-11-12 09:14:40 +01:00
import logging
2021-11-17 13:21:23 +01:00
2021-11-16 14:37:16 +01:00
from cic_eth.server.celery_helper import call
from cic_eth.server.models import TokenBalance, Transaction
2021-11-12 09:14:40 +01:00
log = logging.getLogger(__name__)
2021-11-16 14:37:16 +01:00
def account_balance(address, token_symbol, include_pending=True):
2021-11-12 09:14:40 +01:00
"""account_balance
2021-11-16 14:37:16 +01:00
Retrieve Address Balance
2021-11-12 09:14:40 +01:00
:param address:
:type address: dict | bytes
:param token_symbol:
:type token_symbol: str
:param include_pending:
:type include_pending: bool
2021-11-17 13:21:23 +01:00
:rtype: [TokenBalance]
2021-11-12 09:14:40 +01:00
"""
2021-11-17 13:21:23 +01:00
balance = call("balance", address, token_symbol, include_pending)
log.debug(balance)
2021-11-12 09:14:40 +01:00
#[{'address': '3ff776b6f888980def9d4220858803f9dc5e341e', 'converters': [], 'balance_network': 0}]
2021-11-17 13:21:23 +01:00
return list(map(lambda b: TokenBalance(**b), balance))
2021-11-12 09:14:40 +01:00
2021-11-16 14:37:16 +01:00
def create_account_post(password="", register=True):
2021-11-12 09:14:40 +01:00
"""create_account_post
2021-11-16 14:37:16 +01:00
Creates a new blockchain address
2021-11-12 09:14:40 +01:00
:param password:
:type password: str
:param register:
:type register: bool
2021-11-16 14:37:16 +01:00
:return: Address of the new account
:rtype: string
2021-11-12 09:14:40 +01:00
"""
data = call("create_account", password, register)
return data
2021-11-16 14:37:16 +01:00
def list_transactions(address, limit=10):
2021-11-12 09:14:40 +01:00
"""list_transactions
2021-11-16 14:37:16 +01:00
Retrieve Address Balance
2021-11-12 09:14:40 +01:00
:param address:
:type address: dict | bytes
:param limit:
:type limit: int
2021-11-17 13:21:23 +01:00
:rtype: [Transaction]
2021-11-12 09:14:40 +01:00
"""
2021-11-12 10:41:57 +01:00
data = call('list', address, limit)
# data = task.get()
2021-11-17 13:21:23 +01:00
log.debug(data)
transactions = list(map(lambda t: Transaction(**t), data))
log.debug(transactions)
return transactions
2021-11-12 09:14:40 +01:00
2021-11-16 14:37:16 +01:00
def transfer(from_address, to_address, value, token_symbol):
2021-11-12 09:14:40 +01:00
"""transfer
2021-11-16 14:37:16 +01:00
Performs a transfer of ERC20 tokens from one address to another.
2021-11-12 09:14:40 +01:00
: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
2021-11-16 14:37:16 +01:00
:return: Transaction hash for tranfer operation
:rtype: str, 0x-hex
2021-11-12 09:14:40 +01:00
"""
2021-11-16 14:37:16 +01:00
data = call('transfer', from_address, to_address,
int(value * (10**6)), token_symbol)
return data
def transfer_from(from_address, to_address, value, token_symbol, spender_address):
2021-11-12 09:14:40 +01:00
"""transfer_from
2021-11-16 14:37:16 +01:00
Performs a transfer of ERC20 tokens by one address on behalf of another address to a third party.
2021-11-12 09:14:40 +01:00
: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
2021-11-16 14:37:16 +01:00
:return: Transaction hash for transfer operation
:rtype: str, 0x-hex
2021-11-12 09:14:40 +01:00
"""
2021-11-12 10:41:57 +01:00
2021-11-17 13:21:23 +01:00
data = call("transfer_from", from_address, to_address, int(
value * (10**6)), token_symbol, spender_address)
2021-11-12 10:41:57 +01:00
2021-11-16 14:37:16 +01:00
return data