@gearbox-protocol/sdk 16.0.0-next.45 → 16.0.0-next.46
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/model/index.js +1 -0
- package/dist/cjs/model/withdrawals.schema.js +7 -2
- package/dist/cjs/onchain/accounts/intents/index.js +38 -13
- package/dist/cjs/onchain/accounts/intents/tail.js +112 -5
- package/dist/cjs/onchain/pools/PoolService.js +20 -4
- package/dist/cjs/onchain/positions/PositionsService.js +8 -2
- package/dist/cjs/sdk/prepare/PrepareApi.js +133 -82
- package/dist/esm/model/index.js +2 -2
- package/dist/esm/model/withdrawals.schema.js +7 -3
- package/dist/esm/onchain/accounts/intents/index.js +38 -13
- package/dist/esm/onchain/accounts/intents/tail.js +112 -5
- package/dist/esm/onchain/pools/PoolService.js +20 -4
- package/dist/esm/onchain/positions/PositionsService.js +8 -2
- package/dist/esm/sdk/prepare/PrepareApi.js +133 -82
- package/dist/types/model/index.d.ts +3 -3
- package/dist/types/model/withdrawals.d.ts +24 -5
- package/dist/types/model/withdrawals.schema.d.ts +21 -1
- package/dist/types/onchain/accounts/index.d.ts +2 -2
- package/dist/types/onchain/accounts/intents/index.d.ts +19 -11
- package/dist/types/onchain/accounts/intents/tail.d.ts +13 -3
- package/dist/types/onchain/accounts/intents/types.d.ts +74 -1
- package/dist/types/onchain/index.d.ts +3 -3
- package/dist/types/onchain/pools/PoolService.d.ts +10 -1
- package/dist/types/onchain/pools/index.d.ts +2 -2
- package/dist/types/onchain/pools/types.d.ts +40 -1
- package/dist/types/sdk/index.d.ts +3 -3
- package/dist/types/sdk/prepare/PrepareApi.d.ts +8 -7
- package/dist/types/sdk/prepare/index.d.ts +3 -3
- package/dist/types/sdk/prepare/types.d.ts +85 -28
- package/package.json +1 -1
package/dist/cjs/model/index.js
CHANGED
|
@@ -155,3 +155,4 @@ exports.tokenSchema = require_model_primitives_schema.tokenSchema;
|
|
|
155
155
|
exports.tolerance = require_model_compare_schema.tolerance;
|
|
156
156
|
exports.txCallSchema = require_model_primitives_schema.txCallSchema;
|
|
157
157
|
exports.underlyingTokenSchema = require_model_primitives_schema.underlyingTokenSchema;
|
|
158
|
+
exports.withdrawalOutputAmountSchema = require_model_withdrawals_schema.withdrawalOutputAmountSchema;
|
|
@@ -5,12 +5,16 @@ const require_model_delayed_intent_schema = require("./delayed-intent.schema.js"
|
|
|
5
5
|
let zod_v4 = require("zod/v4");
|
|
6
6
|
//#region src/model/withdrawals.schema.ts
|
|
7
7
|
/**
|
|
8
|
+
* {@link WithdrawalOutputAmount}
|
|
9
|
+
**/
|
|
10
|
+
const withdrawalOutputAmountSchema = require_model_primitives_schema.tokenAmountSchema.extend({ isDelayed: zod_v4.z.boolean() });
|
|
11
|
+
/**
|
|
8
12
|
* {@link PositionClaimableWithdrawal}
|
|
9
13
|
**/
|
|
10
14
|
const positionClaimableWithdrawalSchema = zod_v4.z.object({
|
|
11
15
|
sourceToken: require_model_primitives_schema.tokenSchema,
|
|
12
16
|
withdrawalPhantomToken: require_model_primitives_schema.tokenAmountSchema,
|
|
13
|
-
outputs: zod_v4.z.array(
|
|
17
|
+
outputs: zod_v4.z.array(withdrawalOutputAmountSchema),
|
|
14
18
|
claimCall: require_model_primitives_schema.txCallSchema,
|
|
15
19
|
redeemer: require_onchain_utils_zod.ZodAddress().optional(),
|
|
16
20
|
intent: require_model_delayed_intent_schema.delayedIntentSchema.optional()
|
|
@@ -21,7 +25,7 @@ const positionClaimableWithdrawalSchema = zod_v4.z.object({
|
|
|
21
25
|
const positionPendingWithdrawalSchema = zod_v4.z.object({
|
|
22
26
|
sourceToken: require_model_primitives_schema.tokenSchema,
|
|
23
27
|
withdrawalPhantomToken: require_model_primitives_schema.tokenSchema,
|
|
24
|
-
expectedOutputs: zod_v4.z.array(
|
|
28
|
+
expectedOutputs: zod_v4.z.array(withdrawalOutputAmountSchema),
|
|
25
29
|
claimableAt: require_model_primitives_schema.timestampSchema,
|
|
26
30
|
redeemer: require_onchain_utils_zod.ZodAddress().optional(),
|
|
27
31
|
intent: require_model_delayed_intent_schema.delayedIntentSchema.optional()
|
|
@@ -37,3 +41,4 @@ const positionWithdrawalsSchema = zod_v4.z.object({
|
|
|
37
41
|
exports.positionClaimableWithdrawalSchema = positionClaimableWithdrawalSchema;
|
|
38
42
|
exports.positionPendingWithdrawalSchema = positionPendingWithdrawalSchema;
|
|
39
43
|
exports.positionWithdrawalsSchema = positionWithdrawalsSchema;
|
|
44
|
+
exports.withdrawalOutputAmountSchema = withdrawalOutputAmountSchema;
|
|
@@ -46,21 +46,27 @@ var CreditAccountOperationsService = class extends require_onchain_base_SDKConst
|
|
|
46
46
|
}));
|
|
47
47
|
}
|
|
48
48
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
49
|
+
* Both ends of what a `WITHDRAW` can take out, in underlying: the largest
|
|
50
|
+
* partial withdrawal that keeps leverage and stays inside the facade's debt
|
|
51
|
+
* band, and the net value an exit hands over. They are reported together
|
|
52
|
+
* because a withdraw form needs both — the range it may offer, and the one
|
|
53
|
+
* amount past it that is allowed — and because the distance between them is
|
|
54
|
+
* the account's own, not a constant a caller could assume.
|
|
53
55
|
*
|
|
54
56
|
* Takes no target health factor, unlike {@link maxWithdrawCollateral}: a
|
|
55
57
|
* proportional withdrawal leaves the factor where it found it, and the
|
|
56
58
|
* facade's `minDebt` is what bounds it.
|
|
57
59
|
*
|
|
58
60
|
* @param props - Account slice and the SDK holding its market
|
|
59
|
-
* @returns
|
|
61
|
+
* @returns The two ceilings, see {@link WithdrawCeilings} for the gap between
|
|
62
|
+
* them
|
|
60
63
|
*/
|
|
61
64
|
maxWithdraw(props) {
|
|
62
65
|
const view = require_onchain_accounts_intents_view.accountView(props.creditAccount, props.sdk);
|
|
63
|
-
return
|
|
66
|
+
return {
|
|
67
|
+
partial: require_onchain_accounts_intents_math.maxProportionalWithdrawal(view, view.band),
|
|
68
|
+
exit: view.collateral > 0n ? view.collateral : 0n
|
|
69
|
+
};
|
|
64
70
|
}
|
|
65
71
|
/**
|
|
66
72
|
* Debt a `REPAY` would have to cover to settle the account, in underlying
|
|
@@ -230,17 +236,36 @@ var CreditAccountOperationsService = class extends require_onchain_base_SDKConst
|
|
|
230
236
|
* whole tail: the tokens land on the account and only their quota has to
|
|
231
237
|
* catch up.
|
|
232
238
|
*
|
|
239
|
+
* A claim that brought only part of what the request queued — a legacy Mellow
|
|
240
|
+
* multivault, which pays out what it holds liquid and re-queues the rest — is
|
|
241
|
+
* served in proportion, and what it did not settle comes back as `remainder`:
|
|
242
|
+
* the withdrawal still in flight and the intent to finish it with.
|
|
243
|
+
*
|
|
233
244
|
* @param props - The recorded intent, the account slice as it stands now, and
|
|
234
245
|
* the matured claimable
|
|
235
|
-
* @returns Shaped exactly like {@link startIntent}'s result
|
|
236
|
-
* an operation are consumed the same way
|
|
246
|
+
* @returns Shaped exactly like {@link startIntent}'s result with the remainder
|
|
247
|
+
* beside it, so both halves of an operation are consumed the same way
|
|
237
248
|
*/
|
|
238
249
|
async finishIntent(props) {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
250
|
+
let remainder;
|
|
251
|
+
const result = await this.#preview(props, () => {
|
|
252
|
+
const tail = require_onchain_accounts_intents_tail.planTail({
|
|
253
|
+
intent: props.intent,
|
|
254
|
+
claimable: props.claimable,
|
|
255
|
+
view: require_onchain_accounts_intents_view.accountView(props.creditAccount, props.sdk)
|
|
256
|
+
});
|
|
257
|
+
remainder = tail.remainder;
|
|
258
|
+
return tail.steps;
|
|
259
|
+
});
|
|
260
|
+
if (!result.ok) return result;
|
|
261
|
+
const { operations, state, calls } = result;
|
|
262
|
+
return {
|
|
263
|
+
ok: true,
|
|
264
|
+
operations,
|
|
265
|
+
state,
|
|
266
|
+
calls,
|
|
267
|
+
remainder
|
|
268
|
+
};
|
|
244
269
|
}
|
|
245
270
|
/**
|
|
246
271
|
* Previews opening a brand-new leveraged position.
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_onchain_validation_refusal = require("../../validation/refusal.js");
|
|
3
|
+
const require_onchain_validation_token = require("../../validation/token.js");
|
|
4
|
+
const require_onchain_accounts_intents_utils_common = require("./utils/common.js");
|
|
3
5
|
const require_onchain_accounts_intents_utils_router_path = require("./utils/router-path.js");
|
|
4
6
|
const require_onchain_accounts_intents_plan = require("./plan.js");
|
|
5
7
|
const require_onchain_accounts_intents_operations = require("./operations.js");
|
|
@@ -13,6 +15,10 @@ const require_onchain_accounts_intents_view = require("./view.js");
|
|
|
13
15
|
* Shared by the two callers that need it and must not disagree — the tail as
|
|
14
16
|
* it is previewed days later against the account that really exists, and the
|
|
15
17
|
* tail as it is projected the moment the request is made.
|
|
18
|
+
*
|
|
19
|
+
* A claim that brought only part of what was queued is served in proportion,
|
|
20
|
+
* see {@link partialTail}: the intent's payout and its repayment are cut to the
|
|
21
|
+
* share that arrived, and the rest of both is handed to the next claim.
|
|
16
22
|
*/
|
|
17
23
|
function planTail(args) {
|
|
18
24
|
const { intent, claimable, view } = args;
|
|
@@ -21,17 +27,118 @@ function planTail(args) {
|
|
|
21
27
|
if (!output) throw new require_onchain_validation_refusal.IntentPreviewError("insufficientSourceBalance", void 0, "finishIntent: the claim credits nothing to spend");
|
|
22
28
|
return output;
|
|
23
29
|
};
|
|
30
|
+
const queued = claimable.outputs.find((o) => o.isDelayed && o.amount > 0n);
|
|
31
|
+
if (queued) return partialTail({
|
|
32
|
+
intent,
|
|
33
|
+
claimable,
|
|
34
|
+
queued,
|
|
35
|
+
view
|
|
36
|
+
});
|
|
24
37
|
switch (intent.type) {
|
|
25
|
-
case "WITHDRAW_COLLATERAL": return require_onchain_accounts_intents_plan.planFinishWithdraw(intent, claimable, claimed(), view);
|
|
26
|
-
case "DECREASE_LEVERAGE": return require_onchain_accounts_intents_plan.planFinishDecreaseLeverage(claimable, claimed(), view);
|
|
27
|
-
case "CLOSE_ACCOUNT": return require_onchain_accounts_intents_plan.planFinishCloseAccount(intent, claimable, claimed(), view);
|
|
38
|
+
case "WITHDRAW_COLLATERAL": return whole(require_onchain_accounts_intents_plan.planFinishWithdraw(intent, claimable, claimed(), view));
|
|
39
|
+
case "DECREASE_LEVERAGE": return whole(require_onchain_accounts_intents_plan.planFinishDecreaseLeverage(claimable, claimed(), view));
|
|
40
|
+
case "CLOSE_ACCOUNT": return whole(require_onchain_accounts_intents_plan.planFinishCloseAccount(intent, claimable, claimed(), view));
|
|
28
41
|
case "ADD_COLLATERAL":
|
|
29
42
|
case "INCREASE_LEVERAGE":
|
|
30
43
|
case "DEPOSIT":
|
|
31
|
-
case "DEPOSIT_AND_INCREASE_LEVERAGE": return require_onchain_accounts_intents_plan.planFinishClaimOnly(claimable);
|
|
44
|
+
case "DEPOSIT_AND_INCREASE_LEVERAGE": return whole(require_onchain_accounts_intents_plan.planFinishClaimOnly(claimable));
|
|
32
45
|
default: throw new Error(`${intent.type} - not implemented`);
|
|
33
46
|
}
|
|
34
47
|
}
|
|
48
|
+
const whole = (steps) => ({
|
|
49
|
+
steps,
|
|
50
|
+
remainder: void 0
|
|
51
|
+
});
|
|
52
|
+
/**
|
|
53
|
+
* The tail of a claim that settled only part of the withdrawal it matured.
|
|
54
|
+
*
|
|
55
|
+
* Only a legacy Mellow multivault answers one this way — it pays out what its
|
|
56
|
+
* subvaults hold liquid and queues the rest — and the engine cannot finish an
|
|
57
|
+
* intent it has been given a fraction of the funds for. So the fraction is what
|
|
58
|
+
* it serves: the payout and the repayment are cut in the proportion that
|
|
59
|
+
* arrived, which keeps the withdrawal at the fixed leverage it was asked for
|
|
60
|
+
* instead of paying the wallet out first and deleveraging a claim later, and
|
|
61
|
+
* the untouched half of each is carried to the next claim by the remainder.
|
|
62
|
+
*
|
|
63
|
+
* Two intents cannot be served in part at all. An exit sells the account whole,
|
|
64
|
+
* and it cannot while a withdrawal is in flight — the phantom is neither
|
|
65
|
+
* sellable nor transferable — so a partial claim only repays what it brought
|
|
66
|
+
* and the account is emptied by the claim that brings the last of it. A claim
|
|
67
|
+
* that credited nothing at all leaves nothing to spend, so it is taken alone:
|
|
68
|
+
* it is still worth sending, since it is what moves the queue.
|
|
69
|
+
*/
|
|
70
|
+
function partialTail(args) {
|
|
71
|
+
const { intent, claimable, queued, view } = args;
|
|
72
|
+
const inFlight = require_onchain_validation_token.toTokenAmount(view.sdk, queued.token, queued.amount);
|
|
73
|
+
const claimed = require_onchain_accounts_intents_operations.instantOutput(claimable.outputs);
|
|
74
|
+
if (!claimed) return {
|
|
75
|
+
steps: require_onchain_accounts_intents_plan.planFinishClaimOnly(claimable),
|
|
76
|
+
remainder: {
|
|
77
|
+
inFlight,
|
|
78
|
+
intent
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
switch (intent.type) {
|
|
82
|
+
case "WITHDRAW_COLLATERAL": {
|
|
83
|
+
const served = arrivedShare(claimed, queued, view.sdk);
|
|
84
|
+
const withdrawAmount = part(intent.withdrawAmount, served);
|
|
85
|
+
const debtRepaid = part(intent.debtRepaid, served);
|
|
86
|
+
return {
|
|
87
|
+
steps: require_onchain_accounts_intents_plan.planFinishWithdraw({
|
|
88
|
+
...intent,
|
|
89
|
+
withdrawAmount,
|
|
90
|
+
debtRepaid
|
|
91
|
+
}, claimable, claimed, view),
|
|
92
|
+
remainder: {
|
|
93
|
+
inFlight,
|
|
94
|
+
intent: {
|
|
95
|
+
...intent,
|
|
96
|
+
withdrawAmount: intent.withdrawAmount - withdrawAmount,
|
|
97
|
+
debtRepaid: intent.debtRepaid - debtRepaid
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
case "DECREASE_LEVERAGE":
|
|
103
|
+
case "CLOSE_ACCOUNT": return {
|
|
104
|
+
steps: require_onchain_accounts_intents_plan.planFinishDecreaseLeverage(claimable, claimed, view),
|
|
105
|
+
remainder: {
|
|
106
|
+
inFlight,
|
|
107
|
+
intent
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
case "ADD_COLLATERAL":
|
|
111
|
+
case "INCREASE_LEVERAGE":
|
|
112
|
+
case "DEPOSIT":
|
|
113
|
+
case "DEPOSIT_AND_INCREASE_LEVERAGE": return {
|
|
114
|
+
steps: require_onchain_accounts_intents_plan.planFinishClaimOnly(claimable),
|
|
115
|
+
remainder: {
|
|
116
|
+
inFlight,
|
|
117
|
+
intent
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
default: throw new Error(`${intent.type} - not implemented`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* How much of the redemption this claim was: what it credited, over that plus
|
|
125
|
+
* what it left queued.
|
|
126
|
+
*
|
|
127
|
+
* The phantom stands for its payout one for one — the same reading the request
|
|
128
|
+
* side takes when it names the claim it expects — so only the decimals of the
|
|
129
|
+
* two have to be reconciled before they can be added up.
|
|
130
|
+
*/
|
|
131
|
+
function arrivedShare(claimed, queued, sdk) {
|
|
132
|
+
const rest = require_onchain_accounts_intents_utils_common.toTargetDecimals(queued.amount, queued.token, claimed.token, sdk);
|
|
133
|
+
return {
|
|
134
|
+
got: claimed.amount,
|
|
135
|
+
of: claimed.amount + rest
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/** A share of an amount, rounded down: a tail never promises more than it has. */
|
|
139
|
+
function part(amount, share) {
|
|
140
|
+
return share.of > 0n ? amount * share.got / share.of : 0n;
|
|
141
|
+
}
|
|
35
142
|
/**
|
|
36
143
|
* Where a delayed intent ends up, worked out at the moment it is started.
|
|
37
144
|
*
|
|
@@ -56,7 +163,7 @@ async function projectTail(args) {
|
|
|
56
163
|
const queued = request.outputs.find((o) => o.isDelayed);
|
|
57
164
|
if (!queued || !claim) throw new require_onchain_validation_refusal.IntentPreviewError("noRecordedIntent", void 0, "projectTail: the request queued nothing to claim");
|
|
58
165
|
const next = sliceAfter(creditAccount, delayed.afterRequest);
|
|
59
|
-
const steps = planTail({
|
|
166
|
+
const { steps } = planTail({
|
|
60
167
|
intent: delayed.record,
|
|
61
168
|
claimable: projectedClaimable(request, queued.token, queued.amount, claim),
|
|
62
169
|
view: require_onchain_accounts_intents_view.accountView(next, sdk)
|
|
@@ -30,6 +30,25 @@ function payoutCeiling(market) {
|
|
|
30
30
|
return market.priceOracle.toAmount(pool.underlying, pool.pool.availableLiquidity * LIQUIDITY_SAFETY_NUM / LIQUIDITY_SAFETY_DENOM);
|
|
31
31
|
}
|
|
32
32
|
var PoolService = class extends require_onchain_base_SDKConstruct.SDKConstruct {
|
|
33
|
+
/**
|
|
34
|
+
* {@inheritDoc IPoolsService.getShareBalance}
|
|
35
|
+
*/
|
|
36
|
+
async getShareBalance(props) {
|
|
37
|
+
return this.client.readContract({
|
|
38
|
+
address: this.sdk.marketRegister.findByPool(props.pool).pool.pool.address,
|
|
39
|
+
abi: require_abi_iERC20.ierc20Abi,
|
|
40
|
+
functionName: "balanceOf",
|
|
41
|
+
args: [props.wallet],
|
|
42
|
+
blockNumber: props.blockNumber
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* {@inheritDoc IPoolsService.sharesToUnderlying}
|
|
47
|
+
*/
|
|
48
|
+
sharesToUnderlying(pool, shares) {
|
|
49
|
+
const market = this.sdk.marketRegister.findByPool(pool);
|
|
50
|
+
return market.toUnderlyingAmount(shares * market.pool.pool.dieselRate / require_onchain_constants_math.RAY);
|
|
51
|
+
}
|
|
33
52
|
/**
|
|
34
53
|
* {@inheritDoc IPoolsService.getDepositTokensIn}
|
|
35
54
|
*/
|
|
@@ -415,10 +434,7 @@ var PoolService = class extends require_onchain_base_SDKConstruct.SDKConstruct {
|
|
|
415
434
|
chainId: this.chainId,
|
|
416
435
|
pool: pool.address,
|
|
417
436
|
underlyingToken: market.underlyingToken,
|
|
418
|
-
netValue:
|
|
419
|
-
token: this.sdk.tokensMeta.mustGetToken(market.unwrappedUnderlying),
|
|
420
|
-
...market.priceOracle.toAmount(market.underlying, shares * pool.dieselRate / require_onchain_constants_math.RAY)
|
|
421
|
-
},
|
|
437
|
+
netValue: this.sharesToUnderlying(pool.address, shares),
|
|
422
438
|
apy: { organicApy: require_onchain_market_math.rayToBps(pool.supplyRate) }
|
|
423
439
|
};
|
|
424
440
|
}
|
|
@@ -373,7 +373,10 @@ var PositionsService = class extends require_onchain_base_SDKConstruct.SDKConstr
|
|
|
373
373
|
return {
|
|
374
374
|
sourceToken: this.sdk.tokensMeta.mustGetToken(w.token),
|
|
375
375
|
withdrawalPhantomToken: priceOracle.toTokenAmount(w.withdrawalPhantomToken, w.withdrawalTokenSpent),
|
|
376
|
-
outputs: w.outputs.map((o) =>
|
|
376
|
+
outputs: w.outputs.map((o) => ({
|
|
377
|
+
...priceOracle.toTokenAmount(o.token, o.amount),
|
|
378
|
+
isDelayed: o.isDelayed
|
|
379
|
+
})),
|
|
377
380
|
claimCall: this.#claimTx(w.claimCalls, w.token),
|
|
378
381
|
redeemer: w.redeemer,
|
|
379
382
|
intent: w.intent
|
|
@@ -383,7 +386,10 @@ var PositionsService = class extends require_onchain_base_SDKConstruct.SDKConstr
|
|
|
383
386
|
return {
|
|
384
387
|
sourceToken: this.sdk.tokensMeta.mustGetToken(w.token),
|
|
385
388
|
withdrawalPhantomToken: this.sdk.tokensMeta.mustGetToken(w.withdrawalPhantomToken),
|
|
386
|
-
expectedOutputs: w.expectedOutputs.map((o) =>
|
|
389
|
+
expectedOutputs: w.expectedOutputs.map((o) => ({
|
|
390
|
+
...priceOracle.toTokenAmount(o.token, o.amount),
|
|
391
|
+
isDelayed: o.isDelayed
|
|
392
|
+
})),
|
|
387
393
|
claimableAt: Number(w.claimableAt),
|
|
388
394
|
redeemer: w.redeemer,
|
|
389
395
|
intent: w.intent
|
|
@@ -59,7 +59,7 @@ var PrepareApi = class extends require_onchain_base_MultichainConstruct.Multicha
|
|
|
59
59
|
}));
|
|
60
60
|
const creditAccount = await slice(sdk, position.creditAccount);
|
|
61
61
|
if (!creditAccount) return require_model_result.sdkErr(require_sdk_prepare_errors.creditAccountNotFound(position.creditAccount));
|
|
62
|
-
return
|
|
62
|
+
return finalized(await service(sdk).finishIntent({
|
|
63
63
|
intent,
|
|
64
64
|
claimable: toClaimableWithdrawal(params.claimable),
|
|
65
65
|
creditAccount,
|
|
@@ -74,94 +74,106 @@ var PrepareApi = class extends require_onchain_base_MultichainConstruct.Multicha
|
|
|
74
74
|
/**
|
|
75
75
|
* {@inheritDoc IOpportunitiesPrepare.deposit}
|
|
76
76
|
**/
|
|
77
|
-
deposit(pool, params) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
77
|
+
async deposit(pool, params) {
|
|
78
|
+
try {
|
|
79
|
+
const chain = await this.#chain(pool.chainId);
|
|
80
|
+
const { marketRegister, pools } = chain;
|
|
81
|
+
const tokenIn = params.tokenIn ?? marketRegister.findByPool(pool.pool).pool.underlying;
|
|
82
|
+
const tokenOut = lpRoute(params.tokenOut, () => pools.getDepositTokensOut(pool.pool, tokenIn));
|
|
83
|
+
if (!tokenOut) return unroutable(chain, tokenIn, void 0);
|
|
84
|
+
const state = pools.simulateDeposit({
|
|
85
|
+
pool: pool.pool,
|
|
86
|
+
amount: params.amount,
|
|
87
|
+
tokenIn,
|
|
88
|
+
tokenOut
|
|
89
|
+
});
|
|
90
|
+
const call = pools.addLiquidity({
|
|
91
|
+
collateral: {
|
|
92
|
+
token: state.tokenIn.token.address,
|
|
93
|
+
balance: state.tokenIn.value
|
|
94
|
+
},
|
|
95
|
+
pool: pool.pool,
|
|
96
|
+
wallet: params.wallet,
|
|
97
|
+
meta: pools.getDepositMetadata(pool.pool, tokenIn, tokenOut)
|
|
98
|
+
});
|
|
99
|
+
if (!call) return unroutable(chain, tokenIn, tokenOut);
|
|
100
|
+
return require_model_result.sdkOk({
|
|
101
|
+
operations: [],
|
|
102
|
+
state: await lpState(chain, pool.pool, params.wallet, state, { mints: state.tokenOut.value }),
|
|
103
|
+
calls: call.calls,
|
|
104
|
+
...stateBlock(chain)
|
|
105
|
+
});
|
|
106
|
+
} catch (e) {
|
|
107
|
+
return require_model_result.sdkErr(require_sdk_prepare_errors.unexpectedFailure(e));
|
|
108
|
+
}
|
|
105
109
|
}
|
|
106
110
|
/**
|
|
107
111
|
* {@inheritDoc IOpportunitiesPrepare.withdraw}
|
|
108
112
|
**/
|
|
109
|
-
withdraw(pool, params) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
113
|
+
async withdraw(pool, params) {
|
|
114
|
+
try {
|
|
115
|
+
const chain = await this.#chain(pool.chainId);
|
|
116
|
+
const { pools } = chain;
|
|
117
|
+
const tokenIn = params.tokenIn ?? pool.pool;
|
|
118
|
+
const tokenOut = lpRoute(params.tokenOut, () => pools.getWithdrawalTokensOut(pool.pool, tokenIn));
|
|
119
|
+
if (!tokenOut) return unroutable(chain, tokenIn, void 0);
|
|
120
|
+
const state = pools.simulateWithdraw({
|
|
121
|
+
pool: pool.pool,
|
|
122
|
+
amount: params.amount,
|
|
123
|
+
tokenIn,
|
|
124
|
+
tokenOut
|
|
125
|
+
});
|
|
126
|
+
const { calls } = pools.removeLiquidity({
|
|
127
|
+
pool: pool.pool,
|
|
128
|
+
amount: params.amount,
|
|
129
|
+
wallet: params.wallet,
|
|
130
|
+
permit: void 0,
|
|
131
|
+
meta: pools.getWithdrawalMetadata(pool.pool, tokenIn, tokenOut),
|
|
132
|
+
mode: "withdraw"
|
|
133
|
+
});
|
|
134
|
+
return require_model_result.sdkOk({
|
|
135
|
+
operations: [],
|
|
136
|
+
state: await lpState(chain, pool.pool, params.wallet, state, { burns: state.tokenIn.value }),
|
|
137
|
+
calls,
|
|
138
|
+
...stateBlock(chain)
|
|
139
|
+
});
|
|
140
|
+
} catch (e) {
|
|
141
|
+
return require_model_result.sdkErr(require_sdk_prepare_errors.unexpectedFailure(e));
|
|
142
|
+
}
|
|
135
143
|
}
|
|
136
144
|
/**
|
|
137
145
|
* {@inheritDoc IOpportunitiesPrepare.redeem}
|
|
138
146
|
**/
|
|
139
|
-
redeem(pool, params) {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
147
|
+
async redeem(pool, params) {
|
|
148
|
+
try {
|
|
149
|
+
const chain = await this.#chain(pool.chainId);
|
|
150
|
+
const { pools } = chain;
|
|
151
|
+
const tokenIn = params.tokenIn ?? pool.pool;
|
|
152
|
+
const tokenOut = lpRoute(params.tokenOut, () => pools.getWithdrawalTokensOut(pool.pool, tokenIn));
|
|
153
|
+
if (!tokenOut) return unroutable(chain, tokenIn, void 0);
|
|
154
|
+
const state = pools.simulateRedeem({
|
|
155
|
+
pool: pool.pool,
|
|
156
|
+
amount: params.amount,
|
|
157
|
+
tokenIn,
|
|
158
|
+
tokenOut
|
|
159
|
+
});
|
|
160
|
+
const { calls } = pools.removeLiquidity({
|
|
161
|
+
pool: pool.pool,
|
|
162
|
+
amount: params.amount,
|
|
163
|
+
wallet: params.wallet,
|
|
164
|
+
permit: void 0,
|
|
165
|
+
meta: pools.getWithdrawalMetadata(pool.pool, tokenIn, tokenOut),
|
|
166
|
+
mode: "redeem"
|
|
167
|
+
});
|
|
168
|
+
return require_model_result.sdkOk({
|
|
169
|
+
operations: [],
|
|
170
|
+
state: await lpState(chain, pool.pool, params.wallet, state, { burns: state.tokenIn.value }),
|
|
171
|
+
calls,
|
|
172
|
+
...stateBlock(chain)
|
|
173
|
+
});
|
|
174
|
+
} catch (e) {
|
|
175
|
+
return require_model_result.sdkErr(require_sdk_prepare_errors.unexpectedFailure(e));
|
|
176
|
+
}
|
|
165
177
|
}
|
|
166
178
|
/**
|
|
167
179
|
* {@inheritDoc IOpportunitiesPrepare.openNewStrategy}
|
|
@@ -413,7 +425,7 @@ function toClaimableWithdrawal(claimable) {
|
|
|
413
425
|
outputs: claimable.outputs.map((o) => ({
|
|
414
426
|
token: o.token.address,
|
|
415
427
|
amount: o.value,
|
|
416
|
-
isDelayed:
|
|
428
|
+
isDelayed: o.isDelayed
|
|
417
429
|
})),
|
|
418
430
|
claimCalls: [{
|
|
419
431
|
target: claimable.claimCall.to,
|
|
@@ -470,6 +482,23 @@ function planned(result, at) {
|
|
|
470
482
|
}
|
|
471
483
|
/**
|
|
472
484
|
* {@inheritDoc planned}
|
|
485
|
+
*
|
|
486
|
+
* A tail carries one thing the other results do not: whether the claim it was
|
|
487
|
+
* built on finished the withdrawal, or left part of it queued for another one.
|
|
488
|
+
**/
|
|
489
|
+
function finalized(result, at) {
|
|
490
|
+
if (!result.ok) return refusal(result);
|
|
491
|
+
const { operations, state, calls, remainder } = result;
|
|
492
|
+
return require_model_result.sdkOk({
|
|
493
|
+
operations,
|
|
494
|
+
state,
|
|
495
|
+
calls,
|
|
496
|
+
remainder,
|
|
497
|
+
...at
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* {@inheritDoc planned}
|
|
473
502
|
**/
|
|
474
503
|
function opened(result, at) {
|
|
475
504
|
return result.ok ? require_model_result.sdkOk({
|
|
@@ -512,6 +541,28 @@ function routed(result, at) {
|
|
|
512
541
|
});
|
|
513
542
|
}
|
|
514
543
|
/**
|
|
544
|
+
* The pool's own numbers as the namespace reports them: the trade the service
|
|
545
|
+
* priced, the market it belongs to, and where the wallet's position lands.
|
|
546
|
+
*
|
|
547
|
+
* The position is measured in shares and converted once, rather than added up
|
|
548
|
+
* in underlying, so the figure is exactly what a later
|
|
549
|
+
* `sdk.positions.list()` will report — the same balance through the same rate.
|
|
550
|
+
* A withdrawal larger than the position floors at nothing: the transaction
|
|
551
|
+
* would revert long before it got there, and a negative holding is not a thing
|
|
552
|
+
* a screen can show.
|
|
553
|
+
**/
|
|
554
|
+
async function lpState(sdk, pool, wallet, simulation, moved) {
|
|
555
|
+
const after = await sdk.pools.getShareBalance({
|
|
556
|
+
pool,
|
|
557
|
+
wallet
|
|
558
|
+
}) + (moved.mints ?? -moved.burns);
|
|
559
|
+
return {
|
|
560
|
+
...simulation,
|
|
561
|
+
curator: sdk.marketRegister.findByPool(pool).curator,
|
|
562
|
+
positionAfter: sdk.pools.sharesToUnderlying(pool, after > 0n ? after : 0n)
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
515
566
|
* A pool route the market does not offer, as the refusal a caller reads.
|
|
516
567
|
*
|
|
517
568
|
* `to` is absent where {@link lpRoute} found no output to name at all, which
|
package/dist/esm/model/index.js
CHANGED
|
@@ -22,5 +22,5 @@ import "./response.js";
|
|
|
22
22
|
import { chainFailedSchema, chainMetadataSchema, chainSucceededSchema, dataSourceSchema, responseMetadataSchema, responseSchema } from "./response.schema.js";
|
|
23
23
|
import { isSDKError, sdkErr, sdkOk } from "./result.js";
|
|
24
24
|
import "./withdrawals.js";
|
|
25
|
-
import { positionClaimableWithdrawalSchema, positionPendingWithdrawalSchema, positionWithdrawalsSchema } from "./withdrawals.schema.js";
|
|
26
|
-
export { CHART_METRIC_UNITS, CHART_RANGES, CHART_UNAVAILABLE_CODES, ERROR_ADAPTER_CALL_OUTSIDE_BRACKET, ERROR_INVALID_TRANSACTION_VALUE, ERROR_MALFORMED_BRACKET, ERROR_NON_ADAPTER_CALL_IN_BRACKET, ERROR_UNPREVIEWABLE_ADAPTER_CALL, ERROR_UNPRICEABLE_TOKEN, ERROR_UNSUPPORTED_OUT_OF_BRACKET_CALL, FILTER_ALL, POOL_OPPORTUNITY_CHART_METRICS, POOL_POSITION_CHART_METRICS, STRATEGY_OPPORTUNITY_CHART_METRICS, STRATEGY_POSITION_CHART_METRICS, STRATEGY_POSITION_COLLATERAL_ERROR, amountSchema, apyBreakdownSchema, asEstimated, assetTypeSchema, backendPreferred, booleanParamSchema, borrowRateBreakdownSchema, bpsSchema, chainFailedSchema, chainIdSchema, chainMetadataSchema, chainSucceededSchema, chartBundleSchemaFor, chartDenominationSchema, chartMetricSchema, chartQueryCodec, chartQueryParamsSchema, chartQuerySchema, chartRangeSchema, chartSeriesSchema, chartValueSchema, chartWindowSchema, compareTagOf, curatorNameSchema, curatorSchema, dataSourceSchema, delayedReceivedAssetSchema, encodeFlag, filterAllSchema, filterable, instantReceivedAssetSchema, isFilterSet, isSDKError, leverageSchema, liquidatableAccountFilterSchema, liquidatableAccountSchema, liquidationApprovalSchema, liquidationDetailsSchema, liquidationPositionId, liquidationPositionSchema, matchesLiquidatableAccountFilter, matchesOpportunityFilter, matchesPositionFilter, noticeKindSchema, noticeSchema, offchainOnly, onchainOnly, opportunityBaseSchema, opportunityDetailSchema, opportunityFilterQueryParamsSchema, opportunityFilterQuerySchema, opportunityFilterSchema, opportunityId, opportunityKeySchema, opportunityKindSchema, opportunitySchema, opportunityTotalsSchema, pnlBreakdownSchema, pointRewardsSchema, pointsProgramPnLSchema, pointsProgramSchema, pointsRewardsPnLSchema, poolOpportunityChartMetricSchema, poolOpportunityDetailSchema, poolOpportunityId, poolOpportunityKeySchema, poolOpportunitySchema, poolPositionChartMetricSchema, poolPositionId, poolPositionKeySchema, poolPositionSchema, positionClaimableWithdrawalSchema, positionCollateralSchema, positionFilterQueryParamsSchema, positionFilterQuerySchema, positionFilterSchema, positionId, positionKeySchema, positionKindSchema, positionPendingWithdrawalSchema, positionSchema, positionTransactionKindSchema, positionTransactionSchema, positionWithdrawalsSchema, positionsTotalsSchema, priceFeedDataSchema, priceFeedSummarySchema, quotaAssetSchema, rateCurvePointSchema, rateCurveSchema, receivedAssetSchema, responseMetadataSchema, responseSchema, rewardsPnLSchema, rewardsSchema, sdkErr, sdkOk, strategyOpportunityChartMetricSchema, strategyOpportunityDetailSchema, strategyOpportunityId, strategyOpportunityKeySchema, strategyOpportunitySchema, strategyPositionChartMetricSchema, strategyPositionId, strategyPositionKeySchema, strategyPositionSchema, timestampSchema, tokenAmountSchema, tokenQuotaRateSchema, tokenRewardsPnLSchema, tokenRewardsSchema, tokenSchema, tolerance, txCallSchema, underlyingTokenSchema };
|
|
25
|
+
import { positionClaimableWithdrawalSchema, positionPendingWithdrawalSchema, positionWithdrawalsSchema, withdrawalOutputAmountSchema } from "./withdrawals.schema.js";
|
|
26
|
+
export { CHART_METRIC_UNITS, CHART_RANGES, CHART_UNAVAILABLE_CODES, ERROR_ADAPTER_CALL_OUTSIDE_BRACKET, ERROR_INVALID_TRANSACTION_VALUE, ERROR_MALFORMED_BRACKET, ERROR_NON_ADAPTER_CALL_IN_BRACKET, ERROR_UNPREVIEWABLE_ADAPTER_CALL, ERROR_UNPRICEABLE_TOKEN, ERROR_UNSUPPORTED_OUT_OF_BRACKET_CALL, FILTER_ALL, POOL_OPPORTUNITY_CHART_METRICS, POOL_POSITION_CHART_METRICS, STRATEGY_OPPORTUNITY_CHART_METRICS, STRATEGY_POSITION_CHART_METRICS, STRATEGY_POSITION_COLLATERAL_ERROR, amountSchema, apyBreakdownSchema, asEstimated, assetTypeSchema, backendPreferred, booleanParamSchema, borrowRateBreakdownSchema, bpsSchema, chainFailedSchema, chainIdSchema, chainMetadataSchema, chainSucceededSchema, chartBundleSchemaFor, chartDenominationSchema, chartMetricSchema, chartQueryCodec, chartQueryParamsSchema, chartQuerySchema, chartRangeSchema, chartSeriesSchema, chartValueSchema, chartWindowSchema, compareTagOf, curatorNameSchema, curatorSchema, dataSourceSchema, delayedReceivedAssetSchema, encodeFlag, filterAllSchema, filterable, instantReceivedAssetSchema, isFilterSet, isSDKError, leverageSchema, liquidatableAccountFilterSchema, liquidatableAccountSchema, liquidationApprovalSchema, liquidationDetailsSchema, liquidationPositionId, liquidationPositionSchema, matchesLiquidatableAccountFilter, matchesOpportunityFilter, matchesPositionFilter, noticeKindSchema, noticeSchema, offchainOnly, onchainOnly, opportunityBaseSchema, opportunityDetailSchema, opportunityFilterQueryParamsSchema, opportunityFilterQuerySchema, opportunityFilterSchema, opportunityId, opportunityKeySchema, opportunityKindSchema, opportunitySchema, opportunityTotalsSchema, pnlBreakdownSchema, pointRewardsSchema, pointsProgramPnLSchema, pointsProgramSchema, pointsRewardsPnLSchema, poolOpportunityChartMetricSchema, poolOpportunityDetailSchema, poolOpportunityId, poolOpportunityKeySchema, poolOpportunitySchema, poolPositionChartMetricSchema, poolPositionId, poolPositionKeySchema, poolPositionSchema, positionClaimableWithdrawalSchema, positionCollateralSchema, positionFilterQueryParamsSchema, positionFilterQuerySchema, positionFilterSchema, positionId, positionKeySchema, positionKindSchema, positionPendingWithdrawalSchema, positionSchema, positionTransactionKindSchema, positionTransactionSchema, positionWithdrawalsSchema, positionsTotalsSchema, priceFeedDataSchema, priceFeedSummarySchema, quotaAssetSchema, rateCurvePointSchema, rateCurveSchema, receivedAssetSchema, responseMetadataSchema, responseSchema, rewardsPnLSchema, rewardsSchema, sdkErr, sdkOk, strategyOpportunityChartMetricSchema, strategyOpportunityDetailSchema, strategyOpportunityId, strategyOpportunityKeySchema, strategyOpportunitySchema, strategyPositionChartMetricSchema, strategyPositionId, strategyPositionKeySchema, strategyPositionSchema, timestampSchema, tokenAmountSchema, tokenQuotaRateSchema, tokenRewardsPnLSchema, tokenRewardsSchema, tokenSchema, tolerance, txCallSchema, underlyingTokenSchema, withdrawalOutputAmountSchema };
|
|
@@ -4,12 +4,16 @@ import { delayedIntentSchema } from "./delayed-intent.schema.js";
|
|
|
4
4
|
import { z } from "zod/v4";
|
|
5
5
|
//#region src/model/withdrawals.schema.ts
|
|
6
6
|
/**
|
|
7
|
+
* {@link WithdrawalOutputAmount}
|
|
8
|
+
**/
|
|
9
|
+
const withdrawalOutputAmountSchema = tokenAmountSchema.extend({ isDelayed: z.boolean() });
|
|
10
|
+
/**
|
|
7
11
|
* {@link PositionClaimableWithdrawal}
|
|
8
12
|
**/
|
|
9
13
|
const positionClaimableWithdrawalSchema = z.object({
|
|
10
14
|
sourceToken: tokenSchema,
|
|
11
15
|
withdrawalPhantomToken: tokenAmountSchema,
|
|
12
|
-
outputs: z.array(
|
|
16
|
+
outputs: z.array(withdrawalOutputAmountSchema),
|
|
13
17
|
claimCall: txCallSchema,
|
|
14
18
|
redeemer: ZodAddress().optional(),
|
|
15
19
|
intent: delayedIntentSchema.optional()
|
|
@@ -20,7 +24,7 @@ const positionClaimableWithdrawalSchema = z.object({
|
|
|
20
24
|
const positionPendingWithdrawalSchema = z.object({
|
|
21
25
|
sourceToken: tokenSchema,
|
|
22
26
|
withdrawalPhantomToken: tokenSchema,
|
|
23
|
-
expectedOutputs: z.array(
|
|
27
|
+
expectedOutputs: z.array(withdrawalOutputAmountSchema),
|
|
24
28
|
claimableAt: timestampSchema,
|
|
25
29
|
redeemer: ZodAddress().optional(),
|
|
26
30
|
intent: delayedIntentSchema.optional()
|
|
@@ -33,4 +37,4 @@ const positionWithdrawalsSchema = z.object({
|
|
|
33
37
|
pending: z.array(positionPendingWithdrawalSchema)
|
|
34
38
|
});
|
|
35
39
|
//#endregion
|
|
36
|
-
export { positionClaimableWithdrawalSchema, positionPendingWithdrawalSchema, positionWithdrawalsSchema };
|
|
40
|
+
export { positionClaimableWithdrawalSchema, positionPendingWithdrawalSchema, positionWithdrawalsSchema, withdrawalOutputAmountSchema };
|