@jpool/bond-sdk 0.3.0 → 0.5.0-next.1

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