@anywalletkit/protocol-lending-kamino-solana 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -5,7 +5,12 @@ type AmountOption = bigint | number | 'max';
5
5
  declare function toAmount(amount: bigint | number | string | undefined): bigint;
6
6
  /** Base units → the decimal string Kamino's API takes ("1.5" for 1_500_000n at 6 decimals). */
7
7
  declare function toDecimalString(amount: bigint, decimals: number): string;
8
- /** Decimal string → base units, rounded down (the API reports decimals; the chain counts base units). */
8
+ /**
9
+ * Decimal string → base units, rounded down (the API reports decimals; the chain counts base units).
10
+ * Every figure the API reports — amounts, rates, prices — is non-negative, so a sign is refused: a
11
+ * negative deposit or `tokensPerShare` turned the guard's minimum into permission to drain (scan
12
+ * 2026-10-27 modules, F10/F11).
13
+ */
9
14
  declare function fromDecimalString(value: string | number, decimals: number): bigint;
10
15
  /** Fixed-point helper: `value` (decimal string) scaled by 1e18. */
11
16
  declare const WAD: bigint;
@@ -18,7 +23,11 @@ declare const lessBps: (amount: bigint, bps: number) => bigint;
18
23
  declare const KAMINO_API = "https://api.kamino.finance";
19
24
  /** Kamino's Main Market. */
20
25
  declare const MAIN_MARKET = "7u3HeHxYDLhnCoErrtycNokbQYbWGzLs6JSDqGAv5PfF";
21
- /** The decimal string that means "every share" on /ktx/kvault/withdraw. */
26
+ /**
27
+ * u64::MAX as a decimal share count. @deprecated The API scales `amount` by the shares' decimals and
28
+ * now refuses this as above u64 (2026-10-28, `DECIMAL_TO_LAMPORTS_OVERFLOW`); `withdraw({ amount: 'max' })`
29
+ * passes the wallet's exact share count, read from the chain, instead.
30
+ */
22
31
  declare const WITHDRAW_ALL = "18446744073709551615";
