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