@jpool/bond-sdk 0.2.0 → 0.3.0-next.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.mjs CHANGED
@@ -1,42 +1,46 @@
1
- import { AnchorProvider, Program, BN } from '@coral-xyz/anchor';
2
- import { Connection, PublicKey, SystemProgram, LAMPORTS_PER_SOL, SYSVAR_EPOCH_SCHEDULE_PUBKEY, Transaction, Keypair } from '@solana/web3.js';
1
+ import { Program, AnchorProvider, BN } from '@coral-xyz/anchor';
2
+ import { PublicKey, LAMPORTS_PER_SOL, Transaction } from '@solana/web3.js';
3
3
  import bs58 from 'bs58';
4
- import { readFileSync } from 'fs';
5
4
 
6
5
  // src/client.ts
7
6
 
8
- // src/helpers.ts
9
- var SLOTS_PER_EPOCH_MAINNET = 432e3;
10
- var SLOTS_PER_EPOCH_DEVNET = 4e5;
11
- function slotToEpoch(slot, cluster) {
12
- const slotsPerEpoch = cluster === "mainnet-beta" ? SLOTS_PER_EPOCH_MAINNET : SLOTS_PER_EPOCH_DEVNET;
13
- return Math.floor(slot / slotsPerEpoch);
14
- }
7
+ // src/constants.ts
8
+ var ENV_PROGRAM_ID = {
9
+ // [BondClientEnv.DEV]: new PublicKey('...'),
10
+ };
15
11
 
16
12
  // src/idl/jbond.json
