@elmntl/vaults-v2 1.4.5 → 1.5.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.
Files changed (39) hide show
  1. package/README.md +40 -0
  2. package/dist/accounts/index.d.ts +2 -0
  3. package/dist/accounts/index.d.ts.map +1 -1
  4. package/dist/accounts/index.js +3 -1
  5. package/dist/accounts/index.js.map +1 -1
  6. package/dist/accounts/poolExt.d.ts +101 -0
  7. package/dist/accounts/poolExt.d.ts.map +1 -0
  8. package/dist/accounts/poolExt.js +211 -0
  9. package/dist/accounts/poolExt.js.map +1 -0
  10. package/dist/accounts/position.d.ts +8 -0
  11. package/dist/accounts/position.d.ts.map +1 -1
  12. package/dist/accounts/position.js +22 -0
  13. package/dist/accounts/position.js.map +1 -1
  14. package/dist/accounts/ticket.d.ts +8 -0
  15. package/dist/accounts/ticket.d.ts.map +1 -1
  16. package/dist/accounts/ticket.js +17 -0
  17. package/dist/accounts/ticket.js.map +1 -1
  18. package/dist/generated/idl/elemental_vault_v2.json +1204 -65
  19. package/dist/index.d.ts +3 -2
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +11 -1
  22. package/dist/index.js.map +1 -1
  23. package/dist/programs/elemental.d.ts +32 -1
  24. package/dist/programs/elemental.d.ts.map +1 -1
  25. package/dist/programs/elemental.js +166 -0
  26. package/dist/programs/elemental.js.map +1 -1
  27. package/dist/types.d.ts +53 -0
  28. package/dist/types.d.ts.map +1 -1
  29. package/dist/types.js +25 -2
  30. package/dist/types.js.map +1 -1
  31. package/package.json +1 -1
  32. package/src/accounts/index.ts +2 -0
  33. package/src/accounts/poolExt.ts +272 -0
  34. package/src/accounts/position.ts +22 -0
  35. package/src/accounts/ticket.ts +20 -0
  36. package/src/generated/idl/elemental_vault_v2.json +1204 -65
  37. package/src/index.ts +12 -1
  38. package/src/programs/elemental.ts +198 -1
  39. package/src/types.ts +64 -1
package/src/index.ts CHANGED
@@ -6,7 +6,8 @@ export {
6
6
  ELE7_PROGRAM_ID,
7
7
  MIGRATION_TARGET_PROGRAM_ID,
8
8
  } from "./programs";
9
- export { Pool, Position, WithdrawalTicket } from "./accounts";
9
+ export { Pool, Position, WithdrawalTicket, PoolExt } from "./accounts";
10
+ export type { NavReading, AltDepositQuote, AltExitQuote } from "./accounts";
10
11
  export {
11
12
  PoolData,
12
13
  PositionData,
@@ -17,4 +18,14 @@ export {
17
18
  ONRE_PROGRAM_ID,
18
19
  ONRE_STATE,
19
20
  POOL_FAUCET_CONFIG,
21
+ PoolExtData,
22
+ POOL_EXT_FLAG_ALT_DEPOSITS,
23
+ POOL_EXT_FLAG_ALT_EXITS,
24
+ CONVERSION_SOURCE_POSTED,
25
+ CONVERSION_SOURCE_SCOPE,
26
+ NAV_ONE,
27
+ SCOPE_PROGRAM_ID,
28
+ SCOPE_PRICES,
29
+ SCOPE_INDEX,
30
+ USDC_MINT,
20
31
  } from "./types";
@@ -21,11 +21,12 @@ import {
21
21
  getAccount,
22
22
  TokenAccountNotFoundError,
23
23
  } from "@solana/spl-token";
24
- import { Pool, Position, WithdrawalTicket } from "../accounts";
24
+ import { Pool, Position, WithdrawalTicket, PoolExt } from "../accounts";
25
25
  import {
26
26
  PoolData,
27
27
  PositionData,
28
28
  WithdrawalTicketData,
29
+ PoolExtData,
29
30
  SwapAdapter,
30
31
  FIXED_ONE,
31
32
  ONRE_PROGRAM_ID,
@@ -124,6 +125,14 @@ export class ElementalV2Context {
124
125
  );
125
126
  }
126
127
 
128
+ findPoolExtAddress(poolAddress: PublicKey): PublicKey {
129
+ return PoolExt.findAddress(poolAddress, this.program.programId);
130
+ }
131
+
132
+ findExtAuthority(poolAddress: PublicKey): PublicKey {
133
+ return PoolExt.findAuthority(poolAddress, this.program.programId);
134
+ }
135
+
127
136
  // ============================================================================
128
137
  // POOL OPERATIONS
129
138
  // ============================================================================
@@ -301,6 +310,10 @@ export class ElementalV2Context {
301
310
  bump: 0,
302
311
  ticketMode: 0,
303
312
  ticketCount: 0,
313
+ altDeposited: new BN(0),
314
+ vaultDeposited: new BN(0),
315
+ altWithdrawn: new BN(0),
316
+ vaultWithdrawn: new BN(0),
304
317
  };
305
318
  }
