send_tokens

Function

def send_tokens(account, token_address, to_address, amount, attempts=18):
    token_contract = load_contract(token_address)
    token_info = get_token_info(token_address)
    try:
        tx = token_contract.functions.transfer(
            to_address,
            to_token_decimals(amount, token_info['decimals'])
        ).build_transaction({
            'nonce': get_nonce(account.address),
            'from': account.address
        })
        return broadcast_transaction(account, tx, False, attempts)
    except Exception as e:
        if error := interpret_exception_message(e):
            logging.error("{}. Could not send {} ({}) to {}".format(
                error,
                token_info['name'],
                token_info['symbol'],
                to_address
            ))
        return False

Description

  • Load the contract from token address

  • Get the token info for token decimals

  • Create a transaction to send an amount of tokens to another address

  • Broadcast and return True/False