Skip to main content

Fetching Data

This guide will illustrate how to fetch data from the SDK and details on instances where the SDK needs on-chain data and elobrates on strategies that you can use to fetch data.

On-chain Interactions#

Case 1: Fetching Token Information#

For the SDK to perform queries and actions, it needs data in the format it can work with. For example, for a token, it requires the chainId, address and its decimals.

  • the token address should be a manual input. Refer to this section for some popular token addresses
  • the SDK can then look up other token related information via Fetcher.fetchTokenData


import { ChainId, Token, Fetcher } from '@dynamic/sdk'
const chainId = ChainId.MAINNET
const tokenAddress = '0xdd974d5c2e2928dea5f71b9825b8b646686bd200' // must be checksummed
const KNC: Token = await Fetcher.fetchTokenData(chainId, tokenAddress)

Case 2: Pools#

The best way to get all pools for a specific token pair is to call getPools on the factory, then selecting one of the pools from the returned array. If none exists, an empty array is returned.

  • requires an on-chain lookup.
  • the returned array of multiple pools will be indexed by first-come-first-address. In other words, pools created earlier will have lower index values.

Additionally, you can call the function getUnamplifiedPool on the factory, which returns pool with amplification factor = 1.

As an example, let's try to get data for the USDC-DAI pair.

import { ChainId, Token, Fetcher } from '@dynamic-amm/sdk'
const DAI = new Token(ChainId.MAINNET, '0x6B175474E89094C44Da98b954EedeAC495271d0F', 18)
const USDC = new Token(ChainId.MAINNET, '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', 18)
const pool = await Fetcher.fetchPairData(DAI, WETH[DAI.chainId])

Note that these values can change as frequently as every block, and should be kept up-to-date.