@kamino-finance/metavault-sdk 1.2.0 → 1.3.1
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/README.md +65 -1
- package/dist/classes/metaVaultClient.d.ts +1 -1
- package/dist/classes/metaVaultClient.js +1 -1
- package/dist/server/feeWrapped.d.ts +314 -0
- package/dist/server/feeWrapped.d.ts.map +1 -0
- package/dist/server/feeWrapped.js +615 -0
- package/dist/server/feeWrapped.js.map +1 -0
- package/dist/utils/constants.d.ts +7 -0
- package/dist/utils/constants.d.ts.map +1 -1
- package/dist/utils/constants.js +8 -1
- package/dist/utils/constants.js.map +1 -1
- package/dist/utils/errors.d.ts.map +1 -1
- package/dist/utils/errors.js +55 -3
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/feeWrapped.d.ts +107 -0
- package/dist/utils/feeWrapped.d.ts.map +1 -0
- package/dist/utils/feeWrapped.js +186 -0
- package/dist/utils/feeWrapped.js.map +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -0
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/simulation.d.ts +6 -0
- package/dist/utils/simulation.d.ts.map +1 -1
- package/dist/utils/simulation.js +11 -3
- package/dist/utils/simulation.js.map +1 -1
- package/package.json +2 -2
- package/src/classes/metaVaultClient.ts +1 -1
- package/src/server/feeWrapped.ts +1039 -0
- package/src/utils/constants.ts +8 -0
- package/src/utils/errors.ts +60 -3
- package/src/utils/feeWrapped.ts +251 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/simulation.ts +11 -4
package/README.md
CHANGED
|
@@ -251,6 +251,70 @@ if (limits.blockedBy !== null) {
|
|
|
251
251
|
// `maxDepositLamports` is null when nothing binds; `limitedBy` names the constraint that did.
|
|
252
252
|
```
|
|
253
253
|
|
|
254
|
+
### Fee-wrapped vaults
|
|
255
|
+
|
|
256
|
+
A fee-wrapped vault's only token with a non-zero target weight is a Kamino Vault (kvault) share
|
|
257
|
+
token, its Ktoken, so that the vault admin can charge fees on top of that kvault. It prices the
|
|
258
|
+
Ktoken at a fixed 1.0 (`FEE_WRAPPED_KTOKEN_SCOPE_CONFIGURATION`). Detection is safe for browser
|
|
259
|
+
bundles:
|
|
260
|
+
|
|
261
|
+
```typescript
|
|
262
|
+
import {
|
|
263
|
+
detectFeeWrapping,
|
|
264
|
+
findKtokenVaults,
|
|
265
|
+
getEnabledVaultTokens,
|
|
266
|
+
} from "@kamino-finance/metavault-sdk";
|
|
267
|
+
|
|
268
|
+
const weighted = getEnabledVaultTokens(vaultState)
|
|
269
|
+
.filter((token) => vaultState.vaultTokens[token.index].weight > 0n)
|
|
270
|
+
.map((token) => token.mint);
|
|
271
|
+
const wrapping = detectFeeWrapping(vaultState, await findKtokenVaults(rpc, weighted));
|
|
272
|
+
// null, or the Ktoken's enabled-token entry, its kvault, and the kvault's underlying mint
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
A server holding kvault states already builds the lookup with `ktokenVaultFromState` instead.
|
|
276
|
+
`underlyingDepositBounds` and `underlyingWithdrawBounds` restate `getDepositLimits` and
|
|
277
|
+
`getWithdrawLimits` in the underlying token.
|
|
278
|
+
|
|
279
|
+
Users can also deposit and withdraw the kvault's underlying token. The kvault program's own
|
|
280
|
+
deposit and withdraw then run in the same transaction as `buy` and `sell`. The builders use
|
|
281
|
+
klend-sdk's classes, so they live outside the main entry, for servers (ESM imports add `.js`):
|
|
282
|
+
|
|
283
|
+
```typescript
|
|
284
|
+
import {
|
|
285
|
+
buildFeeWrappedDeposit,
|
|
286
|
+
buildFeeWrappedWithdraw,
|
|
287
|
+
InsufficientKvaultLiquidityError,
|
|
288
|
+
} from "@kamino-finance/metavault-sdk/dist/server/feeWrapped";
|
|
289
|
+
|
|
290
|
+
const kvault = { client: kvaultClient, vault: wrapping.kvault }; // or cached KaminoVault, reserves
|
|
291
|
+
const deposit = await buildFeeWrappedDeposit({
|
|
292
|
+
client,
|
|
293
|
+
user,
|
|
294
|
+
vaultState: vaultAddress,
|
|
295
|
+
wrapping,
|
|
296
|
+
kvault,
|
|
297
|
+
underlyingAmount,
|
|
298
|
+
minSharesOut,
|
|
299
|
+
});
|
|
300
|
+
// Compile deposit.instructions as a v0 transaction against deposit.lookupTables.
|
|
301
|
+
|
|
302
|
+
// Throws InsufficientKvaultLiquidityError, with `maxUnderlyingOut`, when the kvault is short, and
|
|
303
|
+
// KvaultWithdrawBelowMinimumError when a kvault withdraw instruction would pay out too little.
|
|
304
|
+
const withdrawal = await buildFeeWrappedWithdraw({
|
|
305
|
+
client,
|
|
306
|
+
user,
|
|
307
|
+
vaultState: vaultAddress,
|
|
308
|
+
wrapping,
|
|
309
|
+
kvault,
|
|
310
|
+
sharesAmount,
|
|
311
|
+
minUnderlyingOut,
|
|
312
|
+
});
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
`buildKtokenSeedIxs` returns the kvault deposit to prepend to `createVaultIxs` when a new vault's
|
|
316
|
+
first token is a Ktoken and the admin holds less than `INITIAL_DEPOSIT_SIZE` of it.
|
|
317
|
+
|
|
254
318
|
### Decode failures and events
|
|
255
319
|
|
|
256
320
|
```typescript
|
|
@@ -261,7 +325,7 @@ try {
|
|
|
261
325
|
} catch (error) {
|
|
262
326
|
const decoded = parseProgramError(error);
|
|
263
327
|
// Walks the @solana/kit SolanaError cause chain and attributes errors raised inside CPI'd
|
|
264
|
-
// programs (klend, farms, token-acl), not just metavault's own.
|
|
328
|
+
// programs (klend, kvault, token, token-2022, farms, token-acl), not just metavault's own.
|
|
265
329
|
if (decoded !== null) console.error(formatProgramError(decoded));
|
|
266
330
|
}
|
|
267
331
|
|
|
@@ -133,7 +133,7 @@ export declare class MetaVaultClient {
|
|
|
133
133
|
* (a fresh keypair, not a PDA, so `vaultKeypair` must sign) and `init_vault`.
|
|
134
134
|
*
|
|
135
135
|
* `admin` becomes the vault admin and seeds the vault from its ATA of the first token, which
|
|
136
|
-
* must already hold
|
|
136
|
+
* must already hold {@link INITIAL_DEPOSIT_SIZE} of it.
|
|
137
137
|
*/
|
|
138
138
|
createVaultIxs(admin: TransactionSigner, vaultKeypair: TransactionSigner, firstToken: {
|
|
139
139
|
mint: Address;
|
|
@@ -605,7 +605,7 @@ class MetaVaultClient {
|
|
|
605
605
|
* (a fresh keypair, not a PDA, so `vaultKeypair` must sign) and `init_vault`.
|
|
606
606
|
*
|
|
607
607
|
* `admin` becomes the vault admin and seeds the vault from its ATA of the first token, which
|
|
608
|
-
* must already hold
|
|
608
|
+
* must already hold {@link INITIAL_DEPOSIT_SIZE} of it.
|
|
609
609
|
*/
|
|
610
610
|
async createVaultIxs(admin, vaultKeypair, firstToken, options = {}) {
|
|
611
611
|
const lamports = await this._rpc.getMinimumBalanceForRentExemption(constants_1.VAULT_STATE_SPACE).send();
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import { KaminoReserve, KaminoVault, KaminoVaultClient, LedgerInstant, ShareExitLiquidityPlan, WithdrawPenalties } from "@kamino-finance/klend-sdk";
|
|
2
|
+
import { Address, GetBlockTimeApi, GetSlotApi, Instruction, Rpc, TransactionSigner } from "@solana/kit";
|
|
3
|
+
import Decimal from "decimal.js";
|
|
4
|
+
import { MetavaultState } from "../@codegen/metavault/accounts";
|
|
5
|
+
import { MetaVaultClient } from "../classes/metaVaultClient";
|
|
6
|
+
import { FeeWrapping } from "../utils/feeWrapped";
|
|
7
|
+
/**
|
|
8
|
+
* The kvault a builder works against, as klend-sdk objects. Pass what is cached; builders load
|
|
9
|
+
* what is omitted.
|
|
10
|
+
*/
|
|
11
|
+
export interface KvaultContext {
|
|
12
|
+
/** A client for the kvault's program. */
|
|
13
|
+
client: KaminoVaultClient;
|
|
14
|
+
/** The kvault with its state, or its address to load it from. */
|
|
15
|
+
vault: KaminoVault | Address;
|
|
16
|
+
/** States of the reserves in the kvault's allocation; a superset is fine. */
|
|
17
|
+
reserves?: Map<Address, KaminoReserve>;
|
|
18
|
+
/** The instant to accrue interest to. `currentLedgerInstant` when omitted. */
|
|
19
|
+
ledgerInstant?: LedgerInstant;
|
|
20
|
+
/** The kvault's effective withdrawal penalties. Only withdrawals use them. */
|
|
21
|
+
withdrawalPenalties?: WithdrawPenalties;
|
|
22
|
+
/**
|
|
23
|
+
* The addresses the kvault's own lookup table holds. A withdrawal the uninvested balance covers
|
|
24
|
+
* then names, as its backstop, a reserve whose withdraw accounts the table holds; see
|
|
25
|
+
* `backstopReserve`. Only withdrawals use it.
|
|
26
|
+
*/
|
|
27
|
+
lookupTableAddresses?: ReadonlySet<Address>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The current ledger instant at `confirmed`, as klend-sdk's `getCurrentLedgerInstant` reads it. An
|
|
31
|
+
* RPC that has not stored the newest confirmed block yet answers its block time with "block not
|
|
32
|
+
* available" (-32004); the finalized instant, seconds older, stands in then. Any other error is
|
|
33
|
+
* thrown.
|
|
34
|
+
*/
|
|
35
|
+
export declare function currentLedgerInstant(rpc: Rpc<GetSlotApi & GetBlockTimeApi>): Promise<LedgerInstant>;
|
|
36
|
+
/**
|
|
37
|
+
* Closes the wallet's wSOL account into native SOL after a SOL kvault's deposit or withdraw, when
|
|
38
|
+
* it held no wSOL before the transaction: everything in it then came from this transaction. A
|
|
39
|
+
* wallet that held wSOL keeps the account, and a deposit's unspent SOL or a withdrawal's payout
|
|
40
|
+
* stays in it as wSOL. Deposits only ever spend native SOL.
|
|
41
|
+
*/
|
|
42
|
+
export declare function closeWsolIxs(user: TransactionSigner, underlyingMint: Address, heldWsol: bigint): Promise<Instruction[]>;
|
|
43
|
+
/**
|
|
44
|
+
* A kvault deposit too small to mint any Ktokens past `KTOKEN_MARGIN`, so there is nothing for the
|
|
45
|
+
* metavault `buy` to take. `underlyingDepositBounds` gives the smallest deposit that works.
|
|
46
|
+
*/
|
|
47
|
+
export declare class KtokenDepositTooSmallError extends Error {
|
|
48
|
+
readonly kvault: Address;
|
|
49
|
+
/** The underlying base units asked to deposit. */
|
|
50
|
+
readonly underlyingAmount: bigint;
|
|
51
|
+
/** Ktoken base units the deposit is estimated to mint. */
|
|
52
|
+
readonly expectedKtokens: bigint;
|
|
53
|
+
constructor(kvault: Address, underlyingAmount: bigint, expectedKtokens: bigint);
|
|
54
|
+
}
|
|
55
|
+
export interface FeeWrappedDepositParams {
|
|
56
|
+
client: MetaVaultClient;
|
|
57
|
+
user: TransactionSigner;
|
|
58
|
+
vaultState: Address;
|
|
59
|
+
/** The metavault's state. Fetched when omitted. */
|
|
60
|
+
vault?: MetavaultState;
|
|
61
|
+
wrapping: FeeWrapping;
|
|
62
|
+
kvault: KvaultContext;
|
|
63
|
+
/** Underlying base units to deposit; lamports of native SOL for a SOL kvault. */
|
|
64
|
+
underlyingAmount: bigint;
|
|
65
|
+
/** The metavault `buy`'s minimum shares out. */
|
|
66
|
+
minSharesOut: bigint;
|
|
67
|
+
}
|
|
68
|
+
export interface FeeWrappedDepositResult {
|
|
69
|
+
/** In send order: the kvault deposit, then the metavault `buy` with its setup. */
|
|
70
|
+
instructions: Instruction[];
|
|
71
|
+
/** The lookup tables to compile with: the metavault's and the kvault's, those that are set. */
|
|
72
|
+
lookupTables: Address[];
|
|
73
|
+
/** Ktokens the kvault deposit is estimated to mint. */
|
|
74
|
+
expectedKtokens: bigint;
|
|
75
|
+
/**
|
|
76
|
+
* Ktokens the `buy` deposits: the estimate less `KTOKEN_MARGIN`, which is also the least the
|
|
77
|
+
* kvault deposit must mint. What it mints beyond that stays in the wallet.
|
|
78
|
+
*/
|
|
79
|
+
ktokenFloor: bigint;
|
|
80
|
+
}
|
|
81
|
+
/** Deposits the underlying into the kvault, then buys metavault shares with the Ktokens minted. */
|
|
82
|
+
export declare function buildFeeWrappedDeposit(params: FeeWrappedDepositParams): Promise<FeeWrappedDepositResult>;
|
|
83
|
+
/**
|
|
84
|
+
* The kvault cannot pay a redemption in full right now: its uninvested balance plus what its
|
|
85
|
+
* reserves can release falls short. Its withdraw would burn only the Ktokens it can pay for, which
|
|
86
|
+
* the `sell`'s Ktoken minimum cannot catch, so the withdrawal is refused instead.
|
|
87
|
+
*/
|
|
88
|
+
export declare class InsufficientKvaultLiquidityError extends Error {
|
|
89
|
+
readonly kvault: Address;
|
|
90
|
+
/** Underlying base units the redemption needs, after the kvault's withdrawal penalty. */
|
|
91
|
+
readonly neededUnderlying: bigint;
|
|
92
|
+
/** The most underlying base units the kvault can pay right now. */
|
|
93
|
+
readonly maxUnderlyingOut: bigint;
|
|
94
|
+
constructor(kvault: Address, neededUnderlying: bigint, maxUnderlyingOut: bigint);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* One of the kvault withdraw instructions would pay out no more than the kvault's minimum, which
|
|
98
|
+
* the kvault program rejects: the redemption is that small, or its last reserve would add only a
|
|
99
|
+
* sliver. A slightly different amount avoids it.
|
|
100
|
+
*/
|
|
101
|
+
export declare class KvaultWithdrawBelowMinimumError extends Error {
|
|
102
|
+
readonly kvault: Address;
|
|
103
|
+
/** Underlying base units the smallest withdraw instruction would pay out. */
|
|
104
|
+
readonly legUnderlying: bigint;
|
|
105
|
+
/** What each withdraw instruction must pay out more than: the kvault's minimum and penalty. */
|
|
106
|
+
readonly minimum: bigint;
|
|
107
|
+
constructor(kvault: Address, legUnderlying: bigint, minimum: bigint);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* The fewest Ktoken base units whose redemption pays at least `underlying` base units, at
|
|
111
|
+
* `underlyingPerKtoken` base units each, after the kvault's withdrawal penalty: charged by each of
|
|
112
|
+
* the `legs` withdraw instructions the redemption takes.
|
|
113
|
+
*/
|
|
114
|
+
export declare function ktokensForUnderlying(underlying: bigint, underlyingPerKtoken: Decimal, penalties: WithdrawPenalties, legs?: number): bigint;
|
|
115
|
+
/** What the kvault withdraw redeems after the `sell`, and the `sell`'s Ktoken minimum. */
|
|
116
|
+
export interface KtokenRedemption {
|
|
117
|
+
/** `true`: redeem the wallet's whole Ktoken balance, whatever the sale paid. */
|
|
118
|
+
redeemAll: boolean;
|
|
119
|
+
/** Ktokens redeemed; with `redeemAll`, the expected payout. */
|
|
120
|
+
ktokens: bigint;
|
|
121
|
+
/** The `sell`'s `minTokensOut`. */
|
|
122
|
+
minKtokensOut: bigint;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* The redemption rule. A wallet that held no Ktokens before the transaction redeems its whole
|
|
126
|
+
* balance after the sale, which must pay at least `leastKtokens`. One that held some redeems the
|
|
127
|
+
* expected payout less `KTOKEN_MARGIN`, or `leastKtokens` when that is more, and the sale must
|
|
128
|
+
* pay at least as much, so the redemption never reaches the Ktokens already held.
|
|
129
|
+
*
|
|
130
|
+
* @param leastKtokens the Ktokens the caller's minimum underlying needs, `ktokensForUnderlying`.
|
|
131
|
+
*/
|
|
132
|
+
export declare function ktokenRedemption(expectedKtokensOut: bigint, heldKtokens: bigint, leastKtokens: bigint): KtokenRedemption;
|
|
133
|
+
/** Where a kvault redemption's underlying comes from, in base units. */
|
|
134
|
+
export interface RedemptionPlan {
|
|
135
|
+
ktokens: bigint;
|
|
136
|
+
/** What the redemption pays, after the kvault's withdrawal penalty, counted once. */
|
|
137
|
+
netUnderlying: bigint;
|
|
138
|
+
/** Paid from the kvault's uninvested balance. */
|
|
139
|
+
fromAvailable: bigint;
|
|
140
|
+
/** Paid from each reserve, in the order to withdraw them. */
|
|
141
|
+
fromReserves: [Address, bigint][];
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* The reserve a withdrawal the uninvested balance covers names as its backstop: the most liquid
|
|
145
|
+
* of those whose withdraw accounts the kvault's lookup table holds, when there are any, so the
|
|
146
|
+
* transaction stays small. Otherwise, or without `inLookupTable`, the most liquid of all. `null`
|
|
147
|
+
* for a kvault with no allocated reserve.
|
|
148
|
+
*
|
|
149
|
+
* Only this choice looks at the table: a withdrawal that needs reserves takes them in klend-sdk's
|
|
150
|
+
* order, most liquid first.
|
|
151
|
+
*
|
|
152
|
+
* @param reserveLiquidity what each allocated reserve can release now (klend-sdk's
|
|
153
|
+
* `getReserveAllocationAvailableLiquidityToWithdraw`).
|
|
154
|
+
* @param inLookupTable the reserves whose withdraw accounts the table holds
|
|
155
|
+
* (`reservesInLookupTable`).
|
|
156
|
+
*/
|
|
157
|
+
export declare function backstopReserve(reserveLiquidity: ReadonlyMap<Address, Decimal>, inLookupTable?: ReadonlySet<Address>): Address | null;
|
|
158
|
+
/**
|
|
159
|
+
* The reserves of the kvault whose withdraw accounts all sit in `lookupTable`: everything a
|
|
160
|
+
* withdraw instruction names for its reserve, the reserve, its lending market and that market's
|
|
161
|
+
* authority, its liquidity supply and collateral mint, and the kvault's cToken vault for it.
|
|
162
|
+
*/
|
|
163
|
+
export declare function reservesInLookupTable(client: KaminoVaultClient, vault: KaminoVault, reserves: Map<Address, KaminoReserve>, lookupTable: ReadonlySet<Address>): Promise<Set<Address>>;
|
|
164
|
+
/**
|
|
165
|
+
* The redemption klend-sdk planned, in base units.
|
|
166
|
+
*
|
|
167
|
+
* A plan the uninvested balance covers alone still withdraws against a reserve, `backstop`, for
|
|
168
|
+
* nothing from it: the kvault program takes the uninvested balance first, and then makes up from
|
|
169
|
+
* that reserve whatever the balance lost before the transaction landed, instead of paying less.
|
|
170
|
+
* Only a kvault with no allocated reserve, a `null` backstop, withdraws from its balance alone.
|
|
171
|
+
*
|
|
172
|
+
* @param backstop see `backstopReserve`.
|
|
173
|
+
* @param minimum what a withdraw instruction must pay out more than: see `assertLegsAboveMinimum`.
|
|
174
|
+
* @throws {InsufficientKvaultLiquidityError} when the plan leaves part of the redemption unpaid.
|
|
175
|
+
* @throws {KvaultWithdrawBelowMinimumError} when the whole redemption pays out no more than
|
|
176
|
+
* `minimum`, a dust sale.
|
|
177
|
+
*/
|
|
178
|
+
export declare function toRedemptionPlan(kvault: Address, plan: ShareExitLiquidityPlan, backstop: Address | null, minimum: bigint): RedemptionPlan;
|
|
179
|
+
/** One kvault withdraw instruction: the uninvested balance first, then `reserve` when set. */
|
|
180
|
+
export interface RedemptionLeg {
|
|
181
|
+
reserve: Address | null;
|
|
182
|
+
/** Underlying base units the plan has it pay out. */
|
|
183
|
+
underlying: bigint;
|
|
184
|
+
ktokens: bigint;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Splits a redemption into withdraw instructions, one per reserve, the first also taking the
|
|
188
|
+
* uninvested balance.
|
|
189
|
+
*
|
|
190
|
+
* Every leg but the last burns the Ktokens worth what it pays, rounded down on its own, so none
|
|
191
|
+
* asks its reserve for more than planned; the last burns the rest. With `redeemAll` the last burns
|
|
192
|
+
* `u64::MAX`, which the kvault program clamps to the wallet's balance, and the others size from
|
|
193
|
+
* the payout less `KTOKEN_MARGIN`, so a sale paying a little under the expected still leaves the
|
|
194
|
+
* last leg Ktokens to burn.
|
|
195
|
+
*/
|
|
196
|
+
export declare function redemptionLegs(plan: RedemptionPlan, redeemAll: boolean): RedemptionLeg[];
|
|
197
|
+
/**
|
|
198
|
+
* Refuses a split the kvault program would reject: each withdraw instruction must pay out more
|
|
199
|
+
* than `minimum`, the kvault's minimum withdrawal plus its flat penalty.
|
|
200
|
+
*
|
|
201
|
+
* @throws {KvaultWithdrawBelowMinimumError} naming the smallest leg.
|
|
202
|
+
*/
|
|
203
|
+
export declare function assertLegsAboveMinimum(kvault: Address, legs: readonly RedemptionLeg[], minimum: bigint): void;
|
|
204
|
+
/** What a kvault can release now, in base units, the way klend-sdk plans a redemption. */
|
|
205
|
+
export interface KvaultLiquidity {
|
|
206
|
+
fromAvailable: bigint;
|
|
207
|
+
/** What each reserve can release, in the order a redemption withdraws them: most first. */
|
|
208
|
+
fromReserves: bigint[];
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* The most a redemption can pay out with at most `maxWithdrawInstructions` kvault withdraw
|
|
212
|
+
* instructions, after the penalty of each: the uninvested balance and the reserves, most liquid
|
|
213
|
+
* first, as `buildFeeWrappedWithdraw` splits it. A reserve the split would leave at no more than
|
|
214
|
+
* `minimum` ends it, as does a first instruction paying no more than that, which leaves `0n`.
|
|
215
|
+
*
|
|
216
|
+
* `0` asks for the uninvested balance alone: the one withdraw instruction that names only the
|
|
217
|
+
* backstop reserve and draws nothing from reserves.
|
|
218
|
+
*/
|
|
219
|
+
export declare function maxPayoutWithin(liquidity: KvaultLiquidity, maxWithdrawInstructions: number, rules: {
|
|
220
|
+
minimum: bigint;
|
|
221
|
+
penaltyPerExtraInstruction: bigint;
|
|
222
|
+
}): bigint;
|
|
223
|
+
/**
|
|
224
|
+
* The most underlying, in base units, a fee-wrapped withdrawal can pay out right now with at most
|
|
225
|
+
* `maxWithdrawInstructions` kvault withdraw instructions, after the penalty of each. It follows
|
|
226
|
+
* `buildFeeWrappedWithdraw`'s split, so a withdrawal expected to pay this much or less needs no
|
|
227
|
+
* more instructions, unless its last reserve would add only a sliver, which that builder refuses.
|
|
228
|
+
*
|
|
229
|
+
* The count says nothing of which reserves those instructions name, and so of the transaction's
|
|
230
|
+
* size. A withdrawal the kvault's uninvested balance covers names the `backstopReserve`, which can
|
|
231
|
+
* be smaller than a one-instruction withdrawal that draws on the most liquid reserve. `0` asks for
|
|
232
|
+
* that case: the most the uninvested balance alone pays out, with one withdraw instruction that
|
|
233
|
+
* names the backstop and draws nothing from reserves, or `0n` when that is no more than the
|
|
234
|
+
* kvault's minimum withdrawal and flat penalty.
|
|
235
|
+
*/
|
|
236
|
+
export declare function maxUnderlyingOutWithin(kvault: KvaultContext, maxWithdrawInstructions: number): Promise<bigint>;
|
|
237
|
+
export interface FeeWrappedWithdrawParams {
|
|
238
|
+
client: MetaVaultClient;
|
|
239
|
+
user: TransactionSigner;
|
|
240
|
+
vaultState: Address;
|
|
241
|
+
/** The metavault's state. Fetched when omitted. */
|
|
242
|
+
vault?: MetavaultState;
|
|
243
|
+
wrapping: FeeWrapping;
|
|
244
|
+
kvault: KvaultContext;
|
|
245
|
+
/** Metavault shares to sell. */
|
|
246
|
+
sharesAmount: bigint;
|
|
247
|
+
/**
|
|
248
|
+
* The least underlying to receive, in base units. It becomes the `sell`'s Ktoken minimum at the
|
|
249
|
+
* kvault's estimated share price; the underlying itself is not checked on-chain.
|
|
250
|
+
*/
|
|
251
|
+
minUnderlyingOut: bigint;
|
|
252
|
+
/**
|
|
253
|
+
* Ktokens the `sell` of `sharesAmount` pays, from simulating it: `previewSell`'s `tokensOut`, or
|
|
254
|
+
* the `WithdrawResultEvent`'s `availableToSendToUser`. Simulated with `previewSell` if omitted.
|
|
255
|
+
*/
|
|
256
|
+
expectedKtokensOut?: bigint;
|
|
257
|
+
/** The wallet's Ktoken balance before the transaction. Read at `confirmed` when omitted. */
|
|
258
|
+
heldKtokens?: bigint;
|
|
259
|
+
}
|
|
260
|
+
export interface FeeWrappedWithdrawResult {
|
|
261
|
+
/** In send order: the metavault `sell` with its setup, then the kvault withdraw. */
|
|
262
|
+
instructions: Instruction[];
|
|
263
|
+
/** The lookup tables to compile with: the metavault's and the kvault's, those that are set. */
|
|
264
|
+
lookupTables: Address[];
|
|
265
|
+
expectedKtokensOut: bigint;
|
|
266
|
+
/** The `sell`'s `minTokensOut`. */
|
|
267
|
+
minKtokensOut: bigint;
|
|
268
|
+
/** `true` when the kvault withdraw redeems the wallet's whole Ktoken balance. */
|
|
269
|
+
redeemAll: boolean;
|
|
270
|
+
/** Ktokens the kvault withdraw redeems; with `redeemAll`, the expected payout. */
|
|
271
|
+
ktokensRedeemed: bigint;
|
|
272
|
+
/** Kvault withdraw instructions the redemption takes, each charging the withdrawal penalty. */
|
|
273
|
+
withdrawInstructions: number;
|
|
274
|
+
/**
|
|
275
|
+
* Underlying base units the redemption is expected to pay, after the kvault's penalty on each
|
|
276
|
+
* withdraw instruction. The fees the kvault charges as it executes can make the payout a few
|
|
277
|
+
* base units less.
|
|
278
|
+
*/
|
|
279
|
+
expectedUnderlyingOut: bigint;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Sells metavault shares for Ktokens, then redeems them with the kvault (see `ktokenRedemption`).
|
|
283
|
+
*
|
|
284
|
+
* @throws {InsufficientKvaultLiquidityError} when the kvault cannot pay the redemption in full.
|
|
285
|
+
* @throws {KvaultWithdrawBelowMinimumError} when a withdraw instruction would pay out too little.
|
|
286
|
+
*/
|
|
287
|
+
export declare function buildFeeWrappedWithdraw(params: FeeWrappedWithdrawParams): Promise<FeeWrappedWithdrawResult>;
|
|
288
|
+
export interface KtokenSeedParams {
|
|
289
|
+
admin: TransactionSigner;
|
|
290
|
+
/** The kvault whose Ktoken is the new vault's first token. */
|
|
291
|
+
kvault: KvaultContext;
|
|
292
|
+
/** The admin's Ktoken balance. Read at `confirmed` when omitted. */
|
|
293
|
+
heldKtokens?: bigint;
|
|
294
|
+
}
|
|
295
|
+
export interface KtokenSeed {
|
|
296
|
+
/** Prepend to `createVaultIxs`' instructions. Empty when the admin already holds the seed. */
|
|
297
|
+
instructions: Instruction[];
|
|
298
|
+
/** The kvault's lookup table, when the seed deposits and the kvault has one. */
|
|
299
|
+
lookupTables: Address[];
|
|
300
|
+
/** Underlying base units the seed deposits; `0n` when it deposits nothing. */
|
|
301
|
+
underlyingAmount: bigint;
|
|
302
|
+
/** Ktokens the deposit is estimated to mint. */
|
|
303
|
+
expectedKtokens: bigint;
|
|
304
|
+
/** The least the deposit must mint, at least the admin's shortfall. */
|
|
305
|
+
ktokenFloor: bigint;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* The kvault deposit that tops the admin's Ktoken balance up to the `INITIAL_DEPOSIT_SIZE`
|
|
309
|
+
* `init_vault` takes, for a vault whose first token is a Ktoken. It deposits the least the kvault
|
|
310
|
+
* accepts that still mints enough, usually the kvault's minimum deposit; the excess Ktokens stay
|
|
311
|
+
* in the wallet.
|
|
312
|
+
*/
|
|
313
|
+
export declare function buildKtokenSeedIxs(params: KtokenSeedParams): Promise<KtokenSeed>;
|
|
314
|
+
//# sourceMappingURL=feeWrapped.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"feeWrapped.d.ts","sourceRoot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