Price Fetching

The R5 DeFi Engine SDK supports two types of pricing: the mid price and the execution price.

Mid Price

The Mid Price defines the relative value of one token relative to another token. It can be interpreted as the current market-clearing or fair value price of the asset in question.

Consider the mid price for TOKEN1-WR5 (that is, the amount of TOKEN1 per 1 unit of WR5).

import { 
    ChainId,
    Token,
    WR5,
    Route
     } from "r5-defi-engine"

const TOKEN1 = new Token(ChainId.R5, '0x123...', 18)

const pair = await createPair(TOKEN1, WR5[ChainId.R5])

const route = new Route([pair], WR5[TOKEN1.chainId], TOKEN1)

console.log(route.midPrice.toSignificant(4))
console.log(route.midPrice.invert().toSignificant(4))

Execution Price

The execution price takes into account the liquidity available to execute a trade. It will often be lower than the mid price of the asset when executing a SELL operation, and higher than the mid price of the asset when executing a BUY operation.

Consider trading 1 unit of WR5 for TOKEN1.

import { 
    ChainId,
    Token,
    WR5,
    CurrencyAmount,
    TradeType,
    Route,
    Pair,
    Trade 
     } from "r5-defi_engine"
     
const TOKEN1 = new Token(ChainId.R5, '0x123...', 18)

const route = new Route([pair], WR5[TOKEN1.chainId], TOKEN1)

const trade = new Trade(route, CurrencyAmount.fromRawAmount(WR5[TOKEN1.chainId], '1000000000000000000'), TradeType.EXACT_INPUT)

console.log(trade.executionPrice.toSignificant(4))

Last updated