Get the Spot Price of a Pool

Overview

This guide will walk through various examples of fetching pricing from a pool using the SDK or direct calls.

Definitions

Spot price is used as a broader term and it's important to distinguish between the types of pricing data a pool can provide.

Mid Price - the direct price of one asset in terms of the other asset assuming zero fees. The price of the other is the direct inverse

Spot Price - the price of one asset in terms of the other assuming an infinitesimally small amount is traded. In other words, the mid price plus or minus the swap fee depending on which direction the trade occurs

Execution Price - The resulting average price (i.e. ratio of assets sent to received) for executing a swap of a given amount

Setup

All examples using the SDK will require installing the sdk:

And then initializing the SDK:

import { BalancerSDK, BalancerSdkConfig, Network } from '@balancer-labs/sdk';

const config: BalancerSdkConfig = {
  network: Network.MAINNET,
  rpcUrl: `https://mainnet.infura.io/v3/${process.env.INFURA}`,
};
const balancer = new BalancerSDK(config);

Examples

Choosing how to get a spot price

There are few different ways to go about getting the spot price. For example, if you already know a specific pool and want the spot price of the two assets you can call the calcSpotPrice() method on the pool instance shown below. If you just want to know the spot price of a pair across all of Balancer then using the pricing module will first find the most liquid path for the pair and then return the spot price. That is shown in this example.

Weighted Pool Spot Price Using the SDK

// 80/20 BAL/WETH pool
const poolId =
  '0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014';
const pool = await balancer.pools.find(poolId);
if (!pool) throw new BalancerError(BalancerErrorCode.POOL_DOESNT_EXIST);
const spotPrice = await pool.calcSpotPrice(
    '0xba100000625a3754423978a60c9317c58a424e3D', // BAL
    '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2' // WETH
  );

  return spotPrice;
}