0.2.24 • Published 8 months ago

@scalingparrots/uniswap-smart-routing v0.2.24

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months ago

Uniswap Smart Routing for OneClick 🔁

This repository contains routing logic for integrate Uniswap V3 protocol for StableComp Contracts.

It searches for the most efficient way to swap token A for token B, considering splitting swaps across multiple routes, gas costs and format all the data needed to call the OneClickV3 contract.

Testing

First make sure you have run npm install.

npm run test

CLI

The package can be run as a CLI for testing purposes.

First create a .env file in the root of the project and configure:

RPC_URL = '<RPC_URL>'

Installation

npm i @scalingparrots/uniswap-smart-routing

Usage

Function

import {
  OneClickInV3,
  EstimateOneClickInV3,
  OneClickOutV3,
} from "@scalingparrots/uniswap-smart-routing";

Type

import {
  OneClickInSmartRouting,
  OneClickInContract,
  EstimateOneClickInContract,
  OneClickOutSmartRouting,
  OneClickOutContract,
} from "@scalingparrots/uniswap-smart-routing";

OneClickV3 Contract

OneClickInETH

    function OneClickInETH(
        address[][] memory _routev2,
        bytes[] memory _routev3,
        address _poolAddress,
        address _tokenAddress,
        address[] memory _poolTokens,
        address _vault,
        uint256 _crvSlippage,
        bool _oneToken,
        uint256 _indexIn
    ) external payable nonReentrant
NameType
_routev2addressThe routing of UniswapV2 for the swap
_routev3bytesThe routing of UniswapV3 for the swap
_poolAddressaddressCurve pool address
_tokenAddressaddressCurve token address
_poolTokensaddressCurve tokens in the pool
_vaultaddressStablecompVault address
_crvSlippageuint256Slippage for Curve deposit
_oneTokenboolInvest in all tokens or in a one of specified index. False = all tokens, True = one token
_indexInuint256Index of the specified to invest

OneClickIn

    function OneClickIn(
        address[][] memory _routev2,
        bytes[] memory _routev3,
        address _tokenIn,
        address _poolAddress,
        address _tokenAddress,
        address[] memory _poolTokens,
        address _vault,
        uint256 _amountIn,
        uint256 _crvSlippage,
        bool _oneToken,
        uint256 _indexIn
    ) external nonReentrant
NameType
_routev2addressThe routing of UniswapV2 for the swap
_routev3bytesThe routing of UniswapV3 for the swap
_tokenInaddressThe token in to deposit
_poolAddressaddressCurve pool address
_tokenAddressaddressCurve token address
_poolTokensaddressCurve tokens in the pool
_vaultaddressStablecompVault address
_amountInuint256Amount of token in to swap or deposit
_crvSlippageuint256Slippage for Curve deposit
_oneTokenboolInvest in all tokens or in a one of specified index. False = all tokens, True = one token
_indexInuint256Index of the specified to invest

OneClickOutETH

    function OneClickOutETH(
        address[][] memory _routev2,
        bytes[] memory _routev3,
        address _poolAddress,
        address _tokenAddress,
        address[] memory _poolTokens,
        address _vault,
        uint256 _amountOut
    ) external nonReentrant
NameType
_routev2addressThe routing of UniswapV2 for the swap
_routev3bytesThe routing of UniswapV3 for the swap
_poolAddressaddressCurve pool address
_tokenAddressaddressCurve token address
_poolTokensaddressCurve tokens in the pool
_vaultaddressStablecompVault address
_amountOutuint256Amount of token to remove

OneClickOut

    function OneClickOut(
        address[][] memory _routev2,
        bytes[] memory _routev3,
        address _poolAddress,
        address _tokenAddress,
        address[] memory _poolTokens,
        address _vault,
        address _tokenOut,
        uint256 _amountOut
    ) external nonReentrant
NameType
_routev2addressThe routing of UniswapV2 for the swap
_routev3bytesThe routing of UniswapV3 for the swap
_poolAddressaddressCurve pool address
_tokenAddressaddressCurve token address
_poolTokensaddressCurve tokens in the pool
_vaultaddressStablecompVault address
_tokenOutaddressToken we want to get out
_amountOutuint256Amount of token to remove

