chainlink.token_price
Documentation for eth_defi.chainlink.token_price Python module.
Read token price using Chainlink.
See tutorials
Functions
Get the latest price of a native token on any chain in USD. |
|
|
Get the latest price of any token on a chain based on its Chainlink feed. |
- get_native_token_price_with_chainlink(web3)
Get the latest price of a native token on any chain in USD.
Example for ETH:
import os from eth_defi.chainlink.token_price import get_native_token_price_with_chainlink from eth_defi.provider.multi_provider import create_multi_provider_web3 json_rpc_url = os.environ["JSON_RPC_URL"] web3 = create_multi_provider_web3(json_rpc_url) token_name, last_round = get_native_token_price_with_chainlink(web3) price = last_round.price print(f"The chain native token price of is {price} {token_name} / USD")
- Returns
USD exchange rate of the chain native token.
Returned as native token symbol, latest ChainLink round data.
- Raises
NotImplementedError – Chainlink configuration not yet added for this chain.
- Parameters
web3 (web3.main.Web3) –
- Return type
Tuple[str, eth_defi.chainlink.round_data.ChainLinkLatestRoundData]
- get_token_price_with_chainlink(web3, aggregator_address)
Get the latest price of any token on a chain based on its Chainlink feed.
Example for BNB price on Ethereum mainnet:
import os from eth_defi.chainlink.token_price import get_token_price_with_chainlink from eth_defi.provider.multi_provider import create_multi_provider_web3 json_rpc_url = os.environ["JSON_RPC_URL"] web3 = create_multi_provider_web3(json_rpc_url) base_token_symbol, quote_token_symbol, last_round = get_token_price_with_chainlink(web3, "0x14e613AC84a31f709eadbdF89C6CC390fDc9540A") price = last_round.price print(f"The chain native token price of is {price} {base_token_symbol} / {quote_token_symbol}")
This will output:
The chain native token price of is 312.94308698 BNB / USD
- Parameters
aggregator_address (eth_typing.evm.HexAddress) – The Chainlink aggregator contract address
web3 (web3.main.Web3) –
- Returns
USD exchange rate of the chain native token.
Returned as tuple (base token symbol, quote token symbol, price)
- Return type
Tuple[str, str, eth_defi.chainlink.round_data.ChainLinkLatestRoundData]