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