estimateOneClickIn

    function estimateOneClickIn(
        address _tokenIn,
        address _poolAddress,
        address[] memory _poolTokens,
        uint256[] memory _priceTokensIn,
        address _vault,
        uint256 _amountIn,
        uint256 _crvSlippage,
        bool _oneToken,
        uint256 _indexIn
    ) external view returns (uint256 amountTokenOut)
NameType
_tokenInaddressThe token in to deposit
_poolAddressaddressCurve pool address
_poolTokensaddressCurve token address
_priceTokensInuint256Curve tokens price vs tokenIn
_vaultaddressStablecompVault address
_amountInuint256Amount of token to deposit
_crvSlippageuint256Slippage for Curve deposit
_oneTokenboolInvest in all tokens or in a one of specified index. False = all tokens, True = one token
_indexInuint256Index of the specified to invest

estimateOneClickOut

    function estimateOneClickOut(
        address _poolAddress,
        address _tokenAddress,
        address[] memory _poolTokens,
        address _vault,
        address _tokenOut,
        uint256[] memory _priceTokens,
        uint256 _amountOut
    ) external view returns (uint256 amountTokenOut)
NameType
_poolAddressaddressCurve pool address
_tokenAddressaddressCurve token address
_poolTokensaddressCurve tokens in the pool
_vaultaddressStablecompVault address
_tokenOutaddressAddress of the token to withdraw
_priceTokensuint256Prices of the tokens in the Curve pool
_amountOutuint256Amount of share to withdraw

Uniswap Smart Routing for OneClick 🔁

Function

import {
  OneClickInV3,
  EstimateOneClickInV3,
  OneClickOutV3,
} from "@scalingparrots/uniswap-smart-routing";

Type

import type {
  OneClickInSmartRouting,
  OneClickInContract,
  EstimateOneClickInContract,
  OneClickOutSmartRouting,
  OneClickOutContract,
} from "@scalingparrots/uniswap-smart-routing";

OneClickInV3()

OneClickInV3 returns data formatted to call the OneClickIn & OneClickInETH function.

Input

interface OneClickInSmartRouting {
  chainId: ChainId;
  provider: JsonRpcProvider;
  tokenIn: string;
  amountIn: string;
  vault: string;
  crvSlippage: number;
  oneToken: boolean;
  indexIn: number;
}
NameType
chainIdChainIdThe Chain Id of the Blockchain used
providerJsonRpcProviderRPC providers
tokenInstringThe address of the tokenIn
amountInstringThe amount of tokenIn you want to deposit
vaultstringStablecompVault address
crvSlippagenumberSlippage for Curve deposit
oneTokenbooleanInvest in all tokens or in a one of specified index. False = all tokens, True = one token
indexInnumberIndex of the specified to invest.

All amounts and slippages must be input in decimal format, they will be converted to BigNumber by the library.

Output

interface OneClickInContract {
  routev2: string[][];
  routev3: string[];
  tokenIn: string;
  poolAddress: string;
  tokenAddress: string;
  poolTokens: string[];
  vault: string;
  amountIn: string;
  crvSlippage: number;
  oneToken: boolean;
  indexIn: number;
}
NameType
routeV2stringThe routing of UniswapV2 for the swap
routeV3string[]The bytes to routing in UniswapV3
tokenInstringThe address of the tokenIn
poolAddressstringCurve pool address
tokenAddressstringCurve token address
poolTokensstring[]Curve tokens in the pool
vaultstringStablecompVault address
amountInstringAmount of token in to swap or deposit
crvSlippagenumberSlippage for Curve deposit
oneTokenbooleanInvest in all tokens or in a one of specified index.
indexInnumberIndex of the specified to invest.

EstimateOneClickInV3()

estimateOneClickInV3 returns data formatted to call the estimateOneClickIn function.

Input

interface OneClickInSmartRouting {
  chainId: ChainId;
  provider: JsonRpcProvider;
  tokenIn: string;
  amountIn: string;
  vault: string;
  crvSlippage: number;
  oneToken: boolean;
  indexIn: number;
}
NameType
chainIdChainIdThe Chain Id of the Blockchain used
providerJsonRpcProviderRPC providers
tokenInstringThe address of the tokenIn
amountInstringThe amount of tokenIn you want to deposit
vaultstringStablecompVault address
crvSlippagenumberSlippage for Curve deposit
oneTokenbooleanInvest in all tokens or in a one of specified index. False = all tokens, True = one token
indexInnumberIndex of the specified to invest.