23
32
  interface ApiMarket {
24
33
  lendingMarket: string;
@@ -157,7 +166,7 @@ declare class KaminoApi {
157
166
  private ktx;
158
167
  /** Base64 unsigned v0 transaction: deposit `amount` (decimal, token units) into an Earn vault. */
159
168
  buildVaultDeposit(p: KtxVaultDeposit): Promise<string>;
160
- /** Base64 unsigned v0 transaction: redeem `amount` SHARES (decimal) — {@link WITHDRAW_ALL} for every share. */
169
+ /** Base64 unsigned v0 transaction: redeem `amount` SHARES (decimal). */
161
170
  buildVaultWithdraw(p: KtxVaultWithdraw): Promise<string>;
162
171
  buildLendDeposit(p: KtxLend): Promise<string>;
163
172
  buildLendWithdraw(p: KtxLend): Promise<string>;
@@ -165,6 +174,148 @@ declare class KaminoApi {
165
174
  buildLendRepay(p: KtxLend): Promise<string>;
166
175
  }
167
176
 
177
+ /** An account as the wallet's RPC returns it. */
178
+ interface ChainAccount {
179
+ owner: string;
180
+ lamports: bigint;
181
+ data: Uint8Array;
182
+ }
183
+ /** How far the API's figures may sit from the chain's before the operation is refused, in basis points. */
184
+ declare const CHAIN_TOLERANCE_BPS = 100n;
185
+ interface VaultChain {
186
+ tokenMint: string;
187
+ tokenDecimals: number;
188
+ tokenProgram: string;
189
+ sharesMint: string;
190
+ sharesDecimals: number;
191
+ tokenAvailable: bigint;
192
+ sharesIssued: bigint;
193
+ pendingFeesSf: bigint;
194
+ /** Reserves the vault has invested in and the collateral (cTokens) it holds in each. */
195
+ allocations: Array<{
196
+ reserve: string;
197
+ ctokens: bigint;
198
+ }>;
199
+ minDeposit: bigint;
200
+ minWithdraw: bigint;
201
+ farm: string | null;
202
+ }
203
+ /** A kVault's `VaultState`. */
204
+ declare function decodeVault(address: string, acc: ChainAccount | null): VaultChain;
205
+ interface ReserveChain {
206
+ lendingMarket: string;
207
+ mint: string;
208
+ decimals: number;
209
+ tokenProgram: string;
210
+ /** Liquidity the reserve owes its depositors (available + borrowed − fees), scaled by {@link SF}. */
211
+ totalLiquiditySf: bigint;
212
+ /** cTokens issued against it. */
213
+ collateralSupply: bigint;
214
+ /** Cumulative borrow rate (scaled by {@link SF}) at the reserve's last refresh. */
215
+ cumulativeBorrowRate: bigint;
216
+ }
217
+ /** A klend `Reserve`. */
218
+ declare function decodeReserve(address: string, acc: ChainAccount | null): ReserveChain;
219
+ interface ObligationChain {
220
+ lendingMarket: string;
221
+ owner: string;
222
+ deposits: Array<{
223
+ reserve: string;
224
+ ctokens: bigint;
225
+ }>;
226
+ borrows: Array<{
227
+ reserve: string;
228
+ borrowedSf: bigint;
229
+ cumulativeBorrowRate: bigint;
230
+ }>;
231
+ }
232
+ /** A klend `Obligation`. */
233
+ declare function decodeObligation(address: string, acc: ChainAccount | null): ObligationChain;
234
+ /** What an obligation leg is worth in base units of the reserve's token: deposits rounded down, debt (with the interest the reserve has accrued since) up. */
235
+ declare function depositedLiquidity(o: ObligationChain, r: ReserveChain, reserve: string): bigint;
236
+ declare function debtNow(o: ObligationChain, r: ReserveChain, reserve: string): bigint;
237
+ /** A farm `UserState`: whose it is, which farm, and the shares staked (base units). */
238
+ declare function decodeFarmUser(address: string, acc: ChainAccount | null): {
239
+ farm: string;
240
+ owner: string;
241
+ staked: bigint;
242
+ };
243
+ /**
244
+ * Assets per share in token units, as a WAD (what the API's `tokensPerShare` means): the vault's idle
245
+ * tokens plus what its cTokens redeem for at each reserve's rate, less pending fees, over the shares
246
+ * issued. An empty vault prices a share at one token.
247
+ */
248
+ declare function vaultTokensPerShareWad(v: VaultChain, reserves: ReadonlyMap<string, ReserveChain>): bigint;
249
+
250
+ /** Mirrors wallet-solana's `SerializedInstructionRule`. */
251
+ interface InstructionRule {
252
+ program: string;
253
+ /** Hex the data starts with. */
254
+ data?: string;
255
+ /** Exact data length in bytes. */
256
+ dataLength?: number;
257
+ /** Accounts by position: this address or one of these. */
258
+ accounts?: Readonly<Record<number, string | readonly string[]>>;
259
+ }
260
+ /** Anchor discriminators of the instructions the rules allow. */
261
+ declare const IX: Readonly<{
262
+ initUserMetadata: "75a9b045c5170fa2";
263
+ initObligation: "fb0ae74c1b0b9f60";
264
+ initObligationFarmsForReserve: "883f0fbad398a8a4";
265
+ refreshReserve: "02da8aeb4fc91966";
266
+ refreshObligation: "218493e497c04859";
267
+ refreshObligationFarmsForReserve: "8c90fd150a4af803";
268
+ requestElevationGroup: "2477fb8122f00793";
269
+ depositV2: "d8e0bf1bcc9766af";
270
+ withdrawV2: "eb34779895c51407";
271
+ borrowV2: "a1808ff5abc7c206";
272
+ repayV2: "74aed54cb435d290";
273
+ vaultDeposit: "f223c68952e1f2b6";
274
+ vaultWithdraw: "b712469c946da122";
275
+ vaultWithdrawFromAvailable: "1383709baadc2239";
276
+ farmInitializeUser: "6f11b9fa3c7a26fe";
277
+ farmStake: "ceb0ca12c8d1b36c";
278
+ farmUnstake: "5a5f6b2acd7c32e1";
279
+ farmWithdrawUnstaked: "2466bb31dc248443";
280
+ }>;
281
+ /**
282
+ * What every Kamino transaction may carry around the protocol call: compute budget, memos, the
283
+ * wallet's own associated token accounts, and SOL wrapped into (and unwrapped from) its wSOL account.
284
+ */
285
+ declare function plumbing(me: string, wsolAta: string): InstructionRule[];
286
+ interface MarketRuleAccounts {
287
+ me: string;
288
+ market: string;
289
+ obligation: string;
290
+ reserve: string;
291
+ mint: string;
292
+ /** The wallet's token account for `mint` (the one the reserve's token program derives). */
293
+ ata: string;
294
+ wsolAta: string;
295
+ }
296
+ /** A Lend operation: refreshes, the obligation's farm set-up, and the one call the operation is — on this obligation, reserve, mint and ATA. */
297
+ declare function marketRules(op: 'deposit' | 'withdraw' | 'borrow' | 'repay', a: MarketRuleAccounts): InstructionRule[];
298
+ interface VaultRuleAccounts {
299
+ me: string;
300
+ vault: string;
301
+ mint: string;
302
+ sharesMint: string;
303
+ /** The wallet's token account for the vault's asset. */
304
+ tokenAta: string;
305
+ /** The wallet's shares account (either token program's derivation). */
306
+ sharesAtas: readonly string[];
307
+ /** The vault's farm and the wallet's state in it, when the vault has a farm. */
308
+ farm: {
309
+ farm: string;
310
+ userState: string;
311
+ } | null;
312
+ wsolAta: string;
313
+ }
314
+ /** An Earn vault operation: the deposit or redemption on this vault with the wallet's accounts, and the farm steps around it. */
315
+ declare function vaultRules(op: 'deposit' | 'withdraw', a: VaultRuleAccounts, opts: {
316
+ stake: boolean;
317
+ }): InstructionRule[];
318
+
168
319
  declare const SOLANA_MAINNET = "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp";
169
320
  declare const WSOL_MINT = "So11111111111111111111111111111111111111112";
170
321
  /** SOL allowance for rent (ATAs, farm user state) and priority fees on top of the amount itself. */
@@ -193,6 +344,7 @@ interface Expectation {
193
344
  tokenIn?: {
194
345
  mint: string;
195
346
  minAmount: bigint;
347
+ maxAmount?: bigint;
196
348
  };
197
349
  maxTokenOut?: {
198
350
  mint: string;
@@ -201,9 +353,10 @@ interface Expectation {
201
353
  }
202
354
  interface SerializedGuard {
203
355
  programAllowlist: readonly string[];
356
+ instructions: readonly InstructionRule[];
204
357
  expect: Expectation;
205
358
  }
206
- /** The write surface of a `@anywalletkit/wallet-solana` account this module needs. */
359
+ /** The surface of a `@anywalletkit/wallet-solana` account this module needs. */
207
360
  interface SolanaAccountSurface {
208
361
  getAddress(): Promise<string>;
209
362
  checkSerialized?(base64: string, guard: SerializedGuard): Promise<{
@@ -218,6 +371,8 @@ interface SolanaAccountSurface {
218
371
  getMintInfo?(mint: string): Promise<{
219
372
  decimals: number;
220
373
  }>;
374
+ /** Raw accounts in the order asked (wallet-solana 0.4+): the chain state the API's figures are checked against. */
375
+ getAccountInfos?(addresses: readonly string[]): Promise<Array<ChainAccount | null>>;
221
376
  }
222
377
  declare abstract class KaminoBase<TConfig extends KaminoBaseConfig> extends LendingProtocol<TConfig> {
223
378
  readonly api: KaminoApi;
@@ -231,17 +386,22 @@ declare abstract class KaminoBase<TConfig extends KaminoBaseConfig> extends Lend
231
386
  protected me(): Promise<string>;
232
387
  /** Mint decimals: the account reads the mint when it can; well-known mints need no RPC. */
233
388
  protected decimals(mint: string): Promise<number>;
234
- protected guard(expect: Expectation): SerializedGuard;
389
+ protected guard(expect: Expectation, instructions: readonly InstructionRule[]): SerializedGuard;
390
+ /** Accounts from the chain through the wallet's RPC, in the order asked (null where none exists). */
391
+ protected readChain(addresses: readonly string[]): Promise<Array<ChainAccount | null>>;
235
392
  /** The wallet sends `amount` of `mint` (SOL counts on the lamport side) and may lose rent on top. */
236
393
  protected spendExpectation(mint: string, amount: bigint, extraLamports?: bigint): Expectation;
237
- /** The wallet must receive at least `expected × (1 − slippage)` of `mint`; rent is the only SOL it may lose. */
238
- protected receiveExpectation(mint: string, expected: bigint, extraLamports?: bigint): Expectation;
394
+ /**
395
+ * The wallet must receive at least `expected × (1 − slippage)` of `mint` (and at most `max`, when
396
+ * given); rent is the only SOL it may lose.
397
+ */
398
+ protected receiveExpectation(mint: string, expected: bigint, extraLamports?: bigint, max?: bigint): Expectation;
239
399
  /** Simulate the built transaction through the account's guard and return its fee; nothing is signed. */
240
- protected quoteBuilt(base64: string, expect: Expectation): Promise<{
400
+ protected quoteBuilt(base64: string, expect: Expectation, instructions: readonly InstructionRule[]): Promise<{
241
401
  fee: bigint;
242
402
  }>;
243
403
  /** Guard, sign and broadcast the built transaction; the account refuses before signing if the proof fails. */
244
- protected sendBuilt(base64: string, expect: Expectation, method: string): Promise<{
404
+ protected sendBuilt(base64: string, expect: Expectation, instructions: readonly InstructionRule[], method: string): Promise<{
245
405
  hash: string;
246
406
  fee: bigint;
247
407
  }>;
@@ -254,7 +414,8 @@ interface KaminoVaultConfig extends KaminoBaseConfig {
254
414
  /**
255
415
  * Keep the shares in the wallet (default true). The guard then proves the shares arrive. With
256
416
  * `false` they are staked in the vault farm, as the Kamino app does: they never reach the wallet,
257
- * so the guard can only bound what leaves it, and the deposit's destination relies on the API.
417
+ * so the guard can only bound what leaves it; the instruction rules pin the stake to this vault's
418
+ * farm (from the chain) and the wallet's own farm state.
258
419
  */
259
420
  skipFarm?: boolean;
260
421
  /**
@@ -329,10 +490,13 @@ interface VaultWithdrawQuote extends LendingQuote {
329
490
  * does not lend to its depositors.
330
491
  *
331
492
  * The transaction comes from Kamino's API and is signed only after the
332
- * wallet's guard proves it calls only the Kamino / token programs and moves
333
- * exactly the declared balances (the deposit out, the asset in at the quoted
334
- * minimum, no other token account touched). Rates, TVL and positions are the
335
- * API's.
493
+ * wallet's guard proves it carries only the instructions the operation takes,
494
+ * on this vault and the wallet's own accounts, and moves exactly the declared
495
+ * balances (the deposit out, the asset in at the minimum, no other token
496
+ * account touched). The bounds come from the chain: the share price from the
497
+ * vault's state and its reserves, the position from the wallet's shares
498
+ * account and farm stake; the API's price must agree with it. Reads
499
+ * (`getVault`, `getPosition`, APY, TVL) are the API's.
336
500
  */
337
501
  declare class KaminoVaultProtocolSolana extends KaminoBase<KaminoVaultConfig> {
338
502
  readonly vault: string;
@@ -358,6 +522,14 @@ declare class KaminoVaultProtocolSolana extends KaminoBase<KaminoVaultConfig> {
358
522
  /** shares (share units) × tokensPerShare → asset units; decimals may differ between the two mints. */
359
523
  private sharesToAssets;
360
524
  private assetsToShares;
525
+ /**
526
+ * What a write's bounds are computed from, read from the chain (scan 2026-10-27 modules, F2/F11/F12):
527
+ * the vault's state and the reserves it invests in give the share price, the wallet's shares
528
+ * account and farm state its position. The API must describe the same vault — mints, decimals —
529
+ * and quote a `tokensPerShare` within 1% of the chain's; a lying rate is refused, never used.
530
+ */
531
+ private onChain;
532
+ private rules;
361
533
  private plan;
362
534
  private supplyExpectation;
363
535
  quoteSupply(o: KaminoVaultSupplyOptions): Promise<VaultSupplyQuote>;
@@ -420,7 +592,11 @@ interface MarketQuote extends LendingQuote {
420
592
  *
421
593
  * Tokens are named by mint. The first deposit creates the obligation (plus
422
594
  * user metadata and a per-user lookup table) and costs their rent, which the
423
- * guard allows once; every transaction is proven before signing.
595
+ * guard allows once; every transaction is proven before signing: only the
596
+ * instructions the operation takes, on the wallet's vanilla obligation, the
597
+ * resolved reserve and the wallet's own token account. The reserve is checked
598
+ * on chain, and 'max' amounts and the first-deposit allowance come from the
599
+ * obligation's on-chain state.
424
600
  */
425
601
  declare class KaminoMarketProtocolSolana extends KaminoBase<KaminoMarketConfig> {
426
602
  readonly market: string;
@@ -435,21 +611,33 @@ declare class KaminoMarketProtocolSolana extends KaminoBase<KaminoMarketConfig>
435
611
  resolveReserve(mint: string): Promise<ReserveInfo>;
436
612
  /** Obligations and per-mint totals. */
437
613
  getPosition(): Promise<MarketPosition>;
438
- private lend;
439
614
  /**
440
- * A wallet without its vanilla obligation here pays the rent of one on its first deposit. Only
441
- * that obligation counts (a Multiply one does not hold a plain deposit), and an API failure is a
442
- * refusal: guessing "first deposit" would widen what the transaction may spend.
615
+ * The reserve and the wallet's vanilla obligation from the chain (scan 2026-10-27 modules, F1/F6/F10):
616
+ * the reserve must be this market's and lend the token, 'max' comes from the obligation, and the
617
+ * instruction rules pin the operation to that obligation, reserve and the wallet's token account.
443
618
  */
619
+ private prepare;
620
+ private build;
621
+ /** A wallet without its vanilla obligation here (on chain) pays the rent of one on its first deposit. */
444
622
  private firstDepositExtra;
445
- /** Resolve 'max' against the position's supplied / borrowed total for the mint. */
623
+ /**
624
+ * 'max' from the chain: what the vanilla obligation holds in, or owes to, this reserve (debt with the
625
+ * interest the reserve has accrued). When the API reports a figure for that obligation it must agree
626
+ * within 1%: an inflated debt or a negative deposit is refused (scan 2026-10-27 modules, F6/F10).
627
+ */
446
628
  private resolveMax;
629
+ /** The API's figure for one leg of the wallet's vanilla obligation; undefined when it reports none. */
630
+ private reported;
447
631
  quoteSupply(o: SupplyOptions): Promise<MarketQuote>;
448
632
  supply(o: SupplyOptions): Promise<LendingResult>;
449
633
  quoteWithdraw(o: KaminoMarketWithdrawOptions): Promise<MarketQuote>;
450
634
  withdraw(o: KaminoMarketWithdrawOptions): Promise<LendingResult>;
635
+ /** A borrow pays out at most what was asked: a bigger one is debt the user did not choose. */
636
+ private borrowExpectation;
451
637
  quoteBorrow(o: BorrowOptions): Promise<MarketQuote>;
452
638
  borrow(o: BorrowOptions): Promise<LendingResult>;
639
+ /** Interest accrues between the quote and the block: allow the slippage tolerance on top of the debt read. */
640
+ private repayExpectation;
453
641
  quoteRepay(o: KaminoMarketRepayOptions): Promise<MarketQuote>;
454
642
  repay(o: KaminoMarketRepayOptions): Promise<LendingResult>;
455
643
  }
@@ -483,5 +671,9 @@ declare function findProgramAddress(seeds: readonly Uint8Array[], programId: str
483
671
  declare function vanillaObligation(user: string, market: string, klendProgram?: string): string;
484
672
  /** A kVault's shares mint: `["shares", vault]` under the kVault program. */
485
673
  declare function vaultSharesMint(vault: string, kvaultProgram: string): string;
674
+ /** The associated token account of `owner` for `mint` under a token program (classic Token or Token-2022). */
675
+ declare function associatedTokenAddress(owner: string, mint: string, tokenProgram: string): string;
676
+ /** A farm's state for one staker: `["user", farm, owner]` under the farms program (checked against mainnet user states, 2026-10-28). */
677
+ declare function farmUserState(farm: string, owner: string, farmsProgram?: string): string;
486
678
 
487
- export { ADDRESS_LOOKUP_TABLE_PROGRAM, type AmountOption, type ApiLoanLeg, type ApiMarket, type ApiObligation, type ApiReserve, type ApiVault, type ApiVaultAllocation, type ApiVaultMetrics, type ApiVaultPosition, DEFAULT_FIRST_DEPOSIT_ALLOWANCE, DEFAULT_RENT_ALLOWANCE, type Expectation, FARMS_PROGRAM, INSTRUCTION_NAMES, KAMINO_API, KAMINO_PROGRAM_ALLOWLIST, KLEND_PROGRAM, KNOWN_DECIMALS, KVAULT_PROGRAM, KaminoApi, KaminoBase, type KaminoBaseConfig, KaminoError, type KaminoMarketConfig, KaminoMarketProtocolSolana, type KaminoMarketRepayOptions, type KaminoMarketWithdrawOptions, type KaminoVaultConfig, KaminoVaultProtocolSolana, type KaminoVaultSupplyOptions, type KaminoVaultWithdrawOptions, type KtxLend, type KtxVaultDeposit, type KtxVaultWithdraw, MAIN_MARKET, MEMO_PROGRAM, type MarketInfo, type MarketPosition, type MarketQuote, type ReserveInfo, SOLANA_MAINNET, type SerializedGuard, type VaultInfo, type VaultPosition, type VaultSupplyQuote, type VaultWithdrawQuote, WAD, WITHDRAW_ALL, WSOL_MINT, KaminoVaultProtocolSolana as default, divWadUp, findProgramAddress, fromDecimalString, instructionName, lessBps, mulWad, toAmount, toDecimalString, toWad, vanillaObligation, vaultSharesMint };
679
+ export { ADDRESS_LOOKUP_TABLE_PROGRAM, type AmountOption, type ApiLoanLeg, type ApiMarket, type ApiObligation, type ApiReserve, type ApiVault, type ApiVaultAllocation, type ApiVaultMetrics, type ApiVaultPosition, CHAIN_TOLERANCE_BPS, type ChainAccount, DEFAULT_FIRST_DEPOSIT_ALLOWANCE, DEFAULT_RENT_ALLOWANCE, type Expectation, FARMS_PROGRAM, INSTRUCTION_NAMES, IX, type InstructionRule, KAMINO_API, KAMINO_PROGRAM_ALLOWLIST, KLEND_PROGRAM, KNOWN_DECIMALS, KVAULT_PROGRAM, KaminoApi, KaminoBase, type KaminoBaseConfig, KaminoError, type KaminoMarketConfig, KaminoMarketProtocolSolana, type KaminoMarketRepayOptions, type KaminoMarketWithdrawOptions, type KaminoVaultConfig, KaminoVaultProtocolSolana, type KaminoVaultSupplyOptions, type KaminoVaultWithdrawOptions, type KtxLend, type KtxVaultDeposit, type KtxVaultWithdraw, MAIN_MARKET, MEMO_PROGRAM, type MarketInfo, type MarketPosition, type MarketQuote, type MarketRuleAccounts, type ObligationChain, type ReserveChain, type ReserveInfo, SOLANA_MAINNET, type SerializedGuard, type SolanaAccountSurface, type VaultChain, type VaultInfo, type VaultPosition, type VaultRuleAccounts, type VaultSupplyQuote, type VaultWithdrawQuote, WAD, WITHDRAW_ALL, WSOL_MINT, associatedTokenAddress, debtNow, decodeFarmUser, decodeObligation, decodeReserve, decodeVault, KaminoVaultProtocolSolana as default, depositedLiquidity, divWadUp, farmUserState, findProgramAddress, fromDecimalString, instructionName, lessBps, marketRules, mulWad, plumbing, toAmount, toDecimalString, toWad, vanillaObligation, vaultRules, vaultSharesMint, vaultTokensPerShareWad };