@jpool/bond-sdk 0.3.0-next.8 → 0.3.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,56 @@ 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;
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");
941
849
  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
+ );
942
856
  this.program = new anchor.Program(jbond_default, this.provider);
943
857
  }
944
- options;
945
- program;
946
858
  /**
947
859
  * Creates an instance of `JBondClient` using a provided connection and wallet.
948
860
  */
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
- );
861
+ static fromWallet(config, wallet, options) {
862
+ return new this(config, wallet, options);
958
863
  }
959
864
  /**
960
865
  * Creates an instance of `JBondClient` using the provided connection and keypair.
961
866
  */
962
- static fromKeypair(connection, keypair, options) {
963
- return _JBondClient.fromWallet(connection, new NodeWallet(keypair), options);
867
+ static fromKeypair(config, keypair, options) {
868
+ return _JBondClient.fromWallet(config, new NodeWallet(keypair), options);
964
869
  }
965
870
  /**
966
871
  * Get the current program ID.
967
872
  */
968
873
  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;
874
+ return this.options.programId ?? PROGRAM_ID;
976
875
  }
977
876
  /**
978
877
  * Set the environment.
979
878
  */
980
879
  env(env) {
981
- return this.configure("programId", ENV_PROGRAM_ID[env]);
880
+ if (env === "prod" /* PROD */) {
881
+ return this.configure("programId", PROGRAM_ID);
882
+ }
883
+ return this.configure("programId", DEV_PROGRAM_ID);
982
884
  }
983
885
  /**
984
886
  * Configure a specific option.
@@ -987,215 +889,235 @@ var JBondClient = class _JBondClient {
987
889
  this.options[key] = val;
988
890
  return this;
989
891
  }
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));
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
+ );
1052
898
  }
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));
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
+ );
1061
908
  }
1062
- /**
1063
- * Build initialize instruction
1064
- */
909
+ // Build initialize instruction
1065
910
  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({
911
+ const [globalState] = this.getGlobalStatePDA();
912
+ const { authority, reserveAddress } = props;
913
+ return this.program.methods.initialize().accountsStrict({
1070
914
  globalState,
1071
915
  authority,
1072
- reserve
916
+ reserve: reserveAddress,
917
+ systemProgram: web3_js.SystemProgram.programId
1073
918
  }).instruction();
1074
919
  }
1075
- /**
1076
- * Build register validator instruction
1077
- */
920
+ // Build register validator instruction
1078
921
  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({
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({
1085
931
  creator,
1086
932
  globalState,
1087
- validatorBond,
933
+ validatorBondAccount: validatorBondAccountAddress,
1088
934
  identity,
1089
- voteAccount
935
+ voteAccount,
936
+ systemProgram: web3_js.SystemProgram.programId
1090
937
  }).instruction();
1091
938
  }
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
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
1103
948
  }).instruction();
1104
949
  }
1105
- /**
1106
- * Build withdraw collateral instruction
1107
- */
1108
950
  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,
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,
1117
958
  epochSchedule: web3_js.SYSVAR_EPOCH_SCHEDULE_PUBKEY,
1118
- destination
959
+ systemProgram: web3_js.SystemProgram.programId
1119
960
  }).instruction();
1120
961
  }
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
- }
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.");
1136
971
  }
