1
1
import * as crypto from "crypto" ;
2
2
3
- import { AnchorProvider , BN , Program } from "@coral-xyz/anchor" ;
3
+ import { AnchorProvider , BN , Program , Wallet } from "@coral-xyz/anchor" ;
4
4
import {
5
5
getTokenOwnerRecordAddress ,
6
6
PROGRAM_VERSION_V2 ,
@@ -15,6 +15,7 @@ import {
15
15
import type { AnchorWallet } from "@solana/wallet-adapter-react" ;
16
16
import {
17
17
Connection ,
18
+ Keypair ,
18
19
PublicKey ,
19
20
SystemProgram ,
20
21
Transaction ,
@@ -59,24 +60,19 @@ export type PythStakingClientConfig = {
59
60
60
61
export class PythStakingClient {
61
62
connection : Connection ;
62
- wallet : AnchorWallet | undefined ;
63
+ wallet : AnchorWallet ;
63
64
provider : AnchorProvider ;
64
65
stakingProgram : Program < Staking > ;
65
66
integrityPoolProgram : Program < IntegrityPool > ;
66
67
publisherCapsProgram : Program < PublisherCaps > ;
67
68
68
69
constructor ( config : PythStakingClientConfig ) {
69
70
this . connection = config . connection ;
70
- this . wallet = config . wallet ;
71
+ this . wallet = config . wallet ?? new Wallet ( Keypair . generate ( ) ) ;
71
72
72
- // {} as AnchorWallet is a workaround for AnchorProvider requiring a wallet
73
- this . provider = new AnchorProvider (
74
- this . connection ,
75
- this . wallet ?? ( { } as AnchorWallet ) ,
76
- {
77
- skipPreflight : true ,
78
- } ,
79
- ) ;
73
+ this . provider = new AnchorProvider ( this . connection , this . wallet , {
74
+ skipPreflight : true ,
75
+ } ) ;
80
76
this . stakingProgram = new Program ( StakingIdl as Staking , this . provider ) ;
81
77
this . integrityPoolProgram = new Program (
82
78
IntegrityPoolIdl as IntegrityPool ,
@@ -88,14 +84,7 @@ export class PythStakingClient {
88
84
) ;
89
85
}
90
86
91
- private assertWallet ( ) : asserts this is { wallet : AnchorWallet } {
92
- if ( this . wallet === undefined ) {
93
- throw new Error ( "Wallet not set" ) ;
94
- }
95
- }
96
-
97
87
async initGlobalConfig ( config : GlobalConfig ) {
98
- this . assertWallet ( ) ;
99
88
const globalConfigAnchor = convertBigIntToBN ( config ) ;
100
89
const instruction = await this . stakingProgram . methods
101
90
. initConfig ( globalConfigAnchor )
@@ -114,7 +103,6 @@ export class PythStakingClient {
114
103
115
104
/** Gets a users stake accounts */
116
105
public async getAllStakeAccountPositions ( ) : Promise < PublicKey [ ] > {
117
- this . assertWallet ( ) ;
118
106
const positionDataMemcmp = this . stakingProgram . coder . accounts . memcmp (
119
107
"positionData" ,
120
108
) as {
@@ -188,7 +176,6 @@ export class PythStakingClient {
188
176
poolData : PublicKey ;
189
177
y : bigint ;
190
178
} ) {
191
- this . assertWallet ( ) ;
192
179
const yAnchor = convertBigIntToBN ( y ) ;
193
180
const instruction = await this . integrityPoolProgram . methods
194
181
. initializePool ( rewardProgramAuthority , yAnchor )
@@ -202,7 +189,6 @@ export class PythStakingClient {
202
189
}
203
190
204
191
public async getOwnerPythAtaAccount ( ) : Promise < Account > {
205
- this . assertWallet ( ) ;
206
192
const globalConfig = await this . getGlobalConfig ( ) ;
207
193
return getAccount (
208
194
this . connection ,
@@ -242,7 +228,6 @@ export class PythStakingClient {
242
228
stakeAccountPositions : PublicKey ,
243
229
amount : bigint ,
244
230
) {
245
- this . assertWallet ( ) ;
246
231
const instruction = await this . stakingProgram . methods
247
232
. createPosition (
248
233
{
@@ -263,7 +248,6 @@ export class PythStakingClient {
263
248
positionState : PositionState . LOCKED | PositionState . LOCKING ,
264
249
amount : bigint ,
265
250
) {
266
- this . assertWallet ( ) ;
267
251
const stakeAccountPositionsData = await this . getStakeAccountPositions (
268
252
stakeAccountPositions ,
269
253
) ;
@@ -319,7 +303,6 @@ export class PythStakingClient {
319
303
positionState : PositionState . LOCKED | PositionState . LOCKING ,
320
304
amount : bigint ,
321
305
) {
322
- this . assertWallet ( ) ;
323
306
const stakeAccountPositionsData = await this . getStakeAccountPositions (
324
307
stakeAccountPositions ,
325
308
) ;
@@ -372,7 +355,6 @@ export class PythStakingClient {
372
355
}
373
356
374
357
public async hasGovernanceRecord ( config : GlobalConfig ) : Promise < boolean > {
375
- this . assertWallet ( ) ;
376
358
const tokenOwnerRecordAddress = await getTokenOwnerRecordAddress (
377
359
GOVERNANCE_ADDRESS ,
378
360
config . pythGovernanceRealm ,
@@ -388,7 +370,6 @@ export class PythStakingClient {
388
370
}
389
371
390
372
public async createStakeAccountAndDeposit ( amount : bigint ) {
391
- this . assertWallet ( ) ;
392
373
const globalConfig = await this . getGlobalConfig ( ) ;
393
374
394
375
const senderTokenAccount = await getAssociatedTokenAddress (
@@ -470,7 +451,6 @@ export class PythStakingClient {
470
451
stakeAccountPositions : PublicKey ,
471
452
amount : bigint ,
472
453
) {
473
- this . assertWallet ( ) ;
474
454
const globalConfig = await this . getGlobalConfig ( ) ;
475
455
const mint = globalConfig . pythTokenMint ;
476
456
@@ -493,7 +473,6 @@ export class PythStakingClient {
493
473
stakeAccountPositions : PublicKey ,
494
474
amount : bigint ,
495
475
) {
496
- this . assertWallet ( ) ;
497
476
const globalConfig = await this . getGlobalConfig ( ) ;
498
477
const mint = globalConfig . pythTokenMint ;
499
478
@@ -518,7 +497,6 @@ export class PythStakingClient {
518
497
publisher : PublicKey ,
519
498
amount : bigint ,
520
499
) {
521
- this . assertWallet ( ) ;
522
500
const instruction = await this . integrityPoolProgram . methods
523
501
. delegate ( convertBigIntToBN ( amount ) )
524
502
. accounts ( {
@@ -556,7 +534,6 @@ export class PythStakingClient {
556
534
async getAdvanceDelegationRecordInstructions (
557
535
stakeAccountPositions : PublicKey ,
558
536
) {
559
- this . assertWallet ( ) ;
560
537
const poolData = await this . getPoolDataAccount ( ) ;
561
538
const stakeAccountPositionsData = await this . getStakeAccountPositions (
562
539
stakeAccountPositions ,
@@ -612,7 +589,6 @@ export class PythStakingClient {
612
589
}
613
590
614
591
public async advanceDelegationRecord ( stakeAccountPositions : PublicKey ) {
615
- this . assertWallet ( ) ;
616
592
const instructions = await this . getAdvanceDelegationRecordInstructions (
617
593
stakeAccountPositions ,
618
594
) ;
@@ -674,7 +650,6 @@ export class PythStakingClient {
674
650
stakeAccountPositions : PublicKey ,
675
651
newStakeAccountPositions : PublicKey | undefined ,
676
652
) {
677
- this . assertWallet ( ) ;
678
653
const instruction = await this . integrityPoolProgram . methods
679
654
. setPublisherStakeAccount ( )
680
655
. accounts ( {
0 commit comments