# @scalingparrots/uniswap-smart-routing

> Integrate Uniswap V3 protocol for StableComp Contracts

Latest version **0.2.24** (published 2023-08-21) · ISC license · 0 weekly downloads

## Install

```sh
npm install @scalingparrots/uniswap-smart-routing
pnpm add @scalingparrots/uniswap-smart-routing
yarn add @scalingparrots/uniswap-smart-routing
bun add @scalingparrots/uniswap-smart-routing
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.24 |
| Published | 2023-08-21 |
| First published | 2023-01-27 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 331.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Matteo Celani |
| Maintainers | dprimasso, matteocelani, samuele.ferri, soumayaerradi, gianlucamazza |

## Links

- npm: https://www.npmjs.com/package/@scalingparrots/uniswap-smart-routing
- Repository: https://github.com/scalingparrots/uniswap-smart-routing
- Homepage: https://github.com/scalingparrots/uniswap-smart-routing#readme
- Issues: https://github.com/scalingparrots/uniswap-smart-routing/issues
- npm.io page: https://npm.io/package/@scalingparrots/uniswap-smart-routing

## Dependencies (5)

- [dotenv](https://npm.io/package/dotenv.md) ^16.0.3
- [ethers](https://npm.io/package/ethers.md) ^5.7.2
- [@uniswap/v3-sdk](https://npm.io/package/@uniswap/v3-sdk.md) ^3.9.0
- [@uniswap/sdk-core](https://npm.io/package/@uniswap/sdk-core.md) ^3.1.1
- [@uniswap/smart-order-router](https://npm.io/package/@uniswap/smart-order-router.md) ^3.5.0

## Recent versions

- 0.2.24 (latest) — 2023-08-21
- 0.2.23 — 2023-08-21
- 0.2.21 — 2023-07-03
- 0.2.20 — 2023-07-03
- 0.2.19 — 2023-04-18
- 0.2.18 — 2023-04-18
- 0.2.17 — 2023-04-18
- 0.2.16 — 2023-04-17
- 0.2.15 — 2023-04-17
- 0.2.14 — 2023-04-17
- 0.2.13 — 2023-04-17
- 0.2.12 — 2023-04-17
- 0.2.11 — 2023-04-17
- 0.2.10 — 2023-04-17
- 0.2.9 — 2023-04-17
- … 27 more at https://npm.io/package/@scalingparrots/uniswap-smart-routing/versions

## 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`.

```sh
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:

```env
RPC_URL = '<RPC_URL>'
```

## Installation

```sh
npm i @scalingparrots/uniswap-smart-routing
```

## Usage

Function

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

Type

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

## OneClickV3 Contract

### OneClickInETH

```solidity
    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

```solidity
    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

```solidity
    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

```solidity
    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

```solidity
    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

```solidity
    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

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

Type

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

### OneClickInV3()

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

#### Input

```typescript
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

```typescript
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

```typescript
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

```typescript
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

```typescript
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

```typescript
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

```typescript
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

```typescript
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

```typescript
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"
      }
```

---
_Source: https://npm.io/package/@scalingparrots/uniswap-smart-routing · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
