@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
|
@@ -34,6 +34,37 @@ const iGearboxRouterV310Abi = [
|
|
|
34
34
|
outputs: [{ name: "", type: "bytes32[]", internalType: "bytes32[]" }],
|
|
35
35
|
stateMutability: "view"
|
|
36
36
|
},
|
|
37
|
+
{
|
|
38
|
+
type: "function",
|
|
39
|
+
name: "processClaims",
|
|
40
|
+
inputs: [
|
|
41
|
+
{ name: "creditAccount", type: "address", internalType: "address" },
|
|
42
|
+
{
|
|
43
|
+
name: "tData",
|
|
44
|
+
type: "tuple[]",
|
|
45
|
+
internalType: "struct TokenData[]",
|
|
46
|
+
components: [
|
|
47
|
+
{ name: "token", type: "address", internalType: "address" },
|
|
48
|
+
{ name: "balance", type: "uint256", internalType: "uint256" },
|
|
49
|
+
{ name: "leftoverBalance", type: "uint256", internalType: "uint256" },
|
|
50
|
+
{ name: "numSplits", type: "uint256", internalType: "uint256" },
|
|
51
|
+
{ name: "claimRewards", type: "bool", internalType: "bool" }
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
],
|
|
55
|
+
outputs: [
|
|
56
|
+
{
|
|
57
|
+
name: "calls",
|
|
58
|
+
type: "tuple[]",
|
|
59
|
+
internalType: "struct MultiCall[]",
|
|
60
|
+
components: [
|
|
61
|
+
{ name: "target", type: "address", internalType: "address" },
|
|
62
|
+
{ name: "callData", type: "bytes", internalType: "bytes" }
|
|
63
|
+
]
|
|
64
|
+
}
|
|
65
|
+
],
|
|
66
|
+
stateMutability: "nonpayable"
|
|
67
|
+
},
|
|
37
68
|
{
|
|
38
69
|
type: "function",
|
|
39
70
|
name: "routeManyToOne",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { encodeFunctionData, getAddress
|
|
1
|
+
import { encodeFunctionData, getAddress } from "viem";
|
|
2
2
|
import {
|
|
3
3
|
iCreditAccountCompressorAbi,
|
|
4
4
|
iPeripheryCompressorAbi,
|
|
@@ -14,29 +14,15 @@ import {
|
|
|
14
14
|
AP_REWARDS_COMPRESSOR,
|
|
15
15
|
MAX_UINT256,
|
|
16
16
|
MIN_INT96,
|
|
17
|
-
NOT_DEPLOYED,
|
|
18
17
|
PERCENTAGE_FACTOR,
|
|
19
18
|
RAY,
|
|
20
19
|
VERSION_RANGE_310
|
|
21
20
|
} from "../constants/index.js";
|
|
22
21
|
import { assetsMap } from "../router/index.js";
|
|
23
|
-
import {
|
|
24
|
-
auraStakedTokens,
|
|
25
|
-
auraTokens,
|
|
26
|
-
contractsByNetwork,
|
|
27
|
-
convexStakedPhantomTokens,
|
|
28
|
-
convexTokens,
|
|
29
|
-
isAuraStakedToken,
|
|
30
|
-
isConvexStakedPhantomToken,
|
|
31
|
-
isStakingRewardsPhantomToken,
|
|
32
|
-
stakingRewardsTokens,
|
|
33
|
-
tokenDataByNetwork,
|
|
34
|
-
tokenSymbolByAddress
|
|
35
|
-
} from "../sdk-gov-legacy/index.js";
|
|
36
22
|
import { AddressMap, childLogger } from "../utils/index.js";
|
|
37
23
|
import { simulateWithPriceUpdates } from "../utils/viem/index.js";
|
|
38
24
|
import { stringifyGetCreditAccountsArgs } from "./utils.js";
|
|
39
|
-
class
|
|
25
|
+
class AbstractCreditAccountService extends SDKConstruct {
|
|
40
26
|
#compressor;
|
|
41
27
|
#batchSize;
|
|
42
28
|
#logger;
|
|
@@ -162,17 +148,17 @@ class CreditAccountsService extends SDKConstruct {
|
|
|
162
148
|
functionName: "getRewards",
|
|
163
149
|
args: [creditAccount]
|
|
164
150
|
});
|
|
151
|
+
const callData = encodeFunctionData({
|
|
152
|
+
abi: iBaseRewardPoolAbi,
|
|
153
|
+
functionName: "getReward",
|
|
154
|
+
args: []
|
|
155
|
+
});
|
|
165
156
|
const r = rewards.reduce((acc, r2) => {
|
|
166
157
|
const adapter = r2.adapter.toLowerCase();
|
|
167
158
|
const stakedPhantomToken = r2.stakedPhantomToken.toLowerCase();
|
|
168
159
|
const rewardToken = r2.rewardToken.toLowerCase();
|
|
169
160
|
const key = [adapter, stakedPhantomToken].join("-");
|
|
170
161
|
if (!acc[key]) {
|
|
171
|
-
const callData = encodeFunctionData({
|
|
172
|
-
abi: iBaseRewardPoolAbi,
|
|
173
|
-
functionName: "getReward",
|
|
174
|
-
args: []
|
|
175
|
-
});
|
|
176
162
|
acc[key] = {
|
|
177
163
|
adapter,
|
|
178
164
|
stakedPhantomToken,
|
|
@@ -283,120 +269,16 @@ class CreditAccountsService extends SDKConstruct {
|
|
|
283
269
|
const calls = [
|
|
284
270
|
...operation === "close" ? [] : priceUpdates,
|
|
285
271
|
...routerCloseResult.calls,
|
|
286
|
-
...this
|
|
287
|
-
...this
|
|
288
|
-
...this
|
|
272
|
+
...this.prepareDisableQuotas(ca),
|
|
273
|
+
...this.prepareDecreaseDebt(ca),
|
|
274
|
+
...this.prepareDisableTokens(ca),
|
|
289
275
|
...assetsToWithdraw.map(
|
|
290
|
-
(t) => this
|
|
276
|
+
(t) => this.prepareWithdrawToken(ca.creditFacade, t, MAX_UINT256, to)
|
|
291
277
|
)
|
|
292
278
|
];
|
|
293
279
|
const tx = operation === "close" ? cm.creditFacade.closeCreditAccount(ca.creditAccount, calls) : cm.creditFacade.multicall(ca.creditAccount, calls);
|
|
294
280
|
return { tx, calls, routerCloseResult, creditFacade: cm.creditFacade };
|
|
295
281
|
}
|
|
296
|
-
/**
|
|
297
|
-
* Fully repays credit account or repays credit account and keeps it open with zero debt
|
|
298
|
-
- Repays in the following order: price update -> add collateral to cover the debt ->
|
|
299
|
-
-> disable quotas for all tokens -> decrease debt -> disable tokens all tokens -> withdraw all tokens
|
|
300
|
-
* @param {CloseOptions} operation - {@link CloseOptions}: close or zeroDebt
|
|
301
|
-
* @param {RouterCASlice} creditAccount - minimal credit account data {@link RouterCASlice} on which operation is performed on which operation is performed
|
|
302
|
-
* @param {Array<Address>} collateralAssets - tokens to repay dept.
|
|
303
|
-
In the current implementation, this is the (debt+interest+fess) * buffer,
|
|
304
|
-
where buffer refers to amount of tokens which will exceed current debt
|
|
305
|
-
in order to cover possible debt increase over tx execution
|
|
306
|
-
* @param {Array<Asset>} assetsToWithdraw - tokens to withdraw from credit account.
|
|
307
|
-
Typically all non zero ca assets (including unclaimed rewards)
|
|
308
|
-
plus underlying token (to withdraw any exceeding underlying token after repay)
|
|
309
|
-
* @param {Record<Address, PermitResult>} permits - permits of tokens to withdraw (in any permittable token is present) {@link PermitResult}
|
|
310
|
-
* @param {Address} to - Wallet address to withdraw underlying to
|
|
311
|
-
* @returns All necessary data to execute the transaction (call, credit facade)
|
|
312
|
-
*/
|
|
313
|
-
async repayCreditAccount({
|
|
314
|
-
operation,
|
|
315
|
-
collateralAssets,
|
|
316
|
-
assetsToWithdraw: wrapped,
|
|
317
|
-
creditAccount: ca,
|
|
318
|
-
permits,
|
|
319
|
-
to
|
|
320
|
-
}) {
|
|
321
|
-
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
322
|
-
const addCollateral = collateralAssets.filter((a) => a.balance > 0);
|
|
323
|
-
const priceUpdates = await this.getPriceUpdatesForFacade(
|
|
324
|
-
ca.creditManager,
|
|
325
|
-
ca,
|
|
326
|
-
void 0
|
|
327
|
-
);
|
|
328
|
-
const { unwrapCalls, assetsToWithdraw } = this.#prepareUnwrapAndWithdrawCallsV3(
|
|
329
|
-
wrapped,
|
|
330
|
-
true,
|
|
331
|
-
true,
|
|
332
|
-
ca.creditManager
|
|
333
|
-
);
|
|
334
|
-
const calls = [
|
|
335
|
-
...operation === "close" ? [] : priceUpdates,
|
|
336
|
-
...this.#prepareAddCollateral(ca.creditFacade, addCollateral, permits),
|
|
337
|
-
...this.#prepareDisableQuotas(ca),
|
|
338
|
-
...this.#prepareDecreaseDebt(ca),
|
|
339
|
-
...unwrapCalls,
|
|
340
|
-
...this.#prepareDisableTokens(ca),
|
|
341
|
-
// TODO: probably needs a better way to handle reward tokens
|
|
342
|
-
...assetsToWithdraw.map(
|
|
343
|
-
(t) => this.#prepareWithdrawToken(ca.creditFacade, t.token, MAX_UINT256, to)
|
|
344
|
-
)
|
|
345
|
-
];
|
|
346
|
-
const tx = operation === "close" ? cm.creditFacade.closeCreditAccount(ca.creditAccount, calls) : cm.creditFacade.multicall(ca.creditAccount, calls);
|
|
347
|
-
return { tx, calls, creditFacade: cm.creditFacade };
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Fully repays liquidatable account
|
|
351
|
-
- Repay and liquidate is executed in the following order: price update -> add collateral to cover the debt ->
|
|
352
|
-
withdraw all tokens from credit account
|
|
353
|
-
* @param {RouterCASlice} creditAccount - minimal credit account data {@link RouterCASlice} on which operation is performed
|
|
354
|
-
* @param {Array<Address>} collateralAssets - tokens to repay dept.
|
|
355
|
-
In the current implementation, this is the (debt+interest+fess) * buffer,
|
|
356
|
-
where buffer refers to amount of tokens which will exceed current debt
|
|
357
|
-
in order to cover possible debt increase over tx execution
|
|
358
|
-
* @param {Array<Address>} assetsToWithdraw - tokens to withdraw from credit account.
|
|
359
|
-
Typically all non zero ca assets (including unclaimed rewards)
|
|
360
|
-
plus underlying token (to withdraw any exceeding underlying token after repay)
|
|
361
|
-
* @param {Record<Address, PermitResult>} permits - permits of tokens to withdraw (in any permittable token is present) {@link PermitResult}
|
|
362
|
-
* @param {Address} to - Wallet address to withdraw underlying to
|
|
363
|
-
* @returns All necessary data to execute the transaction (call, credit facade)
|
|
364
|
-
*/
|
|
365
|
-
async repayAndLiquidateCreditAccount({
|
|
366
|
-
collateralAssets,
|
|
367
|
-
assetsToWithdraw: wrapped,
|
|
368
|
-
creditAccount: ca,
|
|
369
|
-
permits,
|
|
370
|
-
to
|
|
371
|
-
}) {
|
|
372
|
-
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
373
|
-
const priceUpdates = await this.getPriceUpdatesForFacade(
|
|
374
|
-
ca.creditManager,
|
|
375
|
-
ca,
|
|
376
|
-
void 0
|
|
377
|
-
);
|
|
378
|
-
const addCollateral = collateralAssets.filter((a) => a.balance > 0);
|
|
379
|
-
const { unwrapCalls, assetsToWithdraw } = this.#prepareUnwrapAndWithdrawCallsV3(
|
|
380
|
-
wrapped,
|
|
381
|
-
true,
|
|
382
|
-
true,
|
|
383
|
-
ca.creditManager
|
|
384
|
-
);
|
|
385
|
-
const calls = [
|
|
386
|
-
...priceUpdates,
|
|
387
|
-
...this.#prepareAddCollateral(ca.creditFacade, addCollateral, permits),
|
|
388
|
-
...unwrapCalls,
|
|
389
|
-
...assetsToWithdraw.map(
|
|
390
|
-
(t) => this.#prepareWithdrawToken(ca.creditFacade, t.token, MAX_UINT256, to)
|
|
391
|
-
)
|
|
392
|
-
];
|
|
393
|
-
const tx = cm.creditFacade.liquidateCreditAccount(
|
|
394
|
-
ca.creditAccount,
|
|
395
|
-
to,
|
|
396
|
-
calls
|
|
397
|
-
);
|
|
398
|
-
return { tx, calls, creditFacade: cm.creditFacade };
|
|
399
|
-
}
|
|
400
282
|
/**
|
|
401
283
|
* Updates quota of credit account.
|
|
402
284
|
- CA quota updated in the following order: price update -> update quotas
|
|
@@ -420,7 +302,7 @@ class CreditAccountsService extends SDKConstruct {
|
|
|
420
302
|
);
|
|
421
303
|
const calls = [
|
|
422
304
|
...priceUpdates,
|
|
423
|
-
...this
|
|
305
|
+
...this.prepareUpdateQuotas(creditAccount.creditFacade, {
|
|
424
306
|
minQuota,
|
|
425
307
|
averageQuota
|
|
426
308
|
})
|
|
@@ -457,12 +339,12 @@ class CreditAccountsService extends SDKConstruct {
|
|
|
457
339
|
);
|
|
458
340
|
const calls = [
|
|
459
341
|
...priceUpdatesCalls,
|
|
460
|
-
...this
|
|
342
|
+
...this.prepareAddCollateral(
|
|
461
343
|
creditAccount.creditFacade,
|
|
462
344
|
[asset],
|
|
463
345
|
permit ? { [asset.token]: permit } : {}
|
|
464
346
|
),
|
|
465
|
-
...this
|
|
347
|
+
...this.prepareUpdateQuotas(creditAccount.creditFacade, {
|
|
466
348
|
minQuota,
|
|
467
349
|
averageQuota
|
|
468
350
|
})
|
|
@@ -510,57 +392,6 @@ class CreditAccountsService extends SDKConstruct {
|
|
|
510
392
|
const tx = cm.creditFacade.multicall(creditAccount.creditAccount, calls);
|
|
511
393
|
return { tx, calls, creditFacade: cm.creditFacade };
|
|
512
394
|
}
|
|
513
|
-
/**
|
|
514
|
-
* Withdraws a single collateral from credit account to wallet to and updates quotas;
|
|
515
|
-
technically can withdraw several tokens at once
|
|
516
|
-
- Collateral is withdrawn in the following order: price update -> withdraw token -> update quotas for affected tokens
|
|
517
|
-
* @param {RouterCASlice} creditAccount - minimal credit account data {@link RouterCASlice} on which operation is performed
|
|
518
|
-
* @param {Array<Asset>} averageQuota - average quota for desired token {@link Asset}
|
|
519
|
-
* @param {Array<Asset>} minQuota - minimum quota for desired token {@link Asset}
|
|
520
|
-
* @param {Address} to - Wallet address to withdraw token to
|
|
521
|
-
* @param {Array<Asset>} assetsToWithdraw - permits for asset if it is permittable {@link PermitResult}
|
|
522
|
-
* @returns All necessary data to execute the transaction (call, credit facade)
|
|
523
|
-
*/
|
|
524
|
-
async withdrawCollateral({
|
|
525
|
-
creditAccount,
|
|
526
|
-
assetsToWithdraw: wrapped,
|
|
527
|
-
to,
|
|
528
|
-
minQuota,
|
|
529
|
-
averageQuota
|
|
530
|
-
}) {
|
|
531
|
-
const cm = this.sdk.marketRegister.findCreditManager(
|
|
532
|
-
creditAccount.creditManager
|
|
533
|
-
);
|
|
534
|
-
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
535
|
-
creditAccount.creditManager,
|
|
536
|
-
creditAccount,
|
|
537
|
-
void 0
|
|
538
|
-
);
|
|
539
|
-
const { unwrapCalls, assetsToWithdraw } = this.#prepareUnwrapAndWithdrawCallsV3(
|
|
540
|
-
wrapped,
|
|
541
|
-
false,
|
|
542
|
-
false,
|
|
543
|
-
creditAccount.creditManager
|
|
544
|
-
);
|
|
545
|
-
const calls = [
|
|
546
|
-
...priceUpdatesCalls,
|
|
547
|
-
...unwrapCalls,
|
|
548
|
-
...assetsToWithdraw.map(
|
|
549
|
-
(a) => this.#prepareWithdrawToken(
|
|
550
|
-
creditAccount.creditFacade,
|
|
551
|
-
a.token,
|
|
552
|
-
a.balance,
|
|
553
|
-
to
|
|
554
|
-
)
|
|
555
|
-
),
|
|
556
|
-
...this.#prepareUpdateQuotas(creditAccount.creditFacade, {
|
|
557
|
-
minQuota,
|
|
558
|
-
averageQuota
|
|
559
|
-
})
|
|
560
|
-
];
|
|
561
|
-
const tx = cm.creditFacade.multicall(creditAccount.creditAccount, calls);
|
|
562
|
-
return { tx, calls, creditFacade: cm.creditFacade };
|
|
563
|
-
}
|
|
564
395
|
/**
|
|
565
396
|
* Executes swap specified by given calls, update quotas of affected tokens
|
|
566
397
|
- Swap is executed in the following order: price update -> execute swap path -> update quotas
|
|
@@ -588,7 +419,7 @@ class CreditAccountsService extends SDKConstruct {
|
|
|
588
419
|
const calls = [
|
|
589
420
|
...priceUpdatesCalls,
|
|
590
421
|
...swapCalls,
|
|
591
|
-
...this
|
|
422
|
+
...this.prepareUpdateQuotas(creditAccount.creditFacade, {
|
|
592
423
|
minQuota,
|
|
593
424
|
averageQuota
|
|
594
425
|
})
|
|
@@ -596,45 +427,6 @@ class CreditAccountsService extends SDKConstruct {
|
|
|
596
427
|
const tx = cm.creditFacade.multicall(creditAccount.creditAccount, calls);
|
|
597
428
|
return { tx, calls, creditFacade: cm.creditFacade };
|
|
598
429
|
}
|
|
599
|
-
/**
|
|
600
|
-
* Executes swap specified by given calls, update quotas of affected tokens
|
|
601
|
-
- Claim rewards is executed in the following order: price update -> execute claim calls ->
|
|
602
|
-
-> (optionally: disable reward tokens) -> (optionally: update quotas)
|
|
603
|
-
* @param {RouterCASlice} creditAccount - minimal credit account data {@link RouterCASlice} on which operation is performed
|
|
604
|
-
* @param {Array<Asset>} averageQuota - average quota for desired token;
|
|
605
|
-
in this case can be omitted since rewards tokens do not require quotas {@link Asset}
|
|
606
|
-
* @param {Array<Asset>} minQuota - minimum quota for desired token;
|
|
607
|
-
in this case can be omitted since rewards tokens do not require quotas {@link Asset}
|
|
608
|
-
* @param {Array<MultiCall>} calls - array of MultiCall from getRewards {@link MultiCall}
|
|
609
|
-
* @param {Array<Asset>} tokensToDisable - tokens to disable after rewards claiming;
|
|
610
|
-
sometimes is needed since old credit facade used to enable tokens on claim {@link Asset}
|
|
611
|
-
* @returns All necessary data to execute the transaction (call, credit facade)
|
|
612
|
-
*/
|
|
613
|
-
async claimFarmRewards({
|
|
614
|
-
tokensToDisable,
|
|
615
|
-
calls: claimCalls,
|
|
616
|
-
creditAccount: ca,
|
|
617
|
-
minQuota,
|
|
618
|
-
averageQuota
|
|
619
|
-
}) {
|
|
620
|
-
if (claimCalls.length === 0) throw new Error("No path to execute");
|
|
621
|
-
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
622
|
-
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
623
|
-
ca.creditManager,
|
|
624
|
-
ca,
|
|
625
|
-
averageQuota
|
|
626
|
-
);
|
|
627
|
-
const calls = [
|
|
628
|
-
...priceUpdatesCalls,
|
|
629
|
-
...claimCalls,
|
|
630
|
-
...tokensToDisable.map(
|
|
631
|
-
(a) => this.#prepareDisableToken(ca.creditFacade, a.token)
|
|
632
|
-
),
|
|
633
|
-
...this.#prepareUpdateQuotas(ca.creditFacade, { minQuota, averageQuota })
|
|
634
|
-
];
|
|
635
|
-
const tx = cm.creditFacade.multicall(ca.creditAccount, calls);
|
|
636
|
-
return { tx, calls, creditFacade: cm.creditFacade };
|
|
637
|
-
}
|
|
638
430
|
/**
|
|
639
431
|
* Executes enable/disable tokens specified by given tokens lists and token prices
|
|
640
432
|
* @param {RouterCASlice} creditAccount - minimal credit account data {@link RouterCASlice} on which operation is performed
|
|
@@ -656,7 +448,7 @@ class CreditAccountsService extends SDKConstruct {
|
|
|
656
448
|
const calls = [
|
|
657
449
|
...priceUpdatesCalls,
|
|
658
450
|
...disabledTokens.map(
|
|
659
|
-
(token) => this
|
|
451
|
+
(token) => this.prepareDisableToken(ca.creditFacade, token)
|
|
660
452
|
),
|
|
661
453
|
...this.#prepareEnableTokens(ca.creditFacade, enabledTokens)
|
|
662
454
|
];
|
|
@@ -710,10 +502,10 @@ class CreditAccountsService extends SDKConstruct {
|
|
|
710
502
|
const calls = [
|
|
711
503
|
...priceUpdatesCalls,
|
|
712
504
|
this.#prepareIncreaseDebt(cm.creditFacade, debt),
|
|
713
|
-
...this
|
|
505
|
+
...this.prepareAddCollateral(cm.creditFacade, collateral, permits),
|
|
714
506
|
...openPathCalls,
|
|
715
|
-
...withdrawDebt ? [this
|
|
716
|
-
...this
|
|
507
|
+
...withdrawDebt ? [this.prepareWithdrawToken(cm.creditFacade, cm.underlying, debt, to)] : [],
|
|
508
|
+
...this.prepareUpdateQuotas(cm.creditFacade, {
|
|
717
509
|
minQuota,
|
|
718
510
|
averageQuota
|
|
719
511
|
})
|
|
@@ -902,9 +694,9 @@ class CreditAccountsService extends SDKConstruct {
|
|
|
902
694
|
}
|
|
903
695
|
/**
|
|
904
696
|
* Returns price updates in format that is accepted by various credit facade methods (multicall, close/liquidate, etc...).
|
|
905
|
-
* If there are desiredQuotas and creditAccount update quotaBalance > 0 || (balance > 10n && isEnabled).
|
|
906
|
-
* If there is creditAccount update balance > 10n && isEnabled.
|
|
907
|
-
* If there is desiredQuotas update quotaBalance > 0.
|
|
697
|
+
* - If there are desiredQuotas and creditAccount update quotaBalance > 0 || (balance > 10n && isEnabled). Is used when account has both: balances and quota buys.
|
|
698
|
+
* - If there is creditAccount update balance > 10n && isEnabled. Is used in credit account actions when quota is not being bought.
|
|
699
|
+
* - If there is desiredQuotas update quotaBalance > 0. Is used on credit account opening, when quota is bought for the first time.
|
|
908
700
|
* @param acc
|
|
909
701
|
* @returns
|
|
910
702
|
*/
|
|
@@ -917,7 +709,7 @@ class CreditAccountsService extends SDKConstruct {
|
|
|
917
709
|
);
|
|
918
710
|
return cm.creditFacade.encodeOnDemandPriceUpdates(updates);
|
|
919
711
|
}
|
|
920
|
-
|
|
712
|
+
prepareDisableQuotas(ca) {
|
|
921
713
|
const calls = [];
|
|
922
714
|
for (const { token, quota } of ca.tokens) {
|
|
923
715
|
if (quota > 0n) {
|
|
@@ -933,7 +725,7 @@ class CreditAccountsService extends SDKConstruct {
|
|
|
933
725
|
}
|
|
934
726
|
return calls;
|
|
935
727
|
}
|
|
936
|
-
|
|
728
|
+
prepareUpdateQuotas(creditFacade, { averageQuota, minQuota }) {
|
|
937
729
|
const minRecord = assetsMap(minQuota);
|
|
938
730
|
const calls = averageQuota.map((q) => {
|
|
939
731
|
const minAsset = minRecord.get(q.token);
|
|
@@ -949,7 +741,7 @@ class CreditAccountsService extends SDKConstruct {
|
|
|
949
741
|
});
|
|
950
742
|
return calls;
|
|
951
743
|
}
|
|
952
|
-
|
|
744
|
+
prepareDecreaseDebt(ca) {
|
|
953
745
|
if (ca.debt > 0n) {
|
|
954
746
|
return [
|
|
955
747
|
{
|
|
@@ -964,17 +756,17 @@ class CreditAccountsService extends SDKConstruct {
|
|
|
964
756
|
}
|
|
965
757
|
return [];
|
|
966
758
|
}
|
|
967
|
-
|
|
759
|
+
prepareDisableTokens(ca) {
|
|
968
760
|
const calls = [];
|
|
969
761
|
for (const t of ca.tokens) {
|
|
970
762
|
const isEnabled = (t.mask & ca.enabledTokensMask) !== 0n;
|
|
971
763
|
if (t.token !== ca.underlying && isEnabled && t.quota === 0n) {
|
|
972
|
-
calls.push(this
|
|
764
|
+
calls.push(this.prepareDisableToken(ca.creditFacade, t.token));
|
|
973
765
|
}
|
|
974
766
|
}
|
|
975
767
|
return calls;
|
|
976
768
|
}
|
|
977
|
-
|
|
769
|
+
prepareDisableToken(creditFacade, token) {
|
|
978
770
|
return {
|
|
979
771
|
target: creditFacade,
|
|
980
772
|
callData: encodeFunctionData({
|
|
@@ -994,7 +786,7 @@ class CreditAccountsService extends SDKConstruct {
|
|
|
994
786
|
})
|
|
995
787
|
}));
|
|
996
788
|
}
|
|
997
|
-
|
|
789
|
+
prepareWithdrawToken(creditFacade, token, amount, to) {
|
|
998
790
|
return {
|
|
999
791
|
target: creditFacade,
|
|
1000
792
|
callData: encodeFunctionData({
|
|
@@ -1024,7 +816,7 @@ class CreditAccountsService extends SDKConstruct {
|
|
|
1024
816
|
})
|
|
1025
817
|
};
|
|
1026
818
|
}
|
|
1027
|
-
|
|
819
|
+
prepareAddCollateral(creditFacade, assets, permits) {
|
|
1028
820
|
const calls = assets.map(({ token, balance }) => {
|
|
1029
821
|
const p = permits[token];
|
|
1030
822
|
if (p) {
|
|
@@ -1048,184 +840,6 @@ class CreditAccountsService extends SDKConstruct {
|
|
|
1048
840
|
});
|
|
1049
841
|
return calls;
|
|
1050
842
|
}
|
|
1051
|
-
/**
|
|
1052
|
-
* unwraps staked tokens and optionally claims associated rewards; Should be remove after transition to 3.1
|
|
1053
|
-
* @param acc
|
|
1054
|
-
* @returns
|
|
1055
|
-
*/
|
|
1056
|
-
#prepareUnwrapAndWithdrawCallsV3(assets, claim, withdrawAll, creditManager) {
|
|
1057
|
-
const network = this.sdk.provider.networkType;
|
|
1058
|
-
const suite = this.sdk.marketRegister.findCreditManager(creditManager);
|
|
1059
|
-
const cmAdapters = suite.creditManager.adapters.values().reduce((acc, a) => {
|
|
1060
|
-
const contractLc = a.targetContract.toLowerCase();
|
|
1061
|
-
const adapterLc = a.address.toLowerCase();
|
|
1062
|
-
acc[contractLc] = adapterLc;
|
|
1063
|
-
return acc;
|
|
1064
|
-
}, {});
|
|
1065
|
-
const currentContractsData = Object.entries(
|
|
1066
|
-
contractsByNetwork[network]
|
|
1067
|
-
).reduce(
|
|
1068
|
-
(acc, [symbol, address]) => {
|
|
1069
|
-
if (!!address && address !== NOT_DEPLOYED) {
|
|
1070
|
-
acc[symbol] = address.toLowerCase();
|
|
1071
|
-
}
|
|
1072
|
-
return acc;
|
|
1073
|
-
},
|
|
1074
|
-
{}
|
|
1075
|
-
);
|
|
1076
|
-
const currentTokenData = Object.entries(tokenDataByNetwork[network]).reduce(
|
|
1077
|
-
(acc, [symbol, address]) => {
|
|
1078
|
-
if (!!address && address !== NOT_DEPLOYED) {
|
|
1079
|
-
acc[symbol] = address.toLowerCase();
|
|
1080
|
-
}
|
|
1081
|
-
return acc;
|
|
1082
|
-
},
|
|
1083
|
-
{}
|
|
1084
|
-
);
|
|
1085
|
-
const { aura, convex, sky } = assets.reduce(
|
|
1086
|
-
(acc, a) => {
|
|
1087
|
-
const symbol = tokenSymbolByAddress[a.token];
|
|
1088
|
-
if (isConvexStakedPhantomToken(symbol)) {
|
|
1089
|
-
acc.convex.push(a);
|
|
1090
|
-
} else if (isAuraStakedToken(symbol)) {
|
|
1091
|
-
acc.aura.push(a);
|
|
1092
|
-
} else if (isStakingRewardsPhantomToken(symbol)) {
|
|
1093
|
-
acc.sky.push(a);
|
|
1094
|
-
}
|
|
1095
|
-
return acc;
|
|
1096
|
-
},
|
|
1097
|
-
{ convex: [], aura: [], sky: [] }
|
|
1098
|
-
);
|
|
1099
|
-
const getWithdrawCall = (pool, a) => {
|
|
1100
|
-
return withdrawAll ? this.#withdrawAllAndUnwrap_Convex(pool, claim) : this.#withdrawAndUnwrap_Convex(pool, a.balance, claim);
|
|
1101
|
-
};
|
|
1102
|
-
const getWithdrawCall_Rewards = (pool, a) => {
|
|
1103
|
-
const calls = [
|
|
1104
|
-
withdrawAll ? this.#withdrawAll_Rewards(pool) : this.#withdraw_Rewards(pool, a.balance),
|
|
1105
|
-
...claim ? [this.#claim_Rewards(pool)] : []
|
|
1106
|
-
];
|
|
1107
|
-
return calls;
|
|
1108
|
-
};
|
|
1109
|
-
const convexStkCalls = convex.map((a) => {
|
|
1110
|
-
const symbol = tokenSymbolByAddress[a.token];
|
|
1111
|
-
const { pool } = convexTokens[symbol];
|
|
1112
|
-
const poolAddress = currentContractsData[pool];
|
|
1113
|
-
if (!poolAddress) {
|
|
1114
|
-
throw new Error("Can't withdrawAllAndUnwrap_Convex (convex)");
|
|
1115
|
-
}
|
|
1116
|
-
const poolAddressLc = poolAddress.toLowerCase();
|
|
1117
|
-
return getWithdrawCall(cmAdapters[poolAddressLc], a);
|
|
1118
|
-
});
|
|
1119
|
-
const auraStkCalls = aura.map((a) => {
|
|
1120
|
-
const symbol = tokenSymbolByAddress[a.token];
|
|
1121
|
-
const { pool } = auraTokens[symbol];
|
|
1122
|
-
const poolAddress = currentContractsData[pool];
|
|
1123
|
-
if (!poolAddress) {
|
|
1124
|
-
throw new Error("Can't withdrawAllAndUnwrap_Convex (aura)");
|
|
1125
|
-
}
|
|
1126
|
-
const poolAddressLc = poolAddress.toLowerCase();
|
|
1127
|
-
return getWithdrawCall(cmAdapters[poolAddressLc], a);
|
|
1128
|
-
});
|
|
1129
|
-
const skyStkCalls = sky.flatMap((a) => {
|
|
1130
|
-
const symbol = tokenSymbolByAddress[a.token];
|
|
1131
|
-
const { pool } = stakingRewardsTokens[symbol];
|
|
1132
|
-
const poolAddress = currentContractsData[pool];
|
|
1133
|
-
if (!poolAddress) {
|
|
1134
|
-
throw new Error("Can't withdrawAllAndUnwrap_Convex (sky)");
|
|
1135
|
-
}
|
|
1136
|
-
const poolAddressLc = poolAddress.toLowerCase();
|
|
1137
|
-
return getWithdrawCall_Rewards(cmAdapters[poolAddressLc], a);
|
|
1138
|
-
});
|
|
1139
|
-
const unwrapCalls = [...convexStkCalls, ...auraStkCalls, ...skyStkCalls];
|
|
1140
|
-
const withdraw = assets.map((a) => {
|
|
1141
|
-
const symbol = tokenSymbolByAddress[a.token];
|
|
1142
|
-
if (isConvexStakedPhantomToken(symbol)) {
|
|
1143
|
-
return {
|
|
1144
|
-
...a,
|
|
1145
|
-
token: currentTokenData[convexStakedPhantomTokens[symbol].underlying]
|
|
1146
|
-
};
|
|
1147
|
-
}
|
|
1148
|
-
if (isAuraStakedToken(symbol)) {
|
|
1149
|
-
return {
|
|
1150
|
-
...a,
|
|
1151
|
-
token: currentTokenData[auraStakedTokens[symbol].underlying]
|
|
1152
|
-
};
|
|
1153
|
-
}
|
|
1154
|
-
if (isStakingRewardsPhantomToken(symbol)) {
|
|
1155
|
-
return {
|
|
1156
|
-
...a,
|
|
1157
|
-
token: currentTokenData[stakingRewardsTokens[symbol].underlying]
|
|
1158
|
-
};
|
|
1159
|
-
}
|
|
1160
|
-
return a;
|
|
1161
|
-
});
|
|
1162
|
-
return { unwrapCalls, assetsToWithdraw: withdraw };
|
|
1163
|
-
}
|
|
1164
|
-
// TODO: remove after transition to 3.1
|
|
1165
|
-
#withdrawAndUnwrap_Convex(address, amount, claim) {
|
|
1166
|
-
return {
|
|
1167
|
-
target: address,
|
|
1168
|
-
callData: encodeFunctionData({
|
|
1169
|
-
abi: parseAbi([
|
|
1170
|
-
"function withdrawAndUnwrap(uint256, bool claim) returns (uint256 tokensToEnable, uint256 tokensToDisable)"
|
|
1171
|
-
]),
|
|
1172
|
-
functionName: "withdrawAndUnwrap",
|
|
1173
|
-
args: [amount, claim]
|
|
1174
|
-
})
|
|
1175
|
-
};
|
|
1176
|
-
}
|
|
1177
|
-
// TODO: remove after transition to 3.1
|
|
1178
|
-
#withdrawAllAndUnwrap_Convex(address, claim) {
|
|
1179
|
-
return {
|
|
1180
|
-
target: address,
|
|
1181
|
-
callData: encodeFunctionData({
|
|
1182
|
-
abi: parseAbi([
|
|
1183
|
-
"function withdrawDiffAndUnwrap(uint256 leftoverAmount, bool claim) returns (uint256 tokensToEnable, uint256 tokensToDisable)"
|
|
1184
|
-
]),
|
|
1185
|
-
functionName: "withdrawDiffAndUnwrap",
|
|
1186
|
-
args: [1n, claim]
|
|
1187
|
-
})
|
|
1188
|
-
};
|
|
1189
|
-
}
|
|
1190
|
-
// TODO: remove after transition to 3.1
|
|
1191
|
-
#withdrawAll_Rewards(address) {
|
|
1192
|
-
return {
|
|
1193
|
-
target: address,
|
|
1194
|
-
callData: encodeFunctionData({
|
|
1195
|
-
abi: parseAbi([
|
|
1196
|
-
"function withdrawDiff(uint256 leftoverAmount) external returns (bool useSafePrices)"
|
|
1197
|
-
]),
|
|
1198
|
-
functionName: "withdrawDiff",
|
|
1199
|
-
args: [1n]
|
|
1200
|
-
})
|
|
1201
|
-
};
|
|
1202
|
-
}
|
|
1203
|
-
// TODO: remove after transition to 3.1
|
|
1204
|
-
#withdraw_Rewards(address, amount) {
|
|
1205
|
-
return {
|
|
1206
|
-
target: address,
|
|
1207
|
-
callData: encodeFunctionData({
|
|
1208
|
-
abi: parseAbi([
|
|
1209
|
-
"function withdraw(uint256 amount) external returns (bool useSafePrices)"
|
|
1210
|
-
]),
|
|
1211
|
-
functionName: "withdraw",
|
|
1212
|
-
args: [amount]
|
|
1213
|
-
})
|
|
1214
|
-
};
|
|
1215
|
-
}
|
|
1216
|
-
// TODO: remove after transition to 3.1
|
|
1217
|
-
#claim_Rewards(address) {
|
|
1218
|
-
return {
|
|
1219
|
-
target: address,
|
|
1220
|
-
callData: encodeFunctionData({
|
|
1221
|
-
abi: parseAbi([
|
|
1222
|
-
"function getReward() external returns (bool useSafePrices)"
|
|
1223
|
-
]),
|
|
1224
|
-
functionName: "getReward",
|
|
1225
|
-
args: []
|
|
1226
|
-
})
|
|
1227
|
-
};
|
|
1228
|
-
}
|
|
1229
843
|
/**
|
|
1230
844
|
* Returns addresses of market configurators
|
|
1231
845
|
*/
|
|
@@ -1246,5 +860,5 @@ class CreditAccountsService extends SDKConstruct {
|
|
|
1246
860
|
}
|
|
1247
861
|
}
|
|
1248
862
|
export {
|
|
1249
|
-
|
|
863
|
+
AbstractCreditAccountService
|
|
1250
864
|
};
|