@gearbox-protocol/sdk 3.0.0-next.235 → 3.0.0-next.236
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/core/creditAccount.d.ts +5 -5
- package/lib/core/creditAccount.js +13 -11
- package/lib/core/creditManager.d.ts +0 -12
- package/lib/core/creditManager.js +0 -114
- package/lib/core/pool.d.ts +0 -6
- package/lib/core/pool.js +0 -11
- package/lib/core/rewardConvex.js +1 -1
- package/lib/gearboxRewards/api.d.ts +8 -2
- package/lib/gearboxRewards/api.js +31 -15
- package/lib/index.d.ts +0 -2
- package/lib/index.js +0 -2
- package/lib/pathfinder/core.d.ts +0 -14
- package/lib/pathfinder/core.js +0 -7
- package/lib/pathfinder/index.d.ts +0 -3
- package/lib/pathfinder/index.js +0 -3
- package/lib/payload/creditAccount.d.ts +4 -1
- package/lib/payload/pool.d.ts +0 -6
- package/package.json +1 -1
- package/lib/core/trade.d.ts +0 -38
- package/lib/core/trade.js +0 -110
- package/lib/parsers/ERC20Parser.d.ts +0 -8
- package/lib/parsers/ERC20Parser.js +0 -36
- package/lib/parsers/aaveV2LendingPoolAdapterParser.d.ts +0 -8
- package/lib/parsers/aaveV2LendingPoolAdapterParser.js +0 -21
- package/lib/parsers/aaveV2WrappedATokenAdapterParser.d.ts +0 -8
- package/lib/parsers/aaveV2WrappedATokenAdapterParser.js +0 -21
- package/lib/parsers/abstractParser.d.ts +0 -30
- package/lib/parsers/abstractParser.js +0 -61
- package/lib/parsers/balancerV2VaultParser.d.ts +0 -8
- package/lib/parsers/balancerV2VaultParser.js +0 -32
- package/lib/parsers/compoundV2CTokenAdapterParser.d.ts +0 -8
- package/lib/parsers/compoundV2CTokenAdapterParser.js +0 -21
- package/lib/parsers/convexBaseRewardPoolAdapterParser.d.ts +0 -10
- package/lib/parsers/convexBaseRewardPoolAdapterParser.js +0 -50
- package/lib/parsers/convexBoosterAdapterParser.d.ts +0 -10
- package/lib/parsers/convexBoosterAdapterParser.js +0 -44
- package/lib/parsers/convextRewardPoolParser.d.ts +0 -8
- package/lib/parsers/convextRewardPoolParser.js +0 -21
- package/lib/parsers/creditFacadeParser.d.ts +0 -11
- package/lib/parsers/creditFacadeParser.js +0 -84
- package/lib/parsers/creditManagerParser.d.ts +0 -7
- package/lib/parsers/creditManagerParser.js +0 -22
- package/lib/parsers/curveAdapterParser.d.ts +0 -13
- package/lib/parsers/curveAdapterParser.js +0 -124
- package/lib/parsers/erc626AdapterParser.d.ts +0 -8
- package/lib/parsers/erc626AdapterParser.js +0 -21
- package/lib/parsers/iParser.d.ts +0 -6
- package/lib/parsers/iParser.js +0 -2
- package/lib/parsers/lidoAdapterParser.d.ts +0 -8
- package/lib/parsers/lidoAdapterParser.js +0 -29
- package/lib/parsers/lidoSTETHParser.d.ts +0 -8
- package/lib/parsers/lidoSTETHParser.js +0 -36
- package/lib/parsers/priceOracleParser.d.ts +0 -7
- package/lib/parsers/priceOracleParser.js +0 -23
- package/lib/parsers/txParser.d.ts +0 -37
- package/lib/parsers/txParser.js +0 -170
- package/lib/parsers/uniV2AdapterParser.d.ts +0 -8
- package/lib/parsers/uniV2AdapterParser.js +0 -51
- package/lib/parsers/uniV2AdapterParser.spec.d.ts +0 -1
- package/lib/parsers/uniV2AdapterParser.spec.js +0 -59
- package/lib/parsers/uniV3AdapterParser.d.ts +0 -10
- package/lib/parsers/uniV3AdapterParser.js +0 -101
- package/lib/parsers/uniV3AdapterParser.spec.d.ts +0 -1
- package/lib/parsers/uniV3AdapterParser.spec.js +0 -96
- package/lib/parsers/wstETHAdapterParser.d.ts +0 -8
- package/lib/parsers/wstETHAdapterParser.js +0 -50
- package/lib/parsers/wstETHAdapterParser.spec.d.ts +0 -1
- package/lib/parsers/wstETHAdapterParser.spec.js +0 -37
- package/lib/parsers/yearnAdapterParser.spec.d.ts +0 -1
- package/lib/parsers/yearnAdapterParser.spec.js +0 -54
- package/lib/parsers/yearnV2AdapterParser.d.ts +0 -8
- package/lib/parsers/yearnV2AdapterParser.js +0 -57
- package/lib/pathfinder/balancerVault.d.ts +0 -33
- package/lib/pathfinder/balancerVault.js +0 -59
- package/lib/pathfinder/pathOptions.d.ts +0 -16
- package/lib/pathfinder/pathOptions.js +0 -93
- package/lib/pathfinder/pathOptions.spec.d.ts +0 -1
- package/lib/pathfinder/pathOptions.spec.js +0 -138
- package/lib/pathfinder/pathfinder.d.ts +0 -72
- package/lib/pathfinder/pathfinder.js +0 -176
- package/lib/pathfinder/pathfinder.spec.d.ts +0 -1
- package/lib/pathfinder/pathfinder.spec.js +0 -52
- package/lib/pathfinder/utils.d.ts +0 -34
- package/lib/pathfinder/utils.js +0 -186
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const sdk_gov_1 = require("@gearbox-protocol/sdk-gov");
|
|
4
|
-
const chai_1 = require("chai");
|
|
5
|
-
const pathOptions_1 = require("./pathOptions");
|
|
6
|
-
describe("PathOptionFactory test", () => {
|
|
7
|
-
it("next works correctly", () => {
|
|
8
|
-
const serie = [
|
|
9
|
-
{
|
|
10
|
-
target: sdk_gov_1.tokenDataByNetwork.Mainnet["3Crv"],
|
|
11
|
-
option: 0,
|
|
12
|
-
totalOptions: 3,
|
|
13
|
-
},
|
|
14
|
-
{
|
|
15
|
-
target: sdk_gov_1.tokenDataByNetwork.Mainnet.FRAX3CRV,
|
|
16
|
-
option: 0,
|
|
17
|
-
totalOptions: 2,
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
target: sdk_gov_1.tokenDataByNetwork.Mainnet.LUSD3CRV,
|
|
21
|
-
option: 0,
|
|
22
|
-
totalOptions: 2,
|
|
23
|
-
},
|
|
24
|
-
];
|
|
25
|
-
const expectedNext = JSON.parse(JSON.stringify(serie));
|
|
26
|
-
expectedNext[2].option = 1;
|
|
27
|
-
let next = pathOptions_1.PathOptionFactory.next(serie);
|
|
28
|
-
(0, chai_1.expect)(next).to.be.eql(expectedNext);
|
|
29
|
-
expectedNext[1].option = 1;
|
|
30
|
-
expectedNext[2].option = 0;
|
|
31
|
-
next = pathOptions_1.PathOptionFactory.next(next);
|
|
32
|
-
(0, chai_1.expect)(next).to.be.eql(expectedNext);
|
|
33
|
-
expectedNext[1].option = 1;
|
|
34
|
-
expectedNext[2].option = 1;
|
|
35
|
-
next = pathOptions_1.PathOptionFactory.next(next);
|
|
36
|
-
(0, chai_1.expect)(next).to.be.eql(expectedNext);
|
|
37
|
-
expectedNext[0].option = 1;
|
|
38
|
-
expectedNext[1].option = 0;
|
|
39
|
-
expectedNext[2].option = 0;
|
|
40
|
-
next = pathOptions_1.PathOptionFactory.next(next);
|
|
41
|
-
(0, chai_1.expect)(next).to.be.eql(expectedNext);
|
|
42
|
-
expectedNext[0].option = 1;
|
|
43
|
-
expectedNext[1].option = 0;
|
|
44
|
-
expectedNext[2].option = 1;
|
|
45
|
-
next = pathOptions_1.PathOptionFactory.next(next);
|
|
46
|
-
(0, chai_1.expect)(next).to.be.eql(expectedNext);
|
|
47
|
-
expectedNext[0].option = 1;
|
|
48
|
-
expectedNext[1].option = 1;
|
|
49
|
-
expectedNext[2].option = 0;
|
|
50
|
-
next = pathOptions_1.PathOptionFactory.next(next);
|
|
51
|
-
(0, chai_1.expect)(next).to.be.eql(expectedNext);
|
|
52
|
-
expectedNext[0].option = 1;
|
|
53
|
-
expectedNext[1].option = 1;
|
|
54
|
-
expectedNext[2].option = 1;
|
|
55
|
-
next = pathOptions_1.PathOptionFactory.next(next);
|
|
56
|
-
(0, chai_1.expect)(next).to.be.eql(expectedNext);
|
|
57
|
-
});
|
|
58
|
-
it("generatePathOptions works correctly", () => {
|
|
59
|
-
const balances = {
|
|
60
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet["1INCH"]]: { balance: 100n },
|
|
61
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet["3Crv"]]: { balance: 200n },
|
|
62
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.USDC]: { balance: 200n },
|
|
63
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.FRAX3CRV]: { balance: 200n },
|
|
64
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet["50OHM_50DAI"]]: { balance: 200n },
|
|
65
|
-
};
|
|
66
|
-
const result = pathOptions_1.PathOptionFactory.generatePathOptions(balances, 4, "Mainnet");
|
|
67
|
-
const expected = [
|
|
68
|
-
{
|
|
69
|
-
target: sdk_gov_1.tokenDataByNetwork.Mainnet["3Crv"],
|
|
70
|
-
option: 0,
|
|
71
|
-
totalOptions: 3,
|
|
72
|
-
},
|
|
73
|
-
{
|
|
74
|
-
target: sdk_gov_1.tokenDataByNetwork.Mainnet.FRAX3CRV,
|
|
75
|
-
option: 0,
|
|
76
|
-
totalOptions: 2,
|
|
77
|
-
},
|
|
78
|
-
{
|
|
79
|
-
target: sdk_gov_1.tokenDataByNetwork.Mainnet["50OHM_50DAI"],
|
|
80
|
-
option: 0,
|
|
81
|
-
totalOptions: 2,
|
|
82
|
-
},
|
|
83
|
-
];
|
|
84
|
-
for (let i = 0; i < 3; i++) {
|
|
85
|
-
expected[0].option = i;
|
|
86
|
-
(0, chai_1.expect)(expected).to.be.eql(result[i]);
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
it(" getCurvePools works correctly", () => {
|
|
90
|
-
let balances = {
|
|
91
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet["1INCH"]]: { balance: 100n },
|
|
92
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet["3Crv"]]: { balance: 200n },
|
|
93
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.USDC]: { balance: 200n },
|
|
94
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.FRAX3CRV]: { balance: 200n },
|
|
95
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.LUSD3CRV]: { balance: 200n },
|
|
96
|
-
};
|
|
97
|
-
let expectedCurvePools = [
|
|
98
|
-
"3Crv",
|
|
99
|
-
"FRAX3CRV",
|
|
100
|
-
"LUSD3CRV",
|
|
101
|
-
];
|
|
102
|
-
(0, chai_1.expect)(pathOptions_1.PathOptionFactory.getCurvePools(balances)).to.be.eql(expectedCurvePools);
|
|
103
|
-
balances = {
|
|
104
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet["1INCH"]]: { balance: 100n },
|
|
105
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet["3Crv"]]: { balance: 200n },
|
|
106
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.USDC]: { balance: 200n },
|
|
107
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.yvCurve_FRAX]: { balance: 200n },
|
|
108
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.yvCurve_stETH]: { balance: 200n },
|
|
109
|
-
};
|
|
110
|
-
expectedCurvePools = ["3Crv", "FRAX3CRV", "steCRV"];
|
|
111
|
-
(0, chai_1.expect)(pathOptions_1.PathOptionFactory.getCurvePools(balances)).to.be.eql(expectedCurvePools);
|
|
112
|
-
balances = {
|
|
113
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet["1INCH"]]: { balance: 100n },
|
|
114
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.USDC]: { balance: 200n },
|
|
115
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.cvx3Crv]: { balance: 200n },
|
|
116
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.cvxFRAX3CRV]: { balance: 200n },
|
|
117
|
-
};
|
|
118
|
-
expectedCurvePools = ["3Crv", "FRAX3CRV"];
|
|
119
|
-
(0, chai_1.expect)(pathOptions_1.PathOptionFactory.getCurvePools(balances)).to.be.eql(expectedCurvePools);
|
|
120
|
-
balances = {
|
|
121
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.wstETH]: { balance: 100n },
|
|
122
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.USDC]: { balance: 200n },
|
|
123
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.stkcvx3Crv]: { balance: 200n },
|
|
124
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.stkcvxFRAX3CRV]: { balance: 200n },
|
|
125
|
-
};
|
|
126
|
-
expectedCurvePools = ["3Crv", "FRAX3CRV"];
|
|
127
|
-
(0, chai_1.expect)(pathOptions_1.PathOptionFactory.getCurvePools(balances)).to.be.eql(expectedCurvePools);
|
|
128
|
-
balances = {
|
|
129
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet["3Crv"]]: { balance: 100n },
|
|
130
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.USDC]: { balance: 200n },
|
|
131
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.stkcvx3Crv]: { balance: 200n },
|
|
132
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.cvx3Crv]: { balance: 200n },
|
|
133
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.yvCurve_stETH]: { balance: 200n },
|
|
134
|
-
};
|
|
135
|
-
expectedCurvePools = ["3Crv", "steCRV"];
|
|
136
|
-
(0, chai_1.expect)(pathOptions_1.PathOptionFactory.getCurvePools(balances)).to.be.eql(expectedCurvePools);
|
|
137
|
-
});
|
|
138
|
-
});
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { NetworkType } from "@gearbox-protocol/sdk-gov";
|
|
2
|
-
import { Address, GetContractReturnType, PublicClient } from "viem";
|
|
3
|
-
import { Asset } from "../core/assets";
|
|
4
|
-
import { CreditAccountData } from "../core/creditAccount";
|
|
5
|
-
import { CreditManagerData } from "../core/creditManager";
|
|
6
|
-
import { iRouterV3Abi } from "../types";
|
|
7
|
-
import { PathFinderCloseResult, PathFinderOpenStrategyResult, PathFinderResult, SwapOperation } from "./core";
|
|
8
|
-
interface FindAllSwapsProps {
|
|
9
|
-
creditAccount: CreditAccountData;
|
|
10
|
-
swapOperation: SwapOperation;
|
|
11
|
-
tokenIn: Address;
|
|
12
|
-
tokenOut: Address;
|
|
13
|
-
amount: bigint;
|
|
14
|
-
leftoverAmount: bigint;
|
|
15
|
-
slippage: number;
|
|
16
|
-
}
|
|
17
|
-
interface FindOneTokenPathProps {
|
|
18
|
-
creditAccount: CreditAccountData;
|
|
19
|
-
tokenIn: Address;
|
|
20
|
-
tokenOut: Address;
|
|
21
|
-
amount: bigint;
|
|
22
|
-
slippage: number;
|
|
23
|
-
}
|
|
24
|
-
interface FindBestClosePathProps {
|
|
25
|
-
creditAccount: CreditAccountData;
|
|
26
|
-
creditManager: CreditManagerData;
|
|
27
|
-
expectedBalances: Record<Address, Asset>;
|
|
28
|
-
leftoverBalances: Record<Address, Asset>;
|
|
29
|
-
slippage: number;
|
|
30
|
-
network: NetworkType;
|
|
31
|
-
}
|
|
32
|
-
interface FindOpenStrategyPathProps {
|
|
33
|
-
creditManager: CreditManagerData;
|
|
34
|
-
expectedBalances: Record<Address, Asset>;
|
|
35
|
-
leftoverBalances: Record<Address, Asset>;
|
|
36
|
-
target: Address;
|
|
37
|
-
slippage: number;
|
|
38
|
-
}
|
|
39
|
-
export declare class PathFinder {
|
|
40
|
-
pathFinder: GetContractReturnType<typeof iRouterV3Abi, PublicClient>;
|
|
41
|
-
network: NetworkType;
|
|
42
|
-
protected readonly _connectors: Array<Address>;
|
|
43
|
-
constructor(address: Address, provider: PublicClient, network?: NetworkType);
|
|
44
|
-
findAllSwaps({ creditAccount, swapOperation, tokenIn, tokenOut, amount, leftoverAmount, slippage, }: FindAllSwapsProps): Promise<Array<PathFinderResult>>;
|
|
45
|
-
findOneTokenPath({ creditAccount, tokenIn, tokenOut, amount, slippage, }: FindOneTokenPathProps): Promise<PathFinderResult>;
|
|
46
|
-
/**
|
|
47
|
-
* @dev Finds the best path for opening Credit Account and converting all NORMAL tokens and LP token in the way to TARGET
|
|
48
|
-
* @param cm CreditManagerData which represents credit manager you want to use to open Credit Account
|
|
49
|
-
* @param expectedBalances Expected balances which would be on account accounting also debt. For example,
|
|
50
|
-
* if you open an USDC Credit Account, borrow 50_000 USDC and provide 10 WETH and 10_USDC as collateral
|
|
51
|
-
* from your own funds, expectedBalances should be: { "USDC": 60_000 * (10**6), "<address of WETH>": WAD.mul(10) }
|
|
52
|
-
*
|
|
53
|
-
* @param target Address of symbol of desired token
|
|
54
|
-
* @param slippage Slippage in PERCENTAGE_FORMAT (100% = 10_000) per operation
|
|
55
|
-
* @returns PathFinderOpenStrategyResult which
|
|
56
|
-
*/
|
|
57
|
-
findOpenStrategyPath({ creditManager: cm, expectedBalances, leftoverBalances, target: targetUntyped, slippage, }: FindOpenStrategyPathProps): Promise<PathFinderOpenStrategyResult>;
|
|
58
|
-
/**
|
|
59
|
-
* @dev Finds the path to swap / withdraw all assets from CreditAccount into underlying asset
|
|
60
|
-
* Can bu used for closing Credit Account and for liquidations as well.
|
|
61
|
-
* @param creditAccount CreditAccountData object used for close path computation
|
|
62
|
-
* @param slippage Slippage in PERCENTAGE_FORMAT (100% = 10_000) per operation
|
|
63
|
-
* @return The best option in PathFinderCloseResult format, which
|
|
64
|
-
* - underlyingBalance - total balance of underlying token
|
|
65
|
-
* - calls - list of calls which should be done to swap & unwrap everything to underlying token
|
|
66
|
-
*/
|
|
67
|
-
findBestClosePath({ creditAccount, creditManager: cm, expectedBalances, leftoverBalances, slippage, network, }: FindBestClosePathProps): Promise<PathFinderCloseResult>;
|
|
68
|
-
static compare(r1: PathFinderResult, r2: PathFinderResult): PathFinderResult;
|
|
69
|
-
getAvailableConnectors(availableList: Record<Address, bigint> | Record<Address, true>): `0x${string}`[];
|
|
70
|
-
static getAvailableConnectors(availableList: Record<Address, bigint> | Record<Address, true>, connectors: Address[]): `0x${string}`[];
|
|
71
|
-
}
|
|
72
|
-
export {};
|
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PathFinder = void 0;
|
|
4
|
-
const sdk_gov_1 = require("@gearbox-protocol/sdk-gov");
|
|
5
|
-
const viem_1 = require("viem");
|
|
6
|
-
const types_1 = require("../types");
|
|
7
|
-
const pathOptions_1 = require("./pathOptions");
|
|
8
|
-
const MAX_GAS_PER_ROUTE = 200000000n;
|
|
9
|
-
const GAS_PER_BLOCK = 400000000n;
|
|
10
|
-
class PathFinder {
|
|
11
|
-
pathFinder;
|
|
12
|
-
network;
|
|
13
|
-
_connectors;
|
|
14
|
-
constructor(address, provider, network = "Mainnet") {
|
|
15
|
-
this.pathFinder = (0, viem_1.getContract)({
|
|
16
|
-
address,
|
|
17
|
-
abi: types_1.iRouterV3Abi,
|
|
18
|
-
client: provider,
|
|
19
|
-
});
|
|
20
|
-
this.network = network;
|
|
21
|
-
this._connectors = (0, sdk_gov_1.getConnectors)(network);
|
|
22
|
-
}
|
|
23
|
-
async findAllSwaps({ creditAccount, swapOperation, tokenIn, tokenOut, amount, leftoverAmount, slippage, }) {
|
|
24
|
-
const connectors = this.getAvailableConnectors(creditAccount.balances);
|
|
25
|
-
const swapTask = {
|
|
26
|
-
swapOperation: swapOperation,
|
|
27
|
-
creditAccount: creditAccount.addr,
|
|
28
|
-
tokenIn,
|
|
29
|
-
tokenOut,
|
|
30
|
-
connectors,
|
|
31
|
-
amount,
|
|
32
|
-
leftoverAmount,
|
|
33
|
-
};
|
|
34
|
-
const { result: results } = await this.pathFinder.simulate.findAllSwaps([swapTask, BigInt(slippage)], {
|
|
35
|
-
gas: GAS_PER_BLOCK,
|
|
36
|
-
});
|
|
37
|
-
const unique = {};
|
|
38
|
-
results.forEach(r => {
|
|
39
|
-
const key = `${r.minAmount.toString()}${r.calls
|
|
40
|
-
.map(c => `${c.target.toLowerCase()}${c.callData}`)
|
|
41
|
-
.join("-")}`;
|
|
42
|
-
unique[key] = {
|
|
43
|
-
amount: r.amount,
|
|
44
|
-
minAmount: r.minAmount,
|
|
45
|
-
calls: r.calls,
|
|
46
|
-
};
|
|
47
|
-
});
|
|
48
|
-
return Object.values(unique);
|
|
49
|
-
}
|
|
50
|
-
async findOneTokenPath({ creditAccount, tokenIn, tokenOut, amount, slippage, }) {
|
|
51
|
-
const connectors = this.getAvailableConnectors(creditAccount.balances);
|
|
52
|
-
const { result } = await this.pathFinder.simulate.findOneTokenPath([
|
|
53
|
-
tokenIn,
|
|
54
|
-
amount,
|
|
55
|
-
tokenOut,
|
|
56
|
-
creditAccount.addr,
|
|
57
|
-
connectors,
|
|
58
|
-
BigInt(slippage),
|
|
59
|
-
], {
|
|
60
|
-
gas: GAS_PER_BLOCK,
|
|
61
|
-
});
|
|
62
|
-
return {
|
|
63
|
-
amount: result.amount,
|
|
64
|
-
minAmount: result.minAmount,
|
|
65
|
-
calls: result.calls,
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* @dev Finds the best path for opening Credit Account and converting all NORMAL tokens and LP token in the way to TARGET
|
|
70
|
-
* @param cm CreditManagerData which represents credit manager you want to use to open Credit Account
|
|
71
|
-
* @param expectedBalances Expected balances which would be on account accounting also debt. For example,
|
|
72
|
-
* if you open an USDC Credit Account, borrow 50_000 USDC and provide 10 WETH and 10_USDC as collateral
|
|
73
|
-
* from your own funds, expectedBalances should be: { "USDC": 60_000 * (10**6), "<address of WETH>": WAD.mul(10) }
|
|
74
|
-
*
|
|
75
|
-
* @param target Address of symbol of desired token
|
|
76
|
-
* @param slippage Slippage in PERCENTAGE_FORMAT (100% = 10_000) per operation
|
|
77
|
-
* @returns PathFinderOpenStrategyResult which
|
|
78
|
-
*/
|
|
79
|
-
async findOpenStrategyPath({ creditManager: cm, expectedBalances, leftoverBalances, target: targetUntyped, slippage, }) {
|
|
80
|
-
const target = targetUntyped;
|
|
81
|
-
const input = cm.collateralTokens.map(token => ({
|
|
82
|
-
token,
|
|
83
|
-
balance: expectedBalances[token]?.balance || 0n,
|
|
84
|
-
}));
|
|
85
|
-
const leftover = cm.collateralTokens.map(token => ({
|
|
86
|
-
token,
|
|
87
|
-
balance: leftoverBalances[token]?.balance || 1n,
|
|
88
|
-
}));
|
|
89
|
-
const connectors = this.getAvailableConnectors(cm.supportedTokens);
|
|
90
|
-
const { result: [outBalances, result], } = await this.pathFinder.simulate.findOpenStrategyPath([cm.address, input, leftover, target, connectors, BigInt(slippage)], {
|
|
91
|
-
gas: GAS_PER_BLOCK,
|
|
92
|
-
});
|
|
93
|
-
const balancesAfter = outBalances.reduce((acc, b) => {
|
|
94
|
-
acc[b.token.toLowerCase()] = b.balance;
|
|
95
|
-
return acc;
|
|
96
|
-
}, {});
|
|
97
|
-
return {
|
|
98
|
-
balances: {
|
|
99
|
-
...balancesAfter,
|
|
100
|
-
[target]: (expectedBalances[target]?.balance || 0n) + result.amount,
|
|
101
|
-
},
|
|
102
|
-
minBalances: {
|
|
103
|
-
...balancesAfter,
|
|
104
|
-
[target]: (expectedBalances[target]?.balance || 0n) + result.minAmount,
|
|
105
|
-
},
|
|
106
|
-
calls: result.calls,
|
|
107
|
-
minAmount: result.minAmount,
|
|
108
|
-
amount: result.amount,
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* @dev Finds the path to swap / withdraw all assets from CreditAccount into underlying asset
|
|
113
|
-
* Can bu used for closing Credit Account and for liquidations as well.
|
|
114
|
-
* @param creditAccount CreditAccountData object used for close path computation
|
|
115
|
-
* @param slippage Slippage in PERCENTAGE_FORMAT (100% = 10_000) per operation
|
|
116
|
-
* @return The best option in PathFinderCloseResult format, which
|
|
117
|
-
* - underlyingBalance - total balance of underlying token
|
|
118
|
-
* - calls - list of calls which should be done to swap & unwrap everything to underlying token
|
|
119
|
-
*/
|
|
120
|
-
async findBestClosePath({ creditAccount, creditManager: cm, expectedBalances, leftoverBalances, slippage, network, }) {
|
|
121
|
-
const loopsPerTx = GAS_PER_BLOCK / MAX_GAS_PER_ROUTE;
|
|
122
|
-
const pathOptions = pathOptions_1.PathOptionFactory.generatePathOptions(creditAccount.allBalances, Number(loopsPerTx), network);
|
|
123
|
-
const expected = cm.collateralTokens.map(token => {
|
|
124
|
-
// When we pass expected balances explicitly, we need to mimic router behaviour by filtering out leftover tokens
|
|
125
|
-
// for example, we can have stETH balance of 2, because 1 transforms to 2 because of rebasing
|
|
126
|
-
// https://github.com/Gearbox-protocol/router-v3/blob/c230a3aa568bb432e50463cfddc877fec8940cf5/contracts/RouterV3.sol#L222
|
|
127
|
-
const actual = expectedBalances[token]?.balance || 0n;
|
|
128
|
-
return {
|
|
129
|
-
token,
|
|
130
|
-
balance: actual > 10n ? actual : 0n,
|
|
131
|
-
};
|
|
132
|
-
});
|
|
133
|
-
const leftover = cm.collateralTokens.map(token => ({
|
|
134
|
-
token,
|
|
135
|
-
balance: leftoverBalances[token]?.balance || 1n,
|
|
136
|
-
}));
|
|
137
|
-
const connectors = this.getAvailableConnectors(creditAccount.balances);
|
|
138
|
-
const results = await Promise.all(pathOptions.map(po => this.pathFinder.simulate.findBestClosePath([
|
|
139
|
-
creditAccount.addr,
|
|
140
|
-
expected,
|
|
141
|
-
leftover,
|
|
142
|
-
connectors,
|
|
143
|
-
BigInt(slippage),
|
|
144
|
-
po,
|
|
145
|
-
loopsPerTx,
|
|
146
|
-
false,
|
|
147
|
-
], {
|
|
148
|
-
gas: GAS_PER_BLOCK,
|
|
149
|
-
})));
|
|
150
|
-
const bestResult = results.reduce((best, pathFinderResult) => PathFinder.compare(best, {
|
|
151
|
-
calls: pathFinderResult.result.calls,
|
|
152
|
-
amount: pathFinderResult.result.amount,
|
|
153
|
-
minAmount: pathFinderResult.result.minAmount,
|
|
154
|
-
}), {
|
|
155
|
-
amount: 0n,
|
|
156
|
-
minAmount: 0n,
|
|
157
|
-
calls: [],
|
|
158
|
-
});
|
|
159
|
-
return {
|
|
160
|
-
...bestResult,
|
|
161
|
-
underlyingBalance: bestResult.minAmount +
|
|
162
|
-
creditAccount.allBalances[creditAccount.underlyingToken.toLowerCase()].balance,
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
static compare(r1, r2) {
|
|
166
|
-
return r1.amount > r2.amount ? r1 : r2;
|
|
167
|
-
}
|
|
168
|
-
getAvailableConnectors(availableList) {
|
|
169
|
-
const connectors = PathFinder.getAvailableConnectors(availableList, this._connectors);
|
|
170
|
-
return connectors;
|
|
171
|
-
}
|
|
172
|
-
static getAvailableConnectors(availableList, connectors) {
|
|
173
|
-
return connectors.filter(t => availableList[t] !== undefined);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
exports.PathFinder = PathFinder;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const sdk_gov_1 = require("@gearbox-protocol/sdk-gov");
|
|
4
|
-
const chai_1 = require("chai");
|
|
5
|
-
const viem_1 = require("viem");
|
|
6
|
-
const pathfinder_1 = require("./pathfinder");
|
|
7
|
-
describe("PathFinder test", () => {
|
|
8
|
-
it("compare works correctly", () => {
|
|
9
|
-
const r1 = {
|
|
10
|
-
minAmount: 2000n,
|
|
11
|
-
amount: 20000n,
|
|
12
|
-
calls: [],
|
|
13
|
-
};
|
|
14
|
-
const r2 = {
|
|
15
|
-
minAmount: 3000n,
|
|
16
|
-
amount: 30000n,
|
|
17
|
-
calls: [],
|
|
18
|
-
};
|
|
19
|
-
let result = pathfinder_1.PathFinder.compare(r1, r2);
|
|
20
|
-
(0, chai_1.expect)(result).to.be.eql(r2);
|
|
21
|
-
});
|
|
22
|
-
it("has all expected connectors", () => {
|
|
23
|
-
const pf = new pathfinder_1.PathFinder("", (0, viem_1.createPublicClient)({ transport: (0, viem_1.http)("https://mainnet.infura.io/v3/") }), "Mainnet");
|
|
24
|
-
const allowedTokens = {
|
|
25
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.WETH.toLowerCase()]: true,
|
|
26
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.DAI.toLowerCase()]: true,
|
|
27
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.USDC.toLowerCase()]: true,
|
|
28
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.FRAX.toLowerCase()]: true,
|
|
29
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.rETH.toLowerCase()]: true,
|
|
30
|
-
};
|
|
31
|
-
(0, chai_1.expect)(pf.getAvailableConnectors(allowedTokens)).to.be.deep.equal([
|
|
32
|
-
sdk_gov_1.tokenDataByNetwork.Mainnet.WETH.toLowerCase(),
|
|
33
|
-
sdk_gov_1.tokenDataByNetwork.Mainnet.DAI.toLowerCase(),
|
|
34
|
-
sdk_gov_1.tokenDataByNetwork.Mainnet.USDC.toLowerCase(),
|
|
35
|
-
sdk_gov_1.tokenDataByNetwork.Mainnet.FRAX.toLowerCase(),
|
|
36
|
-
sdk_gov_1.tokenDataByNetwork.Mainnet.rETH.toLowerCase(),
|
|
37
|
-
]);
|
|
38
|
-
});
|
|
39
|
-
it("has all expected connectors, when gew are disabled", () => {
|
|
40
|
-
const pf = new pathfinder_1.PathFinder("", (0, viem_1.createPublicClient)({ transport: (0, viem_1.http)("https://mainnet.infura.io/v3/") }), "Mainnet");
|
|
41
|
-
const allowedTokens = {
|
|
42
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.WETH.toLowerCase()]: true,
|
|
43
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.DAI.toLowerCase()]: true,
|
|
44
|
-
[sdk_gov_1.tokenDataByNetwork.Mainnet.rETH.toLowerCase()]: true,
|
|
45
|
-
};
|
|
46
|
-
(0, chai_1.expect)(pf.getAvailableConnectors(allowedTokens)).to.be.deep.equal([
|
|
47
|
-
sdk_gov_1.tokenDataByNetwork.Mainnet.WETH.toLowerCase(),
|
|
48
|
-
sdk_gov_1.tokenDataByNetwork.Mainnet.DAI.toLowerCase(),
|
|
49
|
-
sdk_gov_1.tokenDataByNetwork.Mainnet.rETH.toLowerCase(),
|
|
50
|
-
]);
|
|
51
|
-
});
|
|
52
|
-
});
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { Address, PublicClient } from "viem";
|
|
2
|
-
import { MultiCall } from "./core";
|
|
3
|
-
export interface FeeInfo {
|
|
4
|
-
type: KnownFeeTypes;
|
|
5
|
-
value: bigint;
|
|
6
|
-
}
|
|
7
|
-
type KnownFeeTypes = "curve" | "lido" | "uniswap_v2" | "uniswap_v3" | "balancer";
|
|
8
|
-
export declare const BALANCER_VAULT_ABI: readonly [{
|
|
9
|
-
readonly inputs: readonly [];
|
|
10
|
-
readonly name: "getSwapFeePercentage";
|
|
11
|
-
readonly outputs: readonly [{
|
|
12
|
-
readonly internalType: "uint256";
|
|
13
|
-
readonly name: "";
|
|
14
|
-
readonly type: "uint256";
|
|
15
|
-
}];
|
|
16
|
-
readonly stateMutability: "view";
|
|
17
|
-
readonly type: "function";
|
|
18
|
-
}];
|
|
19
|
-
export interface FindPathFeesProps {
|
|
20
|
-
calls: Array<MultiCall>;
|
|
21
|
-
provider: PublicClient;
|
|
22
|
-
contractsByAdapter: Record<Address, Address>;
|
|
23
|
-
}
|
|
24
|
-
export declare class PathFinderUtils {
|
|
25
|
-
static findPathFees({ calls, provider, contractsByAdapter, }: FindPathFeesProps): Promise<FeeInfo[]>;
|
|
26
|
-
private static getUniswapV2Fee;
|
|
27
|
-
private static getUniswapV3Fee;
|
|
28
|
-
private static getCurveFeeCall;
|
|
29
|
-
private static getCurveFee;
|
|
30
|
-
private static getBalancerFeeCall;
|
|
31
|
-
private static getBalancerFee;
|
|
32
|
-
private static getLidoFee;
|
|
33
|
-
}
|
|
34
|
-
export {};
|
package/lib/pathfinder/utils.js
DELETED
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PathFinderUtils = exports.BALANCER_VAULT_ABI = void 0;
|
|
4
|
-
const sdk_gov_1 = require("@gearbox-protocol/sdk-gov");
|
|
5
|
-
const balancerV2VaultParser_1 = require("../parsers/balancerV2VaultParser");
|
|
6
|
-
const curveAdapterParser_1 = require("../parsers/curveAdapterParser");
|
|
7
|
-
const lidoAdapterParser_1 = require("../parsers/lidoAdapterParser");
|
|
8
|
-
const txParser_1 = require("../parsers/txParser");
|
|
9
|
-
const uniV2AdapterParser_1 = require("../parsers/uniV2AdapterParser");
|
|
10
|
-
const uniV3AdapterParser_1 = require("../parsers/uniV3AdapterParser");
|
|
11
|
-
const types_1 = require("../types");
|
|
12
|
-
const balancerVault_1 = require("./balancerVault");
|
|
13
|
-
const CURVE_FEE_DECIMALS = 100000000n;
|
|
14
|
-
const BALANCER_FEE_DECIMALS = 10000000000000000n;
|
|
15
|
-
exports.BALANCER_VAULT_ABI = [
|
|
16
|
-
{
|
|
17
|
-
inputs: [],
|
|
18
|
-
name: "getSwapFeePercentage",
|
|
19
|
-
outputs: [
|
|
20
|
-
{
|
|
21
|
-
internalType: "uint256",
|
|
22
|
-
name: "",
|
|
23
|
-
type: "uint256",
|
|
24
|
-
},
|
|
25
|
-
],
|
|
26
|
-
stateMutability: "view",
|
|
27
|
-
type: "function",
|
|
28
|
-
},
|
|
29
|
-
];
|
|
30
|
-
class PathFinderUtils {
|
|
31
|
-
static async findPathFees({ calls, provider, contractsByAdapter, }) {
|
|
32
|
-
const pathObjects = txParser_1.TxParser.parseToObjectMultiCall(calls);
|
|
33
|
-
const { simpleFees, curve, balancer } = pathObjects.reduce((acc, pathSegment) => {
|
|
34
|
-
if (!pathSegment)
|
|
35
|
-
return acc;
|
|
36
|
-
const { callObject, parser } = pathSegment;
|
|
37
|
-
switch (true) {
|
|
38
|
-
case parser instanceof uniV2AdapterParser_1.UniswapV2AdapterParser: {
|
|
39
|
-
const f = this.getUniswapV2Fee(callObject);
|
|
40
|
-
if (f)
|
|
41
|
-
acc.simpleFees.push(f);
|
|
42
|
-
break;
|
|
43
|
-
}
|
|
44
|
-
case parser instanceof uniV3AdapterParser_1.UniswapV3AdapterParser: {
|
|
45
|
-
const f = this.getUniswapV3Fee(callObject);
|
|
46
|
-
if (f)
|
|
47
|
-
acc.simpleFees.push(f);
|
|
48
|
-
break;
|
|
49
|
-
}
|
|
50
|
-
case parser instanceof lidoAdapterParser_1.LidoAdapterParser: {
|
|
51
|
-
const f = this.getLidoFee();
|
|
52
|
-
if (f)
|
|
53
|
-
acc.simpleFees.push(f);
|
|
54
|
-
break;
|
|
55
|
-
}
|
|
56
|
-
case parser instanceof curveAdapterParser_1.CurveAdapterParser: {
|
|
57
|
-
const call = this.getCurveFeeCall(callObject, contractsByAdapter);
|
|
58
|
-
if (call)
|
|
59
|
-
acc.curve.push(call);
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
case parser instanceof balancerV2VaultParser_1.BalancerV2VaultParser: {
|
|
63
|
-
const call = this.getBalancerFeeCall(callObject);
|
|
64
|
-
if (call)
|
|
65
|
-
acc.balancer.push(call);
|
|
66
|
-
break;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return acc;
|
|
70
|
-
}, {
|
|
71
|
-
simpleFees: [],
|
|
72
|
-
curve: [],
|
|
73
|
-
balancer: [],
|
|
74
|
-
});
|
|
75
|
-
const response = (await provider.multicall({
|
|
76
|
-
allowFailure: true,
|
|
77
|
-
multicallAddress: sdk_gov_1.MULTICALL_ADDRESS,
|
|
78
|
-
contracts: [...curve, ...balancer],
|
|
79
|
-
}));
|
|
80
|
-
const curveEnds = curve.length;
|
|
81
|
-
const curveResponse = response.slice(0, curveEnds);
|
|
82
|
-
const balancerEnds = balancer.length;
|
|
83
|
-
const balancerResponse = response.slice(curveEnds, balancerEnds);
|
|
84
|
-
const curveFees = curveResponse.map(r => this.getCurveFee(r));
|
|
85
|
-
const balancerFees = balancerResponse.map(r => this.getBalancerFee(r));
|
|
86
|
-
const fees = [...simpleFees, ...curveFees, ...balancerFees];
|
|
87
|
-
return fees;
|
|
88
|
-
}
|
|
89
|
-
static getUniswapV2Fee(callObject) {
|
|
90
|
-
const { functionName } = callObject;
|
|
91
|
-
switch (functionName) {
|
|
92
|
-
case "swapExactTokensForTokens":
|
|
93
|
-
case "swapTokensForExactTokens":
|
|
94
|
-
case "swapDiffTokensForTokens": {
|
|
95
|
-
// 0.3%
|
|
96
|
-
return {
|
|
97
|
-
type: "uniswap_v2",
|
|
98
|
-
value: 3000n,
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
default:
|
|
102
|
-
return null;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
static getUniswapV3Fee(callObject) {
|
|
106
|
-
const { functionName, args } = callObject;
|
|
107
|
-
switch (functionName) {
|
|
108
|
-
case "exactInputSingle":
|
|
109
|
-
case "exactDiffInputSingle":
|
|
110
|
-
case "exactOutputSingle": {
|
|
111
|
-
const [first] = args;
|
|
112
|
-
const { fee = 0 } = first || {};
|
|
113
|
-
return {
|
|
114
|
-
type: "uniswap_v3",
|
|
115
|
-
value: (0, sdk_gov_1.toBigInt)(fee),
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
default:
|
|
119
|
-
return null;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
static getCurveFeeCall(callObject, contractsByAdapter) {
|
|
123
|
-
const { functionName } = callObject;
|
|
124
|
-
switch (functionName) {
|
|
125
|
-
case "exchange":
|
|
126
|
-
case "exchange_underlying":
|
|
127
|
-
case "exchange_diff":
|
|
128
|
-
case "exchange_diff_underlying": {
|
|
129
|
-
const adapter = (callObject.address || "").toLowerCase();
|
|
130
|
-
const contract = contractsByAdapter[adapter];
|
|
131
|
-
return contract
|
|
132
|
-
? {
|
|
133
|
-
address: contract,
|
|
134
|
-
abi: types_1.iCurvePoolAbi,
|
|
135
|
-
functionName: "fee",
|
|
136
|
-
args: [],
|
|
137
|
-
}
|
|
138
|
-
: null;
|
|
139
|
-
}
|
|
140
|
-
default:
|
|
141
|
-
return null;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
static getCurveFee({ result }) {
|
|
145
|
-
const feeOriginal = result || 0n;
|
|
146
|
-
return {
|
|
147
|
-
type: "curve",
|
|
148
|
-
value: (feeOriginal * sdk_gov_1.PERCENTAGE_FACTOR) / CURVE_FEE_DECIMALS,
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
static getBalancerFeeCall(callObject) {
|
|
152
|
-
const { functionName } = callObject;
|
|
153
|
-
switch (functionName) {
|
|
154
|
-
case "swapDiff":
|
|
155
|
-
case "swap": {
|
|
156
|
-
const [first] = callObject.args;
|
|
157
|
-
const { poolId = "" } = first || {};
|
|
158
|
-
const { address } = (0, balancerVault_1.splitPoolId)(poolId);
|
|
159
|
-
return address
|
|
160
|
-
? {
|
|
161
|
-
address: address,
|
|
162
|
-
abi: exports.BALANCER_VAULT_ABI,
|
|
163
|
-
functionName: "getSwapFeePercentage",
|
|
164
|
-
args: [],
|
|
165
|
-
}
|
|
166
|
-
: null;
|
|
167
|
-
}
|
|
168
|
-
default:
|
|
169
|
-
return null;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
static getBalancerFee({ result }) {
|
|
173
|
-
const feeOriginal = result || 0n;
|
|
174
|
-
return {
|
|
175
|
-
type: "balancer",
|
|
176
|
-
value: (feeOriginal * sdk_gov_1.PERCENTAGE_FACTOR) / BALANCER_FEE_DECIMALS,
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
static getLidoFee() {
|
|
180
|
-
return {
|
|
181
|
-
type: "lido",
|
|
182
|
-
value: 0n,
|
|
183
|
-
};
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
exports.PathFinderUtils = PathFinderUtils;
|