@elmntl/vaults-v2 1.0.11 → 1.2.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.
@@ -1,5 +1,5 @@
1
1
  {
2
- "address": "ELE6rYCZUaegWxVhWM4ef9pXZBAgGUVaVKkTbDvX6BMU",
2
+ "address": "BZBw16QMsX6V2NvYw5Sj7oSkJ3stp4U4X9aC7M9g1XCs",
3
3
  "metadata": {
4
4
  "name": "elemental_vault_v2",
5
5
  "version": "2.0.0",
@@ -106,6 +106,61 @@
106
106
  }
107
107
  ]
108
108
  },
109
+ {
110
+ "name": "create_ticket",
111
+ "docs": [
112
+ "Create a withdrawal ticket for a partial or full amount.",
113
+ "Position stays active with remaining balance.",
114
+ "amount=0 creates a yield-only ticket (rewards without capital)."
115
+ ],
116
+ "discriminator": [
117
+ 16,
118
+ 178,
119
+ 122,
120
+ 25,
121
+ 213,
122
+ 85,
123
+ 96,
124
+ 129
125
+ ],
126
+ "accounts": [
127
+ {
128
+ "name": "payer",
129
+ "writable": true,
130
+ "signer": true
131
+ },
132
+ {
133
+ "name": "position_owner",
134
+ "accounts": [
135
+ {
136
+ "name": "owner",
137
+ "signer": true
138
+ },
139
+ {
140
+ "name": "position",
141
+ "writable": true
142
+ },
143
+ {
144
+ "name": "pool",
145
+ "writable": true
146
+ }
147
+ ]
148
+ },
149
+ {
150
+ "name": "ticket",
151
+ "writable": true
152
+ },
153
+ {
154
+ "name": "system_program"
155
+ }
156
+ ],
157
+ "args": [
158
+ {
159
+ "name": "amount",
160
+ "type": "u64"
161
+ }
162
+ ]
163
+ },
109
164
  {
110
165
  "name": "deactivate",
111
166
  "docs": [
@@ -516,6 +571,71 @@
516
571
  }
517
572
  ]
518
573
  },
574
+ {
575
+ "name": "redeem_ticket",
576
+ "docs": [
577
+ "Redeem a matured withdrawal ticket (after 2-epoch cooldown).",
578
+ "Closes the ticket account and transfers vault_mint to user."
579
+ ],
580
+ "discriminator": [
581
+ 129,
582
+ 120,
583
+ 104,
584
+ 224,
585
+ 155,
586
+ 25,
587
+ 22,
588
+ 192
589
+ ],
590
+ "accounts": [
591
+ {
592
+ "name": "owner",
593
+ "signer": true
594
+ },
595
+ {
596
+ "name": "ticket",
597
+ "writable": true
598
+ },
599
+ {
600
+ "name": "position",
601
+ "writable": true
602
+ },
603
+ {
604
+ "name": "pool",
605
+ "writable": true
606
+ },
607
+ {
608
+ "name": "pool_authority"
609
+ },
610
+ {
611
+ "name": "liquidity_faucet",
612
+ "docs": [
613
+ "Faucet holding vault_mint for withdrawals (funded by admin)"
614
+ ],
615
+ "writable": true
616
+ },
617
+ {
618
+ "name": "user_vault_token_account",
619
+ "docs": [
620
+ "User's vault_mint token account (receives withdrawal)"
621
+ ],
622
+ "writable": true
623
+ },
624
+ {
625
+ "name": "vault_mint",
626
+ "docs": [
627
+ "The vault mint (what user receives)"
628
+ ]
629
+ },
630
+ {
631
+ "name": "token_program",
632
+ "docs": [
633
+ "Token program"
634
+ ]
635
+ }
636
+ ],
637
+ "args": []
638
+ },
519
639
  {
520
640
  "name": "transfer_to_faucet",
521
641
  "docs": [
@@ -597,11 +717,15 @@
597
717
  "accounts": [
598
718
  {
599
719
  "name": "admin",
720
+ "writable": true,
600
721
  "signer": true
601
722
  },
602
723
  {
603
724
  "name": "pool",
604
725
  "writable": true
726
+ },
727
+ {
728
+ "name": "system_program"
605
729
  }
606
730
  ],
607
731
  "args": [
@@ -766,6 +890,19 @@
766
890
  247,
767
891
  208
768
892
  ]
893
+ },
894
+ {
895
+ "name": "WithdrawalTicket",
896
+ "discriminator": [
897
+ 92,
898
+ 140,
899
+ 181,
900
+ 69,
901
+ 244,
902
+ 220,
903
+ 233,
904
+ 156
905
+ ]
769
906
  }
770
907
  ],
