@gearbox-protocol/sdk 3.0.0-vfour.278 → 3.0.0-vfour.279
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/dist/cjs/sdk/router/AbstractRouterContract.js +65 -0
- package/dist/cjs/sdk/router/RouterV300Contract.js +92 -126
- package/dist/cjs/sdk/router/RouterV310Contract.js +131 -2
- package/dist/cjs/sdk/router/helpers.js +41 -0
- package/dist/cjs/sdk/router/index.js +12 -1
- package/dist/esm/sdk/router/AbstractRouterContract.js +41 -0
- package/dist/esm/sdk/router/RouterV300Contract.js +86 -118
- package/dist/esm/sdk/router/RouterV310Contract.js +131 -2
- package/dist/esm/sdk/router/helpers.js +15 -0
- package/dist/esm/sdk/router/index.js +5 -0
- package/dist/types/sdk/accounts/CreditAccountsService.d.ts +14 -14
- package/dist/types/sdk/router/AbstractRouterContract.d.ts +17 -0
- package/dist/types/sdk/router/RouterV300Contract.d.ts +6 -83
- package/dist/types/sdk/router/RouterV310Contract.d.ts +41 -2
- package/dist/types/sdk/router/helpers.d.ts +5 -0
- package/dist/types/sdk/router/index.d.ts +2 -1
- package/dist/types/sdk/router/types.d.ts +111 -0
- package/package.json +1 -1
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var AbstractRouterContract_exports = {};
|
|
20
|
+
__export(AbstractRouterContract_exports, {
|
|
21
|
+
AbstractRouterContract: () => AbstractRouterContract
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(AbstractRouterContract_exports);
|
|
24
|
+
var import_base = require("../base/index.js");
|
|
25
|
+
var import_utils = require("../utils/index.js");
|
|
26
|
+
var import_internal = require("../utils/internal/index.js");
|
|
27
|
+
class AbstractRouterContract extends import_base.BaseContract {
|
|
28
|
+
hooks = new import_internal.Hooks();
|
|
29
|
+
addHook = this.hooks.addHook.bind(this.hooks);
|
|
30
|
+
removeHook = this.hooks.removeHook.bind(this.hooks);
|
|
31
|
+
getExpectedAndLeftover(ca, cm, balances) {
|
|
32
|
+
const b = balances || this.getDefaultExpectedAndLeftover(ca);
|
|
33
|
+
const { leftoverBalances, expectedBalances } = b;
|
|
34
|
+
const expected = new import_utils.AddressMap();
|
|
35
|
+
const leftover = new import_utils.AddressMap();
|
|
36
|
+
for (const token of cm.collateralTokens) {
|
|
37
|
+
const actual = expectedBalances.get(token)?.balance || 0n;
|
|
38
|
+
expected.upsert(token, { token, balance: actual > 10n ? actual : 0n });
|
|
39
|
+
leftover.upsert(token, {
|
|
40
|
+
token,
|
|
41
|
+
balance: leftoverBalances.get(token)?.balance || 1n
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
return { expectedBalances: expected, leftoverBalances: leftover };
|
|
45
|
+
}
|
|
46
|
+
getDefaultExpectedAndLeftover(ca) {
|
|
47
|
+
const expectedBalances = new import_utils.AddressMap();
|
|
48
|
+
const leftoverBalances = new import_utils.AddressMap();
|
|
49
|
+
for (const { token: t, balance, mask } of ca.tokens) {
|
|
50
|
+
const token = t;
|
|
51
|
+
const isEnabled = (mask & ca.enabledTokensMask) !== 0n;
|
|
52
|
+
expectedBalances.upsert(token, { token, balance });
|
|
53
|
+
const decimals = this.sdk.tokensMeta.decimals(token);
|
|
54
|
+
const minBalance = 10n ** BigInt(Math.max(8, decimals) - 8);
|
|
55
|
+
if (balance < minBalance || !isEnabled) {
|
|
56
|
+
leftoverBalances.upsert(token, { token, balance });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return { expectedBalances, leftoverBalances };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
63
|
+
0 && (module.exports = {
|
|
64
|
+
AbstractRouterContract
|
|
65
|
+
});
|
|
@@ -18,19 +18,16 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
var RouterV300Contract_exports = {};
|
|
20
20
|
__export(RouterV300Contract_exports, {
|
|
21
|
-
RouterV300Contract: () => RouterV300Contract
|
|
22
|
-
assetsMap: () => assetsMap,
|
|
23
|
-
balancesMap: () => balancesMap
|
|
21
|
+
RouterV300Contract: () => RouterV300Contract
|
|
24
22
|
});
|
|
25
23
|
module.exports = __toCommonJS(RouterV300Contract_exports);
|
|
26
24
|
var import_viem = require("viem");
|
|
27
25
|
var import_routerV300 = require("../../abi/routerV300.js");
|
|
28
26
|
var import_v300 = require("../../abi/v300.js");
|
|
29
|
-
var import_base = require("../base/index.js");
|
|
30
27
|
var import_constants = require("../constants/index.js");
|
|
31
28
|
var import_sdk_gov_legacy = require("../sdk-gov-legacy/index.js");
|
|
32
|
-
var
|
|
33
|
-
var
|
|
29
|
+
var import_AbstractRouterContract = require("./AbstractRouterContract.js");
|
|
30
|
+
var import_helpers = require("./helpers.js");
|
|
34
31
|
var import_PathOptionFactory = require("./PathOptionFactory.js");
|
|
35
32
|
const MAX_GAS_PER_ROUTE = 200000000n;
|
|
36
33
|
const GAS_PER_BLOCK = 400000000n;
|
|
@@ -48,9 +45,8 @@ const PT_IN = {
|
|
|
48
45
|
const OUT = {
|
|
49
46
|
["0x9D39A5DE30e57443BfF2A8307A4256c8797A3497".toLowerCase()]: "sUSDe"
|
|
50
47
|
};
|
|
51
|
-
class RouterV300Contract extends
|
|
48
|
+
class RouterV300Contract extends import_AbstractRouterContract.AbstractRouterContract {
|
|
52
49
|
#connectors;
|
|
53
|
-
#hooks = new import_internal.Hooks();
|
|
54
50
|
constructor(sdk, address) {
|
|
55
51
|
super(sdk, {
|
|
56
52
|
addr: address,
|
|
@@ -59,8 +55,6 @@ class RouterV300Contract extends import_base.BaseContract {
|
|
|
59
55
|
});
|
|
60
56
|
this.#connectors = (0, import_sdk_gov_legacy.getConnectors)(sdk.provider.networkType);
|
|
61
57
|
}
|
|
62
|
-
addHook = this.#hooks.addHook.bind(this.#hooks);
|
|
63
|
-
removeHook = this.#hooks.removeHook.bind(this.#hooks);
|
|
64
58
|
/**
|
|
65
59
|
* Finds all available swaps for NORMAL tokens
|
|
66
60
|
* @param ca
|
|
@@ -133,7 +127,7 @@ class RouterV300Contract extends import_base.BaseContract {
|
|
|
133
127
|
creditManager.collateralTokens
|
|
134
128
|
);
|
|
135
129
|
const isPTOverrideRedeem = PT_IN[tokenIn.toLowerCase()] && OUT[tokenOut.toLowerCase()];
|
|
136
|
-
const { result } = await (isPTOverrideRedeem ? this
|
|
130
|
+
const { result } = await (isPTOverrideRedeem ? this.#overridePTRedeem(props) : this.contract.simulate.findOneTokenPath(
|
|
137
131
|
[
|
|
138
132
|
tokenIn,
|
|
139
133
|
amount,
|
|
@@ -152,74 +146,6 @@ class RouterV300Contract extends import_base.BaseContract {
|
|
|
152
146
|
calls: [...result.calls]
|
|
153
147
|
};
|
|
154
148
|
}
|
|
155
|
-
// TODO: remove me when new router will be added
|
|
156
|
-
async overridePTRedeem({
|
|
157
|
-
creditAccount,
|
|
158
|
-
creditManager,
|
|
159
|
-
tokenIn,
|
|
160
|
-
tokenOut,
|
|
161
|
-
amount,
|
|
162
|
-
slippage
|
|
163
|
-
}) {
|
|
164
|
-
const pendleSwapperAddress = await this.contract.read.componentAddressById([
|
|
165
|
-
37
|
|
166
|
-
]);
|
|
167
|
-
const cm = this.sdk.marketRegister.findCreditManager(creditManager.address);
|
|
168
|
-
const PENDLE_ROUTER_BY_NETWORK = {
|
|
169
|
-
Mainnet: "0x888888888889758F76e7103c6CbF23ABbF58F946",
|
|
170
|
-
Arbitrum: "0x0",
|
|
171
|
-
Optimism: "0x0",
|
|
172
|
-
Base: "0x0",
|
|
173
|
-
Sonic: "0x0"
|
|
174
|
-
};
|
|
175
|
-
const pendleRouter = PENDLE_ROUTER_BY_NETWORK[this.sdk.provider.networkType];
|
|
176
|
-
const pendleAdapter = cm.creditManager.adapters.mustGet(pendleRouter);
|
|
177
|
-
const pendleSwapper = (0, import_viem.getContract)({
|
|
178
|
-
address: pendleSwapperAddress,
|
|
179
|
-
abi: import_routerV300.iSwapperV300Abi,
|
|
180
|
-
client: this.sdk.provider.publicClient
|
|
181
|
-
});
|
|
182
|
-
const result = await pendleSwapper.simulate.getBestDirectPairSwap([
|
|
183
|
-
{
|
|
184
|
-
swapOperation: 1,
|
|
185
|
-
creditAccount: creditAccount.creditAccount,
|
|
186
|
-
tokenIn,
|
|
187
|
-
tokenOut,
|
|
188
|
-
connectors: [],
|
|
189
|
-
amount,
|
|
190
|
-
leftoverAmount: 0n
|
|
191
|
-
},
|
|
192
|
-
pendleAdapter.address
|
|
193
|
-
]);
|
|
194
|
-
const minAmount = result.result.amount * (import_constants.PERCENTAGE_FACTOR - BigInt(slippage)) / import_constants.PERCENTAGE_FACTOR;
|
|
195
|
-
const storeExpectedBalances = {
|
|
196
|
-
target: creditManager.creditFacade,
|
|
197
|
-
callData: (0, import_viem.encodeFunctionData)({
|
|
198
|
-
abi: import_v300.iCreditFacadeV300MulticallAbi,
|
|
199
|
-
functionName: "storeExpectedBalances",
|
|
200
|
-
args: [[{ token: tokenOut, amount: minAmount }]]
|
|
201
|
-
})
|
|
202
|
-
};
|
|
203
|
-
const compareBalances = {
|
|
204
|
-
target: creditManager.creditFacade,
|
|
205
|
-
callData: (0, import_viem.encodeFunctionData)({
|
|
206
|
-
abi: import_v300.iCreditFacadeV300MulticallAbi,
|
|
207
|
-
functionName: "compareBalances",
|
|
208
|
-
args: []
|
|
209
|
-
})
|
|
210
|
-
};
|
|
211
|
-
return {
|
|
212
|
-
result: {
|
|
213
|
-
amount: result.result.amount,
|
|
214
|
-
minAmount,
|
|
215
|
-
calls: [
|
|
216
|
-
storeExpectedBalances,
|
|
217
|
-
result.result.multiCall,
|
|
218
|
-
compareBalances
|
|
219
|
-
]
|
|
220
|
-
}
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
149
|
/**
|
|
224
150
|
* @dev Finds the best path for opening Credit Account and converting all NORMAL tokens and LP token in the way to TARGET
|
|
225
151
|
* @param creditManager CreditManagerData which represents credit manager you want to use to open Credit Account
|
|
@@ -239,8 +165,8 @@ class RouterV300Contract extends import_base.BaseContract {
|
|
|
239
165
|
slippage
|
|
240
166
|
}) {
|
|
241
167
|
const [expectedMap, leftoverMap] = [
|
|
242
|
-
balancesMap(expectedBalances),
|
|
243
|
-
balancesMap(leftoverBalances)
|
|
168
|
+
(0, import_helpers.balancesMap)(expectedBalances),
|
|
169
|
+
(0, import_helpers.balancesMap)(leftoverBalances)
|
|
244
170
|
];
|
|
245
171
|
const input = cm.collateralTokens.map((token) => ({
|
|
246
172
|
token,
|
|
@@ -296,11 +222,11 @@ class RouterV300Contract extends import_base.BaseContract {
|
|
|
296
222
|
ca,
|
|
297
223
|
cm,
|
|
298
224
|
balances ? {
|
|
299
|
-
expectedBalances: assetsMap(balances.expectedBalances),
|
|
300
|
-
leftoverBalances: assetsMap(balances.leftoverBalances)
|
|
225
|
+
expectedBalances: (0, import_helpers.assetsMap)(balances.expectedBalances),
|
|
226
|
+
leftoverBalances: (0, import_helpers.assetsMap)(balances.leftoverBalances)
|
|
301
227
|
} : void 0
|
|
302
228
|
);
|
|
303
|
-
await this
|
|
229
|
+
await this.hooks.triggerHooks("foundPathOptions", {
|
|
304
230
|
creditAccount: ca.creditAccount,
|
|
305
231
|
pathOptions,
|
|
306
232
|
expected,
|
|
@@ -329,7 +255,7 @@ class RouterV300Contract extends import_base.BaseContract {
|
|
|
329
255
|
calls: [...result2.calls]
|
|
330
256
|
});
|
|
331
257
|
}
|
|
332
|
-
const bestResult = results.reduce(compareRouterResults, {
|
|
258
|
+
const bestResult = results.reduce(import_helpers.compareRouterResults, {
|
|
333
259
|
amount: 0n,
|
|
334
260
|
minAmount: 0n,
|
|
335
261
|
calls: []
|
|
@@ -348,65 +274,105 @@ class RouterV300Contract extends import_base.BaseContract {
|
|
|
348
274
|
}
|
|
349
275
|
/**
|
|
350
276
|
* Finds input to be used with findBestClosePath
|
|
277
|
+
* Is used by batch liquidator
|
|
351
278
|
* @param ca
|
|
352
279
|
* @param cm
|
|
353
280
|
* @returns
|
|
354
281
|
*/
|
|
355
282
|
getFindClosePathInput(ca, cm, balances) {
|
|
356
|
-
const
|
|
357
|
-
|
|
283
|
+
const { expectedBalances, leftoverBalances } = this.getExpectedAndLeftover(
|
|
284
|
+
ca,
|
|
285
|
+
cm,
|
|
286
|
+
balances
|
|
287
|
+
);
|
|
358
288
|
const pathOptions = import_PathOptionFactory.PathOptionFactory.generatePathOptions(
|
|
359
289
|
ca.tokens,
|
|
360
290
|
this.provider.networkType,
|
|
361
291
|
LOOPS_PER_TX
|
|
362
292
|
);
|
|
363
|
-
const expected = cm.collateralTokens.map((token) => {
|
|
364
|
-
const actual = expectedBalances.get(token)?.balance || 0n;
|
|
365
|
-
return {
|
|
366
|
-
token,
|
|
367
|
-
balance: actual > 10n ? actual : 0n
|
|
368
|
-
};
|
|
369
|
-
});
|
|
370
|
-
const leftover = cm.collateralTokens.map((token) => ({
|
|
371
|
-
token,
|
|
372
|
-
balance: leftoverBalances.get(token)?.balance || 1n
|
|
373
|
-
}));
|
|
374
293
|
const connectors = this.getAvailableConnectors(cm.collateralTokens);
|
|
375
|
-
return {
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
const token = t;
|
|
382
|
-
const isEnabled = (mask & ca.enabledTokensMask) !== 0n;
|
|
383
|
-
expectedBalances.upsert(token, { token, balance });
|
|
384
|
-
const decimals = this.sdk.tokensMeta.decimals(token);
|
|
385
|
-
const minBalance = 10n ** BigInt(Math.max(8, decimals) - 8);
|
|
386
|
-
if (balance < minBalance || !isEnabled) {
|
|
387
|
-
leftoverBalances.upsert(token, { token, balance });
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
return { expectedBalances, leftoverBalances };
|
|
294
|
+
return {
|
|
295
|
+
expected: expectedBalances.values(),
|
|
296
|
+
leftover: leftoverBalances.values(),
|
|
297
|
+
connectors,
|
|
298
|
+
pathOptions
|
|
299
|
+
};
|
|
391
300
|
}
|
|
392
301
|
getAvailableConnectors(collateralTokens) {
|
|
393
302
|
return collateralTokens.filter(
|
|
394
303
|
(t) => this.#connectors.includes(t.toLowerCase())
|
|
395
304
|
);
|
|
396
305
|
}
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
306
|
+
// TODO: remove me when new router will be added
|
|
307
|
+
async #overridePTRedeem({
|
|
308
|
+
creditAccount,
|
|
309
|
+
creditManager,
|
|
310
|
+
tokenIn,
|
|
311
|
+
tokenOut,
|
|
312
|
+
amount,
|
|
313
|
+
slippage
|
|
314
|
+
}) {
|
|
315
|
+
const pendleSwapperAddress = await this.contract.read.componentAddressById([
|
|
316
|
+
37
|
|
317
|
+
]);
|
|
318
|
+
const cm = this.sdk.marketRegister.findCreditManager(creditManager.address);
|
|
319
|
+
const PENDLE_ROUTER_BY_NETWORK = {
|
|
320
|
+
Mainnet: "0x888888888889758F76e7103c6CbF23ABbF58F946",
|
|
321
|
+
Arbitrum: "0x0",
|
|
322
|
+
Optimism: "0x0",
|
|
323
|
+
Base: "0x0",
|
|
324
|
+
Sonic: "0x0"
|
|
325
|
+
};
|
|
326
|
+
const pendleRouter = PENDLE_ROUTER_BY_NETWORK[this.sdk.provider.networkType];
|
|
327
|
+
const pendleAdapter = cm.creditManager.adapters.mustGet(pendleRouter);
|
|
328
|
+
const pendleSwapper = (0, import_viem.getContract)({
|
|
329
|
+
address: pendleSwapperAddress,
|
|
330
|
+
abi: import_routerV300.iSwapperV300Abi,
|
|
331
|
+
client: this.sdk.provider.publicClient
|
|
332
|
+
});
|
|
333
|
+
const result = await pendleSwapper.simulate.getBestDirectPairSwap([
|
|
334
|
+
{
|
|
335
|
+
swapOperation: 1,
|
|
336
|
+
creditAccount: creditAccount.creditAccount,
|
|
337
|
+
tokenIn,
|
|
338
|
+
tokenOut,
|
|
339
|
+
connectors: [],
|
|
340
|
+
amount,
|
|
341
|
+
leftoverAmount: 0n
|
|
342
|
+
},
|
|
343
|
+
pendleAdapter.address
|
|
344
|
+
]);
|
|
345
|
+
const minAmount = result.result.amount * (import_constants.PERCENTAGE_FACTOR - BigInt(slippage)) / import_constants.PERCENTAGE_FACTOR;
|
|
346
|
+
const storeExpectedBalances = {
|
|
347
|
+
target: creditManager.creditFacade,
|
|
348
|
+
callData: (0, import_viem.encodeFunctionData)({
|
|
349
|
+
abi: import_v300.iCreditFacadeV300MulticallAbi,
|
|
350
|
+
functionName: "storeExpectedBalances",
|
|
351
|
+
args: [[{ token: tokenOut, amount: minAmount }]]
|
|
352
|
+
})
|
|
353
|
+
};
|
|
354
|
+
const compareBalances = {
|
|
355
|
+
target: creditManager.creditFacade,
|
|
356
|
+
callData: (0, import_viem.encodeFunctionData)({
|
|
357
|
+
abi: import_v300.iCreditFacadeV300MulticallAbi,
|
|
358
|
+
functionName: "compareBalances",
|
|
359
|
+
args: []
|
|
360
|
+
})
|
|
361
|
+
};
|
|
362
|
+
return {
|
|
363
|
+
result: {
|
|
364
|
+
amount: result.result.amount,
|
|
365
|
+
minAmount,
|
|
366
|
+
calls: [
|
|
367
|
+
storeExpectedBalances,
|
|
368
|
+
result.result.multiCall,
|
|
369
|
+
compareBalances
|
|
370
|
+
]
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
}
|
|
406
374
|
}
|
|
407
375
|
// Annotate the CommonJS export names for ESM import in node:
|
|
408
376
|
0 && (module.exports = {
|
|
409
|
-
RouterV300Contract
|
|
410
|
-
assetsMap,
|
|
411
|
-
balancesMap
|
|
377
|
+
RouterV300Contract
|
|
412
378
|
});
|
|
@@ -22,9 +22,11 @@ __export(RouterV310Contract_exports, {
|
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(RouterV310Contract_exports);
|
|
24
24
|
var import_routerV310 = require("../../abi/routerV310.js");
|
|
25
|
-
var
|
|
25
|
+
var import_AbstractRouterContract = require("./AbstractRouterContract.js");
|
|
26
|
+
var import_helpers = require("./helpers.js");
|
|
26
27
|
const abi = import_routerV310.iGearboxRouterV310Abi;
|
|
27
|
-
|
|
28
|
+
const ERR_NOT_IMPLEMENTED = new Error("Not implemented in router v3.1");
|
|
29
|
+
class RouterV310Contract extends import_AbstractRouterContract.AbstractRouterContract {
|
|
28
30
|
constructor(sdk, address) {
|
|
29
31
|
super(sdk, {
|
|
30
32
|
addr: address,
|
|
@@ -32,6 +34,133 @@ class RouterV310Contract extends import_base.BaseContract {
|
|
|
32
34
|
abi
|
|
33
35
|
});
|
|
34
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Finds best path to swap all Normal tokens and tokens "on the way" to target one and vice versa
|
|
39
|
+
* @param creditAccount
|
|
40
|
+
* @param creditManager
|
|
41
|
+
* @param tokenIn
|
|
42
|
+
* @param tokenOut
|
|
43
|
+
* @param amount
|
|
44
|
+
* @param slippage
|
|
45
|
+
* @returns
|
|
46
|
+
*/
|
|
47
|
+
async findOneTokenPath(props) {
|
|
48
|
+
const { result } = await this.contract.simulate.routeOneToOne([
|
|
49
|
+
props.creditAccount.creditAccount,
|
|
50
|
+
props.tokenIn,
|
|
51
|
+
props.amount,
|
|
52
|
+
props.tokenOut,
|
|
53
|
+
BigInt(props.slippage),
|
|
54
|
+
4n
|
|
55
|
+
// TODO:? how many 4n or 0n for underlying
|
|
56
|
+
]);
|
|
57
|
+
return {
|
|
58
|
+
amount: result.amount,
|
|
59
|
+
minAmount: result.minAmount,
|
|
60
|
+
calls: [...result.calls]
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* @dev Finds the best path for opening Credit Account and converting all NORMAL tokens and LP token in the way to TARGET
|
|
65
|
+
* @param creditManager CreditManagerData which represents credit manager you want to use to open Credit Account
|
|
66
|
+
* @param expectedBalances Expected balances which would be on account accounting also debt. For example,
|
|
67
|
+
* if you open an USDC Credit Account, borrow 50_000 USDC and provide 10 WETH and 10_USDC as collateral
|
|
68
|
+
* from your own funds, expectedBalances should be: { "USDC": 60_000 * (10**6), "<address of WETH>": WAD.mul(10) }
|
|
69
|
+
* @param leftoverBalances Balances to keep on account after opening
|
|
70
|
+
* @param target Address of symbol of desired token
|
|
71
|
+
* @param slippage Slippage in PERCENTAGE_FORMAT (100% = 10_000) per operation
|
|
72
|
+
* @returns PathFinderOpenStrategyResult which
|
|
73
|
+
*/
|
|
74
|
+
async findOpenStrategyPath(props) {
|
|
75
|
+
const {
|
|
76
|
+
creditManager: cm,
|
|
77
|
+
expectedBalances,
|
|
78
|
+
leftoverBalances,
|
|
79
|
+
target,
|
|
80
|
+
slippage
|
|
81
|
+
} = props;
|
|
82
|
+
const [expectedMap, leftoverMap] = [
|
|
83
|
+
(0, import_helpers.balancesMap)(expectedBalances),
|
|
84
|
+
(0, import_helpers.balancesMap)(leftoverBalances)
|
|
85
|
+
];
|
|
86
|
+
const tData = cm.collateralTokens.map(
|
|
87
|
+
(token) => ({
|
|
88
|
+
token,
|
|
89
|
+
balance: expectedMap.get(token) ?? 0n,
|
|
90
|
+
leftoverBalance: leftoverMap.get(token) ?? 0n,
|
|
91
|
+
numSplits: 4n
|
|
92
|
+
// TODO:? how many 4n or 0n for underlying
|
|
93
|
+
})
|
|
94
|
+
);
|
|
95
|
+
const { result } = await this.contract.simulate.routeOpenManyToOne([
|
|
96
|
+
cm.address,
|
|
97
|
+
target,
|
|
98
|
+
BigInt(slippage),
|
|
99
|
+
tData
|
|
100
|
+
]);
|
|
101
|
+
return {
|
|
102
|
+
balances: {},
|
|
103
|
+
// TODO:?
|
|
104
|
+
minBalances: {},
|
|
105
|
+
// TODO:?
|
|
106
|
+
amount: result.amount,
|
|
107
|
+
minAmount: result.minAmount,
|
|
108
|
+
calls: [...result.calls]
|
|
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 CreditAccountStruct object used for close path computation
|
|
115
|
+
* @param creditManager CreditManagerSlice for corresponding credit manager
|
|
116
|
+
* @param slippage Slippage in PERCENTAGE_FORMAT (100% = 10_000) per operation
|
|
117
|
+
* @return The best option in PathFinderCloseResult format, which
|
|
118
|
+
* - underlyingBalance - total balance of underlying token
|
|
119
|
+
* - calls - list of calls which should be done to swap & unwrap everything to underlying token
|
|
120
|
+
*/
|
|
121
|
+
async findBestClosePath(props) {
|
|
122
|
+
const { creditAccount: ca, creditManager: cm, slippage, balances } = props;
|
|
123
|
+
const { expectedBalances, leftoverBalances } = this.getExpectedAndLeftover(
|
|
124
|
+
ca,
|
|
125
|
+
cm,
|
|
126
|
+
balances ? {
|
|
127
|
+
expectedBalances: (0, import_helpers.assetsMap)(balances.expectedBalances),
|
|
128
|
+
leftoverBalances: (0, import_helpers.assetsMap)(balances.leftoverBalances)
|
|
129
|
+
} : void 0
|
|
130
|
+
);
|
|
131
|
+
const tData = [];
|
|
132
|
+
for (const token of cm.collateralTokens) {
|
|
133
|
+
tData.push({
|
|
134
|
+
token,
|
|
135
|
+
balance: expectedBalances.get(token)?.balance || 0n,
|
|
136
|
+
leftoverBalance: leftoverBalances.get(token)?.balance || 0n,
|
|
137
|
+
numSplits: 4n
|
|
138
|
+
// TODO:? how many 4n or 0n for underlying
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
const { result } = await this.contract.simulate.routeManyToOne([
|
|
142
|
+
cm.address,
|
|
143
|
+
ca.underlying,
|
|
144
|
+
BigInt(slippage),
|
|
145
|
+
tData
|
|
146
|
+
]);
|
|
147
|
+
return {
|
|
148
|
+
underlyingBalance: 0n,
|
|
149
|
+
// TODO:?
|
|
150
|
+
amount: result.amount,
|
|
151
|
+
minAmount: result.minAmount,
|
|
152
|
+
calls: [...result.calls]
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
findAllSwaps(props) {
|
|
156
|
+
throw ERR_NOT_IMPLEMENTED;
|
|
157
|
+
}
|
|
158
|
+
getAvailableConnectors(collateralTokens) {
|
|
159
|
+
throw ERR_NOT_IMPLEMENTED;
|
|
160
|
+
}
|
|
161
|
+
getFindClosePathInput(ca, cm, balances) {
|
|
162
|
+
throw ERR_NOT_IMPLEMENTED;
|
|
163
|
+
}
|
|
35
164
|
}
|
|
36
165
|
// Annotate the CommonJS export names for ESM import in node:
|
|
37
166
|
0 && (module.exports = {
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var helpers_exports = {};
|
|
20
|
+
__export(helpers_exports, {
|
|
21
|
+
assetsMap: () => assetsMap,
|
|
22
|
+
balancesMap: () => balancesMap,
|
|
23
|
+
compareRouterResults: () => compareRouterResults
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(helpers_exports);
|
|
26
|
+
var import_utils = require("../utils/index.js");
|
|
27
|
+
function balancesMap(assets) {
|
|
28
|
+
return new import_utils.AddressMap(assets.map(({ token, balance }) => [token, balance]));
|
|
29
|
+
}
|
|
30
|
+
function compareRouterResults(a, b) {
|
|
31
|
+
return a.amount > b.amount ? a : b;
|
|
32
|
+
}
|
|
33
|
+
function assetsMap(assets) {
|
|
34
|
+
return new import_utils.AddressMap(assets.map((a) => [a.token, a]));
|
|
35
|
+
}
|
|
36
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
37
|
+
0 && (module.exports = {
|
|
38
|
+
assetsMap,
|
|
39
|
+
balancesMap,
|
|
40
|
+
compareRouterResults
|
|
41
|
+
});
|
|
@@ -3,6 +3,10 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
6
10
|
var __copyProps = (to, from, except, desc) => {
|
|
7
11
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
12
|
for (let key of __getOwnPropNames(from))
|
|
@@ -14,13 +18,20 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
14
18
|
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
19
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
20
|
var router_exports = {};
|
|
21
|
+
__export(router_exports, {
|
|
22
|
+
assetsMap: () => import_helpers.assetsMap
|
|
23
|
+
});
|
|
17
24
|
module.exports = __toCommonJS(router_exports);
|
|
18
25
|
__reExport(router_exports, require("./createRouter.js"), module.exports);
|
|
26
|
+
var import_helpers = require("./helpers.js");
|
|
19
27
|
__reExport(router_exports, require("./RouterV300Contract.js"), module.exports);
|
|
20
28
|
__reExport(router_exports, require("./RouterV310Contract.js"), module.exports);
|
|
29
|
+
__reExport(router_exports, require("./types.js"), module.exports);
|
|
21
30
|
// Annotate the CommonJS export names for ESM import in node:
|
|
22
31
|
0 && (module.exports = {
|
|
32
|
+
assetsMap,
|
|
23
33
|
...require("./createRouter.js"),
|
|
24
34
|
...require("./RouterV300Contract.js"),
|
|
25
|
-
...require("./RouterV310Contract.js")
|
|
35
|
+
...require("./RouterV310Contract.js"),
|
|
36
|
+
...require("./types.js")
|
|
26
37
|
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { BaseContract } from "../base/index.js";
|
|
2
|
+
import { AddressMap } from "../utils/index.js";
|
|
3
|
+
import { Hooks } from "../utils/internal/index.js";
|
|
4
|
+
class AbstractRouterContract extends BaseContract {
|
|
5
|
+
hooks = new Hooks();
|
|
6
|
+
addHook = this.hooks.addHook.bind(this.hooks);
|
|
7
|
+
removeHook = this.hooks.removeHook.bind(this.hooks);
|
|
8
|
+
getExpectedAndLeftover(ca, cm, balances) {
|
|
9
|
+
const b = balances || this.getDefaultExpectedAndLeftover(ca);
|
|
10
|
+
const { leftoverBalances, expectedBalances } = b;
|
|
11
|
+
const expected = new AddressMap();
|
|
12
|
+
const leftover = new AddressMap();
|
|
13
|
+
for (const token of cm.collateralTokens) {
|
|
14
|
+
const actual = expectedBalances.get(token)?.balance || 0n;
|
|
15
|
+
expected.upsert(token, { token, balance: actual > 10n ? actual : 0n });
|
|
16
|
+
leftover.upsert(token, {
|
|
17
|
+
token,
|
|
18
|
+
balance: leftoverBalances.get(token)?.balance || 1n
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
return { expectedBalances: expected, leftoverBalances: leftover };
|
|
22
|
+
}
|
|
23
|
+
getDefaultExpectedAndLeftover(ca) {
|
|
24
|
+
const expectedBalances = new AddressMap();
|
|
25
|
+
const leftoverBalances = new AddressMap();
|
|
26
|
+
for (const { token: t, balance, mask } of ca.tokens) {
|
|
27
|
+
const token = t;
|
|
28
|
+
const isEnabled = (mask & ca.enabledTokensMask) !== 0n;
|
|
29
|
+
expectedBalances.upsert(token, { token, balance });
|
|
30
|
+
const decimals = this.sdk.tokensMeta.decimals(token);
|
|
31
|
+
const minBalance = 10n ** BigInt(Math.max(8, decimals) - 8);
|
|
32
|
+
if (balance < minBalance || !isEnabled) {
|
|
33
|
+
leftoverBalances.upsert(token, { token, balance });
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return { expectedBalances, leftoverBalances };
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export {
|
|
40
|
+
AbstractRouterContract
|
|
41
|
+
};
|