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