All amounts and slippages must be input in decimal format, they will be converted to BigNumber by the library.

Output

interface EstimateOneClickInContract {
  tokenIn: string;
  poolAddress: string;
  poolTokens: string[];
  priceTokenIn: string[];
  vault: string;
  amountIn: string;
  crvSlippage: number;
  oneToken: boolean;
  indexIn: number;
}
NameType
tokenInstringAddress of token in to deposit
poolAddressstringCurve pool address
poolTokensstring[]Curve tokens in the pool
priceTokenInstring[]Curve tokens price vs tokenIn
vaultstringStablecompVault address
amountInstringAmount of token in to swap or deposit
crvSlippagenumberSlippage for Curve deposit
oneTokenbooleanInvest in all tokens or in a one of specified index.
indexInnumberIndex of the specified to invest.

OneClickOutV3()

OneClickOutV3 returns data formatted to call the OneClickOut & OneClickOutETH function.

Input

interface OneClickOutSmartRouting {
  chainId: ChainId;
  provider: JsonRpcProvider;
  tokenOut: string;
  amountOut: string;
  vault: string;
}
NameType
chainIdChainIdThe Chain Id of the Blockchain used
providerJsonRpcProviderRPC providers
tokenOutstringAddress of token we want to get out
amountOutstringAmount of token to remove
vaultstringStablecompVault address

All amounts and slippages must be input in decimal format, they will be converted to BigNumber by the library.

Output

interface OneClickOutContract {
  routev2: string[][];
  routev3: string[];
  poolAddress: string;
  tokenAddress: string;
  poolTokens: string[];
  vault: string;
  tokenOut: string;
  amountOut: string;
}
NameType
routeV2stringThe routing of UniswapV2 for the swap
routeV3string[]The bytes to routing in UniswapV3
poolAddressstringCurve pool address
tokenAddressstringCurve token address
poolTokensstring[]Curve tokens in the pool
vaultstringStablecompVault address
tokenOutstringAddress of token we want to get out
amountOutnumberAmount of token to remove

EstimateOneClickOut()

estimateOneClickInV3 returns data formatted to call the estimateOneClickOut function.

Input

interface OneClickOutSmartRouting {
  chainId: ChainId;
  provider: JsonRpcProvider;
  tokenOut: string;
  amountOut: string;
  vault: string;
}
NameType
chainIdChainIdThe Chain Id of the Blockchain used
providerJsonRpcProviderRPC providers
tokenOutstringAddress of token we want to get out
amountOutstringAmount of token to remove
vaultstringStablecompVault address

All amounts and slippages must be input in decimal format, they will be converted to BigNumber by the library.

Output

interface EstimateOneClickOutContract {
  poolAddress: string;
  tokenAddress: string;
  poolTokens: string[];
  vault: string;
  tokenOut: string;
  priceToken: string[];
  amountOut: string;
}
NameType
poolAddressstringCurve pool address
tokenAddressstringCurve token address
poolTokensstring[]Curve tokens in the pool
vaultstringStablecompVault address
tokenOutstringAddress of token we want to get out
priceTokenstring[]Prices of the tokens in the Curve pool
amountOutnumberAmount of token to remove

Exemple

const vault = "0x3f0d8746d07e7b60974Bbb1F275CD61B071d69D5";
const chainId = 1;
const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_URL);
const token = "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599"; //WBTC
const amount = "0.1";
const amountOut = "2500";
const crvSlippage = 0.1;
const oneToken = false;
const indexIn = 0;

const estimateOneClickIn = await estimateOneClickInV3({
  chainId,
  provider,
  token,
  amount,
  vault,
  crvSlippage,
  oneToken,
  indexIn,
});

const oneClickInV3 = await OneClickInV3({
  chainId,
  provider,
  token,
  amount,
  vault,
  crvSlippage,
  oneToken,
  indexIn,
});

const oneClickOutV3 = await OneClickOutV3({
  chainId,
  provider,
  token,
  amountOut,
  vault,
});

const estimateOneClickOut = await EstimateOneClickOut({
  chainId,
  provider,
  tokenOut,
  amountOut,
  vault,
});

