Skip to main content

Fetch Prices

This guide will focus on the two most important DMM prices: the mid price and the execution price.

Mid Price#

The mid price, basically is the price that reflects the ratio of reserves in one or more pools. Think of it as the relative value of one token in terms of the other. It can be interpreted as the current market-clearing or fair value price of the assets.

The example below shows how one may fetch the mid price for USDC-WETH.

import { ChainId, Token, WETH, Fetcher, Route } from '@dynamic-amm/sdk'
const USDC = new Token(ChainId.MAINNET, '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', 18)
const pool = await Fetcher.fetchPoolData(USDC, WETH[USDC.chainId])
const route = new Route([pool], WETH[USDC.chainId])
console.log(route.midPrice.toSignificant(6)) // 201.306
console.log(route.midPrice.invert().toSignificant(6)) // 0.00496756
  • Price is formmated for better representation

Execution Price#

execution price of a trade represents the ratio of assets sent or received.

For example, we are trading 1 WETH for DAI:

import { ChainId, Token, WETH, Fetcher, Trade, Route, TokenAmount, TradeType } from '@dynamic-amm/sdk'
const DAI = new Token(ChainId.MAINNET, '0x6B175474E89094C44Da98b954EedeAC495271d0F', 18)
const pool = await Fetcher.fetchPoolData(DAI, WETH[DAI.chainId])
const route = new Route([pool], WETH[DAI.chainId])
const trade = new Trade(route, new TokenAmount(WETH[DAI.chainId], '1000000000000000000'), TradeType.EXACT_INPUT)
console.log(trade.executionPrice.toSignificant(6))
console.log(trade.nextMidPrice.toSignificant(6))