get_token_price_with_chainlink
Documentation for eth_defi.chainlink.token_price.get_token_price_with_chainlink function.
- get_token_price_with_chainlink(web3, aggregator_address)[source]
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]