@gearbox-protocol/sdk 8.3.2 → 8.4.0
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/abi/routerV310.js +31 -0
- package/dist/cjs/sdk/accounts/{CreditAccountsService.js → AbstractCreditAccountsService.js} +34 -407
- package/dist/cjs/sdk/accounts/CreditAccountsServiceV300.js +363 -0
- package/dist/cjs/sdk/accounts/CreditAccountsServiceV310.js +200 -0
- package/dist/cjs/sdk/accounts/constants.js +33 -0
- package/dist/cjs/sdk/accounts/createCreditAccountService.js +39 -0
- package/dist/cjs/sdk/accounts/index.js +4 -2
- package/dist/cjs/sdk/router/RouterV300Contract.js +8 -0
- package/dist/cjs/sdk/router/RouterV310Contract.js +24 -0
- package/dist/esm/abi/routerV310.js +31 -0
- package/dist/esm/sdk/accounts/{CreditAccountsService.js → AbstractCreditAccountsService.js} +31 -417
- package/dist/esm/sdk/accounts/CreditAccountsServiceV300.js +351 -0
- package/dist/esm/sdk/accounts/CreditAccountsServiceV310.js +176 -0
- package/dist/esm/sdk/accounts/constants.js +9 -0
- package/dist/esm/sdk/accounts/createCreditAccountService.js +15 -0
- package/dist/esm/sdk/accounts/index.js +2 -1
- package/dist/esm/sdk/router/RouterV300Contract.js +8 -0
- package/dist/esm/sdk/router/RouterV310Contract.js +25 -1
- package/dist/types/abi/routerV310.d.ts +48 -0
- package/dist/types/dev/AccountOpener.d.ts +2 -2
- package/dist/types/sdk/accounts/{CreditAccountsService.d.ts → AbstractCreditAccountsService.d.ts} +12 -67
- package/dist/types/sdk/accounts/CreditAccountsServiceV300.d.ts +25 -0
- package/dist/types/sdk/accounts/CreditAccountsServiceV310.d.ts +24 -0
- package/dist/types/sdk/accounts/constants.d.ts +2 -0
- package/dist/types/sdk/accounts/createCreditAccountService.d.ts +8 -0
- package/dist/types/sdk/accounts/index.d.ts +2 -1
- package/dist/types/sdk/accounts/types.d.ts +130 -1
- package/dist/types/sdk/router/RouterV300Contract.d.ts +5 -1
- package/dist/types/sdk/router/RouterV310Contract.d.ts +53 -1
- package/dist/types/sdk/router/types.d.ts +22 -0
- package/package.json +1 -1
|
@@ -57,6 +57,37 @@ const iGearboxRouterV310Abi = [
|
|
|
57
57
|
outputs: [{ name: "", type: "bytes32[]", internalType: "bytes32[]" }],
|
|
58
58
|
stateMutability: "view"
|
|
59
59
|
},
|
|
60
|
+
{
|
|
61
|
+
type: "function",
|
|
62
|
+
name: "processClaims",
|
|
63
|
+
inputs: [
|
|
64
|
+
{ name: "creditAccount", type: "address", internalType: "address" },
|
|
65
|
+
{
|
|
66
|
+
name: "tData",
|
|
67
|
+
type: "tuple[]",
|
|
68
|
+
internalType: "struct TokenData[]",
|
|
69
|
+
components: [
|
|
70
|
+
{ name: "token", type: "address", internalType: "address" },
|
|
71
|
+
{ name: "balance", type: "uint256", internalType: "uint256" },
|
|
72
|
+
{ name: "leftoverBalance", type: "uint256", internalType: "uint256" },
|
|
73
|
+
{ name: "numSplits", type: "uint256", internalType: "uint256" },
|
|
74
|
+
{ name: "claimRewards", type: "bool", internalType: "bool" }
|
|
75
|
+
]
|
|
76
|
+
}
|
|
77
|
+
],
|
|
78
|
+
outputs: [
|
|
79
|
+
{
|
|
80
|
+
name: "calls",
|
|
81
|
+
type: "tuple[]",
|
|
82
|
+
internalType: "struct MultiCall[]",
|
|
83
|
+
components: [
|
|
84
|
+
{ name: "target", type: "address", internalType: "address" },
|
|
85
|
+
{ name: "callData", type: "bytes", internalType: "bytes" }
|
|
86
|
+
]
|
|
87
|
+
}
|
|
88
|
+
],
|
|
89
|
+
stateMutability: "nonpayable"
|
|
90
|
+
},
|
|
60
91
|
{
|
|
61
92
|
type: "function",
|
|
62
93
|
name: "routeManyToOne",
|
|
@@ -16,11 +16,11 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var
|
|
20
|
-
__export(
|
|
21
|
-
|
|
19
|
+
var AbstractCreditAccountsService_exports = {};
|
|
20
|
+
__export(AbstractCreditAccountsService_exports, {
|
|
21
|
+
AbstractCreditAccountService: () => AbstractCreditAccountService
|
|
22
22
|
});
|
|
23
|
-
module.exports = __toCommonJS(
|
|
23
|
+
module.exports = __toCommonJS(AbstractCreditAccountsService_exports);
|
|
24
24
|
var import_viem = require("viem");
|
|
25
25
|
var import_compressors = require("../../abi/compressors.js");
|
|
26
26
|
var import_iBaseRewardPool = require("../../abi/iBaseRewardPool.js");
|
|
@@ -28,11 +28,10 @@ var import_v300 = require("../../abi/v300.js");
|
|
|
28
28
|
var import_base = require("../base/index.js");
|
|
29
29
|
var import_constants = require("../constants/index.js");
|
|
30
30
|
var import_router = require("../router/index.js");
|
|
31
|
-
var import_sdk_gov_legacy = require("../sdk-gov-legacy/index.js");
|
|
32
31
|
var import_utils = require("../utils/index.js");
|
|
33
32
|
var import_viem2 = require("../utils/viem/index.js");
|
|
34
33
|
var import_utils2 = require("./utils.js");
|
|
35
|
-
class
|
|
34
|
+
class AbstractCreditAccountService extends import_base.SDKConstruct {
|
|
36
35
|
#compressor;
|
|
37
36
|
#batchSize;
|
|
38
37
|
#logger;
|
|
@@ -158,17 +157,17 @@ class CreditAccountsService extends import_base.SDKConstruct {
|
|
|
158
157
|
functionName: "getRewards",
|
|
159
158
|
args: [creditAccount]
|
|
160
159
|
});
|
|
160
|
+
const callData = (0, import_viem.encodeFunctionData)({
|
|
161
|
+
abi: import_iBaseRewardPool.iBaseRewardPoolAbi,
|
|
162
|
+
functionName: "getReward",
|
|
163
|
+
args: []
|
|
164
|
+
});
|
|
161
165
|
const r = rewards.reduce((acc, r2) => {
|
|
162
166
|
const adapter = r2.adapter.toLowerCase();
|
|
163
167
|
const stakedPhantomToken = r2.stakedPhantomToken.toLowerCase();
|
|
164
168
|
const rewardToken = r2.rewardToken.toLowerCase();
|
|
165
169
|
const key = [adapter, stakedPhantomToken].join("-");
|
|
166
170
|
if (!acc[key]) {
|
|
167
|
-
const callData = (0, import_viem.encodeFunctionData)({
|
|
168
|
-
abi: import_iBaseRewardPool.iBaseRewardPoolAbi,
|
|
169
|
-
functionName: "getReward",
|
|
170
|
-
args: []
|
|
171
|
-
});
|
|
172
171
|
acc[key] = {
|
|
173
172
|
adapter,
|
|
174
173
|
stakedPhantomToken,
|
|
@@ -279,120 +278,16 @@ class CreditAccountsService extends import_base.SDKConstruct {
|
|
|
279
278
|
const calls = [
|
|
280
279
|
...operation === "close" ? [] : priceUpdates,
|
|
281
280
|
...routerCloseResult.calls,
|
|
282
|
-
...this
|
|
283
|
-
...this
|
|
284
|
-
...this
|
|
281
|
+
...this.prepareDisableQuotas(ca),
|
|
282
|
+
...this.prepareDecreaseDebt(ca),
|
|
283
|
+
...this.prepareDisableTokens(ca),
|
|
285
284
|
...assetsToWithdraw.map(
|
|
286
|
-
(t) => this
|
|
285
|
+
(t) => this.prepareWithdrawToken(ca.creditFacade, t, import_constants.MAX_UINT256, to)
|
|
287
286
|
)
|
|
288
287
|
];
|
|
289
288
|
const tx = operation === "close" ? cm.creditFacade.closeCreditAccount(ca.creditAccount, calls) : cm.creditFacade.multicall(ca.creditAccount, calls);
|
|
290
289
|
return { tx, calls, routerCloseResult, creditFacade: cm.creditFacade };
|
|
291
290
|
}
|
|
292
|
-
/**
|
|
293
|
-
* Fully repays credit account or repays credit account and keeps it open with zero debt
|
|
294
|
-
- Repays in the following order: price update -> add collateral to cover the debt ->
|
|
295
|
-
-> disable quotas for all tokens -> decrease debt -> disable tokens all tokens -> withdraw all tokens
|
|
296
|
-
* @param {CloseOptions} operation - {@link CloseOptions}: close or zeroDebt
|
|
297
|
-
* @param {RouterCASlice} creditAccount - minimal credit account data {@link RouterCASlice} on which operation is performed on which operation is performed
|
|
298
|
-
* @param {Array<Address>} collateralAssets - tokens to repay dept.
|
|
299
|
-
In the current implementation, this is the (debt+interest+fess) * buffer,
|
|
300
|
-
where buffer refers to amount of tokens which will exceed current debt
|
|
301
|
-
in order to cover possible debt increase over tx execution
|
|
302
|
-
* @param {Array<Asset>} assetsToWithdraw - tokens to withdraw from credit account.
|
|
303
|
-
Typically all non zero ca assets (including unclaimed rewards)
|
|
304
|
-
plus underlying token (to withdraw any exceeding underlying token after repay)
|
|
305
|
-
* @param {Record<Address, PermitResult>} permits - permits of tokens to withdraw (in any permittable token is present) {@link PermitResult}
|
|
306
|
-
* @param {Address} to - Wallet address to withdraw underlying to
|
|
307
|
-
* @returns All necessary data to execute the transaction (call, credit facade)
|
|
308
|
-
*/
|
|
309
|
-
async repayCreditAccount({
|
|
310
|
-
operation,
|
|
311
|
-
collateralAssets,
|
|
312
|
-
assetsToWithdraw: wrapped,
|
|
313
|
-
creditAccount: ca,
|
|
314
|
-
permits,
|
|
315
|
-
to
|
|
316
|
-
}) {
|
|
317
|
-
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
318
|
-
const addCollateral = collateralAssets.filter((a) => a.balance > 0);
|
|
319
|
-
const priceUpdates = await this.getPriceUpdatesForFacade(
|
|
320
|
-
ca.creditManager,
|
|
321
|
-
ca,
|
|
322
|
-
void 0
|
|
323
|
-
);
|
|
324
|
-
const { unwrapCalls, assetsToWithdraw } = this.#prepareUnwrapAndWithdrawCallsV3(
|
|
325
|
-
wrapped,
|
|
326
|
-
true,
|
|
327
|
-
true,
|
|
328
|
-
ca.creditManager
|
|
329
|
-
);
|
|
330
|
-
const calls = [
|
|
331
|
-
...operation === "close" ? [] : priceUpdates,
|
|
332
|
-
...this.#prepareAddCollateral(ca.creditFacade, addCollateral, permits),
|
|
333
|
-
...this.#prepareDisableQuotas(ca),
|
|
334
|
-
...this.#prepareDecreaseDebt(ca),
|
|
335
|
-
...unwrapCalls,
|
|
336
|
-
...this.#prepareDisableTokens(ca),
|
|
337
|
-
// TODO: probably needs a better way to handle reward tokens
|
|
338
|
-
...assetsToWithdraw.map(
|
|
339
|
-
(t) => this.#prepareWithdrawToken(ca.creditFacade, t.token, import_constants.MAX_UINT256, to)
|
|
340
|
-
)
|
|
341
|
-
];
|
|
342
|
-
const tx = operation === "close" ? cm.creditFacade.closeCreditAccount(ca.creditAccount, calls) : cm.creditFacade.multicall(ca.creditAccount, calls);
|
|
343
|
-
return { tx, calls, creditFacade: cm.creditFacade };
|
|
344
|
-
}
|
|
345
|
-
/**
|
|
346
|
-
* Fully repays liquidatable account
|
|
347
|
-
- Repay and liquidate is executed in the following order: price update -> add collateral to cover the debt ->
|
|
348
|
-
withdraw all tokens from credit account
|
|
349
|
-
* @param {RouterCASlice} creditAccount - minimal credit account data {@link RouterCASlice} on which operation is performed
|
|
350
|
-
* @param {Array<Address>} collateralAssets - tokens to repay dept.
|
|
351
|
-
In the current implementation, this is the (debt+interest+fess) * buffer,
|
|
352
|
-
where buffer refers to amount of tokens which will exceed current debt
|
|
353
|
-
in order to cover possible debt increase over tx execution
|
|
354
|
-
* @param {Array<Address>} assetsToWithdraw - tokens to withdraw from credit account.
|
|
355
|
-
Typically all non zero ca assets (including unclaimed rewards)
|
|
356
|
-
plus underlying token (to withdraw any exceeding underlying token after repay)
|
|
357
|
-
* @param {Record<Address, PermitResult>} permits - permits of tokens to withdraw (in any permittable token is present) {@link PermitResult}
|
|
358
|
-
* @param {Address} to - Wallet address to withdraw underlying to
|
|
359
|
-
* @returns All necessary data to execute the transaction (call, credit facade)
|
|
360
|
-
*/
|
|
361
|
-
async repayAndLiquidateCreditAccount({
|
|
362
|
-
collateralAssets,
|
|
363
|
-
assetsToWithdraw: wrapped,
|
|
364
|
-
creditAccount: ca,
|
|
365
|
-
permits,
|
|
366
|
-
to
|
|
367
|
-
}) {
|
|
368
|
-
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
369
|
-
const priceUpdates = await this.getPriceUpdatesForFacade(
|
|
370
|
-
ca.creditManager,
|
|
371
|
-
ca,
|
|
372
|
-
void 0
|
|
373
|
-
);
|
|
374
|
-
const addCollateral = collateralAssets.filter((a) => a.balance > 0);
|
|
375
|
-
const { unwrapCalls, assetsToWithdraw } = this.#prepareUnwrapAndWithdrawCallsV3(
|
|
376
|
-
wrapped,
|
|
377
|
-
true,
|
|
378
|
-
true,
|
|
379
|
-
ca.creditManager
|
|
380
|
-
);
|
|
381
|
-
const calls = [
|
|
382
|
-
...priceUpdates,
|
|
383
|
-
...this.#prepareAddCollateral(ca.creditFacade, addCollateral, permits),
|
|
384
|
-
...unwrapCalls,
|
|
385
|
-
...assetsToWithdraw.map(
|
|
386
|
-
(t) => this.#prepareWithdrawToken(ca.creditFacade, t.token, import_constants.MAX_UINT256, to)
|
|
387
|
-
)
|
|
388
|
-
];
|
|
389
|
-
const tx = cm.creditFacade.liquidateCreditAccount(
|
|
390
|
-
ca.creditAccount,
|
|
391
|
-
to,
|
|
392
|
-
calls
|
|
393
|
-
);
|
|
394
|
-
return { tx, calls, creditFacade: cm.creditFacade };
|
|
395
|
-
}
|
|
396
291
|
/**
|
|
397
292
|
* Updates quota of credit account.
|
|
398
293
|
- CA quota updated in the following order: price update -> update quotas
|
|
@@ -416,7 +311,7 @@ class CreditAccountsService extends import_base.SDKConstruct {
|
|
|
416
311
|
);
|
|
417
312
|
const calls = [
|
|
418
313
|
...priceUpdates,
|
|
419
|
-
...this
|
|
314
|
+
...this.prepareUpdateQuotas(creditAccount.creditFacade, {
|
|
420
315
|
minQuota,
|
|
421
316
|
averageQuota
|
|
422
317
|
})
|
|
@@ -453,12 +348,12 @@ class CreditAccountsService extends import_base.SDKConstruct {
|
|
|
453
348
|
);
|
|
454
349
|
const calls = [
|
|
455
350
|
...priceUpdatesCalls,
|
|
456
|
-
...this
|
|
351
|
+
...this.prepareAddCollateral(
|
|
457
352
|
creditAccount.creditFacade,
|
|
458
353
|
[asset],
|
|
459
354
|
permit ? { [asset.token]: permit } : {}
|
|
460
355
|
),
|
|
461
|
-
...this
|
|
356
|
+
...this.prepareUpdateQuotas(creditAccount.creditFacade, {
|
|
462
357
|
minQuota,
|
|
463
358
|
averageQuota
|
|
464
359
|
})
|
|
@@ -506,57 +401,6 @@ class CreditAccountsService extends import_base.SDKConstruct {
|
|
|
506
401
|
const tx = cm.creditFacade.multicall(creditAccount.creditAccount, calls);
|
|
507
402
|
return { tx, calls, creditFacade: cm.creditFacade };
|
|
508
403
|
}
|
|
509
|
-
/**
|
|
510
|
-
* Withdraws a single collateral from credit account to wallet to and updates quotas;
|
|
511
|
-
technically can withdraw several tokens at once
|
|
512
|
-
- Collateral is withdrawn in the following order: price update -> withdraw token -> update quotas for affected tokens
|
|
513
|
-
* @param {RouterCASlice} creditAccount - minimal credit account data {@link RouterCASlice} on which operation is performed
|
|
514
|
-
* @param {Array<Asset>} averageQuota - average quota for desired token {@link Asset}
|
|
515
|
-
* @param {Array<Asset>} minQuota - minimum quota for desired token {@link Asset}
|
|
516
|
-
* @param {Address} to - Wallet address to withdraw token to
|
|
517
|
-
* @param {Array<Asset>} assetsToWithdraw - permits for asset if it is permittable {@link PermitResult}
|
|
518
|
-
* @returns All necessary data to execute the transaction (call, credit facade)
|
|
519
|
-
*/
|
|
520
|
-
async withdrawCollateral({
|
|
521
|
-
creditAccount,
|
|
522
|
-
assetsToWithdraw: wrapped,
|
|
523
|
-
to,
|
|
524
|
-
minQuota,
|
|
525
|
-
averageQuota
|
|
526
|
-
}) {
|
|
527
|
-
const cm = this.sdk.marketRegister.findCreditManager(
|
|
528
|
-
creditAccount.creditManager
|
|
529
|
-
);
|
|
530
|
-
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
531
|
-
creditAccount.creditManager,
|
|
532
|
-
creditAccount,
|
|
533
|
-
void 0
|
|
534
|
-
);
|
|
535
|
-
const { unwrapCalls, assetsToWithdraw } = this.#prepareUnwrapAndWithdrawCallsV3(
|
|
536
|
-
wrapped,
|
|
537
|
-
false,
|
|
538
|
-
false,
|
|
539
|
-
creditAccount.creditManager
|
|
540
|
-
);
|
|
541
|
-
const calls = [
|
|
542
|
-
...priceUpdatesCalls,
|
|
543
|
-
...unwrapCalls,
|
|
544
|
-
...assetsToWithdraw.map(
|
|
545
|
-
(a) => this.#prepareWithdrawToken(
|
|
546
|
-
creditAccount.creditFacade,
|
|
547
|
-
a.token,
|
|
548
|
-
a.balance,
|
|
549
|
-
to
|
|
550
|
-
)
|
|
551
|
-
),
|
|
552
|
-
...this.#prepareUpdateQuotas(creditAccount.creditFacade, {
|
|
553
|
-
minQuota,
|
|
554
|
-
averageQuota
|
|
555
|
-
})
|
|
556
|
-
];
|
|
557
|
-
const tx = cm.creditFacade.multicall(creditAccount.creditAccount, calls);
|
|
558
|
-
return { tx, calls, creditFacade: cm.creditFacade };
|
|
559
|
-
}
|
|
560
404
|
/**
|
|
561
405
|
* Executes swap specified by given calls, update quotas of affected tokens
|
|
562
406
|
- Swap is executed in the following order: price update -> execute swap path -> update quotas
|
|
@@ -584,7 +428,7 @@ class CreditAccountsService extends import_base.SDKConstruct {
|
|
|
584
428
|
const calls = [
|
|
585
429
|
...priceUpdatesCalls,
|
|
586
430
|
...swapCalls,
|
|
587
|
-
...this
|
|
431
|
+
...this.prepareUpdateQuotas(creditAccount.creditFacade, {
|
|
588
432
|
minQuota,
|
|
589
433
|
averageQuota
|
|
590
434
|
})
|
|
@@ -592,45 +436,6 @@ class CreditAccountsService extends import_base.SDKConstruct {
|
|
|
592
436
|
const tx = cm.creditFacade.multicall(creditAccount.creditAccount, calls);
|
|
593
437
|
return { tx, calls, creditFacade: cm.creditFacade };
|
|
594
438
|
}
|
|
595
|
-
/**
|
|
596
|
-
* Executes swap specified by given calls, update quotas of affected tokens
|
|
597
|
-
- Claim rewards is executed in the following order: price update -> execute claim calls ->
|
|
598
|
-
-> (optionally: disable reward tokens) -> (optionally: update quotas)
|
|
599
|
-
* @param {RouterCASlice} creditAccount - minimal credit account data {@link RouterCASlice} on which operation is performed
|
|
600
|
-
* @param {Array<Asset>} averageQuota - average quota for desired token;
|
|
601
|
-
in this case can be omitted since rewards tokens do not require quotas {@link Asset}
|
|
602
|
-
* @param {Array<Asset>} minQuota - minimum quota for desired token;
|
|
603
|
-
in this case can be omitted since rewards tokens do not require quotas {@link Asset}
|
|
604
|
-
* @param {Array<MultiCall>} calls - array of MultiCall from getRewards {@link MultiCall}
|
|
605
|
-
* @param {Array<Asset>} tokensToDisable - tokens to disable after rewards claiming;
|
|
606
|
-
sometimes is needed since old credit facade used to enable tokens on claim {@link Asset}
|
|
607
|
-
* @returns All necessary data to execute the transaction (call, credit facade)
|
|
608
|
-
*/
|
|
609
|
-
async claimFarmRewards({
|
|
610
|
-
tokensToDisable,
|
|
611
|
-
calls: claimCalls,
|
|
612
|
-
creditAccount: ca,
|
|
613
|
-
minQuota,
|
|
614
|
-
averageQuota
|
|
615
|
-
}) {
|
|
616
|
-
if (claimCalls.length === 0) throw new Error("No path to execute");
|
|
617
|
-
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
618
|
-
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
619
|
-
ca.creditManager,
|
|
620
|
-
ca,
|
|
621
|
-
averageQuota
|
|
622
|
-
);
|
|
623
|
-
const calls = [
|
|
624
|
-
...priceUpdatesCalls,
|
|
625
|
-
...claimCalls,
|
|
626
|
-
...tokensToDisable.map(
|
|
627
|
-
(a) => this.#prepareDisableToken(ca.creditFacade, a.token)
|
|
628
|
-
),
|
|
629
|
-
...this.#prepareUpdateQuotas(ca.creditFacade, { minQuota, averageQuota })
|
|
630
|
-
];
|
|
631
|
-
const tx = cm.creditFacade.multicall(ca.creditAccount, calls);
|
|
632
|
-
return { tx, calls, creditFacade: cm.creditFacade };
|
|
633
|
-
}
|
|
634
439
|
/**
|
|
635
440
|
* Executes enable/disable tokens specified by given tokens lists and token prices
|
|
636
441
|
* @param {RouterCASlice} creditAccount - minimal credit account data {@link RouterCASlice} on which operation is performed
|
|
@@ -652,7 +457,7 @@ class CreditAccountsService extends import_base.SDKConstruct {
|
|
|
652
457
|
const calls = [
|
|
653
458
|
...priceUpdatesCalls,
|
|
654
459
|
...disabledTokens.map(
|
|
655
|
-
(token) => this
|
|
460
|
+
(token) => this.prepareDisableToken(ca.creditFacade, token)
|
|
656
461
|
),
|
|
657
462
|
...this.#prepareEnableTokens(ca.creditFacade, enabledTokens)
|
|
658
463
|
];
|
|
@@ -706,10 +511,10 @@ class CreditAccountsService extends import_base.SDKConstruct {
|
|
|
706
511
|
const calls = [
|
|
707
512
|
...priceUpdatesCalls,
|
|
708
513
|
this.#prepareIncreaseDebt(cm.creditFacade, debt),
|
|
709
|
-
...this
|
|
514
|
+
...this.prepareAddCollateral(cm.creditFacade, collateral, permits),
|
|
710
515
|
...openPathCalls,
|
|
711
|
-
...withdrawDebt ? [this
|
|
712
|
-
...this
|
|
516
|
+
...withdrawDebt ? [this.prepareWithdrawToken(cm.creditFacade, cm.underlying, debt, to)] : [],
|
|
517
|
+
...this.prepareUpdateQuotas(cm.creditFacade, {
|
|
713
518
|
minQuota,
|
|
714
519
|
averageQuota
|
|
715
520
|
})
|
|
@@ -898,9 +703,9 @@ class CreditAccountsService extends import_base.SDKConstruct {
|
|
|
898
703
|
}
|
|
899
704
|
/**
|
|
900
705
|
* Returns price updates in format that is accepted by various credit facade methods (multicall, close/liquidate, etc...).
|
|
901
|
-
* If there are desiredQuotas and creditAccount update quotaBalance > 0 || (balance > 10n && isEnabled).
|
|
902
|
-
* If there is creditAccount update balance > 10n && isEnabled.
|
|
903
|
-
* If there is desiredQuotas update quotaBalance > 0.
|
|
706
|
+
* - If there are desiredQuotas and creditAccount update quotaBalance > 0 || (balance > 10n && isEnabled). Is used when account has both: balances and quota buys.
|
|
707
|
+
* - If there is creditAccount update balance > 10n && isEnabled. Is used in credit account actions when quota is not being bought.
|
|
708
|
+
* - If there is desiredQuotas update quotaBalance > 0. Is used on credit account opening, when quota is bought for the first time.
|
|
904
709
|
* @param acc
|
|
905
710
|
* @returns
|
|
906
711
|
*/
|
|
@@ -913,7 +718,7 @@ class CreditAccountsService extends import_base.SDKConstruct {
|
|
|
913
718
|
);
|
|
914
719
|
return cm.creditFacade.encodeOnDemandPriceUpdates(updates);
|
|
915
720
|
}
|
|
916
|
-
|
|
721
|
+
prepareDisableQuotas(ca) {
|
|
917
722
|
const calls = [];
|
|
918
723
|
for (const { token, quota } of ca.tokens) {
|
|
919
724
|
if (quota > 0n) {
|
|
@@ -929,7 +734,7 @@ class CreditAccountsService extends import_base.SDKConstruct {
|
|
|
929
734
|
}
|
|
930
735
|
return calls;
|
|
931
736
|
}
|
|
932
|
-
|
|
737
|
+
prepareUpdateQuotas(creditFacade, { averageQuota, minQuota }) {
|
|
933
738
|
const minRecord = (0, import_router.assetsMap)(minQuota);
|
|
934
739
|
const calls = averageQuota.map((q) => {
|
|
935
740
|
const minAsset = minRecord.get(q.token);
|
|
@@ -945,7 +750,7 @@ class CreditAccountsService extends import_base.SDKConstruct {
|
|
|
945
750
|
});
|
|
946
751
|
return calls;
|
|
947
752
|
}
|
|
948
|
-
|
|
753
|
+
prepareDecreaseDebt(ca) {
|
|
949
754
|
if (ca.debt > 0n) {
|
|
950
755
|
return [
|
|
951
756
|
{
|
|
@@ -960,17 +765,17 @@ class CreditAccountsService extends import_base.SDKConstruct {
|
|
|
960
765
|
}
|
|
961
766
|
return [];
|
|
962
767
|
}
|
|
963
|
-
|
|
768
|
+
prepareDisableTokens(ca) {
|
|
964
769
|
const calls = [];
|
|
965
770
|
for (const t of ca.tokens) {
|
|
966
771
|
const isEnabled = (t.mask & ca.enabledTokensMask) !== 0n;
|
|
967
772
|
if (t.token !== ca.underlying && isEnabled && t.quota === 0n) {
|
|
968
|
-
calls.push(this
|
|
773
|
+
calls.push(this.prepareDisableToken(ca.creditFacade, t.token));
|
|
969
774
|
}
|
|
970
775
|
}
|
|
971
776
|
return calls;
|
|
972
777
|
}
|
|
973
|
-
|
|
778
|
+
prepareDisableToken(creditFacade, token) {
|
|
974
779
|
return {
|
|
975
780
|
target: creditFacade,
|
|
976
781
|
callData: (0, import_viem.encodeFunctionData)({
|
|
@@ -990,7 +795,7 @@ class CreditAccountsService extends import_base.SDKConstruct {
|
|
|
990
795
|
})
|
|
991
796
|
}));
|
|
992
797
|
}
|
|
993
|
-
|
|
798
|
+
prepareWithdrawToken(creditFacade, token, amount, to) {
|
|
994
799
|
return {
|
|
995
800
|
target: creditFacade,
|
|
996
801
|
callData: (0, import_viem.encodeFunctionData)({
|
|
@@ -1020,7 +825,7 @@ class CreditAccountsService extends import_base.SDKConstruct {
|
|
|
1020
825
|
})
|
|
1021
826
|
};
|
|
1022
827
|
}
|
|
1023
|
-
|
|
828
|
+
prepareAddCollateral(creditFacade, assets, permits) {
|
|
1024
829
|
const calls = assets.map(({ token, balance }) => {
|
|
1025
830
|
const p = permits[token];
|
|
1026
831
|
if (p) {
|
|
@@ -1044,184 +849,6 @@ class CreditAccountsService extends import_base.SDKConstruct {
|
|
|
1044
849
|
});
|
|
1045
850
|
return calls;
|
|
1046
851
|
}
|
|
1047
|
-
/**
|
|
1048
|
-
* unwraps staked tokens and optionally claims associated rewards; Should be remove after transition to 3.1
|
|
1049
|
-
* @param acc
|
|
1050
|
-
* @returns
|
|
1051
|
-
*/
|
|
1052
|
-
#prepareUnwrapAndWithdrawCallsV3(assets, claim, withdrawAll, creditManager) {
|
|
1053
|
-
const network = this.sdk.provider.networkType;
|
|
1054
|
-
const suite = this.sdk.marketRegister.findCreditManager(creditManager);
|
|
1055
|
-
const cmAdapters = suite.creditManager.adapters.values().reduce((acc, a) => {
|
|
1056
|
-
const contractLc = a.targetContract.toLowerCase();
|
|
1057
|
-
const adapterLc = a.address.toLowerCase();
|
|
1058
|
-
acc[contractLc] = adapterLc;
|
|
1059
|
-
return acc;
|
|
1060
|
-
}, {});
|
|
1061
|
-
const currentContractsData = Object.entries(
|
|
1062
|
-
import_sdk_gov_legacy.contractsByNetwork[network]
|
|
1063
|
-
).reduce(
|
|
1064
|
-
(acc, [symbol, address]) => {
|
|
1065
|
-
if (!!address && address !== import_constants.NOT_DEPLOYED) {
|
|
1066
|
-
acc[symbol] = address.toLowerCase();
|
|
1067
|
-
}
|
|
1068
|
-
return acc;
|
|
1069
|
-
},
|
|
1070
|
-
{}
|
|
1071
|
-
);
|
|
1072
|
-
const currentTokenData = Object.entries(import_sdk_gov_legacy.tokenDataByNetwork[network]).reduce(
|
|
1073
|
-
(acc, [symbol, address]) => {
|
|
1074
|
-
if (!!address && address !== import_constants.NOT_DEPLOYED) {
|
|
1075
|
-
acc[symbol] = address.toLowerCase();
|
|
1076
|
-
}
|
|
1077
|
-
return acc;
|
|
1078
|
-
},
|
|
1079
|
-
{}
|
|
1080
|
-
);
|
|
1081
|
-
const { aura, convex, sky } = assets.reduce(
|
|
1082
|
-
(acc, a) => {
|
|
1083
|
-
const symbol = import_sdk_gov_legacy.tokenSymbolByAddress[a.token];
|
|
1084
|
-
if ((0, import_sdk_gov_legacy.isConvexStakedPhantomToken)(symbol)) {
|
|
1085
|
-
acc.convex.push(a);
|
|
1086
|
-
} else if ((0, import_sdk_gov_legacy.isAuraStakedToken)(symbol)) {
|
|
1087
|
-
acc.aura.push(a);
|
|
1088
|
-
} else if ((0, import_sdk_gov_legacy.isStakingRewardsPhantomToken)(symbol)) {
|
|
1089
|
-
acc.sky.push(a);
|
|
1090
|
-
}
|
|
1091
|
-
return acc;
|
|
1092
|
-
},
|
|
1093
|
-
{ convex: [], aura: [], sky: [] }
|
|
1094
|
-
);
|
|
1095
|
-
const getWithdrawCall = (pool, a) => {
|
|
1096
|
-
return withdrawAll ? this.#withdrawAllAndUnwrap_Convex(pool, claim) : this.#withdrawAndUnwrap_Convex(pool, a.balance, claim);
|
|
1097
|
-
};
|
|
1098
|
-
const getWithdrawCall_Rewards = (pool, a) => {
|
|
1099
|
-
const calls = [
|
|
1100
|
-
withdrawAll ? this.#withdrawAll_Rewards(pool) : this.#withdraw_Rewards(pool, a.balance),
|
|
1101
|
-
...claim ? [this.#claim_Rewards(pool)] : []
|
|
1102
|
-
];
|
|
1103
|
-
return calls;
|
|
1104
|
-
};
|
|
1105
|
-
const convexStkCalls = convex.map((a) => {
|
|
1106
|
-
const symbol = import_sdk_gov_legacy.tokenSymbolByAddress[a.token];
|
|
1107
|
-
const { pool } = import_sdk_gov_legacy.convexTokens[symbol];
|
|
1108
|
-
const poolAddress = currentContractsData[pool];
|
|
1109
|
-
if (!poolAddress) {
|
|
1110
|
-
throw new Error("Can't withdrawAllAndUnwrap_Convex (convex)");
|
|
1111
|
-
}
|
|
1112
|
-
const poolAddressLc = poolAddress.toLowerCase();
|
|
1113
|
-
return getWithdrawCall(cmAdapters[poolAddressLc], a);
|
|
1114
|
-
});
|
|
1115
|
-
const auraStkCalls = aura.map((a) => {
|
|
1116
|
-
const symbol = import_sdk_gov_legacy.tokenSymbolByAddress[a.token];
|
|
1117
|
-
const { pool } = import_sdk_gov_legacy.auraTokens[symbol];
|
|
1118
|
-
const poolAddress = currentContractsData[pool];
|
|
1119
|
-
if (!poolAddress) {
|
|
1120
|
-
throw new Error("Can't withdrawAllAndUnwrap_Convex (aura)");
|
|
1121
|
-
}
|
|
1122
|
-
const poolAddressLc = poolAddress.toLowerCase();
|
|
1123
|
-
return getWithdrawCall(cmAdapters[poolAddressLc], a);
|
|
1124
|
-
});
|
|
1125
|
-
const skyStkCalls = sky.flatMap((a) => {
|
|
1126
|
-
const symbol = import_sdk_gov_legacy.tokenSymbolByAddress[a.token];
|
|
1127
|
-
const { pool } = import_sdk_gov_legacy.stakingRewardsTokens[symbol];
|
|
1128
|
-
const poolAddress = currentContractsData[pool];
|
|
1129
|
-
if (!poolAddress) {
|
|
1130
|
-
throw new Error("Can't withdrawAllAndUnwrap_Convex (sky)");
|
|
1131
|
-
}
|
|
1132
|
-
const poolAddressLc = poolAddress.toLowerCase();
|
|
1133
|
-
return getWithdrawCall_Rewards(cmAdapters[poolAddressLc], a);
|
|
1134
|
-
});
|
|
1135
|
-
const unwrapCalls = [...convexStkCalls, ...auraStkCalls, ...skyStkCalls];
|
|
1136
|
-
const withdraw = assets.map((a) => {
|
|
1137
|
-
const symbol = import_sdk_gov_legacy.tokenSymbolByAddress[a.token];
|
|
1138
|
-
if ((0, import_sdk_gov_legacy.isConvexStakedPhantomToken)(symbol)) {
|
|
1139
|
-
return {
|
|
1140
|
-
...a,
|
|
1141
|
-
token: currentTokenData[import_sdk_gov_legacy.convexStakedPhantomTokens[symbol].underlying]
|
|
1142
|
-
};
|
|
1143
|
-
}
|
|
1144
|
-
if ((0, import_sdk_gov_legacy.isAuraStakedToken)(symbol)) {
|
|
1145
|
-
return {
|
|
1146
|
-
...a,
|
|
1147
|
-
token: currentTokenData[import_sdk_gov_legacy.auraStakedTokens[symbol].underlying]
|
|
1148
|
-
};
|
|
1149
|
-
}
|
|
1150
|
-
if ((0, import_sdk_gov_legacy.isStakingRewardsPhantomToken)(symbol)) {
|
|
1151
|
-
return {
|
|
1152
|
-
...a,
|
|
1153
|
-
token: currentTokenData[import_sdk_gov_legacy.stakingRewardsTokens[symbol].underlying]
|
|
1154
|
-
};
|
|
1155
|
-
}
|
|
1156
|
-
return a;
|
|
1157
|
-
});
|
|
1158
|
-
return { unwrapCalls, assetsToWithdraw: withdraw };
|
|
1159
|
-
}
|
|
1160
|
-
// TODO: remove after transition to 3.1
|
|
1161
|
-
#withdrawAndUnwrap_Convex(address, amount, claim) {
|
|
1162
|
-
return {
|
|
1163
|
-
target: address,
|
|
1164
|
-
callData: (0, import_viem.encodeFunctionData)({
|
|
1165
|
-
abi: (0, import_viem.parseAbi)([
|
|
1166
|
-
"function withdrawAndUnwrap(uint256, bool claim) returns (uint256 tokensToEnable, uint256 tokensToDisable)"
|
|
1167
|
-
]),
|
|
1168
|
-
functionName: "withdrawAndUnwrap",
|
|
1169
|
-
args: [amount, claim]
|
|
1170
|
-
})
|
|
1171
|
-
};
|
|
1172
|
-
}
|
|
1173
|
-
// TODO: remove after transition to 3.1
|
|
1174
|
-
#withdrawAllAndUnwrap_Convex(address, claim) {
|
|
1175
|
-
return {
|
|
1176
|
-
target: address,
|
|
1177
|
-
callData: (0, import_viem.encodeFunctionData)({
|
|
1178
|
-
abi: (0, import_viem.parseAbi)([
|
|
1179
|
-
"function withdrawDiffAndUnwrap(uint256 leftoverAmount, bool claim) returns (uint256 tokensToEnable, uint256 tokensToDisable)"
|
|
1180
|
-
]),
|
|
1181
|
-
functionName: "withdrawDiffAndUnwrap",
|
|
1182
|
-
args: [1n, claim]
|
|
1183
|
-
})
|
|
1184
|
-
};
|
|
1185
|
-
}
|
|
1186
|
-
// TODO: remove after transition to 3.1
|
|
1187
|
-
#withdrawAll_Rewards(address) {
|
|
1188
|
-
return {
|
|
1189
|
-
target: address,
|
|
1190
|
-
callData: (0, import_viem.encodeFunctionData)({
|
|
1191
|
-
abi: (0, import_viem.parseAbi)([
|
|
1192
|
-
"function withdrawDiff(uint256 leftoverAmount) external returns (bool useSafePrices)"
|
|
1193
|
-
]),
|
|
1194
|
-
functionName: "withdrawDiff",
|
|
1195
|
-
args: [1n]
|
|
1196
|
-
})
|
|
1197
|
-
};
|
|
1198
|
-
}
|
|
1199
|
-
// TODO: remove after transition to 3.1
|
|
1200
|
-
#withdraw_Rewards(address, amount) {
|
|
1201
|
-
return {
|
|
1202
|
-
target: address,
|
|
1203
|
-
callData: (0, import_viem.encodeFunctionData)({
|
|
1204
|
-
abi: (0, import_viem.parseAbi)([
|
|
1205
|
-
"function withdraw(uint256 amount) external returns (bool useSafePrices)"
|
|
1206
|
-
]),
|
|
1207
|
-
functionName: "withdraw",
|
|
1208
|
-
args: [amount]
|
|
1209
|
-
})
|
|
1210
|
-
};
|
|
1211
|
-
}
|
|
1212
|
-
// TODO: remove after transition to 3.1
|
|
1213
|
-
#claim_Rewards(address) {
|
|
1214
|
-
return {
|
|
1215
|
-
target: address,
|
|
1216
|
-
callData: (0, import_viem.encodeFunctionData)({
|
|
1217
|
-
abi: (0, import_viem.parseAbi)([
|
|
1218
|
-
"function getReward() external returns (bool useSafePrices)"
|
|
1219
|
-
]),
|
|
1220
|
-
functionName: "getReward",
|
|
1221
|
-
args: []
|
|
1222
|
-
})
|
|
1223
|
-
};
|
|
1224
|
-
}
|
|
1225
852
|
/**
|
|
1226
853
|
* Returns addresses of market configurators
|
|
1227
854
|
*/
|
|
@@ -1243,5 +870,5 @@ class CreditAccountsService extends import_base.SDKConstruct {
|
|
|
1243
870
|
}
|
|
1244
871
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1245
872
|
0 && (module.exports = {
|
|
1246
|
-
|
|
873
|
+
AbstractCreditAccountService
|
|
1247
874
|
});
|