This function fetches Uniswap reserve data for a given token address on a given network.
ethers.getDefaultProvider
, else it is the one specified by the passed provider.export async function getTokenReserves(tokenAddress: string,chainIdOrProvider: ChainIdOrProvider = 1): Promise<TokenReservesNormalized>
Parameter | Type | Description |
---|---|---|
tokenAddress | string | The checksummed address of a token with a Uniswap exchange. |
chainIdOrProvider | ChainIdOrProvider | A supported chain id (1 , 3 , 4 , or 42 ), or an underlying web3 provider connected to a chain with a supported chain id. |
const tokenAddress = '0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359' // DAI Mainnetconst chainIdOrProvider: ChainIdOrProvider = 1 // could be e.g. window.ethereum insteadconst tokenReserves: TokenReservesNormalized = await getTokenReserves(tokenAddress, chainIdOrProvider)/*{// details for the passed tokentoken: {chainId: 1,address: '0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359',decimals: 18},// details for the Uniswap exchange of the passed tokenexchange: {chainId: 1,address: '0x09cabEC1eAd1c0Ba254B09efb3EE13841712bE14',decimals: 18},// details for the ETH portion of the reserves of the passed tokenethReserve: {token: {chainId: 1,address: 'ETH',decimals: 18},amount: <BigNumber>},// details for the token portion of the reserves of the passed tokentokenReserve: {token: {chainId: 1,address: '0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359',decimals: 18},amount: <BigNumber>}}*/