1137
- return this.program.methods.claim(lamports).accountsPartial({
972
+ return this.program.methods.withdrawCompensation(amountLamports).accountsStrict({
1138
973
  globalState,
1139
- validatorBond,
1140
- reserve,
1141
- authority
974
+ validatorBondAccount: validatorBondAccountAddress,
975
+ reserve: reserveAddress,
976
+ authority,
977
+ systemProgram: web3_js.SystemProgram.programId
1142
978
  }).instruction();
1143
979
  }
1144
- /**
1145
- * Build set authority instruction
1146
- */
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
1147
1009
  async buildSetAuthorityInstruction(props) {
1148
- const [globalState] = this.pda.globalState();
1149
- const authority = props.authority ?? this.provider.wallet.publicKey;
1010
+ const [globalState] = this.getGlobalStatePDA();
1150
1011
  return this.program.methods.setAuthority().accountsStrict({
1151
1012
  globalState,
1152
- authority,
1013
+ authority: props.authority,
1153
1014
  newAuthority: props.newAuthority
1154
1015
  }).instruction();
1155
1016
  }
1156
1017
  /**
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
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)
1175
1021
  */
1176
- async getValidatorBondBalance(vote) {
1177
- const [address] = this.pda.validatorBond(new web3_js.PublicKey(vote));
1178
- const accountInfo = await this.connection.getAccountInfo(address);
1022
+ async getValidatorCollateralBalance(voteAccount) {
1023
+ const [validatorBondAccountAddress] = this.getValidatorBondPDA(voteAccount);
1024
+ const accountInfo = await this.connection.getAccountInfo(validatorBondAccountAddress);
1179
1025
  if (!accountInfo) {
1180
1026
  return 0;
1181
1027
  }
1182
- const { data, lamports } = accountInfo;
1183
- const rentExempt = await this.connection.getMinimumBalanceForRentExemption(data.length);
1184
- const availableBalance = Math.max(0, lamports - rentExempt);
1028
+ const rentExempt = await this.connection.getMinimumBalanceForRentExemption(
1029
+ accountInfo.data.length
1030
+ );
1031
+ const availableBalance = Math.max(0, accountInfo.lamports - rentExempt);
1185
1032
  return availableBalance / web3_js.LAMPORTS_PER_SOL;
1186
1033
  }
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
+ }
1187
1109
  /**
1188
1110
  * Get transaction history grouped by epochs
1189
- * @param vote - The vote account to get history for
1111
+ * @param voteAccount - The vote account to get history for
1190
1112
  * @param epochsCount - Number of recent epochs to return (default: 10)
1191
1113
  * @returns Array of epoch history items sorted by epoch (descending)
1192
1114
  */
1193
- async getHistoryGroupedByEpochs(vote, epochsCount = 10) {
1194
- const epochInfo = await this.connection.getEpochInfo();
1195
- const fullHistory = await this.getFullHistory(new web3_js.PublicKey(vote));
1115
+ async getHistoryGroupedByEpochs(voteAccount, epochsCount = 10) {
1116
+ const currentEpoch = await this.getCurrentEpoch();
1117
+ const fullHistory = await this.getFullHistory(voteAccount);
1196
1118
  const epochMap = /* @__PURE__ */ new Map();
1197
1119
  for (const item of fullHistory) {
1198
- if (item.epoch < epochInfo.epoch - epochsCount + 1) {
1120
+ if (item.epoch < currentEpoch - epochsCount + 1) {
1199
1121
  continue;
1200
1122
  }
1201
1123
  if (!epochMap.has(item.epoch)) {
@@ -1220,21 +1142,19 @@ var JBondClient = class _JBondClient {
1220
1142
  for (const epochData of epochMap.values()) {
1221
1143
  epochData.balanceChange = epochData.deposits - epochData.withdrawals;
1222
1144
  }
1223
- return [...epochMap.values()].toSorted((a, b) => b.epoch - a.epoch);
1145
+ const result = [...epochMap.values()].toSorted((a, b) => b.epoch - a.epoch);
1146
+ return result;
1224
1147
  }
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(
1148
+ async getHistory(voteAccount, options) {
1149
+ const [ValidatorBondAccount] = this.getValidatorBondPDA(voteAccount);
1150
+ const signatures = await this.connection.getSignaturesForAddress(
1231
1151
  ValidatorBondAccount,
1232
1152
  {
1233
1153
  limit: options?.limit || 1e3,
1234
1154
  before: options?.before,
1235
1155
  until: options?.until
1236
1156
  }
1237
- )).filter((sig) => !sig.err);
1157
+ );
1238
1158
  const signatureStrings = signatures.map((sig) => sig.signature);
1239
1159
  const BATCH_SIZE = 100;
1240
1160
  const allTransactions = [];
@@ -1252,7 +1172,7 @@ var JBondClient = class _JBondClient {
1252
1172
  const history = [];
1253
1173
  for (const [idx, tx] of allTransactions.entries()) {
1254
1174
  const sigInfo = signatures[idx];
1255
- if (!tx || !tx.meta || tx.meta.err !== null) {
1175
+ if (!tx || !tx.meta) {
1256
1176
  continue;
1257
1177
  }
1258
1178
  try {
@@ -1274,10 +1194,10 @@ var JBondClient = class _JBondClient {
1274
1194
  const dataBuffer = bs58__default.default.decode(data);
1275
1195
  if (dataBuffer.length >= 16) {
1276
1196
  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");
1197
+ const bondInitDiscriminator = this.getInstructionDiscriminator("bondInit");
1198
+ const bondTopUpDiscriminator = this.getInstructionDiscriminator("bondTopUp");
1199
+ const withdrawCompensationDiscriminator = this.getInstructionDiscriminator("withdrawCompensation");
1200
+ const bondWithdrawDiscriminator = this.getInstructionDiscriminator("bondWithdraw");
1281
1201
  const amountBytes = dataBuffer.slice(8, 16);
1282
1202
  const amountBN = new anchor.BN(amountBytes, "le");
1283
1203
  amount = amountBN.toNumber() / web3_js.LAMPORTS_PER_SOL;
@@ -1322,11 +1242,7 @@ var JBondClient = class _JBondClient {
1322
1242
  }
1323
1243
  return history.toSorted((a, b) => b.slot - a.slot);
1324
1244
  }
1325
- /**
1326
- * Get full transaction history by paginating through results
1327
- * @param voteAccount
1328
- * @param pageSize
1329
- */
1245
+ // Helper method to get paginated history
1330
1246
  async getFullHistory(voteAccount, pageSize = 100) {
1331
1247
  const allHistory = [];
1332
1248
  let before;
@@ -1346,11 +1262,6 @@ var JBondClient = class _JBondClient {
1346
1262
  }
1347
1263
  return allHistory;
1348
1264
  }
1349
- /**
1350
- * Get instruction discriminator from IDL
1351
- * @param instructionName
1352
- * @private
1353
- */
1354
1265
  getInstructionDiscriminator(instructionName) {
1355
1266
  const instruction = this.program.idl.instructions.find((ix) => ix.name === instructionName);
1356
1267
  if (!instruction) {
@@ -1365,8 +1276,8 @@ var JBondClient = class _JBondClient {
1365
1276
 
1366
1277
  exports.BondClientEnv = BondClientEnv;
1367
1278
  exports.BondTransactionType = BondTransactionType;
1368
- exports.ENV_PROGRAM_ID = ENV_PROGRAM_ID;
1279
+ exports.DEV_PROGRAM_ID = DEV_PROGRAM_ID;
1369
1280
  exports.JBondClient = JBondClient;
1370
- exports.NodeWallet = NodeWallet;
1281
+ exports.PROGRAM_ID = PROGRAM_ID;
1371
1282
  //# sourceMappingURL=index.js.map
1372
1283
  //# sourceMappingURL=index.js.map