306
319
 
@@ -350,6 +363,172 @@ export class ElementalV2Context {
350
363
  );
351
364
  }
352
365
 
366
+ // ============================================================================
367
+ // POOL EXT (dual-token edges, ELE7 ele7-rwa-v1+)
368
+ // ============================================================================
369
+
370
+ /** The pool's PoolExt, or null when the pool has no second token (every pre-upgrade pool, all ELE6 pools). */
371
+ async loadPoolExt(pool: Pool): Promise<PoolExt | null> {
372
+ const address = this.findPoolExtAddress(pool.address);
373
+ const data = await (this.program.account as any).poolExt.fetchNullable(address);
374
+ if (!data) return null;
375
+ return new PoolExt(pool, address, data as PoolExtData);
376
+ }
377
+
378
+ /** PoolExts for many pools in one RPC round-trip (null where absent). */
379
+ async loadPoolExts(pools: Pool[]): Promise<(PoolExt | null)[]> {
380
+ const addresses = pools.map((p) => this.findPoolExtAddress(p.address));
381
+ const accounts = await (this.program.account as any).poolExt.fetchMultiple(addresses);
382
+ return accounts.map((data: PoolExtData | null, i: number) =>
383
+ data ? new PoolExt(pools[i], addresses[i], data) : null,
384
+ );
385
+ }
386
+
387
+ private async ensureAtaInstruction(mint: PublicKey, owner: PublicKey): Promise<{ ata: PublicKey; ix: TransactionInstruction | null }> {
388
+ const ata = getAssociatedTokenAddressSync(mint, owner, true);
389
+ try {
390
+ await getAccount(this.connection, ata);
391
+ return { ata, ix: null };
392
+ } catch (err) {
393
+ if (err instanceof TokenAccountNotFoundError) {
394
+ return { ata, ix: createAssociatedTokenAccountInstruction(this.walletAddress, ata, owner, mint) };
395
+ }
396
+ throw err;
397
+ }
398
+ }
399
+
400
+ private altOracleRemaining(ext: PoolExt): { pubkey: PublicKey; isSigner: boolean; isWritable: boolean }[] {
401
+ const oracle = ext.navOracle;
402
+ return oracle ? [{ pubkey: oracle, isSigner: false, isWritable: false }] : [];
403
+ }
404
+
405
+ /**
406
+ * Deposit the pool's alt token (USDC into an RWA pool, or the RWA into a USDC pool).
407
+ * `amount` is in human alt tokens. The credit lands in vault units at the on-chain
408
+ * NAV; `slippageBps` bounds how far the NAV may move between this quote and
409
+ * execution (program error CreditBelowMinimum beyond it). Returns the quote used.
410
+ */
411
+ async depositAltInstructions(
412
+ position: Position,
413
+ ext: PoolExt,
414
+ amount: number | string,
415
+ slippageBps = 10,
416
+ ): Promise<{ instructions: TransactionInstruction[]; quote: import("../accounts").AltDepositQuote }> {
417
+ if (!ext.pool.address.equals(position.pool.address)) throw new Error("PoolExt does not belong to the position's pool");
418
+ if (!ext.altDepositsEnabled) throw new Error("Alt deposits are disabled on this pool");
419
+ const quote = await ext.quoteDeposit(this.connection, amount);
420
+ if (quote.nav.isStale) throw new Error(`NAV is stale (${quote.nav.ageSeconds}s > ${ext.navMaxAge}s)`);
421
+ const minCredit = quote.credit.muln(10_000 - slippageBps).divn(10_000);
422
+
423
+ const instructions: TransactionInstruction[] = [];
424
+ if (!position.exists) instructions.push(await this.createPositionInstruction(position.pool));
425
+ const userAltAccount = getAssociatedTokenAddressSync(ext.altMintAddress, this.walletAddress, true);
426
+ const altMintInfo = await this.connection.getAccountInfo(ext.altMintAddress);
427
+ const tokenProgram = altMintInfo?.owner ?? TOKEN_PROGRAM_ID;
428
+
429
+ instructions.push(
430
+ await (this.program.methods as any)
431
+ .depositAlt(quote.altIn, minCredit)
432
+ .accounts({
433
+ positionOwner: { owner: this.walletAddress, position: position.address, pool: position.pool.address },
434
+ poolExt: ext.address,
435
+ userAltAccount,
436
+ altHolder: ext.altHolderAddress,
437
+ altMint: ext.altMintAddress,
438
+ tokenProgram,
439
+ })
440
+ .remainingAccounts(this.altOracleRemaining(ext))
441
+ .instruction(),
442
+ );
443
+ return { instructions, quote };
444
+ }
445
+
446
+ /**
447
+ * Request an exit paid in the alt token. `amount` is in human VAULT tokens, like
448
+ * `createTicket`: "0" = yield only, anything >= capital + yield = full exit.
449
+ * The program locks the NAV and fee onto the ticket; the payout is fixed from here.
450
+ */
451
+ async createTicketAltInstructions(
452
+ position: Position,
453
+ ext: PoolExt,
454
+ amount: number | string,
455
+ ): Promise<TransactionInstruction[]> {
456
+ if (!ext.pool.address.equals(position.pool.address)) throw new Error("PoolExt does not belong to the position's pool");
457
+ if (!ext.altExitsEnabled) throw new Error("Alt exits are disabled on this pool");
458
+ const amountBN = new BN(new Decimal(amount.toString()).mul(position.pool.vaultMultiplier).toFixed(0));
459
+ const ticketAddress = this.findTicketAddress(position.pool.address, position.ticketCount);
460
+ const ix = await (this.program.methods as any)
461
+ .createTicketAlt(amountBN)
462
+ .accounts({
463
+ payer: this.walletAddress,
464
+ positionOwner: { owner: this.walletAddress, position: position.address, pool: position.pool.address },
465
+ poolExt: ext.address,
466
+ ticket: ticketAddress,
467
+ systemProgram: SystemProgram.programId,
468
+ })
469
+ .remainingAccounts(this.altOracleRemaining(ext))
470
+ .instruction();
471
+ return [ix];
472
+ }
473
+
474
+ /** Redeem a matured alt ticket from the alt faucet. Creates the user's alt ATA if needed. */
475
+ async redeemTicketAltInstructions(
476
+ ticket: WithdrawalTicket,
477
+ position: Position,
478
+ ext: PoolExt,
479
+ ): Promise<TransactionInstruction[]> {
480
+ if (!ticket.isAltPayout) throw new Error("Ticket pays the vault token; use redeemTicketInstructions");
481
+ if (!ext.pool.address.equals(position.pool.address)) throw new Error("PoolExt does not belong to the position's pool");
482
+ const instructions: TransactionInstruction[] = [];
483
+ const { ata: userAltAccount, ix: createAta } = await this.ensureAtaInstruction(ext.altMintAddress, this.walletAddress);
484
+ if (createAta) instructions.push(createAta);
485
+ const altMintInfo = await this.connection.getAccountInfo(ext.altMintAddress);
486
+ const tokenProgram = altMintInfo?.owner ?? TOKEN_PROGRAM_ID;
487
+ // Anchor 0.30 optional account: the program id stands for None.
488
+ const feeDestinationAccount = ext.feeDestination
489
+ ? getAssociatedTokenAddressSync(ext.altMintAddress, ext.feeDestination, true)
490
+ : this.program.programId;
491
+
492
+ instructions.push(
493
+ await (this.program.methods as any)
494
+ .redeemTicketAlt()
495
+ .accounts({
496
+ owner: this.walletAddress,
497
+ ticket: ticket.address,
498
+ position: position.address,
499
+ pool: position.pool.address,
500
+ extAuthority: ext.authorityAddress,
501
+ poolExt: ext.address,
502
+ altFaucet: ext.altFaucetAddress,
503
+ userAltAccount,
504
+ feeDestinationAccount,
505
+ altMint: ext.altMintAddress,
506
+ tokenProgram,
507
+ })
508
+ .instruction(),
509
+ );
510
+ return instructions;
511
+ }
512
+
513
+ /** Permissionless: sweep the alt holder to the pool's manager wallet (ops). */
514
+ async flushAltToManagerInstruction(pool: Pool, ext: PoolExt): Promise<TransactionInstruction> {
515
+ const managerAltAccount = getAssociatedTokenAddressSync(ext.altMintAddress, pool.data.managerWallet, true);
516
+ const altMintInfo = await this.connection.getAccountInfo(ext.altMintAddress);
517
+ const tokenProgram = altMintInfo?.owner ?? TOKEN_PROGRAM_ID;
518
+ return await (this.program.methods as any)
519
+ .flushAltToManager()
520
+ .accounts({
521
+ pool: pool.address,
522
+ extAuthority: ext.authorityAddress,
523
+ poolExt: ext.address,
524
+ altHolder: ext.altHolderAddress,
525
+ managerAltAccount,
526
+ altMint: ext.altMintAddress,
527
+ tokenProgram,
528
+ })
529
+ .instruction();
530
+ }
531
+
353
532
  // ============================================================================
