@scalingparrots/uniswap-smart-routing
v0.2.24
Published
Integrate Uniswap V3 protocol for StableComp Contracts
Downloads
99
Keywords
Readme
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
| Name | Type | | | -------------- | ------- | ----------------------------------------------------------------------------------------- | | _routev2 | address | The routing of UniswapV2 for the swap | | _routev3 | bytes | The routing of UniswapV3 for the swap | | _poolAddress | address | Curve pool address | | _tokenAddress | address | Curve token address | | _poolTokens | address | Curve tokens in the pool | | _vault | address | StablecompVault address | | _crvSlippage | uint256 | Slippage for Curve deposit | | _oneToken | bool | Invest in all tokens or in a one of specified index. False = all tokens, True = one token | | _indexIn | uint256 | Index 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
| Name | Type | | | -------------- | ------- | ----------------------------------------------------------------------------------------- | | _routev2 | address | The routing of UniswapV2 for the swap | | _routev3 | bytes | The routing of UniswapV3 for the swap | | _tokenIn | address | The token in to deposit | | _poolAddress | address | Curve pool address | | _tokenAddress | address | Curve token address | | _poolTokens | address | Curve tokens in the pool | | _vault | address | StablecompVault address | | _amountIn | uint256 | Amount of token in to swap or deposit | | _crvSlippage | uint256 | Slippage for Curve deposit | | _oneToken | bool | Invest in all tokens or in a one of specified index. False = all tokens, True = one token | | _indexIn | uint256 | Index 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
| Name | Type | | | -------------- | ------- | ------------------------------------- | | _routev2 | address | The routing of UniswapV2 for the swap | | _routev3 | bytes | The routing of UniswapV3 for the swap | | _poolAddress | address | Curve pool address | | _tokenAddress | address | Curve token address | | _poolTokens | address | Curve tokens in the pool | | _vault | address | StablecompVault address | | _amountOut | uint256 | Amount 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
| Name | Type | | | -------------- | ------- | ------------------------------------- | | _routev2 | address | The routing of UniswapV2 for the swap | | _routev3 | bytes | The routing of UniswapV3 for the swap | | _poolAddress | address | Curve pool address | | _tokenAddress | address | Curve token address | | _poolTokens | address | Curve tokens in the pool | | _vault | address | StablecompVault address | | _tokenOut | address | Token we want to get out | | _amountOut | uint256 | Amount 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)
| Name | Type | | | --------------- | ------- | ----------------------------------------------------------------------------------------- | | _tokenIn | address | The token in to deposit | | _poolAddress | address | Curve pool address | | _poolTokens | address | Curve token address | | _priceTokensIn | uint256 | Curve tokens price vs tokenIn | | _vault | address | StablecompVault address | | _amountIn | uint256 | Amount of token to deposit | | _crvSlippage | uint256 | Slippage for Curve deposit | | _oneToken | bool | Invest in all tokens or in a one of specified index. False = all tokens, True = one token | | _indexIn | uint256 | Index 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)
| Name | Type | | | -------------- | ------- | -------------------------------------- | | _poolAddress | address | Curve pool address | | _tokenAddress | address | Curve token address | | _poolTokens | address | Curve tokens in the pool | | _vault | address | StablecompVault address | | _tokenOut | address | Address of the token to withdraw | | _priceTokens | uint256 | Prices of the tokens in the Curve pool | | _amountOut | uint256 | Amount 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;
}
| Name | Type | | | ----------- | --------------- | ----------------------------------------------------------------------------------------- | | chainId | ChainId | The Chain Id of the Blockchain used | | provider | JsonRpcProvider | RPC providers | | tokenIn | string | The address of the tokenIn | | amountIn | string | The amount of tokenIn you want to deposit | | vault | string | StablecompVault address | | crvSlippage | number | Slippage for Curve deposit | | oneToken | boolean | Invest in all tokens or in a one of specified index. False = all tokens, True = one token | | indexIn | number | Index 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;
}
| Name | Type | | | ------------ | ---------- | ---------------------------------------------------- | | routeV2 | string[][] | The routing of UniswapV2 for the swap | | routeV3 | string[] | The bytes to routing in UniswapV3 | | tokenIn | string | The address of the tokenIn | | poolAddress | string | Curve pool address | | tokenAddress | string | Curve token address | | poolTokens | string[] | Curve tokens in the pool | | vault | string | StablecompVault address | | amountIn | string | Amount of token in to swap or deposit | | crvSlippage | number | Slippage for Curve deposit | | oneToken | boolean | Invest in all tokens or in a one of specified index. | | indexIn | number | Index 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;
}
| Name | Type | | | ----------- | --------------- | ----------------------------------------------------------------------------------------- | | chainId | ChainId | The Chain Id of the Blockchain used | | provider | JsonRpcProvider | RPC providers | | tokenIn | string | The address of the tokenIn | | amountIn | string | The amount of tokenIn you want to deposit | | vault | string | StablecompVault address | | crvSlippage | number | Slippage for Curve deposit | | oneToken | boolean | Invest in all tokens or in a one of specified index. False = all tokens, True = one token | | indexIn | number | Index 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;
}
| Name | Type | | | ------------ | -------- | ---------------------------------------------------- | | tokenIn | string | Address of token in to deposit | | poolAddress | string | Curve pool address | | poolTokens | string[] | Curve tokens in the pool | | priceTokenIn | string[] | Curve tokens price vs tokenIn | | vault | string | StablecompVault address | | amountIn | string | Amount of token in to swap or deposit | | crvSlippage | number | Slippage for Curve deposit | | oneToken | boolean | Invest in all tokens or in a one of specified index. | | indexIn | number | Index 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;
}
| Name | Type | | | --------- | --------------- | ----------------------------------- | | chainId | ChainId | The Chain Id of the Blockchain used | | provider | JsonRpcProvider | RPC providers | | tokenOut | string | Address of token we want to get out | | amountOut | string | Amount of token to remove | | vault | string | StablecompVault 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;
}
| Name | Type | | | ------------ | ---------- | ------------------------------------- | | routeV2 | string[][] | The routing of UniswapV2 for the swap | | routeV3 | string[] | The bytes to routing in UniswapV3 | | poolAddress | string | Curve pool address | | tokenAddress | string | Curve token address | | poolTokens | string[] | Curve tokens in the pool | | vault | string | StablecompVault address | | tokenOut | string | Address of token we want to get out | | amountOut | number | Amount 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;
}
| Name | Type | | | --------- | --------------- | ----------------------------------- | | chainId | ChainId | The Chain Id of the Blockchain used | | provider | JsonRpcProvider | RPC providers | | tokenOut | string | Address of token we want to get out | | amountOut | string | Amount of token to remove | | vault | string | StablecompVault 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;
}
| Name | Type | | | ------------ | -------- | -------------------------------------- | | poolAddress | string | Curve pool address | | tokenAddress | string | Curve token address | | poolTokens | string[] | Curve tokens in the pool | | vault | string | StablecompVault address | | tokenOut | string | Address of token we want to get out | | priceToken | string[] | Prices of the tokens in the Curve pool | | amountOut | number | Amount 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"
}