@gearbox-protocol/sdk 14.12.0-next.41 → 14.12.0-next.42
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/buildDelayedPreview.js +38 -16
- package/dist/cjs/sdk/accounts/withdrawal-compressor/intent-codec.js +11 -8
- package/dist/esm/preview/preview/buildDelayedPreview.js +41 -17
- package/dist/esm/sdk/accounts/withdrawal-compressor/intent-codec.js +11 -8
- package/dist/types/preview/preview/buildDelayedPreview.d.ts +8 -26
- package/dist/types/sdk/accounts/withdrawal-compressor/intent-codec.d.ts +1 -1
- package/dist/types/sdk/accounts/withdrawal-compressor/types.d.ts +13 -3
- package/package.json +1 -1
|
@@ -38,23 +38,12 @@ function buildDelayedPreview(afterInstant, before, detected, convert, receivedTo
|
|
|
38
38
|
repayFromClaim(post, request.claimToken, converter.convert, claimed);
|
|
39
39
|
break;
|
|
40
40
|
case "WITHDRAW_COLLATERAL": {
|
|
41
|
-
|
|
42
|
-
intent.withdrawAmount,
|
|
43
|
-
post.balances.getOrZero(intent.withdrawToken)
|
|
44
|
-
);
|
|
45
|
-
if (withdrawn > 0n) {
|
|
46
|
-
post.balances.dec(intent.withdrawToken, withdrawn);
|
|
47
|
-
collateralWithdrawn.upsert(intent.withdrawToken, withdrawn);
|
|
48
|
-
}
|
|
49
|
-
const claimSpentOnWithdrawal = (0, import_viem.isAddressEqual)(
|
|
50
|
-
intent.withdrawToken,
|
|
51
|
-
request.claimToken
|
|
52
|
-
) ? withdrawn : 0n;
|
|
53
|
-
repayFromClaim(
|
|
41
|
+
applyWithdrawCollateral(
|
|
54
42
|
post,
|
|
55
|
-
request
|
|
56
|
-
|
|
57
|
-
|
|
43
|
+
request,
|
|
44
|
+
intent,
|
|
45
|
+
converter,
|
|
46
|
+
collateralWithdrawn
|
|
58
47
|
);
|
|
59
48
|
break;
|
|
60
49
|
}
|
|
@@ -89,6 +78,39 @@ function applyClaim(post, request) {
|
|
|
89
78
|
post.balances.inc(request.claimToken, claimed);
|
|
90
79
|
return claimed;
|
|
91
80
|
}
|
|
81
|
+
function applyWithdrawCollateral(post, request, intent, converter, collateralWithdrawn) {
|
|
82
|
+
const { withdrawToken, withdrawAmount, debtRepaid } = intent;
|
|
83
|
+
const { claimToken } = request;
|
|
84
|
+
const sameToken = (0, import_viem.isAddressEqual)(withdrawToken, claimToken);
|
|
85
|
+
const fromBalance = import_common_utils.BigIntMath.min(
|
|
86
|
+
withdrawAmount,
|
|
87
|
+
post.balances.getOrZero(withdrawToken)
|
|
88
|
+
);
|
|
89
|
+
post.balances.dec(withdrawToken, fromBalance);
|
|
90
|
+
let withdrawn = fromBalance;
|
|
91
|
+
const missing = withdrawAmount - fromBalance;
|
|
92
|
+
if (missing > 0n && !sameToken) {
|
|
93
|
+
const available = post.balances.getOrZero(claimToken);
|
|
94
|
+
const cost = converter.convert(withdrawToken, claimToken, missing);
|
|
95
|
+
if (cost > 0n && cost <= available) {
|
|
96
|
+
post.balances.dec(claimToken, cost);
|
|
97
|
+
withdrawn += missing;
|
|
98
|
+
} else if (available > 0n) {
|
|
99
|
+
post.balances.dec(claimToken, available);
|
|
100
|
+
withdrawn += converter.convert(claimToken, withdrawToken, available);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (withdrawn > 0n) {
|
|
104
|
+
collateralWithdrawn.upsert(withdrawToken, withdrawn);
|
|
105
|
+
}
|
|
106
|
+
const remaining = post.balances.getOrZero(claimToken);
|
|
107
|
+
if (remaining > 0n) {
|
|
108
|
+
const proceeds = converter.convert(claimToken, post.underlying, remaining);
|
|
109
|
+
post.balances.dec(claimToken, remaining);
|
|
110
|
+
post.balances.inc(post.underlying, proceeds);
|
|
111
|
+
post.repay(import_common_utils.BigIntMath.min(proceeds, debtRepaid));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
92
114
|
function repayFromClaim(post, claimToken, convert, amount) {
|
|
93
115
|
const spent = import_common_utils.BigIntMath.min(amount, post.balances.getOrZero(claimToken));
|
|
94
116
|
if (spent <= 0n) {
|
|
@@ -25,7 +25,7 @@ __export(intent_codec_exports, {
|
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(intent_codec_exports);
|
|
27
27
|
var import_viem = require("viem");
|
|
28
|
-
const DELAYED_INTENT_VERSION =
|
|
28
|
+
const DELAYED_INTENT_VERSION = 2;
|
|
29
29
|
const DELAYED_INTENT_TYPES = {
|
|
30
30
|
INCREASE_LEVERAGE: 1,
|
|
31
31
|
DEPOSIT: 2,
|
|
@@ -43,7 +43,9 @@ const TO_PARAMS = [...HEADER_PARAMS, { type: "address", name: "to" }];
|
|
|
43
43
|
const WITHDRAW_COLLATERAL_PARAMS = [
|
|
44
44
|
...TO_PARAMS,
|
|
45
45
|
{ type: "address", name: "withdrawToken" },
|
|
46
|
-
{ type: "uint256", name: "withdrawAmount" }
|
|
46
|
+
{ type: "uint256", name: "withdrawAmount" },
|
|
47
|
+
{ type: "address", name: "sourceToken" },
|
|
48
|
+
{ type: "uint256", name: "debtRepaid" }
|
|
47
49
|
];
|
|
48
50
|
function encodeDelayedIntent(intent) {
|
|
49
51
|
const version = DELAYED_INTENT_VERSION;
|
|
@@ -58,7 +60,9 @@ function encodeDelayedIntent(intent) {
|
|
|
58
60
|
intentType,
|
|
59
61
|
intent.to,
|
|
60
62
|
intent.withdrawToken,
|
|
61
|
-
intent.withdrawAmount
|
|
63
|
+
intent.withdrawAmount,
|
|
64
|
+
intent.sourceToken,
|
|
65
|
+
intent.debtRepaid
|
|
62
66
|
]);
|
|
63
67
|
case "DEPOSIT":
|
|
64
68
|
case "DEPOSIT_AND_INCREASE_LEVERAGE":
|
|
@@ -86,15 +90,14 @@ function decodeDelayedIntent(data) {
|
|
|
86
90
|
case DELAYED_INTENT_TYPES.DEPOSIT_AND_INCREASE_LEVERAGE:
|
|
87
91
|
return { type: "DEPOSIT_AND_INCREASE_LEVERAGE" };
|
|
88
92
|
case DELAYED_INTENT_TYPES.WITHDRAW_COLLATERAL: {
|
|
89
|
-
const [, , to, withdrawToken, withdrawAmount] = (0, import_viem.decodeAbiParameters)(
|
|
90
|
-
WITHDRAW_COLLATERAL_PARAMS,
|
|
91
|
-
data
|
|
92
|
-
);
|
|
93
|
+
const [, , to, withdrawToken, withdrawAmount, sourceToken, debtRepaid] = (0, import_viem.decodeAbiParameters)(WITHDRAW_COLLATERAL_PARAMS, data);
|
|
93
94
|
return {
|
|
94
95
|
type: "WITHDRAW_COLLATERAL",
|
|
95
96
|
to,
|
|
96
97
|
withdrawToken,
|
|
97
|
-
withdrawAmount
|
|
98
|
+
withdrawAmount,
|
|
99
|
+
sourceToken,
|
|
100
|
+
debtRepaid
|
|
98
101
|
};
|
|
99
102
|
}
|
|
100
103
|
case DELAYED_INTENT_TYPES.CLOSE_ACCOUNT: {
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { isAddressEqual } from "viem";
|
|
2
2
|
import { BigIntMath } from "../../common-utils/index.js";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
AssetsMap
|
|
5
|
+
} from "../../sdk/index.js";
|
|
4
6
|
import {
|
|
5
7
|
ERROR_UNPRICEABLE_TOKEN,
|
|
6
8
|
PREVIEW_DUST
|
|
@@ -18,23 +20,12 @@ function buildDelayedPreview(afterInstant, before, detected, convert, receivedTo
|
|
|
18
20
|
repayFromClaim(post, request.claimToken, converter.convert, claimed);
|
|
19
21
|
break;
|
|
20
22
|
case "WITHDRAW_COLLATERAL": {
|
|
21
|
-
|
|
22
|
-
intent.withdrawAmount,
|
|
23
|
-
post.balances.getOrZero(intent.withdrawToken)
|
|
24
|
-
);
|
|
25
|
-
if (withdrawn > 0n) {
|
|
26
|
-
post.balances.dec(intent.withdrawToken, withdrawn);
|
|
27
|
-
collateralWithdrawn.upsert(intent.withdrawToken, withdrawn);
|
|
28
|
-
}
|
|
29
|
-
const claimSpentOnWithdrawal = isAddressEqual(
|
|
30
|
-
intent.withdrawToken,
|
|
31
|
-
request.claimToken
|
|
32
|
-
) ? withdrawn : 0n;
|
|
33
|
-
repayFromClaim(
|
|
23
|
+
applyWithdrawCollateral(
|
|
34
24
|
post,
|
|
35
|
-
request
|
|
36
|
-
|
|
37
|
-
|
|
25
|
+
request,
|
|
26
|
+
intent,
|
|
27
|
+
converter,
|
|
28
|
+
collateralWithdrawn
|
|
38
29
|
);
|
|
39
30
|
break;
|
|
40
31
|
}
|
|
@@ -69,6 +60,39 @@ function applyClaim(post, request) {
|
|
|
69
60
|
post.balances.inc(request.claimToken, claimed);
|
|
70
61
|
return claimed;
|
|
71
62
|
}
|
|
63
|
+
function applyWithdrawCollateral(post, request, intent, converter, collateralWithdrawn) {
|
|
64
|
+
const { withdrawToken, withdrawAmount, debtRepaid } = intent;
|
|
65
|
+
const { claimToken } = request;
|
|
66
|
+
const sameToken = isAddressEqual(withdrawToken, claimToken);
|
|
67
|
+
const fromBalance = BigIntMath.min(
|
|
68
|
+
withdrawAmount,
|
|
69
|
+
post.balances.getOrZero(withdrawToken)
|
|
70
|
+
);
|
|
71
|
+
post.balances.dec(withdrawToken, fromBalance);
|
|
72
|
+
let withdrawn = fromBalance;
|
|
73
|
+
const missing = withdrawAmount - fromBalance;
|
|
74
|
+
if (missing > 0n && !sameToken) {
|
|
75
|
+
const available = post.balances.getOrZero(claimToken);
|
|
76
|
+
const cost = converter.convert(withdrawToken, claimToken, missing);
|
|
77
|
+
if (cost > 0n && cost <= available) {
|
|
78
|
+
post.balances.dec(claimToken, cost);
|
|
79
|
+
withdrawn += missing;
|
|
80
|
+
} else if (available > 0n) {
|
|
81
|
+
post.balances.dec(claimToken, available);
|
|
82
|
+
withdrawn += converter.convert(claimToken, withdrawToken, available);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (withdrawn > 0n) {
|
|
86
|
+
collateralWithdrawn.upsert(withdrawToken, withdrawn);
|
|
87
|
+
}
|
|
88
|
+
const remaining = post.balances.getOrZero(claimToken);
|
|
89
|
+
if (remaining > 0n) {
|
|
90
|
+
const proceeds = converter.convert(claimToken, post.underlying, remaining);
|
|
91
|
+
post.balances.dec(claimToken, remaining);
|
|
92
|
+
post.balances.inc(post.underlying, proceeds);
|
|
93
|
+
post.repay(BigIntMath.min(proceeds, debtRepaid));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
72
96
|
function repayFromClaim(post, claimToken, convert, amount) {
|
|
73
97
|
const spent = BigIntMath.min(amount, post.balances.getOrZero(claimToken));
|
|
74
98
|
if (spent <= 0n) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { decodeAbiParameters, encodeAbiParameters } from "viem";
|
|
2
|
-
const DELAYED_INTENT_VERSION =
|
|
2
|
+
const DELAYED_INTENT_VERSION = 2;
|
|
3
3
|
const DELAYED_INTENT_TYPES = {
|
|
4
4
|
INCREASE_LEVERAGE: 1,
|
|
5
5
|
DEPOSIT: 2,
|
|
@@ -17,7 +17,9 @@ const TO_PARAMS = [...HEADER_PARAMS, { type: "address", name: "to" }];
|
|
|
17
17
|
const WITHDRAW_COLLATERAL_PARAMS = [
|
|
18
18
|
...TO_PARAMS,
|
|
19
19
|
{ type: "address", name: "withdrawToken" },
|
|
20
|
-
{ type: "uint256", name: "withdrawAmount" }
|
|
20
|
+
{ type: "uint256", name: "withdrawAmount" },
|
|
21
|
+
{ type: "address", name: "sourceToken" },
|
|
22
|
+
{ type: "uint256", name: "debtRepaid" }
|
|
21
23
|
];
|
|
22
24
|
function encodeDelayedIntent(intent) {
|
|
23
25
|
const version = DELAYED_INTENT_VERSION;
|
|
@@ -32,7 +34,9 @@ function encodeDelayedIntent(intent) {
|
|
|
32
34
|
intentType,
|
|
33
35
|
intent.to,
|
|
34
36
|
intent.withdrawToken,
|
|
35
|
-
intent.withdrawAmount
|
|
37
|
+
intent.withdrawAmount,
|
|
38
|
+
intent.sourceToken,
|
|
39
|
+
intent.debtRepaid
|
|
36
40
|
]);
|
|
37
41
|
case "DEPOSIT":
|
|
38
42
|
case "DEPOSIT_AND_INCREASE_LEVERAGE":
|
|
@@ -60,15 +64,14 @@ function decodeDelayedIntent(data) {
|
|
|
60
64
|
case DELAYED_INTENT_TYPES.DEPOSIT_AND_INCREASE_LEVERAGE:
|
|
61
65
|
return { type: "DEPOSIT_AND_INCREASE_LEVERAGE" };
|
|
62
66
|
case DELAYED_INTENT_TYPES.WITHDRAW_COLLATERAL: {
|
|
63
|
-
const [, , to, withdrawToken, withdrawAmount] = decodeAbiParameters(
|
|
64
|
-
WITHDRAW_COLLATERAL_PARAMS,
|
|
65
|
-
data
|
|
66
|
-
);
|
|
67
|
+
const [, , to, withdrawToken, withdrawAmount, sourceToken, debtRepaid] = decodeAbiParameters(WITHDRAW_COLLATERAL_PARAMS, data);
|
|
67
68
|
return {
|
|
68
69
|
type: "WITHDRAW_COLLATERAL",
|
|
69
70
|
to,
|
|
70
71
|
withdrawToken,
|
|
71
|
-
withdrawAmount
|
|
72
|
+
withdrawAmount,
|
|
73
|
+
sourceToken,
|
|
74
|
+
debtRepaid
|
|
72
75
|
};
|
|
73
76
|
}
|
|
74
77
|
case DELAYED_INTENT_TYPES.CLOSE_ACCOUNT: {
|
|
@@ -8,34 +8,16 @@ import { type InstantOperationPreview } from "./types.js";
|
|
|
8
8
|
export type ConvertFn = (token: Address, to: Address, amount: bigint) => bigint;
|
|
9
9
|
/**
|
|
10
10
|
* Builds the best-effort preview of the account state after the detected
|
|
11
|
-
* delayed withdrawal is claimed and its intent (if any) is resumed
|
|
11
|
+
* delayed withdrawal is claimed and its intent (if any) is resumed:
|
|
12
|
+
* the claim itself followed by the intent-specific tail
|
|
12
13
|
*
|
|
13
|
-
* Pure function:
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* `ERROR_UNPRICEABLE_TOKEN` error on the preview.
|
|
14
|
+
* Pure function: the input states are never mutated and no network access is performed.
|
|
15
|
+
* Swaps are estimated with the injected conversion; tokens it cannot price contribute
|
|
16
|
+
* nothing and set a non-fatal `ERROR_UNPRICEABLE_TOKEN` error on the
|
|
17
|
+
* preview.
|
|
18
18
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* Intent-specific resume:
|
|
23
|
-
* - `CLOSE_ACCOUNT`: everything is swapped into the underlying, the debt is
|
|
24
|
-
* fully repaid and the rest is withdrawn to the user as `receivedToken`
|
|
25
|
-
* (the unwrapped underlying for RWA markets, converting 1:1 with the
|
|
26
|
-
* underlying; the underlying itself otherwise).
|
|
27
|
-
* - `DECREASE_LEVERAGE`: the claimed amount is swapped into the underlying
|
|
28
|
-
* and used to decrease the debt.
|
|
29
|
-
* - `WITHDRAW_COLLATERAL`: the recorded amount of the recorded token is
|
|
30
|
-
* withdrawn to the user, the rest of the claimed amount decreases the debt.
|
|
31
|
-
* - other intents and `intent: undefined`: claim-only, the post-claim swap
|
|
32
|
-
* target is not recoverable from the intent.
|
|
33
|
-
*
|
|
34
|
-
* The changes (`debtChange`, `quotasChange`, `assetsChange`) are reported
|
|
35
|
-
* relative to the account state before the whole transaction, not the
|
|
36
|
-
* post-instant-part state: to the user the delayed preview answers "where will
|
|
37
|
-
* the account end up compared to now", so the transient phantom token
|
|
38
|
-
* (minted by the instant part, burned by the claim) nets out to nothing.
|
|
19
|
+
* The changes (e.g. `debtChange`) are reported relative to the account
|
|
20
|
+
* state before the whole transaction.
|
|
39
21
|
*
|
|
40
22
|
* @param afterInstant - Account state after the instant part of the
|
|
41
23
|
* transaction.
|
|
@@ -4,7 +4,7 @@ import type { DelayedIntent } from "./types.js";
|
|
|
4
4
|
* Current version of the delayed intent encoding schema.
|
|
5
5
|
* Bump when the layout of any intent type changes.
|
|
6
6
|
**/
|
|
7
|
-
export declare const DELAYED_INTENT_VERSION =
|
|
7
|
+
export declare const DELAYED_INTENT_VERSION = 2;
|
|
8
8
|
/**
|
|
9
9
|
* Stable uint8 discriminants for intent types.
|
|
10
10
|
* Starts at 1 so that all-zero data cannot decode as a valid intent.
|
|
@@ -31,8 +31,9 @@ export interface DelayedDepositAndIncreaseLeverageIntent {
|
|
|
31
31
|
}
|
|
32
32
|
/**
|
|
33
33
|
* App: 2.1 Withdraw — withdraw selected token at fixed leverage.
|
|
34
|
-
*
|
|
35
|
-
*
|
|
34
|
+
* Primary goal is `withdrawAmount` of `withdrawToken` (W of T). Debt cut is
|
|
35
|
+
* residual from the claim/path after reserving W. `sourceToken` (S) funds the
|
|
36
|
+
* delayed path; `debtRepaid` is 0 when debt was already repaid on start.
|
|
36
37
|
**/
|
|
37
38
|
export interface DelayedWithdrawCollateralIntent {
|
|
38
39
|
type: "WITHDRAW_COLLATERAL";
|
|
@@ -46,9 +47,18 @@ export interface DelayedWithdrawCollateralIntent {
|
|
|
46
47
|
**/
|
|
47
48
|
withdrawToken: Address;
|
|
48
49
|
/**
|
|
49
|
-
* Amount of `withdrawToken` to withdraw to the wallet after the claim
|
|
50
|
+
* Amount of `withdrawToken` to withdraw to the wallet after the claim (W)
|
|
50
51
|
**/
|
|
51
52
|
withdrawAmount: bigint;
|
|
53
|
+
/**
|
|
54
|
+
* Token that funds the delayed path / debt conversion (S)
|
|
55
|
+
**/
|
|
56
|
+
sourceToken: Address;
|
|
57
|
+
/**
|
|
58
|
+
* Desired debt decrease in underlying units remaining for resume.
|
|
59
|
+
* `0n` when debt was already repaid on the delayed-start branch.
|
|
60
|
+
**/
|
|
61
|
+
debtRepaid: bigint;
|
|
52
62
|
}
|
|
53
63
|
/**
|
|
54
64
|
* App: 2.2 Withdraw — close account (receive leftover to wallet).
|