@@ -54,12 +54,12 @@ import type { Staking } from "../types/staking";
54
54
55
55
export type PythStakingClientConfig = {
56
56
connection : Connection ;
57
- wallet : AnchorWallet ;
57
+ wallet : AnchorWallet | undefined ;
58
58
} ;
59
59
60
60
export class PythStakingClient {
61
61
connection : Connection ;
62
- wallet : AnchorWallet ;
62
+ wallet : AnchorWallet | undefined ;
63
63
provider : AnchorProvider ;
64
64
stakingProgram : Program < Staking > ;
65
65
integrityPoolProgram : Program < IntegrityPool > ;
@@ -68,9 +68,15 @@ export class PythStakingClient {
68
68
constructor ( config : PythStakingClientConfig ) {
69
69
this . connection = config . connection ;
70
70
this . wallet = config . wallet ;
71
- this . provider = new AnchorProvider ( this . connection , this . wallet , {
72
- skipPreflight : true ,
73
- } ) ;
71
+
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
+ ) ;
74
80
this . stakingProgram = new Program ( StakingIdl as Staking , this . provider ) ;
75
81
this . integrityPoolProgram = new Program (
76
82
IntegrityPoolIdl as IntegrityPool ,
@@ -82,7 +88,14 @@ export class PythStakingClient {
82
88
) ;
83
89
}
84
90
91
+ private assertWallet ( ) : asserts this is { wallet : AnchorWallet } {
92
+ if ( this . wallet === undefined ) {
93
+ throw new Error ( "Wallet not set" ) ;
94
+ }
95
+ }
96
+
85
97
async initGlobalConfig ( config : GlobalConfig ) {
98
+ this . assertWallet ( ) ;
86
99
const globalConfigAnchor = convertBigIntToBN ( config ) ;
87
100
const instruction = await this . stakingProgram . methods
88
101
. initConfig ( globalConfigAnchor )
@@ -100,9 +113,8 @@ export class PythStakingClient {
100
113
}
101
114
102
115
/** Gets a users stake accounts */
103
- public async getAllStakeAccountPositions (
104
- user : PublicKey ,
105
- ) : Promise < PublicKey [ ] > {
116
+ public async getAllStakeAccountPositions ( ) : Promise < PublicKey [ ] > {
117
+ this . assertWallet ( ) ;
106
118
const positionDataMemcmp = this . stakingProgram . coder . accounts . memcmp (
107
119
"positionData" ,
108
120
) as {
@@ -121,7 +133,7 @@ export class PythStakingClient {
121
133
{
122
134
memcmp : {
123
135
offset : 8 ,
124
- bytes : user . toBase58 ( ) ,
136
+ bytes : this . wallet . publicKey . toBase58 ( ) ,
125
137
} ,
126
138
} ,
127
139
] ,
@@ -176,6 +188,7 @@ export class PythStakingClient {
176
188
poolData : PublicKey ;
177
189
y : bigint ;
178
190
} ) {
191
+ this . assertWallet ( ) ;
179
192
const yAnchor = convertBigIntToBN ( y ) ;
180
193
const instruction = await this . integrityPoolProgram . methods
181
194
. initializePool ( rewardProgramAuthority , yAnchor )
@@ -189,6 +202,7 @@ export class PythStakingClient {
189
202
}
190
203
191
204
public async getOwnerPythAtaAccount ( ) : Promise < Account > {
205
+ this . assertWallet ( ) ;
192
206
const globalConfig = await this . getGlobalConfig ( ) ;
193
207
return getAccount (
194
208
this . connection ,
@@ -228,6 +242,7 @@ export class PythStakingClient {
228
242
stakeAccountPositions : PublicKey ,
229
243
amount : bigint ,
230
244
) {
245
+ this . assertWallet ( ) ;
231
246
const instruction = await this . stakingProgram . methods
232
247
. createPosition (
233
248
{
@@ -248,6 +263,7 @@ export class PythStakingClient {
248
263
positionState : PositionState . LOCKED | PositionState . LOCKING ,
249
264
amount : bigint ,
250
265
) {
266
+ this . assertWallet ( ) ;
251
267
const stakeAccountPositionsData = await this . getStakeAccountPositions (
252
268
stakeAccountPositions ,
253
269
) ;
@@ -303,6 +319,7 @@ export class PythStakingClient {
303
319
positionState : PositionState . LOCKED | PositionState . LOCKING ,
304
320
amount : bigint ,
305
321
) {
322
+ this . assertWallet ( ) ;
306
323
const stakeAccountPositionsData = await this . getStakeAccountPositions (
307
324
stakeAccountPositions ,
308
325
) ;
@@ -355,6 +372,7 @@ export class PythStakingClient {
355
372
}
356
373
357
374
public async hasGovernanceRecord ( config : GlobalConfig ) : Promise < boolean > {
375
+ this . assertWallet ( ) ;
358
376
const tokenOwnerRecordAddress = await getTokenOwnerRecordAddress (
359
377
GOVERNANCE_ADDRESS ,
360
378
config . pythGovernanceRealm ,
@@ -370,6 +388,7 @@ export class PythStakingClient {
370
388
}
371
389
372
390
public async createStakeAccountAndDeposit ( amount : bigint ) {
391
+ this . assertWallet ( ) ;
373
392
const globalConfig = await this . getGlobalConfig ( ) ;
374
393
375
394
const senderTokenAccount = await getAssociatedTokenAddress (
@@ -451,6 +470,7 @@ export class PythStakingClient {
451
470
stakeAccountPositions : PublicKey ,
452
471
amount : bigint ,
453
472
) {
473
+ this . assertWallet ( ) ;
454
474
const globalConfig = await this . getGlobalConfig ( ) ;
455
475
const mint = globalConfig . pythTokenMint ;
456
476
@@ -473,6 +493,7 @@ export class PythStakingClient {
473
493
stakeAccountPositions : PublicKey ,
474
494
amount : bigint ,
475
495
) {
496
+ this . assertWallet ( ) ;
476
497
const globalConfig = await this . getGlobalConfig ( ) ;
477
498
const mint = globalConfig . pythTokenMint ;
478
499
@@ -497,6 +518,7 @@ export class PythStakingClient {
497
518
publisher : PublicKey ,
498
519
amount : bigint ,
499
520
) {
521
+ this . assertWallet ( ) ;
500
522
const instruction = await this . integrityPoolProgram . methods
501
523
. delegate ( convertBigIntToBN ( amount ) )
502
524
. accounts ( {
@@ -534,6 +556,7 @@ export class PythStakingClient {
534
556
async getAdvanceDelegationRecordInstructions (
535
557
stakeAccountPositions : PublicKey ,
536
558
) {
559
+ this . assertWallet ( ) ;
537
560
const poolData = await this . getPoolDataAccount ( ) ;
538
561
const stakeAccountPositionsData = await this . getStakeAccountPositions (
539
562
stakeAccountPositions ,
@@ -589,6 +612,7 @@ export class PythStakingClient {
589
612
}
590
613
591
614
public async advanceDelegationRecord ( stakeAccountPositions : PublicKey ) {
615
+ this . assertWallet ( ) ;
592
616
const instructions = await this . getAdvanceDelegationRecordInstructions (
593
617
stakeAccountPositions ,
594
618
) ;
@@ -612,7 +636,7 @@ export class PythStakingClient {
612
636
613
637
for ( const instruction of instructions . advanceDelegationRecordInstructions ) {
614
638
const tx = new Transaction ( ) . add ( instruction ) ;
615
- tx . feePayer = this . wallet . publicKey ;
639
+ tx . feePayer = PublicKey . default ;
616
640
const res = await this . connection . simulateTransaction ( tx ) ;
617
641
const val = res . value . returnData ?. data [ 0 ] ;
618
642
if ( val === undefined ) {
@@ -650,6 +674,7 @@ export class PythStakingClient {
650
674
stakeAccountPositions : PublicKey ,
651
675
newStakeAccountPositions : PublicKey | undefined ,
652
676
) {
677
+ this . assertWallet ( ) ;
653
678
const instruction = await this . integrityPoolProgram . methods
654
679
. setPublisherStakeAccount ( )
655
680
. accounts ( {
0 commit comments