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