Return

      estimateOneClickIn: {
        "tokenIn": "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599",
        "poolAddress": "0xDcEF968d416a41Cdac0ED8702fAC8128A64241A2",
        "poolTokens": [
          "0x853d955aCEf822Db058eb8505911ED77F175b99e",
          "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
        ],
        "priceTokenIn": [
          "1175306013499988321881",
          "1174932005"
        ],
        "vault": "0x3f0d8746d07e7b60974Bbb1F275CD61B071d69D5",
        "amountIn": "10000000",
        "crvSlippage": 10,
        "oneToken": false,
        "indexIn": 0
      }

      oneClickInV3: {
        "routev2": [],
        "routev3": [
          "0x2260fac5e5542a773aa44fbcfedf7c193bc2c5990001f4a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000064853d955acef822db058eb8505911ed77f175b99e",
          "0x2260fac5e5542a773aa44fbcfedf7c193bc2c5990001f4a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
        ],
        "tokenIn": "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599",
        "poolAddress": "0xDcEF968d416a41Cdac0ED8702fAC8128A64241A2",
        "tokenAddress": "0x3175Df0976dFA876431C2E9eE6Bc45b65d3473CC",
        "poolTokens": [
          "0x853d955aCEf822Db058eb8505911ED77F175b99e",
          "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
        ],
        "vault": "0x3f0d8746d07e7b60974Bbb1F275CD61B071d69D5",
        "amountIn": "10000000",
        "crvSlippage": 10,
        "oneToken": false,
        "indexIn": 0
      }

      estimateOneClickOut: {
        "poolAddress": "0xDcEF968d416a41Cdac0ED8702fAC8128A64241A2",
        "tokenAddress": "0x3175Df0976dFA876431C2E9eE6Bc45b65d3473CC",
        "poolTokens": [
          "0x853d955aCEf822Db058eb8505911ED77F175b99e",
          "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
        ],
        "vault": "0x3f0d8746d07e7b60974Bbb1F275CD61B071d69D5",
        "tokenOut": "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599",
        "priceToken": [
          "5338154",
          "5341338"
        ],
        "amountOut": "2500000000000000000000"
      }

      oneClickOutV3: {
        "routev2": [],
        "routev3": [
          "0x853d955acef822db058eb8505911ed77f175b99e000064a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480001f42260fac5e5542a773aa44fbcfedf7c193bc2c599",
          "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb480001f42260fac5e5542a773aa44fbcfedf7c193bc2c599"
        ],
        "poolAddress": "0xDcEF968d416a41Cdac0ED8702fAC8128A64241A2",
        "tokenAddress": "0x3175Df0976dFA876431C2E9eE6Bc45b65d3473CC",
        "poolTokens": [
          "0x853d955aCEf822Db058eb8505911ED77F175b99e",
          "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
        ],
        "vault": "0x3f0d8746d07e7b60974Bbb1F275CD61B071d69D5",
        "tokenOut": "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599",
        "amountOut": "2500000000000000000000"
      }
0.2.24

8 months ago

0.2.23

8 months ago

0.2.21

10 months ago

0.2.20

10 months ago

0.2.19

1 year ago

0.2.18

1 year ago

0.2.17

1 year ago

0.2.16

1 year ago

0.2.15

1 year ago

0.2.14

1 year ago

0.2.13

1 year ago

0.2.12

1 year ago

0.2.11

1 year ago

0.2.10

1 year ago

0.2.7

1 year ago

0.2.6

1 year ago

0.2.9

1 year ago

0.2.8

1 year ago

0.2.3

1 year ago

0.2.2

1 year ago

0.2.5

1 year ago

0.2.4

1 year ago

0.1.10

1 year ago

0.1.11

1 year ago

0.1.12

1 year ago

0.1.0

1 year ago

0.2.1

1 year ago

0.1.2

1 year ago

0.0.3

1 year ago

0.2.0

1 year ago

0.1.1

1 year ago

0.0.2

1 year ago

0.1.8

1 year ago

0.1.7

1 year ago

0.1.9

1 year ago

0.1.4

1 year ago

0.0.5

1 year ago

0.1.3

1 year ago

0.0.4

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.0.1

1 year ago