771
908
  "events": [
@@ -968,6 +1105,16 @@
968
1105
  "code": 6023,
969
1106
  "name": "NoFundsToFlush",
970
1107
  "msg": "No funds available to flush"
1108
+ },
1109
+ {
1110
+ "code": 6024,
1111
+ "name": "TicketModeActive",
1112
+ "msg": "Ticket mode is active - use create_ticket/redeem_ticket"
1113
+ },
1114
+ {
1115
+ "code": 6025,
1116
+ "name": "LegacyDeactivationPending",
1117
+ "msg": "Legacy deactivation pending - complete withdraw before using tickets"
971
1118
  }
972
1119
  ],
973
1120
  "types": [
@@ -1431,13 +1578,18 @@
1431
1578
  "type": "u8"
1432
1579
  },
1433
1580
  {
1434
- "name": "_padding",
1435
- "type": {
1436
- "array": [
1437
- "u8",
1438
- 3
1439
- ]
1440
- }
1581
+ "name": "ticket_mode",
1582
+ "docs": [
1583
+ "Whether this position uses ticket-based withdrawals (1=yes, 0=legacy)"
1584
+ ],
1585
+ "type": "u8"
1586
+ },
1587
+ {
1588
+ "name": "ticket_count",
1589
+ "docs": [
1590
+ "Auto-incrementing counter for ticket PDA derivation"
1591
+ ],
1592
+ "type": "u16"
1441
1593
  },
1442
1594
  {
1443
1595
  "name": "_reserved",
@@ -1577,6 +1729,57 @@
1577
1729
  }
1578
1730
  ]
1579
1731
  }
1732
+ },
1733
+ {
1734
+ "name": "WithdrawalTicket",
1735
+ "type": {
1736
+ "kind": "struct",
1737
+ "fields": [
1738
+ {
1739
+ "name": "pool",
1740
+ "type": "pubkey"
1741
+ },
1742
+ {
1743
+ "name": "owner",
1744
+ "type": "pubkey"
1745
+ },
1746
+ {
1747
+ "name": "amount",
1748
+ "docs": [
1749
+ "Principal amount being withdrawn"
1750
+ ],
1751
+ "type": "u64"
1752
+ },
1753
+ {
1754
+ "name": "reward_amount",
1755
+ "docs": [
1756
+ "Reward amount claimed at ticket creation"
1757
+ ],
1758
+ "type": "u64"
1759
+ },
1760
+ {
1761
+ "name": "created_epoch_index",
1762
+ "docs": [
1763
+ "Epoch index when ticket was created (must wait 2 epochs)"
1764
+ ],
1765
+ "type": "u32"
1766
+ },
1767
+ {
1768
+ "name": "ticket_index",
1769
+ "docs": [
1770
+ "Ticket index for this owner+pool (auto-incremented from position.ticket_count)"
1771
+ ],
1772
+ "type": "u16"
1773
+ },
1774
+ {
1775
+ "name": "bump",
1776
+ "docs": [
1777
+ "Bump seed for PDA"
1778
+ ],
1779
+ "type": "u8"
1780
+ }
1781
+ ]
1782
+ }
1580
1783
  }
1581
1784
  ]
1582
1785
  }
package/src/index.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  // Main exports
2
2
  export { ElementalV2Context, VAULT_PROGRAM_ID } from "./programs";