354
533
  // INSTRUCTION BUILDERS
355
534
  // ============================================================================
@@ -1078,6 +1257,24 @@ export class ElementalV2Context {
1078
1257
  return await this.sendTransaction(instructions);
1079
1258
  }
1080
1259
 
1260
+ async depositAlt(
1261
+ position: Position,
1262
+ ext: PoolExt,
1263
+ amount: number | string,
1264
+ slippageBps?: number,
1265
+ ): Promise<TransactionSignature> {
1266
+ const { instructions } = await this.depositAltInstructions(position, ext, amount, slippageBps);
1267
+ return await this.sendTransaction(instructions);
1268
+ }
1269
+
1270
+ async createTicketAlt(position: Position, ext: PoolExt, amount: number | string): Promise<TransactionSignature> {
1271
+ return await this.sendTransaction(await this.createTicketAltInstructions(position, ext, amount));
1272
+ }
1273
+
1274
+ async redeemTicketAlt(ticket: WithdrawalTicket, position: Position, ext: PoolExt): Promise<TransactionSignature> {
1275
+ return await this.sendTransaction(await this.redeemTicketAltInstructions(ticket, position, ext));
1276
+ }
1277
+
1081
1278
  async fundFaucet(
1082
1279
  pool: Pool,
1083
1280
  amount: number | string,
package/src/types.ts CHANGED
@@ -58,6 +58,14 @@ export interface PositionData {
58
58
  bump: number;
59
59
  ticketMode: number;
60
60
  ticketCount: number;
61
+ /** Cumulative alt-token deposits (alt units). Zero on positions created before ele7-rwa-v1. */
62
+ altDeposited: BN;
63
+ /** Cumulative vault-token deposits (vault units). Zero on positions created before ele7-rwa-v1. */
64
+ vaultDeposited: BN;
65
+ /** Cumulative alt-token payouts, net of fee (alt units). */
66
+ altWithdrawn: BN;
67
+ /** Cumulative vault-token payouts (vault units). */
68
+ vaultWithdrawn: BN;
61
69
  }
62
70
 
63
71
  // Raw withdrawal ticket data from on-chain account
@@ -69,8 +77,61 @@ export interface WithdrawalTicketData {
69
77
  createdEpochIndex: number;
70
78
  ticketIndex: number;
71
79
  bump: number;
80
+ /** 0 = paid in vault_mint via redeem_ticket; 1 = paid in the pool's alt token via redeem_ticket_alt. */
81
+ payoutMint: number;
82
+ /** NAV (USDC per 1 RWA, 1e9-scaled) locked when an alt ticket was created; 0 otherwise. */
83
+ navLocked: BN;
84
+ /** Exit fee (bps) locked when an alt ticket was created; 0 otherwise. */
85
+ feeBpsLocked: number;
72
86
  }
73
87
 
88
+ /**
89
+ * Raw PoolExt data (ELE7 ele7-rwa-v1+): a second token at a pool's edges.
90
+ * The pool's ledger stays in vault_mint; the alt converts at the NAV.
91
+ */
92
+ export interface PoolExtData {
93
+ pool: PublicKey;
94
+ altMint: PublicKey;
95
+ altHolder: PublicKey;
96
+ altFaucet: PublicKey;
97
+ feeDestination: PublicKey;
98
+ nav: BN;
99
+ navUpdatedAt: BN;
100
+ navMaxAge: number;
101
+ exitFeeBps: number;
102
+ altDecimals: number;
103
+ flags: number;
104
+ conversionSource: number;
105
+ bump: number;
106
+ navOracle: PublicKey;
107
+ navOracleIndex: number;
108
+ altIsUsdc: number;
109
+ extAuthorityBump: number;
110
+ }
111
+
112
+ /** PoolExt.flags bits */
113
+ export const POOL_EXT_FLAG_ALT_DEPOSITS = 1;
114
+ export const POOL_EXT_FLAG_ALT_EXITS = 2;
115
+ /** PoolExt.conversion_source values */
116
+ export const CONVERSION_SOURCE_POSTED = 0;
117
+ export const CONVERSION_SOURCE_SCOPE = 1;
118
+ /** NAV scale: USDC per 1 RWA × 1e9 */
119
+ export const NAV_ONE = new BN(1_000_000_000);
120
+
121
+ // Kamino Scope oracle (mainnet) — the on-chain NAV source for every RWA pool
122
+ export const SCOPE_PROGRAM_ID = new PublicKey("HFn8GnPADiny6XqUoWE8uRPPxb29ikn4yTuPa9MF2fWJ");
123
+ export const SCOPE_PRICES = new PublicKey("3t4JZcueEzTbVP6kLxXrL3VpWx45jDer4eqysweBchNH");
124
+ export const SCOPE_PRICES_LEN = 28_712;
125
+ export const SCOPE_ENTRIES_OFFSET = 40;
126
+ export const SCOPE_ENTRY_SIZE = 56;
127
+ /** Scope price-chain indices confirmed from the Kamino reserves (2026-11-16) */
128
+ export const SCOPE_INDEX: Record<string, number> = {
129
+ ONYC: 350,
130
+ NOPAL: 505,
131
+ AUTO: 451,
132
+ };
133
+ export const USDC_MINT = new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");
134
+
74
135
  // Position status
75
136
  export type PositionStatus =
76
137
  | "inactive"
@@ -107,7 +168,9 @@ export const POOL_FAUCET_CONFIG: Record<string, string> = {
107
168
  "FYiyK57wcpmVbQ2VWcFQxF15VRdQPp4S7pYdGwSQM1QK": "52cusstXjvXn9wWjW895GdKqHVKET9QxDc8n83Mztokx",
108
169
  // USDC X1 pool (ELE7, 10% APR) - separate faucet for withdrawals (created 2026-08-26)
109
170
  "FHhmYxqE5WLKZRqf3F2iFz4uey2CHH9iktbWwFuHhBqy": "Ad5moKb3yUguzDZNH3B1tcyz5Wk9DneTsD7fHmLzuTpn",
110
- // nOPAL X1 pool (ex-USDC X3) (ELE7, 17% APR, nOPAL) - separate faucet for withdrawals (created 2026-10-28)
171
+ // nOPAL X1 pool (ELE7, 17% APR, nOPAL-denominated, USDC edge via PoolExt) - vault faucet (created 2026-11-19)
172
+ "Fm2J5ah8nZ2eXPbWD8XWsZhm8x99ymDUNKzTNmN2Dfqp": "5yCGAbyKE4zzdEpyFBRJaHwyM78nYvgn9C4tpnDz8guG",
173
+ // USDC X3 pool (ELE7, 17% APR, USDC) - empty; was labelled "nOPAL X1" until the nOPAL-denominated pool above replaced it (2026-11-19)
111
174
  "H9b7iXE8XN39DWKxQH2rC2SXmkEb9Tdq2RjhSeKmeerq": "4tuFb14LGanZK5V4gMgCWRXGBwsuSGi1AcaH5DYmMbhg",
112
175
  // AUTO X1 pool (ex-USDC X4) (ELE7, 12% APR, srAUTO) - separate faucet for withdrawals (created 2026-10-28)
113
176
  "5eEDZf3hzz39xiNBnUdbK8mpmrcKNieQSNVAsFPMpJbg": "CcaiuFU1oKeLXuks6zDggLHaBwLGUsQRywWqbTZiCP6B",