Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@many-things/osmosis-router

Package Overview
Dependencies
Maintainers
4
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@many-things/osmosis-router

Minimal SDK for Osmosis Swap Estimation

  • 1.1.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
4
Created
Source

satellite-antenna
@many-things/osmosis-router

Minimal SDK for Osmosis Swap Estimation
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;
};

FAQs

Package last updated on 20 Dec 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc