@jpool/bond-sdk 0.1.0 → 0.3.0-next.0

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