@gearbox-protocol/sdk 14.12.0-next.10 → 14.12.0-next.11
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/preview/preview/applyInnerOperations.js +6 -8
- package/dist/cjs/preview/preview/previewAdjustCreditAccount.js +3 -8
- package/dist/cjs/preview/preview/previewOpenCreditAccount.js +5 -7
- package/dist/cjs/sdk/accounts/CreditAccountsServiceV310.js +50 -19
- package/dist/cjs/sdk/utils/AssetsMap.js +39 -4
- package/dist/esm/preview/preview/applyInnerOperations.js +6 -8
- package/dist/esm/preview/preview/previewAdjustCreditAccount.js +3 -8
- package/dist/esm/preview/preview/previewOpenCreditAccount.js +5 -7
- package/dist/esm/sdk/accounts/CreditAccountsServiceV310.js +50 -19
- package/dist/esm/sdk/utils/AssetsMap.js +39 -4
- package/dist/types/sdk/accounts/CreditAccountsServiceV310.d.ts +10 -2
- package/dist/types/sdk/accounts/types.d.ts +64 -6
- package/dist/types/sdk/utils/AssetsMap.d.ts +19 -2
- package/package.json +1 -1
|
@@ -45,7 +45,7 @@ function applyInnerOperations(sdk, multicall, state) {
|
|
|
45
45
|
state.balances.inc(op.token, op.amount);
|
|
46
46
|
break;
|
|
47
47
|
case "WithdrawCollateral": {
|
|
48
|
-
const running = state.balances.
|
|
48
|
+
const running = state.balances.getOrZero(op.token);
|
|
49
49
|
const amount = op.amount === import_sdk.MAX_UINT256 ? running > 0n ? running : 0n : op.amount;
|
|
50
50
|
state.balances.dec(op.token, amount);
|
|
51
51
|
state.collateralWithdrawn.inc(op.token, amount);
|
|
@@ -113,16 +113,14 @@ function applyQuotaChanges(initialQuotas, changes) {
|
|
|
113
113
|
if (change === import_sdk.MIN_INT96) {
|
|
114
114
|
final.upsert(token, 0n);
|
|
115
115
|
} else {
|
|
116
|
-
const next =
|
|
116
|
+
const next = final.getOrZero(token) + change;
|
|
117
117
|
final.upsert(token, next > 0n ? next : 0n);
|
|
118
118
|
}
|
|
119
119
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
})).filter(({ balance }) => balance !== 0n);
|
|
125
|
-
return { quotas, quotasChange };
|
|
120
|
+
return {
|
|
121
|
+
quotas: final.toAssets(0n),
|
|
122
|
+
quotasChange: final.difference(initialQuotas).toAssets()
|
|
123
|
+
};
|
|
126
124
|
}
|
|
127
125
|
// Annotate the CommonJS export names for ESM import in node:
|
|
128
126
|
0 && (module.exports = {
|
|
@@ -50,9 +50,7 @@ async function previewAdjustCreditAccount(input, operation, options) {
|
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
const state = (0, import_applyInnerOperations.makeInnerOperationsState)();
|
|
53
|
-
|
|
54
|
-
state.balances.upsert(token, balance);
|
|
55
|
-
}
|
|
53
|
+
state.balances = initialBalances.clone();
|
|
56
54
|
state.debt = ca.debt;
|
|
57
55
|
state.totalDebt = ca.debt + ca.accruedInterest + ca.accruedFees;
|
|
58
56
|
(0, import_applyInnerOperations.applyInnerOperations)(sdk, operation.multicall, state);
|
|
@@ -65,11 +63,8 @@ async function previewAdjustCreditAccount(input, operation, options) {
|
|
|
65
63
|
initialQuotas,
|
|
66
64
|
state.quotaChanges
|
|
67
65
|
);
|
|
68
|
-
const assets = state.balances.toAssets(
|
|
69
|
-
const assetsChange = state.balances.
|
|
70
|
-
token,
|
|
71
|
-
balance: balance - (initialBalances.get(token) ?? 0n)
|
|
72
|
-
})).filter(({ balance }) => balance !== 0n);
|
|
66
|
+
const assets = state.balances.toAssets(1n);
|
|
67
|
+
const assetsChange = state.balances.difference(initialBalances).toAssets(1n);
|
|
73
68
|
const totalValue = assets.reduce(
|
|
74
69
|
(acc, { token, balance }) => acc + market.priceOracle.convert(token, market.underlying, balance),
|
|
75
70
|
0n
|
|
@@ -31,17 +31,15 @@ function previewOpenCreditAccount(input, operation) {
|
|
|
31
31
|
);
|
|
32
32
|
const state = (0, import_applyInnerOperations.makeInnerOperationsState)();
|
|
33
33
|
(0, import_applyInnerOperations.applyInnerOperations)(sdk, operation.multicall, state);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
(acc, { token, balance }) => acc + market.priceOracle.convert(token, market.underlying, balance),
|
|
37
|
-
0n
|
|
34
|
+
const collateralValue = state.collateralAdded.sum(
|
|
35
|
+
(token, balance) => market.priceOracle.convert(token, market.underlying, balance)
|
|
38
36
|
);
|
|
39
|
-
collateral = (0, import_unwrapNativeCollateral.unwrapNativeCollateral)(
|
|
40
|
-
|
|
37
|
+
const collateral = (0, import_unwrapNativeCollateral.unwrapNativeCollateral)(
|
|
38
|
+
state.collateralAdded.toAssets(),
|
|
41
39
|
value,
|
|
42
40
|
sdk.addressProvider.getAddress(import_sdk.AP_WETH_TOKEN, import_sdk.NO_VERSION)
|
|
43
41
|
);
|
|
44
|
-
const assets = state.balances.
|
|
42
|
+
const assets = state.balances.toAssets(1n);
|
|
45
43
|
return {
|
|
46
44
|
operation: operation.operation,
|
|
47
45
|
creditManager: operation.creditManager,
|
|
@@ -620,14 +620,12 @@ class CreditAccountsServiceV310 extends import_base.SDKConstruct {
|
|
|
620
620
|
creditManager: cm.creditManager,
|
|
621
621
|
slippage
|
|
622
622
|
});
|
|
623
|
-
const operationCalls =
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
)
|
|
630
|
-
];
|
|
623
|
+
const operationCalls = await this.assembleCloseCreditAccountCalls({
|
|
624
|
+
creditAccount: ca,
|
|
625
|
+
routerCalls: routerCloseResult.calls,
|
|
626
|
+
assetsToWithdraw,
|
|
627
|
+
to
|
|
628
|
+
});
|
|
631
629
|
const calls = operation === "close" ? operationCalls : await this.#prependPriceUpdates(ca.creditManager, operationCalls, ca);
|
|
632
630
|
const tx = await this.#closeCreditAccountTx(
|
|
633
631
|
cm,
|
|
@@ -637,6 +635,32 @@ class CreditAccountsServiceV310 extends import_base.SDKConstruct {
|
|
|
637
635
|
);
|
|
638
636
|
return { tx, calls, routerCloseResult, creditFacade: cm.creditFacade };
|
|
639
637
|
}
|
|
638
|
+
/**
|
|
639
|
+
* {@inheritDoc ICreditAccountsService.assembleCloseCreditAccountCalls}
|
|
640
|
+
*/
|
|
641
|
+
async assembleCloseCreditAccountCalls({
|
|
642
|
+
creditAccount: ca,
|
|
643
|
+
routerCalls,
|
|
644
|
+
assetsToWithdraw,
|
|
645
|
+
to
|
|
646
|
+
}) {
|
|
647
|
+
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
648
|
+
await this.sdk.tokensMeta.loadTokenData(cm.underlying);
|
|
649
|
+
const underlying = this.sdk.tokensMeta.mustGet(cm.underlying);
|
|
650
|
+
if (this.sdk.tokensMeta.isRWAUnderlying(underlying)) {
|
|
651
|
+
throw new Error(
|
|
652
|
+
"closeCreditAccount is not supported for RWA underlying credit accounts"
|
|
653
|
+
);
|
|
654
|
+
}
|
|
655
|
+
return [
|
|
656
|
+
...routerCalls,
|
|
657
|
+
...this.#prepareDisableQuotas(ca),
|
|
658
|
+
...this.#prepareDecreaseDebt(ca),
|
|
659
|
+
...assetsToWithdraw.map(
|
|
660
|
+
(t) => this.#prepareWithdrawToken(ca.creditFacade, t, import_constants.MAX_UINT256, to)
|
|
661
|
+
)
|
|
662
|
+
];
|
|
663
|
+
}
|
|
640
664
|
/**
|
|
641
665
|
* {@inheritDoc ICreditAccountsService.updateQuotas}
|
|
642
666
|
**/
|
|
@@ -1306,8 +1330,23 @@ class CreditAccountsServiceV310 extends import_base.SDKConstruct {
|
|
|
1306
1330
|
/**
|
|
1307
1331
|
* {@inheritDoc ICreditAccountsService.repayCreditAccount}
|
|
1308
1332
|
*/
|
|
1309
|
-
async repayCreditAccount({
|
|
1310
|
-
operation,
|
|
1333
|
+
async repayCreditAccount(props) {
|
|
1334
|
+
const { operation, creditAccount: ca } = props;
|
|
1335
|
+
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
1336
|
+
const operationCalls = await this.assembleRepayCreditAccountCalls(props);
|
|
1337
|
+
const calls = operation === "close" ? operationCalls : await this.#prependPriceUpdates(ca.creditManager, operationCalls, ca);
|
|
1338
|
+
const tx = await this.#closeCreditAccountTx(
|
|
1339
|
+
cm,
|
|
1340
|
+
ca.creditAccount,
|
|
1341
|
+
calls,
|
|
1342
|
+
operation
|
|
1343
|
+
);
|
|
1344
|
+
return { tx, calls, creditFacade: cm.creditFacade };
|
|
1345
|
+
}
|
|
1346
|
+
/**
|
|
1347
|
+
* {@inheritDoc ICreditAccountsService.assembleRepayCreditAccountCalls}
|
|
1348
|
+
*/
|
|
1349
|
+
async assembleRepayCreditAccountCalls({
|
|
1311
1350
|
collateralAssets,
|
|
1312
1351
|
assetsToWithdraw,
|
|
1313
1352
|
creditAccount: ca,
|
|
@@ -1316,7 +1355,6 @@ class CreditAccountsServiceV310 extends import_base.SDKConstruct {
|
|
|
1316
1355
|
tokensToClaim,
|
|
1317
1356
|
calls: wrapCalls = []
|
|
1318
1357
|
}) {
|
|
1319
|
-
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
1320
1358
|
const addCollateral = collateralAssets.filter((a) => a.balance > 0);
|
|
1321
1359
|
const router = this.sdk.routerFor(ca);
|
|
1322
1360
|
const unwrapCalls = await this.getRedeemDiffCalls(1n, ca.creditManager) ?? [];
|
|
@@ -1335,14 +1373,7 @@ class CreditAccountsServiceV310 extends import_base.SDKConstruct {
|
|
|
1335
1373
|
(t) => this.#prepareWithdrawToken(ca.creditFacade, t.token, import_constants.MAX_UINT256, to)
|
|
1336
1374
|
)
|
|
1337
1375
|
];
|
|
1338
|
-
|
|
1339
|
-
const tx = await this.#closeCreditAccountTx(
|
|
1340
|
-
cm,
|
|
1341
|
-
ca.creditAccount,
|
|
1342
|
-
calls,
|
|
1343
|
-
operation
|
|
1344
|
-
);
|
|
1345
|
-
return { tx, calls, creditFacade: cm.creditFacade };
|
|
1376
|
+
return operationCalls;
|
|
1346
1377
|
}
|
|
1347
1378
|
/**
|
|
1348
1379
|
* {@inheritDoc ICreditAccountsService.repayAndLiquidateCreditAccount}
|
|
@@ -33,11 +33,17 @@ class AssetsMap extends import_AddressMap.AddressMap {
|
|
|
33
33
|
name
|
|
34
34
|
);
|
|
35
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Looks up a token balance, treating a missing key as `0n`.
|
|
38
|
+
*/
|
|
39
|
+
getOrZero(address) {
|
|
40
|
+
return this.get(address) ?? 0n;
|
|
41
|
+
}
|
|
36
42
|
/**
|
|
37
43
|
* Adds `amount` to the token balance, treating a missing key as `0n`.
|
|
38
44
|
*/
|
|
39
45
|
inc(address, amount) {
|
|
40
|
-
this.upsert(address,
|
|
46
|
+
this.upsert(address, this.getOrZero(address) + amount);
|
|
41
47
|
}
|
|
42
48
|
/**
|
|
43
49
|
* Subtracts `amount` from the token balance, treating a missing key as `0n`.
|
|
@@ -45,12 +51,41 @@ class AssetsMap extends import_AddressMap.AddressMap {
|
|
|
45
51
|
dec(address, amount) {
|
|
46
52
|
this.inc(address, -amount);
|
|
47
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Per-token difference `this - before`, non-zero entries only.
|
|
56
|
+
*
|
|
57
|
+
* Assumes this map's keys are a superset of `before`'s keys; tokens present
|
|
58
|
+
* only in `before` are not reported.
|
|
59
|
+
*/
|
|
60
|
+
difference(before) {
|
|
61
|
+
const result = new AssetsMap();
|
|
62
|
+
for (const [token, balance] of this.entries()) {
|
|
63
|
+
const change = balance - before.getOrZero(token);
|
|
64
|
+
if (change !== 0n) {
|
|
65
|
+
result.upsert(token, change);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Sums `fn` over all entries.
|
|
72
|
+
*/
|
|
73
|
+
sum(fn) {
|
|
74
|
+
return this.entries().reduce(
|
|
75
|
+
(acc, [token, balance]) => acc + fn(token, balance),
|
|
76
|
+
0n
|
|
77
|
+
);
|
|
78
|
+
}
|
|
48
79
|
/**
|
|
49
80
|
* Converts the map to an array of {@link Asset} objects.
|
|
50
|
-
* @param
|
|
81
|
+
* @param minBalance - If provided, keeps only entries with a balance
|
|
82
|
+
* strictly greater than this threshold (e.g. `1n` to filter out dust).
|
|
51
83
|
*/
|
|
52
|
-
toAssets(
|
|
53
|
-
|
|
84
|
+
toAssets(minBalance) {
|
|
85
|
+
const abs = (balance) => balance > 0n ? balance : -balance;
|
|
86
|
+
return this.entries().filter(
|
|
87
|
+
([, balance]) => minBalance === void 0 || abs(balance) > minBalance
|
|
88
|
+
).map(([token, balance]) => ({ token, balance }));
|
|
54
89
|
}
|
|
55
90
|
clone() {
|
|
56
91
|
return new AssetsMap(this.entries(), this.name);
|
|
@@ -24,7 +24,7 @@ function applyInnerOperations(sdk, multicall, state) {
|
|
|
24
24
|
state.balances.inc(op.token, op.amount);
|
|
25
25
|
break;
|
|
26
26
|
case "WithdrawCollateral": {
|
|
27
|
-
const running = state.balances.
|
|
27
|
+
const running = state.balances.getOrZero(op.token);
|
|
28
28
|
const amount = op.amount === MAX_UINT256 ? running > 0n ? running : 0n : op.amount;
|
|
29
29
|
state.balances.dec(op.token, amount);
|
|
30
30
|
state.collateralWithdrawn.inc(op.token, amount);
|
|
@@ -92,16 +92,14 @@ function applyQuotaChanges(initialQuotas, changes) {
|
|
|
92
92
|
if (change === MIN_INT96) {
|
|
93
93
|
final.upsert(token, 0n);
|
|
94
94
|
} else {
|
|
95
|
-
const next =
|
|
95
|
+
const next = final.getOrZero(token) + change;
|
|
96
96
|
final.upsert(token, next > 0n ? next : 0n);
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
})).filter(({ balance }) => balance !== 0n);
|
|
104
|
-
return { quotas, quotasChange };
|
|
99
|
+
return {
|
|
100
|
+
quotas: final.toAssets(0n),
|
|
101
|
+
quotasChange: final.difference(initialQuotas).toAssets()
|
|
102
|
+
};
|
|
105
103
|
}
|
|
106
104
|
export {
|
|
107
105
|
applyInnerOperations,
|
|
@@ -35,9 +35,7 @@ async function previewAdjustCreditAccount(input, operation, options) {
|
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
const state = makeInnerOperationsState();
|
|
38
|
-
|
|
39
|
-
state.balances.upsert(token, balance);
|
|
40
|
-
}
|
|
38
|
+
state.balances = initialBalances.clone();
|
|
41
39
|
state.debt = ca.debt;
|
|
42
40
|
state.totalDebt = ca.debt + ca.accruedInterest + ca.accruedFees;
|
|
43
41
|
applyInnerOperations(sdk, operation.multicall, state);
|
|
@@ -50,11 +48,8 @@ async function previewAdjustCreditAccount(input, operation, options) {
|
|
|
50
48
|
initialQuotas,
|
|
51
49
|
state.quotaChanges
|
|
52
50
|
);
|
|
53
|
-
const assets = state.balances.toAssets(
|
|
54
|
-
const assetsChange = state.balances.
|
|
55
|
-
token,
|
|
56
|
-
balance: balance - (initialBalances.get(token) ?? 0n)
|
|
57
|
-
})).filter(({ balance }) => balance !== 0n);
|
|
51
|
+
const assets = state.balances.toAssets(1n);
|
|
52
|
+
const assetsChange = state.balances.difference(initialBalances).toAssets(1n);
|
|
58
53
|
const totalValue = assets.reduce(
|
|
59
54
|
(acc, { token, balance }) => acc + market.priceOracle.convert(token, market.underlying, balance),
|
|
60
55
|
0n
|
|
@@ -14,17 +14,15 @@ function previewOpenCreditAccount(input, operation) {
|
|
|
14
14
|
);
|
|
15
15
|
const state = makeInnerOperationsState();
|
|
16
16
|
applyInnerOperations(sdk, operation.multicall, state);
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
(acc, { token, balance }) => acc + market.priceOracle.convert(token, market.underlying, balance),
|
|
20
|
-
0n
|
|
17
|
+
const collateralValue = state.collateralAdded.sum(
|
|
18
|
+
(token, balance) => market.priceOracle.convert(token, market.underlying, balance)
|
|
21
19
|
);
|
|
22
|
-
collateral = unwrapNativeCollateral(
|
|
23
|
-
|
|
20
|
+
const collateral = unwrapNativeCollateral(
|
|
21
|
+
state.collateralAdded.toAssets(),
|
|
24
22
|
value,
|
|
25
23
|
sdk.addressProvider.getAddress(AP_WETH_TOKEN, NO_VERSION)
|
|
26
24
|
);
|
|
27
|
-
const assets = state.balances.
|
|
25
|
+
const assets = state.balances.toAssets(1n);
|
|
28
26
|
return {
|
|
29
27
|
operation: operation.operation,
|
|
30
28
|
creditManager: operation.creditManager,
|
|
@@ -615,14 +615,12 @@ class CreditAccountsServiceV310 extends SDKConstruct {
|
|
|
615
615
|
creditManager: cm.creditManager,
|
|
616
616
|
slippage
|
|
617
617
|
});
|
|
618
|
-
const operationCalls =
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
)
|
|
625
|
-
];
|
|
618
|
+
const operationCalls = await this.assembleCloseCreditAccountCalls({
|
|
619
|
+
creditAccount: ca,
|
|
620
|
+
routerCalls: routerCloseResult.calls,
|
|
621
|
+
assetsToWithdraw,
|
|
622
|
+
to
|
|
623
|
+
});
|
|
626
624
|
const calls = operation === "close" ? operationCalls : await this.#prependPriceUpdates(ca.creditManager, operationCalls, ca);
|
|
627
625
|
const tx = await this.#closeCreditAccountTx(
|
|
628
626
|
cm,
|
|
@@ -632,6 +630,32 @@ class CreditAccountsServiceV310 extends SDKConstruct {
|
|
|
632
630
|
);
|
|
633
631
|
return { tx, calls, routerCloseResult, creditFacade: cm.creditFacade };
|
|
634
632
|
}
|
|
633
|
+
/**
|
|
634
|
+
* {@inheritDoc ICreditAccountsService.assembleCloseCreditAccountCalls}
|
|
635
|
+
*/
|
|
636
|
+
async assembleCloseCreditAccountCalls({
|
|
637
|
+
creditAccount: ca,
|
|
638
|
+
routerCalls,
|
|
639
|
+
assetsToWithdraw,
|
|
640
|
+
to
|
|
641
|
+
}) {
|
|
642
|
+
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
643
|
+
await this.sdk.tokensMeta.loadTokenData(cm.underlying);
|
|
644
|
+
const underlying = this.sdk.tokensMeta.mustGet(cm.underlying);
|
|
645
|
+
if (this.sdk.tokensMeta.isRWAUnderlying(underlying)) {
|
|
646
|
+
throw new Error(
|
|
647
|
+
"closeCreditAccount is not supported for RWA underlying credit accounts"
|
|
648
|
+
);
|
|
649
|
+
}
|
|
650
|
+
return [
|
|
651
|
+
...routerCalls,
|
|
652
|
+
...this.#prepareDisableQuotas(ca),
|
|
653
|
+
...this.#prepareDecreaseDebt(ca),
|
|
654
|
+
...assetsToWithdraw.map(
|
|
655
|
+
(t) => this.#prepareWithdrawToken(ca.creditFacade, t, MAX_UINT256, to)
|
|
656
|
+
)
|
|
657
|
+
];
|
|
658
|
+
}
|
|
635
659
|
/**
|
|
636
660
|
* {@inheritDoc ICreditAccountsService.updateQuotas}
|
|
637
661
|
**/
|
|
@@ -1301,8 +1325,23 @@ class CreditAccountsServiceV310 extends SDKConstruct {
|
|
|
1301
1325
|
/**
|
|
1302
1326
|
* {@inheritDoc ICreditAccountsService.repayCreditAccount}
|
|
1303
1327
|
*/
|
|
1304
|
-
async repayCreditAccount({
|
|
1305
|
-
operation,
|
|
1328
|
+
async repayCreditAccount(props) {
|
|
1329
|
+
const { operation, creditAccount: ca } = props;
|
|
1330
|
+
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
1331
|
+
const operationCalls = await this.assembleRepayCreditAccountCalls(props);
|
|
1332
|
+
const calls = operation === "close" ? operationCalls : await this.#prependPriceUpdates(ca.creditManager, operationCalls, ca);
|
|
1333
|
+
const tx = await this.#closeCreditAccountTx(
|
|
1334
|
+
cm,
|
|
1335
|
+
ca.creditAccount,
|
|
1336
|
+
calls,
|
|
1337
|
+
operation
|
|
1338
|
+
);
|
|
1339
|
+
return { tx, calls, creditFacade: cm.creditFacade };
|
|
1340
|
+
}
|
|
1341
|
+
/**
|
|
1342
|
+
* {@inheritDoc ICreditAccountsService.assembleRepayCreditAccountCalls}
|
|
1343
|
+
*/
|
|
1344
|
+
async assembleRepayCreditAccountCalls({
|
|
1306
1345
|
collateralAssets,
|
|
1307
1346
|
assetsToWithdraw,
|
|
1308
1347
|
creditAccount: ca,
|
|
@@ -1311,7 +1350,6 @@ class CreditAccountsServiceV310 extends SDKConstruct {
|
|
|
1311
1350
|
tokensToClaim,
|
|
1312
1351
|
calls: wrapCalls = []
|
|
1313
1352
|
}) {
|
|
1314
|
-
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
1315
1353
|
const addCollateral = collateralAssets.filter((a) => a.balance > 0);
|
|
1316
1354
|
const router = this.sdk.routerFor(ca);
|
|
1317
1355
|
const unwrapCalls = await this.getRedeemDiffCalls(1n, ca.creditManager) ?? [];
|
|
@@ -1330,14 +1368,7 @@ class CreditAccountsServiceV310 extends SDKConstruct {
|
|
|
1330
1368
|
(t) => this.#prepareWithdrawToken(ca.creditFacade, t.token, MAX_UINT256, to)
|
|
1331
1369
|
)
|
|
1332
1370
|
];
|
|
1333
|
-
|
|
1334
|
-
const tx = await this.#closeCreditAccountTx(
|
|
1335
|
-
cm,
|
|
1336
|
-
ca.creditAccount,
|
|
1337
|
-
calls,
|
|
1338
|
-
operation
|
|
1339
|
-
);
|
|
1340
|
-
return { tx, calls, creditFacade: cm.creditFacade };
|
|
1371
|
+
return operationCalls;
|
|
1341
1372
|
}
|
|
1342
1373
|
/**
|
|
1343
1374
|
* {@inheritDoc ICreditAccountsService.repayAndLiquidateCreditAccount}
|
|
@@ -10,11 +10,17 @@ class AssetsMap extends AddressMap {
|
|
|
10
10
|
name
|
|
11
11
|
);
|
|
12
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Looks up a token balance, treating a missing key as `0n`.
|
|
15
|
+
*/
|
|
16
|
+
getOrZero(address) {
|
|
17
|
+
return this.get(address) ?? 0n;
|
|
18
|
+
}
|
|
13
19
|
/**
|
|
14
20
|
* Adds `amount` to the token balance, treating a missing key as `0n`.
|
|
15
21
|
*/
|
|
16
22
|
inc(address, amount) {
|
|
17
|
-
this.upsert(address,
|
|
23
|
+
this.upsert(address, this.getOrZero(address) + amount);
|
|
18
24
|
}
|
|
19
25
|
/**
|
|
20
26
|
* Subtracts `amount` from the token balance, treating a missing key as `0n`.
|
|
@@ -22,12 +28,41 @@ class AssetsMap extends AddressMap {
|
|
|
22
28
|
dec(address, amount) {
|
|
23
29
|
this.inc(address, -amount);
|
|
24
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Per-token difference `this - before`, non-zero entries only.
|
|
33
|
+
*
|
|
34
|
+
* Assumes this map's keys are a superset of `before`'s keys; tokens present
|
|
35
|
+
* only in `before` are not reported.
|
|
36
|
+
*/
|
|
37
|
+
difference(before) {
|
|
38
|
+
const result = new AssetsMap();
|
|
39
|
+
for (const [token, balance] of this.entries()) {
|
|
40
|
+
const change = balance - before.getOrZero(token);
|
|
41
|
+
if (change !== 0n) {
|
|
42
|
+
result.upsert(token, change);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Sums `fn` over all entries.
|
|
49
|
+
*/
|
|
50
|
+
sum(fn) {
|
|
51
|
+
return this.entries().reduce(
|
|
52
|
+
(acc, [token, balance]) => acc + fn(token, balance),
|
|
53
|
+
0n
|
|
54
|
+
);
|
|
55
|
+
}
|
|
25
56
|
/**
|
|
26
57
|
* Converts the map to an array of {@link Asset} objects.
|
|
27
|
-
* @param
|
|
58
|
+
* @param minBalance - If provided, keeps only entries with a balance
|
|
59
|
+
* strictly greater than this threshold (e.g. `1n` to filter out dust).
|
|
28
60
|
*/
|
|
29
|
-
toAssets(
|
|
30
|
-
|
|
61
|
+
toAssets(minBalance) {
|
|
62
|
+
const abs = (balance) => balance > 0n ? balance : -balance;
|
|
63
|
+
return this.entries().filter(
|
|
64
|
+
([, balance]) => minBalance === void 0 || abs(balance) > minBalance
|
|
65
|
+
).map(([token, balance]) => ({ token, balance }));
|
|
31
66
|
}
|
|
32
67
|
clone() {
|
|
33
68
|
return new AssetsMap(this.entries(), this.name);
|
|
@@ -6,7 +6,7 @@ import type { RWAOpenAccountRequirements } from "../market/rwa/index.js";
|
|
|
6
6
|
import type { OnchainSDK } from "../OnchainSDK.js";
|
|
7
7
|
import type { RouterCASlice } from "../router/index.js";
|
|
8
8
|
import type { MultiCall, RawTx } from "../types/index.js";
|
|
9
|
-
import type { AccountToCheck, AddCollateralProps, AssembleCaUpdateCallsProps, ChangeDeptProps, ClaimDelayedProps, ClaimFarmRewardsProps, CloseCreditAccountProps, CloseCreditAccountResult, CreditAccountOperationResult, CreditAccountTokensSlice, CreditManagerOperationResult, DefaultPartialLiquidationParams, ExecuteSwapProps, FullyLiquidateProps, FullyLiquidateResult, GetApprovalAddressProps, GetConnectedBotsResult, GetConnectedMigrationBotsResult, GetCreditAccountsOptions, GetOpenAccountRequirementsProps, GetPendingWithdrawalsProps, GetPendingWithdrawalsResult, ICreditAccountsService, OpenCAProps, PartiallyLiquidateProps, PreviewDelayedWithdrawalProps, PreviewDelayedWithdrawalResult, RepayAndLiquidateCreditAccountProps, RepayCreditAccountProps, Rewards, SetBotProps, StartDelayedWithdrawalProps, UpdateQuotasProps, WithdrawCollateralProps } from "./types.js";
|
|
9
|
+
import type { AccountToCheck, AddCollateralProps, AssembleCaUpdateCallsProps, AssembleCloseCreditAccountCallsProps, AssembleRepayCreditAccountCallsProps, ChangeDeptProps, ClaimDelayedProps, ClaimFarmRewardsProps, CloseCreditAccountProps, CloseCreditAccountResult, CreditAccountOperationResult, CreditAccountTokensSlice, CreditManagerOperationResult, DefaultPartialLiquidationParams, ExecuteSwapProps, FullyLiquidateProps, FullyLiquidateResult, GetApprovalAddressProps, GetConnectedBotsResult, GetConnectedMigrationBotsResult, GetCreditAccountsOptions, GetOpenAccountRequirementsProps, GetPendingWithdrawalsProps, GetPendingWithdrawalsResult, ICreditAccountsService, OpenCAProps, PartiallyLiquidateProps, PreviewDelayedWithdrawalProps, PreviewDelayedWithdrawalResult, RepayAndLiquidateCreditAccountProps, RepayCreditAccountProps, Rewards, SetBotProps, StartDelayedWithdrawalProps, UpdateQuotasProps, WithdrawCollateralProps } from "./types.js";
|
|
10
10
|
/**
|
|
11
11
|
* Options for configuring the credit account service.
|
|
12
12
|
**/
|
|
@@ -79,6 +79,10 @@ export declare class CreditAccountsServiceV310 extends SDKConstruct implements I
|
|
|
79
79
|
* {@inheritDoc ICreditAccountsService.closeCreditAccount}
|
|
80
80
|
**/
|
|
81
81
|
closeCreditAccount({ operation, assetsToWithdraw, creditAccount: ca, to, slippage, closePath, }: CloseCreditAccountProps): Promise<CloseCreditAccountResult>;
|
|
82
|
+
/**
|
|
83
|
+
* {@inheritDoc ICreditAccountsService.assembleCloseCreditAccountCalls}
|
|
84
|
+
*/
|
|
85
|
+
assembleCloseCreditAccountCalls({ creditAccount: ca, routerCalls, assetsToWithdraw, to, }: AssembleCloseCreditAccountCallsProps): Promise<Array<MultiCall>>;
|
|
82
86
|
/**
|
|
83
87
|
* {@inheritDoc ICreditAccountsService.updateQuotas}
|
|
84
88
|
**/
|
|
@@ -178,7 +182,11 @@ export declare class CreditAccountsServiceV310 extends SDKConstruct implements I
|
|
|
178
182
|
/**
|
|
179
183
|
* {@inheritDoc ICreditAccountsService.repayCreditAccount}
|
|
180
184
|
*/
|
|
181
|
-
repayCreditAccount(
|
|
185
|
+
repayCreditAccount(props: RepayCreditAccountProps): Promise<CreditAccountOperationResult>;
|
|
186
|
+
/**
|
|
187
|
+
* {@inheritDoc ICreditAccountsService.assembleRepayCreditAccountCalls}
|
|
188
|
+
*/
|
|
189
|
+
assembleRepayCreditAccountCalls({ collateralAssets, assetsToWithdraw, creditAccount: ca, permits, to, tokensToClaim, calls: wrapCalls, }: AssembleRepayCreditAccountCallsProps): Promise<Array<MultiCall>>;
|
|
182
190
|
/**
|
|
183
191
|
* {@inheritDoc ICreditAccountsService.repayAndLiquidateCreditAccount}
|
|
184
192
|
*/
|
|
@@ -182,16 +182,27 @@ export interface CloseCreditAccountProps {
|
|
|
182
182
|
*/
|
|
183
183
|
closePath?: RouterCloseResult;
|
|
184
184
|
}
|
|
185
|
-
|
|
185
|
+
/**
|
|
186
|
+
* Input for {@link ICreditAccountsService.assembleCloseCreditAccountCalls}.
|
|
187
|
+
*/
|
|
188
|
+
export type AssembleCloseCreditAccountCallsProps = {
|
|
186
189
|
/**
|
|
187
|
-
*
|
|
190
|
+
* Minimal credit account data on which operation is performed.
|
|
188
191
|
*/
|
|
189
|
-
|
|
192
|
+
creditAccount: RouterCASlice;
|
|
190
193
|
/**
|
|
191
|
-
* close
|
|
194
|
+
* Pathfinder close router calls (`closePath.calls`).
|
|
192
195
|
*/
|
|
193
|
-
|
|
194
|
-
|
|
196
|
+
routerCalls: Array<MultiCall>;
|
|
197
|
+
/**
|
|
198
|
+
* Tokens to withdraw from credit account after close path swaps.
|
|
199
|
+
*/
|
|
200
|
+
assetsToWithdraw: Array<Address>;
|
|
201
|
+
/**
|
|
202
|
+
* Wallet address to withdraw tokens to.
|
|
203
|
+
*/
|
|
204
|
+
to: Address;
|
|
205
|
+
};
|
|
195
206
|
export interface RepayAndLiquidateCreditAccountProps {
|
|
196
207
|
/**
|
|
197
208
|
* Tokens to repay debt.
|
|
@@ -220,6 +231,31 @@ export interface RepayAndLiquidateCreditAccountProps {
|
|
|
220
231
|
permits: Record<string, PermitResult>;
|
|
221
232
|
tokensToClaim: Asset[];
|
|
222
233
|
}
|
|
234
|
+
/**
|
|
235
|
+
* Input for {@link ICreditAccountsService.assembleRepayCreditAccountCalls}.
|
|
236
|
+
*/
|
|
237
|
+
export type AssembleRepayCreditAccountCallsProps = {
|
|
238
|
+
collateralAssets: Array<Asset>;
|
|
239
|
+
assetsToWithdraw: Array<Asset>;
|
|
240
|
+
creditAccount: RouterCASlice;
|
|
241
|
+
to: Address;
|
|
242
|
+
permits: Record<string, PermitResult>;
|
|
243
|
+
tokensToClaim: Asset[];
|
|
244
|
+
/**
|
|
245
|
+
* RWA wrap multicall entries (from getRWAWrapCalls).
|
|
246
|
+
*/
|
|
247
|
+
calls?: Array<MultiCall>;
|
|
248
|
+
};
|
|
249
|
+
export interface RepayCreditAccountProps extends RepayAndLiquidateCreditAccountProps {
|
|
250
|
+
/**
|
|
251
|
+
* RWA wrap multicall entries (from getRWAWrapCalls).
|
|
252
|
+
*/
|
|
253
|
+
calls?: Array<MultiCall>;
|
|
254
|
+
/**
|
|
255
|
+
* close or zeroDebt
|
|
256
|
+
*/
|
|
257
|
+
operation: CloseOptions;
|
|
258
|
+
}
|
|
223
259
|
export interface PrepareUpdateQuotasProps {
|
|
224
260
|
/**
|
|
225
261
|
* average quota for desired token
|
|
@@ -798,6 +834,17 @@ export interface ICreditAccountsService extends Construct {
|
|
|
798
834
|
* @returns Raw transaction ready to be signed and sent
|
|
799
835
|
*/
|
|
800
836
|
partiallyLiquidate(props: PartiallyLiquidateProps): Promise<RawTx>;
|
|
837
|
+
/**
|
|
838
|
+
* Builds close multicall calls without price feed updates.
|
|
839
|
+
*
|
|
840
|
+
* Same operation sequence as {@link closeCreditAccount} (close path swaps,
|
|
841
|
+
* disable quotas, decrease debt, withdraw assets), but does not prepend
|
|
842
|
+
* price updates and does not build the facade transaction.
|
|
843
|
+
*
|
|
844
|
+
* @param props - {@link AssembleCloseCreditAccountCallsProps}
|
|
845
|
+
* @returns Raw facade multicall payload for close (before price feed updates)
|
|
846
|
+
*/
|
|
847
|
+
assembleCloseCreditAccountCalls(props: AssembleCloseCreditAccountCallsProps): Promise<Array<MultiCall>>;
|
|
801
848
|
/**
|
|
802
849
|
* Closes credit account or closes credit account and keeps it open with zero debt.
|
|
803
850
|
* - Ca is closed in the following order: price update -> close path to swap all tokens into underlying ->
|
|
@@ -1009,6 +1056,17 @@ export interface ICreditAccountsService extends Construct {
|
|
|
1009
1056
|
* @return All necessary data to execute the transaction (call, credit facade)
|
|
1010
1057
|
*/
|
|
1011
1058
|
withdrawCollateral(props: WithdrawCollateralProps): Promise<CreditAccountOperationResult>;
|
|
1059
|
+
/**
|
|
1060
|
+
* Builds repay multicall calls without price feed updates.
|
|
1061
|
+
*
|
|
1062
|
+
* Same operation sequence as {@link repayCreditAccount} (add collateral, wrap calls,
|
|
1063
|
+
* disable quotas, decrease debt, redeem/unwrap, claim rewards, withdraw assets),
|
|
1064
|
+
* but does not prepend price updates and does not build the facade transaction.
|
|
1065
|
+
*
|
|
1066
|
+
* @param props - {@link AssembleRepayCreditAccountCallsProps}
|
|
1067
|
+
* @returns Raw facade multicall payload for repay (before price feed updates)
|
|
1068
|
+
*/
|
|
1069
|
+
assembleRepayCreditAccountCalls(props: AssembleRepayCreditAccountCallsProps): Promise<Array<MultiCall>>;
|
|
1012
1070
|
/**
|
|
1013
1071
|
* Fully repays credit account or repays credit account and keeps it open with zero debt
|
|
1014
1072
|
* - Repays in the following order: price update -> add collateral to cover the debt ->
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Address } from "viem";
|
|
1
2
|
import type { Asset } from "../base/types.js";
|
|
2
3
|
import { AddressMap } from "./AddressMap.js";
|
|
3
4
|
/**
|
|
@@ -10,6 +11,10 @@ export declare class AssetsMap extends AddressMap<bigint> {
|
|
|
10
11
|
* @param name - Optional label used in error messages when a lookup fails.
|
|
11
12
|
*/
|
|
12
13
|
constructor(entries?: Array<[string, bigint] | Asset>, name?: string);
|
|
14
|
+
/**
|
|
15
|
+
* Looks up a token balance, treating a missing key as `0n`.
|
|
16
|
+
*/
|
|
17
|
+
getOrZero(address: string): bigint;
|
|
13
18
|
/**
|
|
14
19
|
* Adds `amount` to the token balance, treating a missing key as `0n`.
|
|
15
20
|
*/
|
|
@@ -18,10 +23,22 @@ export declare class AssetsMap extends AddressMap<bigint> {
|
|
|
18
23
|
* Subtracts `amount` from the token balance, treating a missing key as `0n`.
|
|
19
24
|
*/
|
|
20
25
|
dec(address: string, amount: bigint): void;
|
|
26
|
+
/**
|
|
27
|
+
* Per-token difference `this - before`, non-zero entries only.
|
|
28
|
+
*
|
|
29
|
+
* Assumes this map's keys are a superset of `before`'s keys; tokens present
|
|
30
|
+
* only in `before` are not reported.
|
|
31
|
+
*/
|
|
32
|
+
difference(before: AssetsMap): AssetsMap;
|
|
33
|
+
/**
|
|
34
|
+
* Sums `fn` over all entries.
|
|
35
|
+
*/
|
|
36
|
+
sum(fn: (token: Address, balance: bigint) => bigint): bigint;
|
|
21
37
|
/**
|
|
22
38
|
* Converts the map to an array of {@link Asset} objects.
|
|
23
|
-
* @param
|
|
39
|
+
* @param minBalance - If provided, keeps only entries with a balance
|
|
40
|
+
* strictly greater than this threshold (e.g. `1n` to filter out dust).
|
|
24
41
|
*/
|
|
25
|
-
toAssets(
|
|
42
|
+
toAssets(minBalance?: bigint): Asset[];
|
|
26
43
|
clone(): AssetsMap;
|
|
27
44
|
}
|