Skip to main content
Version: V1

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 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';
// DMM Factory Address if using Ethereum Mainnet
const DMMFactoryAddress = '0x833e4083B7ae46CeA85695c4f7ed25CDAd8886dE';
const USDC = new Token(
ChainId.MAINNET,
'0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
18,
);
const pool = await Fetcher.fetchPairData(
USDC,
WETH[USDC.chainId],
DMMFactoryAddress,
);
const route = new Route([pool], WETH[USDC.chainId], DMMFactoryAddress);
console.log(route.midPrice.toSignificant(6)); // 201.306
console.log(route.midPrice.invert().toSignificant(6)); // 0.00496756
  • Price is formated 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';
// DMM Factory Address if using Ethereum Mainnet
const DMMFactoryAddress = '0x833e4083B7ae46CeA85695c4f7ed25CDAd8886dE';
const DAI = new Token(
ChainId.MAINNET,
'0x6B175474E89094C44Da98b954EedeAC495271d0F',
18,
);
const pool = await Fetcher.fetchPairData(
DAI,
WETH[DAI.chainId],
DMMFactoryAddress,
);
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));