Skip to content

Commit

Permalink
Merge pull request #34 from curvefi/multi-pool-router
Browse files Browse the repository at this point in the history
Multi-pool Router
  • Loading branch information
Macket authored Apr 6, 2022
2 parents c7039cb + 2f245a0 commit d35c769
Show file tree
Hide file tree
Showing 7 changed files with 591 additions and 5 deletions.
63 changes: 61 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,66 @@ import curve from "@curvefi/api";
})()
```
## Exchange using all pools
## Exchange
### Router exchange
```ts
import curve from "@curvefi/api";

(async () => {
await curve.init('JsonRpc', {}, { gasPrice: 0, chainId: 1 });

console.log(await curve.getBalances(['DAI', 'CRV']));
// [ '9900.0', '100049.744832225238317557' ]

const { route, output } = await curve.getBestRouteAndOutput('DAI', 'CRV', '1000');
// OR await curve.getBestPoolAndOutput('0x6B175474E89094C44Da98b954EedeAC495271d0F', '0xD533a949740bb3306d119CC777fa900bA034cd52', '10000');
const expected = await curve.routerExchangeExpected('DAI', 'CRV', '1000');
// OR await curve.exchangeExpected('0x6B175474E89094C44Da98b954EedeAC495271d0F', '0xD533a949740bb3306d119CC777fa900bA034cd52', '10000');

console.log(route, output, expected);
// route = [
// {
// poolId: '3pool',
// poolAddress: '0xbEbc44782C7dB0a1A60Cb6fe97d0b483032FF1C7',
// outputCoinAddress: '0xdac17f958d2ee523a2206206994597c13d831ec7',
// i: 0,
// j: 2,
// swapType: 1,
// swapAddress: '0x0000000000000000000000000000000000000000'
// },
// {
// poolId: 'tricrypto2',
// poolAddress: '0xD51a44d3FaE010294C616388b506AcdA1bfAAE46',
// outputCoinAddress: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
// i: 0,
// j: 2,
// swapType: 3,
// swapAddress: '0x0000000000000000000000000000000000000000'
// },
// {
// poolId: 'crveth',
// poolAddress: '0x8301AE4fc9c624d1D396cbDAa1ed877821D7C511',
// outputCoinAddress: '0xd533a949740bb3306d119cc777fa900ba034cd52',
// i: 0,
// j: 1,
// swapType: 3,
// swapAddress: '0x0000000000000000000000000000000000000000'
// }
// ]
//
// output = expected = 378.881631202862354937

await curve.routerExchange('DAI', 'CRV', '1000')
// OR await curve.exchange('0x6B175474E89094C44Da98b954EedeAC495271d0F', '0xD533a949740bb3306d119CC777fa900bA034cd52', '10000');

console.log(await curve.getBalances(['DAI', 'CRV']));
// [ '8900.0', '100428.626463428100672494' ]
})()
```
### Single-pool exchange
```ts
import curve from "@curvefi/api";
Expand All @@ -521,7 +580,7 @@ import curve from "@curvefi/api";
})()
```
## Cross-Asset Exchange
### Cross-Asset Exchange
```ts
import curve from "@curvefi/api";
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@curvefi/api",
"version": "1.22.0",
"version": "1.23.0",
"description": "JavaScript library for curve.fi",
"main": "lib/index.js",
"scripts": {
Expand Down
14 changes: 14 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ import {
crossAssetExchangeEstimateGas,
crossAssetExchange,
getUserPoolList,
getBestRouteAndOutput,
routerExchangeExpected,
routerExchangeIsApproved,
routerExchangeApproveEstimateGas,
routerExchangeApprove,
routerExchangeEstimateGas,
routerExchange,
} from "./pools";
import { curve as _curve } from "./curve";
import {
Expand Down Expand Up @@ -103,12 +110,19 @@ const curve = {
crossAssetExchangeApprove,
crossAssetExchange,
getUserPoolList,
getBestRouteAndOutput,
routerExchangeExpected,
routerExchangeIsApproved,
routerExchangeApprove,
routerExchange,
estimateGas: {
ensureAllowance: ensureAllowanceEstimateGas,
exchangeApprove: exchangeApproveEstimateGas,
exchange: exchangeEstimateGas,
crossAssetExchangeApprove: crossAssetExchangeApproveEstimateGas,
crossAssetExchange: crossAssetExchangeEstimateGas,
routerExchangeApprove: routerExchangeApproveEstimateGas,
routerExchange: routerExchangeEstimateGas,
},
boosting: {
getCrv,
Expand Down
19 changes: 18 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,27 @@ export interface ISinglePoolSwapData {
poolAddress: string,
i: number,
j: number,
swapType: 1 | 2 | 3 | 4,
swapType: 1 | 2 | 3 | 4 | 5,
swapAddress: string, // for swapType == 4
}

export interface ISinglePoolSwapDataAndOutput extends ISinglePoolSwapData {
_output: ethers.BigNumber,
}

export interface IRouteStep {
poolId: string,
poolAddress: string,
outputCoinAddress: string,
i: number,
j: number,
swapType: 1 | 2 | 3 | 4 | 5,
swapAddress: string, // for swapType == 4
}

export interface IRoute {
steps: IRouteStep[],
_output: ethers.BigNumber,
outputUsd: number,
txCostUsd: number,
}
Loading

0 comments on commit d35c769

Please sign in to comment.