17
13
  var jbond_default = {
18
- address: "8vrqsFHnDdjMYMwahytRzmmYgRqCMmka8X6DJUXGQzWr",
14
+ address: "FxKkNnBhadZQuZF7B7Fmh915NenoFyCzwPyZmoVoRdLC",
19
15
  metadata: {
20
16
  name: "jbond",
21
17
  version: "0.1.0",
22
18
  spec: "0.1.0",
23
- description: "Validator compensation program for boosting APR"
19
+ description: "Jpool Bond Program"
24
20
  },
25
21
  instructions: [
26
22
  {
27
- name: "bond_init",
23
+ name: "claim",
28
24
  docs: [
29
- "Register validator and fund initial collateral"
25
+ "Claims compensation for a validator.",
26
+ "Transfers funds from the validator's bond account to reserve.",
27
+ "",
28
+ "# Arguments",
29
+ "* `amount` - Amount of compensation to claim (must be positive).",
30
+ "",
31
+ "# Errors",
32
+ "Fails if the authority is invalid, if the validator is not active,",
33
+ "if the amount is zero, or if there are insufficient funds."
30
34
  ],
31
35
  discriminator: [
32
- 220,
36
+ 62,
37
+ 198,
38
+ 214,
39
+ 193,
40
+ 213,
41
+ 159,
33
42
  108,
34
- 156,
35
- 81,
36
- 16,
37
- 185,
38
- 144,
39
- 157
43
+ 210
40
44
  ],
41
45
  accounts: [
42
46
  {
@@ -65,7 +69,7 @@ var jbond_default = {
65
69
  }
66
70
  },
67
71
  {
68
- name: "validator_bond_account",
72
+ name: "validator_bond",
69
73
  writable: true,
70
74
  pda: {
71
75
  seeds: [
@@ -90,23 +94,21 @@ var jbond_default = {
90
94
  },
91
95
  {
92
96
  kind: "account",
93
- path: "vote_account"
97
+ path: "validator_bond.vote_account",
98
+ account: "ValidatorBond"
94
99
  }
95
100
  ]
96
101
  }
97
102
  },
98
103
  {
99
- name: "identity"
100
- },
101
- {
102
- name: "vote_account"
104
+ name: "reserve",
105
+ writable: true
103
106
  },
104
107
  {
105
- name: "creator",
108
+ name: "authority",
106
109
  docs: [
107
- "The account creating the bond (pays for account creation and initial collateral)"
110
+ "Authority that can trigger claims"
108
111
  ],
109
- writable: true,
110
112
  signer: true
111
113
  },
112
114
  {
@@ -116,97 +118,124 @@ var jbond_default = {
116
118
  ],
117
119
  args: [
118
120
  {
119
- name: "initial_collateral",
121
+ name: "amount",
120
122
  type: "u64"
121
- },
122
- {
123
- name: "withdrawal_authority",
124
- type: {
125
- option: "pubkey"
126
- }
127
123
  }
128
124
  ]
129
125
  },
130
126
  {
131
- name: "bond_top_up",
127
+ name: "initialize",
132
128
  docs: [
133
- "Top up collateral for existing validator"
129
+ "Initializes the global state for the bonding program.",
130
+ "Sets the authority and reserve account for managing validator bonds.",
131
+ "",
132
+ "# Errors",
133
+ "Fails if the global state account is already initialized or if the payer lacks",
134
+ "sufficient funds."
134
135
  ],
135
136
  discriminator: [
136
- 132,
137
- 225,
138
- 254,
139
- 187,
137
+ 175,
138
+ 175,
139
+ 109,
140
+ 31,
141
+ 13,
140
142
  152,
141
- 162,
142
- 176,
143
- 66
143
+ 155,
144
+ 237
144
145
  ],
145
146
  accounts: [
146
147
  {
147
- name: "validator_bond_account",
148
+ name: "global_state",
148
149
  writable: true,
149
150
  pda: {
150
151
  seeds: [
151
152
  {
152
153
  kind: "const",
153
154
  value: [
154
- 118,
155
+ 103,
156
+ 108,
157
+ 111,
158
+ 98,
155
159
  97,
156
160
  108,
157
- 105,
158
- 100,
161
+ 95,
162
+ 115,
163
+ 116,
159
164
  97,
160
165
  116,
161
- 111,
162
- 114,
163
- 95,
164
- 98,
165
- 111,
166
- 110,
167
- 100
166
+ 101
168
167
  ]
169
- },
170
- {
171
- kind: "account",
172
- path: "validator_bond_account.vote_account",
173
- account: "ValidatorBondAccount"
174
168
  }
175
169
  ]
176
170
  }
177
171
  },
178
172
  {
179
- name: "depositor",
173
+ name: "authority",
180
174
  writable: true,
181
175
  signer: true
182
176
  },
177
+ {
178
+ name: "reserve"
179
+ },
183
180
  {
184
181
  name: "system_program",
185
182
  address: "11111111111111111111111111111111"
186
183
  }
187
184
  ],
188
- args: [
189
- {
190
- name: "amount",
191
- type: "u64"
192
- }
193
- ]
185
+ args: []
194
186
  },
195
187
  {
196
- name: "bond_withdraw",
188
+ name: "register",
189
+ docs: [
190
+ "Registers a new validator with the bonding program.",
191
+ "Sets up the validator's bond account and deposits initial collateral.",
192
+ "",
193
+ "# Arguments",
194
+ "* `initial_collateral` - Amount of collateral to lock (must be positive).",
195
+ "* `withdrawal_authority` - Optional key that can withdraw collateral later.",
196
+ "",
197
+ "# Errors",
198
+ "Fails if the validator is already registered, if the vote account is invalid,",
199
+ "or if the initial collateral transfer fails."
200
+ ],
197
201
  discriminator: [
198
- 99,
199
- 64,
200
- 127,
202
+ 211,
203
+ 124,
204
+ 67,
205
+ 15,
206
+ 211,
207
+ 194,
201
208
  178,
202
- 53,
203
- 117,
204
- 70,
205
- 204
209
+ 240
206
210
  ],
207
211
  accounts: [
208
212
  {
209
- name: "validator_bond_account",
213
+ name: "global_state",
214
+ writable: true,
215
+ pda: {
216
+ seeds: [
217
+ {
218
+ kind: "const",
219
+ value: [
220
+ 103,
221
+ 108,
222
+ 111,
223
+ 98,
224
+ 97,
225
+ 108,
226
+ 95,
227
+ 115,
228
+ 116,
229
+ 97,
230
+ 116,
231
+ 101
232
+ ]
233
+ }
234
+ ]
235
+ }
236
+ },
237
+ {
238
+ name: "validator_bond",
210
239
  writable: true,
211
240
  pda: {
212
241
  seeds: [
@@ -231,24 +260,24 @@ var jbond_default = {
231
260
  },
232
261
  {
233
262
  kind: "account",
234
- path: "validator_bond_account.vote_account",
235
- account: "ValidatorBondAccount"
263
+ path: "vote_account"
236
264
  }
237
265
  ]
238
266
  }
239
267
  },
240
268
  {
241
- name: "withdrawal_authority",
242
- writable: true,
243
- signer: true
269
+ name: "identity"
244
270
  },
245
271
  {
246
- name: "destination",
247
- writable: true
272
+ name: "vote_account"
248
273
  },
249
274
  {
250
- name: "epoch_schedule",
251
- address: "SysvarEpochSchedu1e111111111111111111111111"
275
+ name: "creator",
276
+ docs: [
277
+ "The account creating the bond (pays for account creation and initial collateral)"
278
+ ],
279
+ writable: true,
280
+ signer: true
252
281
  },
253
282
  {
254
283
  name: "system_program",
@@ -257,71 +286,28 @@ var jbond_default = {
257
286
  ],
258
287
  args: [
259
288
  {
260
- name: "amount",
289
+ name: "initial_collateral",
261
290
  type: "u64"
262
- }
263
- ]
264
- },
265
- {
266
- name: "initialize",
267
- docs: [
268
- "Initialize the global state and reserve vault"
269
- ],
270
- discriminator: [
271
- 175,
272
- 175,
273
- 109,
274
- 31,
275
- 13,
276
- 152,
277
- 155,
278
- 237
279
- ],
280
- accounts: [
281
- {
282
- name: "global_state",
283
- writable: true,
284
- pda: {
285
- seeds: [
286
- {
287
- kind: "const",
288
- value: [
289
- 103,
290
- 108,
291
- 111,
292
- 98,
293
- 97,
294
- 108,
295
- 95,
296
- 115,
297
- 116,
298
- 97,
299
- 116,
300
- 101
301
- ]
302
- }
303
- ]
304
- }
305
- },
306
- {
307
- name: "authority",
308
- writable: true,
309
- signer: true
310
- },
311
- {
312
- name: "reserve"
313
291
  },
314
292
  {
315
- name: "system_program",
316
- address: "11111111111111111111111111111111"
293
+ name: "withdrawal_authority",
294
+ type: {
295
+ option: "pubkey"
296
+ }
317
297
  }
318
- ],
319
- args: []
298
+ ]
320
299
  },
321
300
  {
322
301
  name: "set_authority",
323
302
  docs: [
324
- "Change the bond authority"
303
+ "Sets a new withdrawal authority for a validator's bond account.",
304
+ "Allows changing who can withdraw collateral in the future.",
305
+ "",
306
+ "# Arguments",
307
+ "* `new_authority` - Optional new authority key (None to restrict to identity).",
308
+ "",
309
+ "# Errors",
310
+ "Fails if the signer is not authorized to change the authority."
325
311
  ],
326
312
  discriminator: [
327
313
  133,
@@ -373,48 +359,105 @@ var jbond_default = {
373
359
  args: []
374
360
  },
375
361
  {
376
- name: "withdraw_compensation",
362
+ name: "top_up",
377
363
  docs: [
378
- "Withdraw compensation from validator to reserve (oracle only)"
364
+ "Adds additional collateral to an existing validator's bond.",
365
+ "Increases the collateral balance, enhancing the validator's stake.",
366
+ "",
367
+ "# Arguments",
368
+ "* `amount` - Amount to add.",
369
+ "",
370
+ "# Errors",
371
+ "Fails if the validator is not active, if the amount is zero,",
372
+ "or if the transfer of funds fails."
379
373
  ],
380
374
  discriminator: [
381
- 10,
382
- 228,
383
- 22,
384
- 213,
385
- 205,
386
- 117,
387
- 181,
388
- 75
375
+ 236,
376
+ 225,
377
+ 96,
378
+ 9,
379
+ 60,
380
+ 106,
381
+ 77,
382
+ 208
389
383
  ],
390
384
  accounts: [
391
385
  {
392
- name: "global_state",
386
+ name: "validator_bond",
393
387
  writable: true,
394
388
  pda: {
395
389
  seeds: [
396
390
  {
397
391
  kind: "const",
398
392
  value: [
399
- 103,
400
- 108,
401
- 111,
402
- 98,
393
+ 118,
403
394
  97,
404
395
  108,
405
- 95,
406
- 115,
407
- 116,
396
+ 105,
397
+ 100,
408
398
  97,
409
399
  116,
410
- 101
400
+ 111,
401
+ 114,
402
+ 95,
403
+ 98,
404
+ 111,
405
+ 110,
406
+ 100
411
407
  ]
408
+ },
409
+ {
410
+ kind: "account",
411
+ path: "validator_bond.vote_account",
412
+ account: "ValidatorBond"
412
413
  }
413
414
  ]
414
415
  }
415
416
  },
416
417
  {
417
- name: "validator_bond_account",
418
+ name: "payer",
419
+ writable: true,
420
+ signer: true
421
+ },
422
+ {
423
+ name: "system_program",
424
+ address: "11111111111111111111111111111111"
425
+ }
426
+ ],
427
+ args: [
428
+ {
429
+ name: "amount",
430
+ type: "u64"
431
+ }
432
+ ]
433
+ },
434
+ {
435
+ name: "withdraw",
436
+ docs: [
437
+ "Withdraws collateral from a validator's bond.",
438
+ "Decreases the collateral balance, allowing the validator to reclaim funds.",
439
+ "",
440
+ "# Arguments",
441
+ "* `amount` - Amount to withdraw (must not exceed available balance).",
442
+ "",
443
+ "# Errors",
444
+ "Fails if the validator is not active, if the amount is zero,",
445
+ "if the withdrawal authority is invalid, or if the withdrawal would",
446
+ "leave insufficient collateral."
447
+ ],
448
+ discriminator: [
449
+ 183,
450
+ 18,
451
+ 70,
452
+ 156,
453
+ 148,
454
+ 109,
455
+ 161,
456
+ 34
457
+ ],
458
+ accounts: [
459
+ {
460
+ name: "validator_bond",
418
461
  writable: true,
419
462
  pda: {
420
463
  seeds: [
@@ -439,22 +482,20 @@ var jbond_default = {
439
482
  },
440
483
  {
441
484
  kind: "account",
442
- path: "validator_bond_account.vote_account",
443
- account: "ValidatorBondAccount"
485
+ path: "validator_bond.vote_account",
486
+ account: "ValidatorBond"
444
487
  }
445
488
  ]
446
489
  }
447
490
  },
448
491
  {
449
- name: "reserve",
450
- writable: true
492
+ name: "withdrawal_authority",
493
+ writable: true,
494
+ signer: true
451
495
  },
452
496
  {
453
- name: "authority",
454
- docs: [
455
- "Authority that can trigger claims"
456
- ],
457
- signer: true
497
+ name: "destination",
498
+ writable: true
458
499
  },
459
500
  {
460
501
  name: "system_program",
@@ -484,16 +525,16 @@ var jbond_default = {
484
525
  ]
485
526
  },
486
527
  {
487
- name: "ValidatorBondAccount",
528
+ name: "ValidatorBond",
488
529
  discriminator: [
489
- 25,
490
- 67,
491
- 241,
492
- 227,
493
- 226,
494
- 104,
495
- 108,
496
- 73
530
+ 82,
531
+ 127,
532
+ 243,
533
+ 208,
534
+ 195,
535
+ 42,
536
+ 80,
537
+ 35
497
538
  ]
498
539
  }
499
540
  ],
@@ -618,7 +659,7 @@ var jbond_default = {
618
659
  type: "u64"
619
660
  },
620
661
  {
621
- name: "new_total",
662
+ name: "post_balance",
622
663
  type: "u64"
623
664
  },
624
665
  {
@@ -642,7 +683,7 @@ var jbond_default = {
642
683
  type: "u64"
643
684
  },
644
685
  {
645
- name: "remaining_collateral",
686
+ name: "post_balance",
646
687
  type: "u64"
647
688
  },
648
689
  {
@@ -666,11 +707,7 @@ var jbond_default = {
666
707
  type: "u64"
667
708
  },
668
709
  {
669
- name: "remaining_collateral",
670
- type: "u64"
671
- },
672
- {
673
- name: "total_withdrawn",
710
+ name: "post_balance",
674
711
  type: "u64"
675
712
  },
676
713
  {
@@ -691,68 +728,117 @@ var jbond_default = {
691
728
  fields: [
692
729
  {
693
730
  name: "authority",
731
+ docs: [
732
+ "The authority allowed to manage the program"
733
+ ],
694
734
  type: "pubkey"
695
735
  },
696
736
  {
697
737
  name: "reserve",
738
+ docs: [
739
+ "The reserve vault holding SOL for bonding and compensation"
740
+ ],
698
741
  type: "pubkey"
699
742
  },
700
743
  {
701
744
  name: "total_validators",
702
- type: "u32"
745
+ docs: [
746
+ "Total number of registered validators"
747
+ ],
748
+ type: "u16"
703
749
  },
704
750
  {
705
- name: "total_withdrawn",
751
+ name: "total_compensation_amount",
752
+ docs: [
753
+ "Total compensation amount paid out"
754
+ ],
706
755
  type: "u64"
707
756
  },
708
757
  {
709
758
  name: "bump",
759
+ docs: [
760
+ "Bump for the PDA"
761
+ ],
710
762
  type: "u8"
711
763
  }
712
764
  ]
713
765
  }
714
766
  },
715
767
  {
716
- name: "ValidatorBondAccount",
768
+ name: "ValidatorBond",
717
769
  type: {
718
770
  kind: "struct",
719
771
  fields: [
720
772
  {
721
773
  name: "identity",
774
+ docs: [
775
+ "The validator identity pubkey"
776
+ ],
722
777
  type: "pubkey"
723
778
  },
724
779
  {
725
780
  name: "vote_account",
781
+ docs: [
782
+ "The validator vote account pubkey"
783
+ ],
726
784
  type: "pubkey"
727
785
  },
728
786
  {
729
787
  name: "creator",
788
+ docs: [
789
+ "Creator of the validator bond account"
790
+ ],
730
791
  type: "pubkey"
731
792
  },
732
793
  {
733
794
  name: "withdrawal_authority",
795
+ docs: [
796
+ "Authority allowed to withdraw funds (if None, only identity can withdraw)"
797
+ ],
734
798
  type: {
735
799
  option: "pubkey"
736
800
  }
737
801
  },
738
802
  {
739
- name: "total_withdrawn",
803
+ name: "total_compensation_amount",
804
+ docs: [
805
+ "Total compensation claimed for the validator"
806
+ ],
740
807
  type: "u64"
741
808
  },
742
809
  {
743
- name: "last_withdrawal_epoch",
810
+ name: "last_compensation_amount",
811
+ docs: [
812
+ "Last compensation amount claimed"
813
+ ],
744
814
  type: "u64"
745
815
  },
746
816
  {
747
- name: "is_active",
748
- type: "bool"
817
+ name: "last_compensation_epoch",
818
+ docs: [
819
+ "Last epoch when compensation was claimed"
820
+ ],
821
+ type: "u64"
749
822
  },
750
823
  {
751
824
  name: "created_at",
825
+ docs: [
826
+ "Timestamp when the bond account was created"
827
+ ],
752
828
  type: "i64"
753
829
  },
830
+ {
831
+ name: "is_active",
832
+ docs: [
833
+ "Whether the validator is currently active"
834
+ ],
835
+ type: "bool"
836
+ },
754
837
  {
755
838
  name: "bump",
839
+ docs: [
840
+ "Bump for the PDA"
841
+ ],
756
842
  type: "u8"
757
843
  }
758
844
  ]
@@ -764,7 +850,7 @@ var jbond_default = {
764
850
  kind: "struct",
765
851
  fields: [
766
852
  {
767
- name: "validator",
853
+ name: "identity",
768
854
  type: "pubkey"
769
855
  },
770
856
  {
@@ -772,7 +858,7 @@ var jbond_default = {
772
858
  type: "pubkey"
773
859
  },
774
860
  {
775
- name: "collateral_amount",
861
+ name: "initial_collateral",
776
862
  type: "u64"
777
863
  },
778
864
  {
@@ -824,119 +910,176 @@ var NodeWallet = class {
824
910
  }
825
911
  };
826
912
 
913
+ // src/utils/index.ts
914
+ var SLOTS_PER_EPOCH_MAINNET = 432e3;
915
+ var SLOTS_PER_EPOCH_DEVNET = 4e5;
916
+ function slotToEpoch(slot, cluster) {
917
+ const slotsPerEpoch = cluster === "mainnet-beta" ? SLOTS_PER_EPOCH_MAINNET : SLOTS_PER_EPOCH_DEVNET;
918
+ return Math.floor(slot / slotsPerEpoch);
919
+ }
920
+
827
921
  // src/client.ts
922
+ var BondClientEnv = /* @__PURE__ */ ((BondClientEnv2) => {
923
+ BondClientEnv2["DEV"] = "dev";
924
+ BondClientEnv2["STAGE"] = "stage";
925
+ BondClientEnv2["PROD"] = "prod";
926
+ return BondClientEnv2;
927
+ })(BondClientEnv || {});
828
928
  var JBondClient = class _JBondClient {
829
- connection;
830
- program;
831
- provider;
832
- config;
833
- constructor(config, wallet) {
834
- this.config = config;
835
- this.connection = new Connection(config.rpcUrl, "confirmed");
836
- this.provider = new AnchorProvider(
837
- this.connection,
838
- // @ts-expect-error support anonymous
839
- wallet ?? { publicKey: PublicKey.default },
840
- AnchorProvider.defaultOptions()
841
- );
929
+ constructor(provider, options) {
930
+ this.provider = provider;
931
+ this.options = options ?? {};
842
932
  this.program = new Program(jbond_default, this.provider);
843
933
  }
934
+ options;
935
+ program;
844
936
  /**
845
937
  * Creates an instance of `JBondClient` using a provided connection and wallet.
846
938
  */
847
- static fromWallet(config, wallet) {
848
- return new this(config, wallet);
939
+ static fromWallet(connection, wallet, options) {
940
+ return new this(
941
+ new AnchorProvider(
942
+ connection,
943
+ // @ts-expect-error support anonymous
944
+ wallet ?? { publicKey: PublicKey.default },
945
+ AnchorProvider.defaultOptions()
946
+ ),
947
+ options
948
+ );
849
949
  }
850
950
  /**
851
951
  * Creates an instance of `JBondClient` using the provided connection and keypair.
852
952
  */
853
- static fromKeypair(config, keypair) {
854
- return _JBondClient.fromWallet(config, new NodeWallet(keypair));
953
+ static fromKeypair(connection, keypair, options) {
954
+ return _JBondClient.fromWallet(connection, new NodeWallet(keypair), options);
855
955
  }
856
- // Get PDA for global state
857
- getGlobalStatePDA() {
858
- return PublicKey.findProgramAddressSync(
859
- [Buffer.from("global_state")],
860
- this.program.programId
861
- );
956
+ /**
957
+ * Get the current program ID.
958
+ */
959
+ get programId() {
960
+ return this.options.programId ?? this.program.programId;
862
961
  }
863
- // Get PDA for validator bond account
864
- getValidatorBondPDA(voteAccount) {
865
- return PublicKey.findProgramAddressSync(
866
- [
867
- Buffer.from("validator_bond"),
868
- voteAccount.toBuffer()
869
- ],
870
- this.program.programId
871
- );
962
+ /**
963
+ * Get the current connection.
964
+ */
965
+ get connection() {
966
+ return this.provider.connection;
967
+ }
968
+ /**
969
+ * Set the environment.
970
+ */
971
+ env(env) {
972
+ return this.configure("programId", ENV_PROGRAM_ID[env]);
973
+ }
974
+ /**
975
+ * Configure a specific option.
976
+ */
977
+ configure(key, val) {
978
+ this.options[key] = val;
979
+ return this;
872
980
  }
873
- // Build initialize instruction
981
+ /**
982
+ * Program Derived Addresses (PDAs)
983
+ */
984
+ pda = {
985
+ globalState: () => {
986
+ return PublicKey.findProgramAddressSync(
987
+ [Buffer.from("global_state")],
988
+ this.programId
989
+ );
990
+ },
991
+ validatorBond: (voteAccount) => {
992
+ return PublicKey.findProgramAddressSync(
993
+ [
994
+ Buffer.from("validator_bond"),
995
+ voteAccount.toBuffer()
996
+ ],
997
+ this.programId
998
+ );
999
+ }
1000
+ };
1001
+ /**
1002
+ * Build initialize instruction
1003
+ * @param props
1004
+ */
874
1005
  async buildInitializeInstruction(props) {
875
- const [globalState] = this.getGlobalStatePDA();
876
- return this.program.methods.initialize().accountsStrict({
1006
+ const [globalState] = this.pda.globalState();
1007
+ const { authority, reserveAddress: reserve } = props;
1008
+ return this.program.methods.initialize().accountsPartial({
877
1009
  globalState,
878
- authority: props.authority,
879
- reserve: this.config.reserveAddress,
880
- systemProgram: SystemProgram.programId
1010
+ authority,
1011
+ reserve
881
1012
  }).instruction();
882
1013
  }
883
- // Build register validator instruction
1014
+ /**
1015
+ * Build register validator instruction
1016
+ * @param props
1017
+ */
884
1018
  async buildRegisterValidatorInstruction(props) {
885
1019
  const { creator, identity, voteAccount, initialCollateral, withdrawalAuthority } = props;
886
- const [validatorBondAccountAddress] = this.getValidatorBondPDA(voteAccount);
1020
+ const [validatorBondAccountAddress] = this.pda.validatorBond(voteAccount);
887
1021
  const accountInfo = await this.connection.getAccountInfo(validatorBondAccountAddress);
888
1022
  if (accountInfo) {
889
1023
  throw new Error("Validator bond account already exists");
890
1024
  }
891
- const [globalState] = this.getGlobalStatePDA();
1025
+ const [globalState] = this.pda.globalState();
892
1026
  const collateralLamports = new BN(initialCollateral * LAMPORTS_PER_SOL);
893
- return this.program.methods.bondInit(collateralLamports, withdrawalAuthority ?? null).accountsStrict({
1027
+ return this.program.methods.register(collateralLamports, withdrawalAuthority ?? null).accountsPartial({
894
1028
  creator,
895
1029
  globalState,
896
- validatorBondAccount: validatorBondAccountAddress,
1030
+ validatorBond: validatorBondAccountAddress,
897
1031
  identity,
898
- voteAccount,
899
- systemProgram: SystemProgram.programId
1032
+ voteAccount
900
1033
  }).instruction();
901
1034
  }
902
- // Build top up collateral instruction
1035
+ /**
1036
+ * Build top up collateral instruction
1037
+ * @param props
1038
+ */
903
1039
  buildTopUpCollateralInstruction(props) {
904
1040
  const { user, voteAccount, amount } = props;
905
- const [validatorBondAccountAddress] = this.getValidatorBondPDA(voteAccount);
1041
+ const [validatorBondAccountAddress] = this.pda.validatorBond(voteAccount);
906
1042
  const amountLamports = new BN(amount * LAMPORTS_PER_SOL);
907
- return this.program.methods.bondTopUp(amountLamports).accountsStrict({
908
- validatorBondAccount: validatorBondAccountAddress,
909
- depositor: user,
910
- systemProgram: SystemProgram.programId
1043
+ return this.program.methods.topUp(amountLamports).accountsPartial({
1044
+ validatorBond: validatorBondAccountAddress,
1045
+ payer: user
911
1046
  }).instruction();
912
1047
  }
913
1048
  async buildWithdrawCollateralInstruction(props) {
914
1049
  const { withdrawalAuthority: user, voteAccount, destination, amount } = props;
915
- const [validatorBondAccountAddress] = this.getValidatorBondPDA(voteAccount);
1050
+ const [validatorBondAccountAddress] = this.pda.validatorBond(voteAccount);
916
1051
  const amountLamports = new BN(amount * LAMPORTS_PER_SOL);
917
- return this.program.methods.bondWithdraw(amountLamports).accountsStrict({
918
- validatorBondAccount: validatorBondAccountAddress,
1052
+ return this.program.methods.withdraw(amountLamports).accountsPartial({
1053
+ validatorBond: validatorBondAccountAddress,
919
1054
  withdrawalAuthority: user,
920
- destination,
921
- epochSchedule: SYSVAR_EPOCH_SCHEDULE_PUBKEY,
922
- systemProgram: SystemProgram.programId
1055
+ destination
923
1056
  }).instruction();
924
1057
  }
925
- // Build withdraw compensation instruction
926
- buildWithdrawCompensationInstruction(props) {
1058
+ /**
1059
+ * Build withdraw compensation instruction
1060
+ * @param props
1061
+ */
1062
+ async buildWithdrawCompensationInstruction(props) {
927
1063
  const { authority, voteAccount, amount } = props;
928
- const [globalState] = this.getGlobalStatePDA();
929
- const [validatorBondAccountAddress] = this.getValidatorBondPDA(voteAccount);
1064
+ const [globalState] = this.pda.globalState();
1065
+ const [validatorBondAccountAddress] = this.pda.validatorBond(voteAccount);
930
1066
  const amountLamports = new BN(amount * LAMPORTS_PER_SOL);
931
- return this.program.methods.withdrawCompensation(amountLamports).accountsStrict({
1067
+ const reserveAddress = (await this.getGlobalState())?.reserveAddress;
1068
+ if (!reserveAddress) {
1069
+ throw new Error("Reserve address is not set in the global state. The program might not be initialized yet.");
1070
+ }
1071
+ return this.program.methods.claim(amountLamports).accountsPartial({
932
1072
  globalState,
933
- validatorBondAccount: validatorBondAccountAddress,
934
- reserve: this.config.reserveAddress,
935
- authority,
936
- systemProgram: SystemProgram.programId
1073
+ validatorBond: validatorBondAccountAddress,
1074
+ reserve: reserveAddress,
1075
+ authority
937
1076
  }).instruction();
938
1077
  }
939
- // Build multiple withdraw compensation instructions
1078
+ /**
1079
+ * Build multiple withdraw compensation instructions
1080
+ * @param authority
1081
+ * @param withdrawals
1082
+ */
940
1083
  buildWithdrawCompensationsInstructions(authority, withdrawals) {
941
1084
  return Promise.all(
942
1085
  withdrawals.map(
@@ -944,9 +1087,12 @@ var JBondClient = class _JBondClient {
944
1087
  )
945
1088
  );
946
1089
  }
947
- // Get validator bond account state
948
- async getValidatorBondAccount(voteAccount) {
949
- const [validatorBondAccountAddress] = this.getValidatorBondPDA(voteAccount);
1090
+ /**
1091
+ * Get validator bond account state
1092
+ * @param vote
1093
+ */
1094
+ async getValidatorBond(vote) {
1095
+ const [validatorBondAccountAddress] = this.pda.validatorBond(vote);
950
1096
  try {
951
1097
  const account = await this.program.account.validatorBondAccount.fetch(validatorBondAccountAddress);
952
1098
  return {
@@ -957,16 +1103,18 @@ var JBondClient = class _JBondClient {
957
1103
  lastWithdrawalEpoch: account.lastWithdrawalEpoch.toNumber(),
958
1104
  isActive: account.isActive,
959
1105
  createdAt: account.createdAt.toNumber() * 1e3,
960
- // TODO
1106
+ // Convert i64 Unix seconds (BN) to JS milliseconds
961
1107
  bump: account.bump
962
1108
  };
963
1109
  } catch {
964
1110
  return null;
965
1111
  }
966
1112
  }
967
- // Build set authority instruction
1113
+ /**
1114
+ * Build set authority instruction
1115
+ */
968
1116
  async buildSetAuthorityInstruction(props) {
969
- const [globalState] = this.getGlobalStatePDA();
1117
+ const [globalState] = this.pda.globalState();
970
1118
  return this.program.methods.setAuthority().accountsStrict({
971
1119
  globalState,
972
1120
  authority: props.authority,
@@ -975,11 +1123,11 @@ var JBondClient = class _JBondClient {
975
1123
  }
976
1124
  /**
977
1125
  * Get the collateral balance of a validator bond account
978
- * @param voteAccount - The vote account public key
1126
+ * @param vote - The vote account public key
979
1127
  * @returns The available collateral balance in SOL (excluding rent-exempt amount)
980
1128
  */
981
- async getValidatorCollateralBalance(voteAccount) {
982
- const [validatorBondAccountAddress] = this.getValidatorBondPDA(voteAccount);
1129
+ async getValidatorCollateralBalance(vote) {
1130
+ const [validatorBondAccountAddress] = this.pda.validatorBond(vote);
983
1131
  const accountInfo = await this.connection.getAccountInfo(validatorBondAccountAddress);
984
1132
  if (!accountInfo) {
985
1133
  return 0;
@@ -990,34 +1138,38 @@ var JBondClient = class _JBondClient {
990
1138
  const availableBalance = Math.max(0, accountInfo.lamports - rentExempt);
991
1139
  return availableBalance / LAMPORTS_PER_SOL;
992
1140
  }
993
- // Get global state state
1141
+ /**
1142
+ * Get global state
1143
+ */
994
1144
  async getGlobalState() {
995
- const [globalState] = this.getGlobalStatePDA();
996
- try {
997
- const account = await this.program.account.globalState.fetch(globalState);
998
- return {
999
- authority: account.authority.toString(),
1000
- totalValidators: account.totalValidators,
1001
- totalWithdrawn: account.totalWithdrawn.toNumber() / LAMPORTS_PER_SOL
1002
- };
1003
- } catch {
1004
- return null;
1005
- }
1145
+ const [globalState] = this.pda.globalState();
1146
+ const account = await this.program.account.globalState.fetch(globalState);
1147
+ return {
1148
+ authority: account.authority.toString(),
1149
+ totalValidators: account.totalValidators,
1150
+ totalCompensationAmount: account.totalCompensationAmount.toNumber() / LAMPORTS_PER_SOL,
1151
+ reserveAddress: account.reserve.toString()
1152
+ };
1006
1153
  }
1007
- // Get current epoch
1154
+ /**
1155
+ * Get current epoch
1156
+ */
1008
1157
  async getCurrentEpoch() {
1009
1158
  const epochInfo = await this.connection.getEpochInfo();
1010
1159
  return epochInfo.epoch;
1011
1160
  }
1012
- // Helper methods for backward compatibility (can be removed if not needed)
1013
- async initialize(authority) {
1014
- const authorityPubkey = authority || this.provider.wallet.publicKey;
1015
- const ix = await this.buildInitializeInstruction({ authority: authorityPubkey });
1016
- const tx = await this.provider.sendAndConfirm(
1161
+ /**
1162
+ * Helper methods for backward compatibility (can be removed if not needed)
1163
+ * @param props
1164
+ */
1165
+ async initialize(props) {
1166
+ const { reserveAddress } = props;
1167
+ const authority = props.authority ?? this.provider.wallet.publicKey;
1168
+ const ix = await this.buildInitializeInstruction({ reserveAddress, authority });
1169
+ return await this.provider.sendAndConfirm(
1017
1170
  new Transaction().add(ix),
1018
1171
  []
1019
1172
  );
1020
- return tx;
1021
1173
  }
1022
1174
  // Note: this method will be removed or changed in the future (CLI only)
1023
1175
  async registerValidator(voteAccount, initialCollateral, withdrawalAuthority, identity) {
@@ -1025,44 +1177,40 @@ var JBondClient = class _JBondClient {
1025
1177
  const ix = await this.buildRegisterValidatorInstruction(
1026
1178
  { creator: identityPubkey, identity: identityPubkey, voteAccount, initialCollateral, withdrawalAuthority }
1027
1179
  );
1028
- const tx = await this.provider.sendAndConfirm(
1180
+ return await this.provider.sendAndConfirm(
1029
1181
  new Transaction().add(ix),
1030
1182
  []
1031
1183
  );
1032
- return tx;
1033
1184
  }
1034
1185
  async topUpCollateral(voteAccount, amount, validator) {
1035
1186
  const userPubkey = validator ?? this.provider.wallet.publicKey;
1036
1187
  const ix = await this.buildTopUpCollateralInstruction(
1037
1188
  { user: userPubkey, voteAccount, amount }
1038
1189
  );
1039
- const tx = await this.provider.sendAndConfirm(
1190
+ return await this.provider.sendAndConfirm(
1040
1191
  new Transaction().add(ix),
1041
1192
  []
1042
1193
  );
1043
- return tx;
1044
1194
  }
1045
1195
  async withdrawCollateral(voteAccount, destination, amount, withdrawalAuthority) {
1046
1196
  const authorityPubkey = withdrawalAuthority ?? this.provider.wallet.publicKey;
1047
1197
  const ix = await this.buildWithdrawCollateralInstruction(
1048
1198
  { withdrawalAuthority: authorityPubkey, voteAccount, destination, amount }
1049
1199
  );
1050
- const tx = await this.provider.sendAndConfirm(
1200
+ return await this.provider.sendAndConfirm(
1051
1201
  new Transaction().add(ix),
1052
1202
  []
1053
1203
  );
1054
- return tx;
1055
1204
  }
1056
1205
  async claimCompensation(voteAccount, amount, authority) {
1057
1206
  const authorityPubkey = authority ?? this.provider.wallet.publicKey;
1058
1207
  const ix = await this.buildWithdrawCompensationInstruction(
1059
1208
  { authority: authorityPubkey, voteAccount, amount }
1060
1209
  );
1061
- const tx = await this.provider.sendAndConfirm(
1210
+ return await this.provider.sendAndConfirm(
1062
1211
  new Transaction().add(ix),
1063
1212
  []
1064
1213
  );
1065
- return tx;
1066
1214
  }
1067
1215
  /**
1068
1216
  * Get transaction history grouped by epochs
@@ -1100,11 +1248,10 @@ var JBondClient = class _JBondClient {
1100
1248
  for (const epochData of epochMap.values()) {
1101
1249
  epochData.balanceChange = epochData.deposits - epochData.withdrawals;
1102
1250
  }
1103
- const result = [...epochMap.values()].toSorted((a, b) => b.epoch - a.epoch);
1104
- return result;
1251
+ return [...epochMap.values()].toSorted((a, b) => b.epoch - a.epoch);
1105
1252
  }
1106
1253
  async getHistory(voteAccount, options) {
1107
- const [ValidatorBondAccount] = this.getValidatorBondPDA(voteAccount);
1254
+ const [ValidatorBondAccount] = this.pda.validatorBond(voteAccount);
1108
1255
  const signatures = await this.connection.getSignaturesForAddress(
1109
1256
  ValidatorBondAccount,
1110
1257
  {
@@ -1137,7 +1284,7 @@ var JBondClient = class _JBondClient {
1137
1284
  const slot = tx.slot || 0;
1138
1285
  const instructions = tx.transaction.message.instructions;
1139
1286
  for (const instruction of instructions) {
1140
- if ("programId" in instruction && instruction.programId.equals(this.program.programId)) {
1287
+ if ("programId" in instruction && instruction.programId.equals(this.options.programId ?? this.program.programId)) {
1141
1288
  const ixData = instruction;
1142
1289
  if ("parsed" in ixData && ixData.parsed) {
1143
1290
  continue;
@@ -1200,7 +1347,11 @@ var JBondClient = class _JBondClient {
1200
1347
  }
1201
1348
  return history.toSorted((a, b) => b.slot - a.slot);
1202
1349
  }
1203
- // Helper method to get paginated history
1350
+ /**
1351
+ * Get full transaction history by paginating through results
1352
+ * @param voteAccount
1353
+ * @param pageSize
1354
+ */
1204
1355
  async getFullHistory(voteAccount, pageSize = 100) {
1205
1356
  const allHistory = [];
1206
1357
  let before;
@@ -1220,6 +1371,11 @@ var JBondClient = class _JBondClient {
1220
1371
  }
1221
1372
  return allHistory;
1222
1373
  }
1374
+ /**
1375
+ * Get instruction discriminator from IDL
1376
+ * @param instructionName
1377
+ * @private
1378
+ */
1223
1379
  getInstructionDiscriminator(instructionName) {
1224
1380
  const instruction = this.program.idl.instructions.find((ix) => ix.name === instructionName);
1225
1381
  if (!instruction) {
@@ -1232,33 +1388,6 @@ var JBondClient = class _JBondClient {
1232
1388
  }
1233
1389
  };
1234
1390
 
1235
- // src/common.ts
1236
- var PROGRAM_ID = "8vrqsFHnDdjMYMwahytRzmmYgRqCMmka8X6DJUXGQzWr";
1237
- var RESERVE_ADDRESS = "61mS9nEir6jx6cvte6NzQpyrFk3Fj4krMNLuHhi4tjJz";
1238
- function loadConfig() {
1239
- const rpcUrl = process.env.SOLANA_RPC_URL || "https://api.devnet.solana.com";
1240
- const programId = new PublicKey(
1241
- PROGRAM_ID
1242
- );
1243
- const reserveAddress = new PublicKey(
1244
- RESERVE_ADDRESS
1245
- );
1246
- return {
1247
- rpcUrl,
1248
- programId,
1249
- reserveAddress
1250
- };
1251
- }
1252
- function loadKeypair(keypairPath) {
1253
- try {
1254
- const keypairString = readFileSync(keypairPath, "utf8");
1255
- const keypairData = JSON.parse(keypairString);
1256
- return Keypair.fromSecretKey(new Uint8Array(keypairData));
1257
- } catch (error) {
1258
- throw new Error(`Failed to load keypair from ${keypairPath}: ${error}`);
1259
- }
1260
- }
1261
-
1262
- export { BondTransactionType, JBondClient, PROGRAM_ID, RESERVE_ADDRESS, loadConfig, loadKeypair };
1391
+ export { BondClientEnv, BondTransactionType, ENV_PROGRAM_ID, JBondClient, NodeWallet };
1263
1392
  //# sourceMappingURL=index.mjs.map
1264
1393
  //# sourceMappingURL=index.mjs.map