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