TokenDetails
Documentation for eth_defi.token.TokenDetails Python class.
- class TokenDetails
Bases:
objectERC-20 token Python presentation.
A helper class to work with ERC-20 tokens.
Read on-chain data, deal with token value decimal conversions.
Any field can be
Nonefor non-well-formed tokens.Supports one-way pickling
Example how to get USDC details on Polygon:
usdc = fetch_erc20_details(web3, "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174") # USDC on Polygon formatted = f"Token {usdc.name} ({usdc.symbol}) at {usdc.address} on chain {usdc.chain_id}" assert formatted == "Token USD Coin (PoS) (USDC) at 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174 on chain 137"
Attributes summary
The address of this token.
The address of this token.
The EVM chain id where this token lives.
Number of decimals
Alias for underlying Web3 contract method
Token name e.g.
Token symbol e.g.
Token supply as raw units
The underlying ERC-20 contract proxy class instance
Extra metadata, e.g.
Methods summary
__init__(contract[, name, symbol, ...])approve(to, amount)Prepare a ERC20.approve() transaction with human-readable amount.
convert_to_decimals(raw_amount)Convert raw token units to decimals.
convert_to_raw(decimal_amount)Convert decimalised token amount to raw uint256.
export()Create a serialisable entry of this class.
fetch_balance_of(address[, block_identifier])Get an address token balance.
fetch_raw_balance_of(address[, block_identifier])Get an address token balance.
generate_cache_key(chain_id, address)Generate a cache key for this token.
Smell test for stablecoins.
transfer(to, amount)Prepare a ERC20.transfer() transaction with human-readable amount.
- contract: web3.contract.contract.Contract
The underlying ERC-20 contract proxy class instance
- name: Optional[str] = None
Token name e.g.
USD Circle
- symbol: Optional[str] = None
Token symbol e.g.
USDC
- total_supply: Optional[int] = None
Token supply as raw units
- decimals: Optional[int] = None
Number of decimals
- property chain_id: int
The EVM chain id where this token lives.
- property address: eth_typing.evm.HexAddress
The address of this token.
See also
address_lower().
- property address_lower: eth_typing.evm.HexAddress
The address of this token.
Always lowercase.
- property functions: web3.contract.contract.ContractFunctions
Alias for underlying Web3 contract method
- convert_to_decimals(raw_amount)
Convert raw token units to decimals.
Example:
details = fetch_erc20_details(web3, token_address) # Convert 1 wei units to edcimals assert details.convert_to_decimals(1) == Decimal("0.0000000000000001")
- Parameters
raw_amount (int) –
- Return type
- convert_to_raw(decimal_amount)
Convert decimalised token amount to raw uint256.
Example:
details = fetch_erc20_details(web3, token_address) # Convert 1.0 USDC to raw unit with 6 decimals assert details.convert_to_raw(1) == 1_000_000
- Parameters
decimal_amount (decimal.Decimal) –
- Return type
- fetch_balance_of(address, block_identifier='latest')
Get an address token balance.
- Parameters
block_identifier – A specific block to query if doing archive node historical queries
address (Union[eth_typing.evm.HexAddress, str]) –
- Returns
Converted to decimal using
convert_to_decimal()- Return type
- transfer(to, amount)
Prepare a ERC20.transfer() transaction with human-readable amount.
Example:
another_new_depositor = web3.eth.accounts[6] tx_hash = base_usdc.transfer(another_new_depositor, Decimal(500)).transact({"from": usdc_holder, "gas": 100_000}) assert_transaction_success_with_explanation(web3, tx_hash)
- Returns
Bound contract function you need to turn to a tx
- Parameters
to (Union[eth_typing.evm.HexAddress, str]) –
amount (decimal.Decimal) –
- Return type
web3.contract.contract.ContractFunction
- approve(to, amount)
Prepare a ERC20.approve() transaction with human-readable amount.
Example:
usdc_amount = Decimal(9.00) tx_hash = usdc.approve(vault.address, usdc_amount).transact({"from": depositor}) assert_transaction_success_with_explanation(web3, tx_hash)
- Returns
Bound contract function you need to turn to a tx
- Parameters
to (Union[eth_typing.evm.HexAddress, str]) –
amount (decimal.Decimal) –
- Return type
web3.contract.contract.ContractFunction
- fetch_raw_balance_of(address, block_identifier='latest')
Get an address token balance.
- Parameters
block_identifier – A specific block to query if doing archive node historical queries
address (Union[eth_typing.evm.HexAddress, str]) –
- Returns
Raw token amount.
- Return type
- static generate_cache_key(chain_id, address)
Generate a cache key for this token.
Cached by (chain, address) as a string
Validate the inputs before generating the key
Address is always lowercase
- export()
Create a serialisable entry of this class.
Removes web3 connection and such unserialisable data.
- Returns
Python dict of exported data.
- Return type
- is_stablecoin_like()
Smell test for stablecoins.
Symbol check for common stablecoins
Not immune to scams
For the list see
is_stablecoin_like()
- Returns
True if we think this could be a stablecoin.
- Return type
- __init__(contract, name=None, symbol=None, total_supply=None, decimals=None, extra_data=<factory>)