1.1.8 • Published 1 year ago

@many-things/osmosis-router v1.1.8

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago
yarn add @many-things/osmosis-router
# or use NPM
npm install @many-things/osmosis-router

✅ You can...

  • Estimate Swap
  • Fetch a complete list of LP Pools
  • Fetch routes using in/out tokens
  • Calculate spot price(using USDC(axlUSDC) as tokenOutCurrency)

🚀 Usage

First, Define Currencies for your in/out tokens.

import { Currency } from '@keplr-wallet/types';

const tokenInCurrency: Currency = {
  coinDenom: 'OSMO',
  coinMinimalDenom: 'uosmo',
  coinDecimals: 6,
};
const tokenOutCurrency: Currency = {
  coinDenom: 'USDC',
  coinMinimalDenom:
    'ibc/D189335C6E4A68B513C10AB227BF1C1D38C746766278BA3EEB4FB14124F1D858',
  coinDecimals: 6,
};

And just call estimateSwap with the params.

Works like magic!

import { estimateSwap } from '@many-things/osmosis-router';

// 1 OSMO = 0.899660 USDC
const amount: string = (1 * 10 ** 6).toString();
estimateSwap(tokenInCurrency, tokenOutCurrency, amount); // CoinPretty (0.899660 USDC)

Note

estimateSwap does the following:

  1. Update pools in Osmosis
  2. Get Routes (in -> out)
  3. Estimate swap using resolved route

The following is the current code for estimateSwap. You can use build your own custom implementation for efficiency, if you're estimating multiple times(e.g. using pools that are already fetched/cached).

import { type Pool } from '@many-things/osmosis-router';
import Axios, { AxiosInstance } from 'axios';

const defaultInstance = Axios.create({
  baseURL: OSMOSIS_CHAIN_REST,
});

export const estimateSwap = async (
  tokenInCurrency: Currency,
  tokenOutCurrency: Currency,
  amount: string,
  pools?: Pool[],
  instance: AxiosInstance = defaultInstance,
): Promise<CoinPretty> => {
  const { getOsmosisPools, getOsmosisRoutes, getOsmosisSwapEstimation } =
    await import('@many-things/osmosis-router');
  if (!pools) {
    pools = await getOsmosisPools({
      instance,
    }).catch((e) => {
      console.log(e);
      return [];
    });
  }

  const routes = getOsmosisRoutes({
    tokenInCurrency,
    tokenOutCurrency,
    amount,
    pools,
  });

  const tokenOut = getOsmosisSwapEstimation({
    tokenInCurrency,
    tokenOutCurrency,
    amount,
    pools,
    routes,
  });
  return tokenOut;
};