@gearbox-protocol/sdk 0.0.38 → 0.0.39
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/pathfinder.d.ts +12 -6
- package/lib/core/pathfinder.js +116 -3
- package/lib/core/token.d.ts +1 -0
- package/lib/core/token.js +8 -4
- package/package.json +1 -1
- package/src/core/pathfinder.ts +94 -20
- package/src/core/token.ts +9 -4
package/lib/core/pathfinder.d.ts
CHANGED
|
@@ -2,12 +2,18 @@ import { BigNumber } from "ethers";
|
|
|
2
2
|
import { CreditManagerData } from "./creditManager";
|
|
3
3
|
import { MultiCall } from "./multicall";
|
|
4
4
|
import { SupportedTokens } from "./token";
|
|
5
|
-
export
|
|
6
|
-
calls: Array<MultiCall>;
|
|
7
|
-
balances: Record<SupportedTokens, BigNumber>;
|
|
8
|
-
|
|
9
|
-
pool: SupportedTokens;
|
|
10
|
-
creditManager: CreditManagerData;
|
|
5
|
+
export declare class Path {
|
|
6
|
+
readonly calls: Array<MultiCall>;
|
|
7
|
+
readonly balances: Record<SupportedTokens, BigNumber>;
|
|
8
|
+
protected _gasUsed: number;
|
|
9
|
+
readonly pool: SupportedTokens;
|
|
10
|
+
readonly creditManager: CreditManagerData;
|
|
11
|
+
constructor(opts: {
|
|
12
|
+
gasUsed: number;
|
|
13
|
+
balances: Record<SupportedTokens, BigNumber>;
|
|
14
|
+
pool: SupportedTokens;
|
|
15
|
+
creditManager: CreditManagerData;
|
|
16
|
+
});
|
|
11
17
|
getBestPath(): Promise<Path>;
|
|
12
18
|
}
|
|
13
19
|
export interface PathAsset {
|
package/lib/core/pathfinder.js
CHANGED
|
@@ -1,16 +1,129 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
2
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ConnectorPathAsset = void 0;
|
|
39
|
+
exports.ConnectorPathAsset = exports.Path = void 0;
|
|
40
|
+
// const path = new Path({gasUsed:0, balances: creditAccount.balances, })
|
|
41
|
+
// const closurePath = await path.getBestPath();
|
|
42
|
+
// closurePath.calls -> multicalls (!)
|
|
43
|
+
// closurePath.balances[path.pool] = (X)
|
|
44
|
+
// closurePath.balances[!path.pool] = 0 / 1;
|
|
45
|
+
//
|
|
46
|
+
// [0,0,0, 100]
|
|
47
|
+
// [ 12, 12, 12, 0]
|
|
48
|
+
var Path = /** @class */ (function () {
|
|
49
|
+
function Path(opts) {
|
|
50
|
+
this.calls = [];
|
|
51
|
+
this._gasUsed = opts.gasUsed;
|
|
52
|
+
this.balances = opts.balances;
|
|
53
|
+
this.pool = opts.pool;
|
|
54
|
+
this.creditManager = opts.creditManager;
|
|
55
|
+
}
|
|
56
|
+
Path.prototype.getBestPath = function () {
|
|
57
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
58
|
+
return __generator(this, function (_a) {
|
|
59
|
+
// const nextToken = Object.entries(this.balances).filter(([token, balance]) => balance.gt(1) ) //.sort((a,b) => {})
|
|
60
|
+
// if (nextToken.length ===1 ) {
|
|
61
|
+
// }
|
|
62
|
+
// Get balances and keep non-zero only
|
|
63
|
+
// Find token with highest priority
|
|
64
|
+
// Get token type of this token
|
|
65
|
+
// switch (type) {
|
|
66
|
+
// case TokenType.YearnValut:
|
|
67
|
+
// const assetPathClass = new Path(tokenData);
|
|
68
|
+
// return assetPathClass.getBestPath(this);
|
|
69
|
+
// ...
|
|
70
|
+
//}
|
|
71
|
+
throw new Error("Not implmented");
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
return Path;
|
|
76
|
+
}());
|
|
77
|
+
exports.Path = Path;
|
|
4
78
|
var ConnectorPathAsset = /** @class */ (function () {
|
|
5
79
|
function ConnectorPathAsset() {
|
|
6
80
|
}
|
|
7
81
|
ConnectorPathAsset.prototype.getBestPath = function (p) {
|
|
8
82
|
console.log(p);
|
|
9
|
-
// How to unwrap
|
|
83
|
+
// How to unwrap
|
|
10
84
|
// set balance of current token to 0
|
|
11
|
-
// add balnace what we get
|
|
85
|
+
// add balnace what we get
|
|
12
86
|
// add call
|
|
13
87
|
// create new Paths
|
|
88
|
+
// 3CRV:
|
|
89
|
+
// coinst balance3crv = p.balance["3rcv"];
|
|
90
|
+
// const p.balance["3rcv"] = 0;
|
|
91
|
+
// DAI
|
|
92
|
+
// const daiPath = {...p};
|
|
93
|
+
// const daiPath["Dai"] = await curve.getAmount(t balance3crv);
|
|
94
|
+
// const pathDai = new Path(daiPath)
|
|
95
|
+
// ...
|
|
96
|
+
// const bestPath = pathDai.balance[0] > pathUSDC.balance(0) ? pathDai : pathUsdc;
|
|
97
|
+
// --- DAI
|
|
98
|
+
// --|
|
|
99
|
+
// --- USDC
|
|
100
|
+
//
|
|
101
|
+
// NORMAL TOKEN
|
|
102
|
+
// ============
|
|
103
|
+
//
|
|
104
|
+
// - NORMAL -> POOL (1 pair) => #1
|
|
105
|
+
// for(let conn of connectors) {
|
|
106
|
+
// const result = getAmount([NORMAL, conn, pool])
|
|
107
|
+
// }
|
|
108
|
+
// => best Path to get max Pool tokens
|
|
109
|
+
// const resultInConnector = getAmount([NORMAL, conn]);
|
|
110
|
+
// calls.push("Uniswap.swap(NORMAL, conn)");
|
|
111
|
+
// CRV => CRV -> USDC -> DAI
|
|
112
|
+
// LINK => LINK -> USDC -> DAI
|
|
113
|
+
//
|
|
114
|
+
// [UniV2.swap("CRV", "USDC"),
|
|
115
|
+
// UniV3.swap("LINK", "USDC"),
|
|
116
|
+
// Curve.exchange("USDC", "DAI")]
|
|
117
|
+
//
|
|
118
|
+
// Below is not optimal:
|
|
119
|
+
// [ UniV2.swap("CRV -> USDC -> DAI"),
|
|
120
|
+
// UniV2.swap("LINK -> USDC -> DAI")]
|
|
121
|
+
//
|
|
122
|
+
//
|
|
123
|
+
// [ UniV2.swap("CRV -> USDC")
|
|
124
|
+
// UniV2.swap("USDC -> DAI"),
|
|
125
|
+
// UniV2.swap("LINK -> USDC"),
|
|
126
|
+
// UniV2.swap("USDC -> DAI") ]
|
|
14
127
|
// return maxValuable(new Paths.getBestPath())
|
|
15
128
|
throw new Error("Method not implemented.");
|
|
16
129
|
};
|
package/lib/core/token.d.ts
CHANGED
package/lib/core/token.js
CHANGED
|
@@ -113,16 +113,20 @@ exports.supportedTokens = {
|
|
|
113
113
|
},
|
|
114
114
|
// YEARN TOKENS
|
|
115
115
|
yvDAI: {
|
|
116
|
-
type: TokenType.YEARN_VAULT
|
|
116
|
+
type: TokenType.YEARN_VAULT,
|
|
117
|
+
underlying: "DAI",
|
|
117
118
|
},
|
|
118
119
|
yvUSDC: {
|
|
119
|
-
type: TokenType.YEARN_VAULT
|
|
120
|
+
type: TokenType.YEARN_VAULT,
|
|
121
|
+
underlying: "USDC"
|
|
120
122
|
},
|
|
121
123
|
yvWETH: {
|
|
122
|
-
type: TokenType.YEARN_VAULT
|
|
124
|
+
type: TokenType.YEARN_VAULT,
|
|
125
|
+
underlying: "WETH",
|
|
123
126
|
},
|
|
124
127
|
yvWBTC: {
|
|
125
|
-
type: TokenType.YEARN_VAULT
|
|
128
|
+
type: TokenType.YEARN_VAULT,
|
|
129
|
+
underlying: "WBTC"
|
|
126
130
|
},
|
|
127
131
|
// CURVE LP TOKENS
|
|
128
132
|
"3Crv": {
|
package/package.json
CHANGED
package/src/core/pathfinder.ts
CHANGED
|
@@ -3,24 +3,54 @@ import { CreditManagerData } from "./creditManager";
|
|
|
3
3
|
import { MultiCall } from "./multicall";
|
|
4
4
|
import { SupportedTokens } from "./token";
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
//
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
//
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
6
|
+
// const path = new Path({gasUsed:0, balances: creditAccount.balances, })
|
|
7
|
+
// const closurePath = await path.getBestPath();
|
|
8
|
+
// closurePath.calls -> multicalls (!)
|
|
9
|
+
// closurePath.balances[path.pool] = (X)
|
|
10
|
+
// closurePath.balances[!path.pool] = 0 / 1;
|
|
11
|
+
|
|
12
|
+
//
|
|
13
|
+
// [0,0,0, 100]
|
|
14
|
+
// [ 12, 12, 12, 0]
|
|
15
|
+
|
|
16
|
+
export class Path {
|
|
17
|
+
public readonly calls: Array<MultiCall> = [];
|
|
18
|
+
public readonly balances: Record<SupportedTokens, BigNumber>;
|
|
19
|
+
// usedTokens: Array<SupportedTokens>;
|
|
20
|
+
protected _gasUsed: number;
|
|
21
|
+
public readonly pool: SupportedTokens;
|
|
22
|
+
public readonly creditManager: CreditManagerData;
|
|
23
|
+
|
|
24
|
+
constructor(opts: {
|
|
25
|
+
gasUsed: number;
|
|
26
|
+
balances: Record<SupportedTokens, BigNumber>;
|
|
27
|
+
pool: SupportedTokens;
|
|
28
|
+
creditManager: CreditManagerData;
|
|
29
|
+
}) {
|
|
30
|
+
this._gasUsed = opts.gasUsed;
|
|
31
|
+
this.balances = opts.balances;
|
|
32
|
+
this.pool = opts.pool;
|
|
33
|
+
this.creditManager = opts.creditManager;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async getBestPath(): Promise<Path> {
|
|
37
|
+
// const nextToken = Object.entries(this.balances).filter(([token, balance]) => balance.gt(1) ) //.sort((a,b) => {})
|
|
38
|
+
// if (nextToken.length ===1 ) {
|
|
39
|
+
|
|
40
|
+
// }
|
|
41
|
+
|
|
42
|
+
// Get balances and keep non-zero only
|
|
43
|
+
// Find token with highest priority
|
|
44
|
+
// Get token type of this token
|
|
45
|
+
// switch (type) {
|
|
46
|
+
// case TokenType.YearnValut:
|
|
47
|
+
// const assetPathClass = new Path(tokenData);
|
|
48
|
+
// return assetPathClass.getBestPath(this);
|
|
49
|
+
// ...
|
|
50
|
+
|
|
51
|
+
//}
|
|
52
|
+
throw new Error("Not implmented");
|
|
53
|
+
}
|
|
24
54
|
}
|
|
25
55
|
|
|
26
56
|
export interface PathAsset {
|
|
@@ -30,11 +60,55 @@ export interface PathAsset {
|
|
|
30
60
|
export class ConnectorPathAsset implements PathAsset {
|
|
31
61
|
getBestPath(p: Path): Promise<Path> {
|
|
32
62
|
console.log(p);
|
|
33
|
-
// How to unwrap
|
|
63
|
+
// How to unwrap
|
|
34
64
|
// set balance of current token to 0
|
|
35
|
-
// add balnace what we get
|
|
65
|
+
// add balnace what we get
|
|
36
66
|
// add call
|
|
37
67
|
// create new Paths
|
|
68
|
+
|
|
69
|
+
// 3CRV:
|
|
70
|
+
// coinst balance3crv = p.balance["3rcv"];
|
|
71
|
+
// const p.balance["3rcv"] = 0;
|
|
72
|
+
// DAI
|
|
73
|
+
// const daiPath = {...p};
|
|
74
|
+
// const daiPath["Dai"] = await curve.getAmount(t balance3crv);
|
|
75
|
+
|
|
76
|
+
// const pathDai = new Path(daiPath)
|
|
77
|
+
// ...
|
|
78
|
+
// const bestPath = pathDai.balance[0] > pathUSDC.balance(0) ? pathDai : pathUsdc;
|
|
79
|
+
|
|
80
|
+
// --- DAI
|
|
81
|
+
// --|
|
|
82
|
+
// --- USDC
|
|
83
|
+
//
|
|
84
|
+
// NORMAL TOKEN
|
|
85
|
+
// ============
|
|
86
|
+
//
|
|
87
|
+
// - NORMAL -> POOL (1 pair) => #1
|
|
88
|
+
// for(let conn of connectors) {
|
|
89
|
+
// const result = getAmount([NORMAL, conn, pool])
|
|
90
|
+
// }
|
|
91
|
+
// => best Path to get max Pool tokens
|
|
92
|
+
// const resultInConnector = getAmount([NORMAL, conn]);
|
|
93
|
+
// calls.push("Uniswap.swap(NORMAL, conn)");
|
|
94
|
+
|
|
95
|
+
// CRV => CRV -> USDC -> DAI
|
|
96
|
+
// LINK => LINK -> USDC -> DAI
|
|
97
|
+
//
|
|
98
|
+
// [UniV2.swap("CRV", "USDC"),
|
|
99
|
+
// UniV3.swap("LINK", "USDC"),
|
|
100
|
+
// Curve.exchange("USDC", "DAI")]
|
|
101
|
+
//
|
|
102
|
+
// Below is not optimal:
|
|
103
|
+
// [ UniV2.swap("CRV -> USDC -> DAI"),
|
|
104
|
+
// UniV2.swap("LINK -> USDC -> DAI")]
|
|
105
|
+
//
|
|
106
|
+
//
|
|
107
|
+
// [ UniV2.swap("CRV -> USDC")
|
|
108
|
+
// UniV2.swap("USDC -> DAI"),
|
|
109
|
+
// UniV2.swap("LINK -> USDC"),
|
|
110
|
+
// UniV2.swap("USDC -> DAI") ]
|
|
111
|
+
|
|
38
112
|
// return maxValuable(new Paths.getBestPath())
|
|
39
113
|
throw new Error("Method not implemented.");
|
|
40
114
|
}
|
package/src/core/token.ts
CHANGED
|
@@ -36,6 +36,7 @@ export type TokenDataI =
|
|
|
36
36
|
}
|
|
37
37
|
| {
|
|
38
38
|
type: TokenType.YEARN_VAULT;
|
|
39
|
+
underlying: SupportedTokens,
|
|
39
40
|
}
|
|
40
41
|
| {
|
|
41
42
|
type: TokenType.META_CURVE_LP;
|
|
@@ -222,19 +223,23 @@ export const supportedTokens: Record<SupportedTokens, TokenDataI> = {
|
|
|
222
223
|
|
|
223
224
|
// YEARN TOKENS
|
|
224
225
|
yvDAI: {
|
|
225
|
-
type: TokenType.YEARN_VAULT
|
|
226
|
+
type: TokenType.YEARN_VAULT,
|
|
227
|
+
underlying: "DAI",
|
|
226
228
|
},
|
|
227
229
|
|
|
228
230
|
yvUSDC: {
|
|
229
|
-
type: TokenType.YEARN_VAULT
|
|
231
|
+
type: TokenType.YEARN_VAULT,
|
|
232
|
+
underlying: "USDC"
|
|
230
233
|
},
|
|
231
234
|
|
|
232
235
|
yvWETH: {
|
|
233
|
-
type: TokenType.YEARN_VAULT
|
|
236
|
+
type: TokenType.YEARN_VAULT,
|
|
237
|
+
underlying: "WETH",
|
|
234
238
|
},
|
|
235
239
|
|
|
236
240
|
yvWBTC: {
|
|
237
|
-
type: TokenType.YEARN_VAULT
|
|
241
|
+
type: TokenType.YEARN_VAULT,
|
|
242
|
+
underlying: "WBTC"
|
|
238
243
|
},
|
|
239
244
|
|
|
240
245
|
// CURVE LP TOKENS
|