3
- export { Pool, Position } from "./accounts";
3
+ export { Pool, Position, WithdrawalTicket } from "./accounts";
4
4
  export {
5
5
  PoolData,
6
6
  PositionData,
7
+ WithdrawalTicketData,
7
8
  PositionStatus,
8
9
  SwapAdapter,
9
10
  FIXED_ONE,
@@ -21,10 +21,11 @@ import {
21
21
  getAccount,
22
22
  TokenAccountNotFoundError,
23
23
  } from "@solana/spl-token";
24
- import { Pool, Position } from "../accounts";
24
+ import { Pool, Position, WithdrawalTicket } from "../accounts";
25
25
  import {
26
26
  PoolData,
27
27
  PositionData,
28
+ WithdrawalTicketData,
28
29
  SwapAdapter,
29
30
  FIXED_ONE,
30
31
  ONRE_PROGRAM_ID,
@@ -41,9 +42,14 @@ export class ElementalV2Context {
41
42
  public readonly program: Program;
42
43
  private listeners: number[] = [];
43
44
 
44
- constructor(provider: AnchorProvider) {
45
+ constructor(provider: AnchorProvider, programId?: PublicKey) {
45
46
  this.provider = provider;
46
- this.program = new Program(idl as Idl, provider);
47
+ if (programId) {
48
+ const customIdl = { ...idl, address: programId.toBase58() };
49
+ this.program = new Program(customIdl as Idl, provider);
50
+ } else {
51
+ this.program = new Program(idl as Idl, provider);
52
+ }
47
53
  }
48
54
 
49
55
  get connection(): Connection {
@@ -61,7 +67,7 @@ export class ElementalV2Context {
61
67
  findPoolAuthority(poolAddress: PublicKey): PublicKey {
62
68
  return PublicKey.findProgramAddressSync(
63
69
  [Buffer.from("pool_authority"), poolAddress.toBuffer()],
64
- VAULT_PROGRAM_ID,
70
+ this.program.programId,
65
71
  )[0];
66
72
  }
67
73
 
@@ -75,10 +81,23 @@ export class ElementalV2Context {
75
81
  (ownerAddress || this.walletAddress).toBuffer(),
76
82
  poolAddress.toBuffer(),
77
83
  ],
78
- VAULT_PROGRAM_ID,
84
+ this.program.programId,
79
85
  )[0];
80
86
  }
81
87
 
88
+ findTicketAddress(
89
+ poolAddress: PublicKey,
90
+ ticketIndex: number,
91
+ ownerAddress?: PublicKey,
92
+ ): PublicKey {
93
+ return WithdrawalTicket.findAddress(
94
+ ownerAddress || this.walletAddress,
95
+ poolAddress,
96
+ ticketIndex,
97
+ this.program.programId,
98
+ );
99
+ }
100
+
82
101
  // ============================================================================
83
102
  // POOL OPERATIONS
84
103
  // ============================================================================
@@ -98,7 +117,7 @@ export class ElementalV2Context {
98
117
  const vaultDecimals =
99
118
  (vaultMintInfo.value?.data as any)?.parsed?.info?.decimals ?? 9;
100
119
 
101
- return new Pool(address, poolData, depositDecimals, vaultDecimals);
120
+ return new Pool(address, poolData, depositDecimals, vaultDecimals, this.program.programId);
102
121
  }
103
122
 
104
123
  async loadPools(addresses: PublicKey[]): Promise<Pool[]> {
@@ -121,10 +140,10 @@ export class ElementalV2Context {
121
140
  const vaultDecimals =
122
141
  (vaultMintInfo.value?.data as any)?.parsed?.info?.decimals ?? 9;
123
142
  pools.push(
124
- new Pool(addresses[i], data, depositDecimals, vaultDecimals),
143
+ new Pool(addresses[i], data, depositDecimals, vaultDecimals, this.program.programId),
125
144
  );
126
145
  } else {
127
- pools.push(new Pool(addresses[i], undefined));
146
+ pools.push(new Pool(addresses[i], undefined, undefined, undefined, this.program.programId));
128
147
  }
129
148
  }
130
149
  return pools;
@@ -193,9 +212,57 @@ export class ElementalV2Context {
193
212
  claimingAmount: new BN(0),
194
213
  lastUpdatedEpochIndex: -1,
195
214
  bump: 0,
215
+ ticketMode: 0,
216
+ ticketCount: 0,
196
217
  };
197
218
  }
198
219
 
220
+ // ============================================================================
221
+ // TICKET OPERATIONS
222
+ // ============================================================================
223
+
224
+ /** Load all withdrawal tickets for an owner on a pool. */
225
+ async loadTickets(
226
+ pool: Pool,
227
+ ownerAddress?: PublicKey,
228
+ ): Promise<WithdrawalTicket[]> {
229
+ const owner = ownerAddress || this.walletAddress;
230
+ const accounts = await (this.program.account as any).withdrawalTicket.all([
231
+ { memcmp: { offset: 8, bytes: pool.address.toBase58() } },
232
+ { memcmp: { offset: 40, bytes: owner.toBase58() } },
233
+ ]);
234
+ return accounts.map(
235
+ (acc: any) =>
236
+ new WithdrawalTicket(
237
+ pool,
238
+ acc.publicKey,
239
+ acc.account as WithdrawalTicketData,
240
+ ),
241
+ );
242
+ }
243
+
244
+ /** Load a specific ticket by index. */
245
+ async loadTicket(
246
+ pool: Pool,
247
+ ticketIndex: number,
248
+ ownerAddress?: PublicKey,
249
+ ): Promise<WithdrawalTicket | null> {
250
+ const address = this.findTicketAddress(
251
+ pool.address,
252
+ ticketIndex,
253
+ ownerAddress,
254
+ );
255
+ const data = await (
256
+ this.program.account as any
257
+ ).withdrawalTicket.fetchNullable(address);
258
+ if (!data) return null;
259
+ return new WithdrawalTicket(
260
+ pool,
261
+ address,
262
+ data as WithdrawalTicketData,
263
+ );
264
+ }
265
+
199
266
  // ============================================================================
200
267
  // INSTRUCTION BUILDERS
201
268
  // ============================================================================
@@ -635,6 +702,93 @@ export class ElementalV2Context {
635
702
  return [ix];
636
703
  }
637
704
 
705
+ // ============================================================================
706
+ // TICKET INSTRUCTION BUILDERS
707
+ // ============================================================================
708
+
709
+ /**
710
+ * Create a withdrawal ticket.
711
+ * - amount > 0: withdraw capital (rounded to min_deposit_amount multiples) + proportional rewards
712
+ * - amount = 0: yield-only ticket (withdraw all accrued rewards, capital untouched)
713
+ *
714
+ * Ticket index is auto-assigned from position.ticketCount.
715
+ */
716
+ async createTicketInstructions(
717
+ position: Position,
718
+ amount: BN,
719
+ ): Promise<TransactionInstruction[]> {
720
+ const ticketAddress = this.findTicketAddress(
721
+ position.pool.address,
722
+ position.ticketCount,
723
+ );
724
+
725
+ const ix = await (this.program.methods as any)
726
+ .createTicket(amount)
727
+ .accounts({
728
+ payer: this.walletAddress,
729
+ positionOwner: {
730
+ owner: this.walletAddress,
731
+ position: position.address,
732
+ pool: position.pool.address,
733
+ },
734
+ ticket: ticketAddress,
735
+ systemProgram: SystemProgram.programId,
736
+ })
737
+ .instruction();
738
+
739
+ return [ix];
740
+ }
741
+
742
+ async redeemTicketInstructions(
743
+ ticket: WithdrawalTicket,
744
+ position: Position,
745
+ ): Promise<TransactionInstruction[]> {
746
+ const instructions: TransactionInstruction[] = [];
747
+ const pool = position.pool;
748
+
749
+ const userVaultTokenAccount = getAssociatedTokenAddressSync(
750
+ pool.vaultMintAddress,
751
+ this.walletAddress,
752
+ true,
753
+ );
754
+
755
+ // Create ATA if needed
756
+ try {
757
+ await getAccount(this.connection, userVaultTokenAccount);
758
+ } catch (err) {
759
+ if (err instanceof TokenAccountNotFoundError) {
760
+ instructions.push(
761
+ createAssociatedTokenAccountInstruction(
762
+ this.walletAddress,
763
+ userVaultTokenAccount,
764
+ this.walletAddress,
765
+ pool.vaultMintAddress,
766
+ ),
767
+ );
768
+ } else {
769
+ throw err;
770
+ }
771
+ }
772
+
773
+ const ix = await (this.program.methods as any)
774
+ .redeemTicket()
775
+ .accounts({
776
+ owner: this.walletAddress,
777
+ ticket: ticket.address,
778
+ position: position.address,
779
+ pool: pool.address,
780
+ poolAuthority: pool.authorityAddress,
781
+ liquidityFaucet: pool.liquidityFaucetAddress,
782
+ userVaultTokenAccount,
783
+ vaultMint: pool.vaultMintAddress,
784
+ tokenProgram: TOKEN_PROGRAM_ID,
785
+ })
786
+ .instruction();
787
+
788
+ instructions.push(ix);
789
+ return instructions;
790
+ }
791
+
638
792
  async updateEpochInstruction(pool: Pool): Promise<TransactionInstruction> {
639
793
  return await (this.program.methods as any)
640
794
  .updateEpoch()
@@ -748,6 +902,22 @@ export class ElementalV2Context {
748
902
  return await this.sendTransaction(instructions);
749
903
  }
750
904
 
905
+ async createTicket(
906
+ position: Position,
907
+ amount: BN,
908
+ ): Promise<TransactionSignature> {
909
+ const instructions = await this.createTicketInstructions(position, amount);
910
+ return await this.sendTransaction(instructions);
911
+ }
912
+
913
+ async redeemTicket(
914
+ ticket: WithdrawalTicket,
915
+ position: Position,
916
+ ): Promise<TransactionSignature> {
917
+ const instructions = await this.redeemTicketInstructions(ticket, position);
918
+ return await this.sendTransaction(instructions);
919
+ }
920
+
751
921
  async fundFaucet(
752
922
  pool: Pool,
753
923
  amount: number | string,
package/src/types.ts CHANGED
@@ -54,6 +54,19 @@ export interface PositionData {
54
54
  claimingAmount: BN;
55
55
  lastUpdatedEpochIndex: number;
56
56
  bump: number;
57
+ ticketMode: number;
58
+ ticketCount: number;
59
+ }
60
+
61
+ // Raw withdrawal ticket data from on-chain account
62
+ export interface WithdrawalTicketData {
63
+ pool: PublicKey;
64
+ owner: PublicKey;
65
+ amount: BN;
66
+ rewardAmount: BN;
67
+ createdEpochIndex: number;
68
+ ticketIndex: number;
69
+ bump: number;
57
70
  }
58
71
 
59
72
  // Position status