Skip to content

Commit 88f0e18

Browse files
feat(staking): add default instead of assertWallet (#1938)
* feat(staking): add default instead of assertWallet * fix
1 parent fbcb16e commit 88f0e18

File tree

2 files changed

+8
-33
lines changed

2 files changed

+8
-33
lines changed

apps/staking/src/hooks/use-api.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const State = {
4545
onCreateAccount: (newAccount: PublicKey) => Promise<void>,
4646
) => ({
4747
type: StateType.LoadedNoStakeAccount as const,
48-
dashboardDataCacheKey: client.wallet?.publicKey.toBase58(),
48+
dashboardDataCacheKey: client.wallet.publicKey.toBase58(),
4949
loadData: () => api.loadData(client, hermesClient),
5050
deposit: async (amount: bigint) => {
5151
const account = await api.createStakeAccountAndDeposit(client, amount);

governance/pyth_staking_sdk/src/pyth-staking-client.ts

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as crypto from "crypto";
22

3-
import { AnchorProvider, BN, Program } from "@coral-xyz/anchor";
3+
import { AnchorProvider, BN, Program, Wallet } from "@coral-xyz/anchor";
44
import {
55
getTokenOwnerRecordAddress,
66
PROGRAM_VERSION_V2,
@@ -15,6 +15,7 @@ import {
1515
import type { AnchorWallet } from "@solana/wallet-adapter-react";
1616
import {
1717
Connection,
18+
Keypair,
1819
PublicKey,
1920
SystemProgram,
2021
Transaction,
@@ -59,24 +60,19 @@ export type PythStakingClientConfig = {
5960

6061
export class PythStakingClient {
6162
connection: Connection;
62-
wallet: AnchorWallet | undefined;
63+
wallet: AnchorWallet;
6364
provider: AnchorProvider;
6465
stakingProgram: Program<Staking>;
6566
integrityPoolProgram: Program<IntegrityPool>;
6667
publisherCapsProgram: Program<PublisherCaps>;
6768

6869
constructor(config: PythStakingClientConfig) {
6970
this.connection = config.connection;
70-
this.wallet = config.wallet;
71+
this.wallet = config.wallet ?? new Wallet(Keypair.generate());
7172

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+
});
8076
this.stakingProgram = new Program(StakingIdl as Staking, this.provider);
8177
this.integrityPoolProgram = new Program(
8278
IntegrityPoolIdl as IntegrityPool,
@@ -88,14 +84,7 @@ export class PythStakingClient {
8884
);
8985
}
9086

91-
private assertWallet(): asserts this is { wallet: AnchorWallet } {
92-
if (this.wallet === undefined) {
93-
throw new Error("Wallet not set");
94-
}
95-
}
96-
9787
async initGlobalConfig(config: GlobalConfig) {
98-
this.assertWallet();
9988
const globalConfigAnchor = convertBigIntToBN(config);
10089
const instruction = await this.stakingProgram.methods
10190
.initConfig(globalConfigAnchor)
@@ -114,7 +103,6 @@ export class PythStakingClient {
114103

115104
/** Gets a users stake accounts */
116105
public async getAllStakeAccountPositions(): Promise<PublicKey[]> {
117-
this.assertWallet();
118106
const positionDataMemcmp = this.stakingProgram.coder.accounts.memcmp(
119107
"positionData",
120108
) as {
@@ -188,7 +176,6 @@ export class PythStakingClient {
188176
poolData: PublicKey;
189177
y: bigint;
190178
}) {
191-
this.assertWallet();
192179
const yAnchor = convertBigIntToBN(y);
193180
const instruction = await this.integrityPoolProgram.methods
194181
.initializePool(rewardProgramAuthority, yAnchor)
@@ -202,7 +189,6 @@ export class PythStakingClient {
202189
}
203190

204191
public async getOwnerPythAtaAccount(): Promise<Account> {
205-
this.assertWallet();
206192
const globalConfig = await this.getGlobalConfig();
207193
return getAccount(
208194
this.connection,
@@ -242,7 +228,6 @@ export class PythStakingClient {
242228
stakeAccountPositions: PublicKey,
243229
amount: bigint,
244230
) {
245-
this.assertWallet();
246231
const instruction = await this.stakingProgram.methods
247232
.createPosition(
248233
{
@@ -263,7 +248,6 @@ export class PythStakingClient {
263248
positionState: PositionState.LOCKED | PositionState.LOCKING,
264249
amount: bigint,
265250
) {
266-
this.assertWallet();
267251
const stakeAccountPositionsData = await this.getStakeAccountPositions(
268252
stakeAccountPositions,
269253
);
@@ -319,7 +303,6 @@ export class PythStakingClient {
319303
positionState: PositionState.LOCKED | PositionState.LOCKING,
320304
amount: bigint,
321305
) {
322-
this.assertWallet();
323306
const stakeAccountPositionsData = await this.getStakeAccountPositions(
324307
stakeAccountPositions,
325308
);
@@ -372,7 +355,6 @@ export class PythStakingClient {
372355
}
373356

374357
public async hasGovernanceRecord(config: GlobalConfig): Promise<boolean> {
375-
this.assertWallet();
376358
const tokenOwnerRecordAddress = await getTokenOwnerRecordAddress(
377359
GOVERNANCE_ADDRESS,
378360
config.pythGovernanceRealm,
@@ -388,7 +370,6 @@ export class PythStakingClient {
388370
}
389371

390372
public async createStakeAccountAndDeposit(amount: bigint) {
391-
this.assertWallet();
392373
const globalConfig = await this.getGlobalConfig();
393374

394375
const senderTokenAccount = await getAssociatedTokenAddress(
@@ -470,7 +451,6 @@ export class PythStakingClient {
470451
stakeAccountPositions: PublicKey,
471452
amount: bigint,
472453
) {
473-
this.assertWallet();
474454
const globalConfig = await this.getGlobalConfig();
475455
const mint = globalConfig.pythTokenMint;
476456

@@ -493,7 +473,6 @@ export class PythStakingClient {
493473
stakeAccountPositions: PublicKey,
494474
amount: bigint,
495475
) {
496-
this.assertWallet();
497476
const globalConfig = await this.getGlobalConfig();
498477
const mint = globalConfig.pythTokenMint;
499478

@@ -518,7 +497,6 @@ export class PythStakingClient {
518497
publisher: PublicKey,
519498
amount: bigint,
520499
) {
521-
this.assertWallet();
522500
const instruction = await this.integrityPoolProgram.methods
523501
.delegate(convertBigIntToBN(amount))
524502
.accounts({
@@ -556,7 +534,6 @@ export class PythStakingClient {
556534
async getAdvanceDelegationRecordInstructions(
557535
stakeAccountPositions: PublicKey,
558536
) {
559-
this.assertWallet();
560537
const poolData = await this.getPoolDataAccount();
561538
const stakeAccountPositionsData = await this.getStakeAccountPositions(
562539
stakeAccountPositions,
@@ -612,7 +589,6 @@ export class PythStakingClient {
612589
}
613590

614591
public async advanceDelegationRecord(stakeAccountPositions: PublicKey) {
615-
this.assertWallet();
616592
const instructions = await this.getAdvanceDelegationRecordInstructions(
617593
stakeAccountPositions,
618594
);
@@ -674,7 +650,6 @@ export class PythStakingClient {
674650
stakeAccountPositions: PublicKey,
675651
newStakeAccountPositions: PublicKey | undefined,
676652
) {
677-
this.assertWallet();
678653
const instruction = await this.integrityPoolProgram.methods
679654
.setPublisherStakeAccount()
680655
.accounts({

0 commit comments

Comments
 (0)