diff --git a/apps/staking/src/hooks/use-api.tsx b/apps/staking/src/hooks/use-api.tsx index 19cfd51acb..7e744c5114 100644 --- a/apps/staking/src/hooks/use-api.tsx +++ b/apps/staking/src/hooks/use-api.tsx @@ -45,7 +45,7 @@ const State = { onCreateAccount: (newAccount: PublicKey) => Promise, ) => ({ type: StateType.LoadedNoStakeAccount as const, - dashboardDataCacheKey: client.wallet?.publicKey.toBase58(), + dashboardDataCacheKey: client.wallet.publicKey.toBase58(), loadData: () => api.loadData(client, hermesClient), deposit: async (amount: bigint) => { const account = await api.createStakeAccountAndDeposit(client, amount); diff --git a/governance/pyth_staking_sdk/src/pyth-staking-client.ts b/governance/pyth_staking_sdk/src/pyth-staking-client.ts index 69e403726f..c31c7b0356 100644 --- a/governance/pyth_staking_sdk/src/pyth-staking-client.ts +++ b/governance/pyth_staking_sdk/src/pyth-staking-client.ts @@ -1,6 +1,6 @@ import * as crypto from "crypto"; -import { AnchorProvider, BN, Program } from "@coral-xyz/anchor"; +import { AnchorProvider, BN, Program, Wallet } from "@coral-xyz/anchor"; import { getTokenOwnerRecordAddress, PROGRAM_VERSION_V2, @@ -15,6 +15,7 @@ import { import type { AnchorWallet } from "@solana/wallet-adapter-react"; import { Connection, + Keypair, PublicKey, SystemProgram, Transaction, @@ -59,7 +60,7 @@ export type PythStakingClientConfig = { export class PythStakingClient { connection: Connection; - wallet: AnchorWallet | undefined; + wallet: AnchorWallet; provider: AnchorProvider; stakingProgram: Program; integrityPoolProgram: Program; @@ -67,16 +68,11 @@ export class PythStakingClient { constructor(config: PythStakingClientConfig) { this.connection = config.connection; - this.wallet = config.wallet; + this.wallet = config.wallet ?? new Wallet(Keypair.generate()); - // {} as AnchorWallet is a workaround for AnchorProvider requiring a wallet - this.provider = new AnchorProvider( - this.connection, - this.wallet ?? ({} as AnchorWallet), - { - skipPreflight: true, - }, - ); + this.provider = new AnchorProvider(this.connection, this.wallet, { + skipPreflight: true, + }); this.stakingProgram = new Program(StakingIdl as Staking, this.provider); this.integrityPoolProgram = new Program( IntegrityPoolIdl as IntegrityPool, @@ -88,14 +84,7 @@ export class PythStakingClient { ); } - private assertWallet(): asserts this is { wallet: AnchorWallet } { - if (this.wallet === undefined) { - throw new Error("Wallet not set"); - } - } - async initGlobalConfig(config: GlobalConfig) { - this.assertWallet(); const globalConfigAnchor = convertBigIntToBN(config); const instruction = await this.stakingProgram.methods .initConfig(globalConfigAnchor) @@ -114,7 +103,6 @@ export class PythStakingClient { /** Gets a users stake accounts */ public async getAllStakeAccountPositions(): Promise { - this.assertWallet(); const positionDataMemcmp = this.stakingProgram.coder.accounts.memcmp( "positionData", ) as { @@ -188,7 +176,6 @@ export class PythStakingClient { poolData: PublicKey; y: bigint; }) { - this.assertWallet(); const yAnchor = convertBigIntToBN(y); const instruction = await this.integrityPoolProgram.methods .initializePool(rewardProgramAuthority, yAnchor) @@ -202,7 +189,6 @@ export class PythStakingClient { } public async getOwnerPythAtaAccount(): Promise { - this.assertWallet(); const globalConfig = await this.getGlobalConfig(); return getAccount( this.connection, @@ -242,7 +228,6 @@ export class PythStakingClient { stakeAccountPositions: PublicKey, amount: bigint, ) { - this.assertWallet(); const instruction = await this.stakingProgram.methods .createPosition( { @@ -263,7 +248,6 @@ export class PythStakingClient { positionState: PositionState.LOCKED | PositionState.LOCKING, amount: bigint, ) { - this.assertWallet(); const stakeAccountPositionsData = await this.getStakeAccountPositions( stakeAccountPositions, ); @@ -319,7 +303,6 @@ export class PythStakingClient { positionState: PositionState.LOCKED | PositionState.LOCKING, amount: bigint, ) { - this.assertWallet(); const stakeAccountPositionsData = await this.getStakeAccountPositions( stakeAccountPositions, ); @@ -372,7 +355,6 @@ export class PythStakingClient { } public async hasGovernanceRecord(config: GlobalConfig): Promise { - this.assertWallet(); const tokenOwnerRecordAddress = await getTokenOwnerRecordAddress( GOVERNANCE_ADDRESS, config.pythGovernanceRealm, @@ -388,7 +370,6 @@ export class PythStakingClient { } public async createStakeAccountAndDeposit(amount: bigint) { - this.assertWallet(); const globalConfig = await this.getGlobalConfig(); const senderTokenAccount = await getAssociatedTokenAddress( @@ -470,7 +451,6 @@ export class PythStakingClient { stakeAccountPositions: PublicKey, amount: bigint, ) { - this.assertWallet(); const globalConfig = await this.getGlobalConfig(); const mint = globalConfig.pythTokenMint; @@ -493,7 +473,6 @@ export class PythStakingClient { stakeAccountPositions: PublicKey, amount: bigint, ) { - this.assertWallet(); const globalConfig = await this.getGlobalConfig(); const mint = globalConfig.pythTokenMint; @@ -518,7 +497,6 @@ export class PythStakingClient { publisher: PublicKey, amount: bigint, ) { - this.assertWallet(); const instruction = await this.integrityPoolProgram.methods .delegate(convertBigIntToBN(amount)) .accounts({ @@ -556,7 +534,6 @@ export class PythStakingClient { async getAdvanceDelegationRecordInstructions( stakeAccountPositions: PublicKey, ) { - this.assertWallet(); const poolData = await this.getPoolDataAccount(); const stakeAccountPositionsData = await this.getStakeAccountPositions( stakeAccountPositions, @@ -612,7 +589,6 @@ export class PythStakingClient { } public async advanceDelegationRecord(stakeAccountPositions: PublicKey) { - this.assertWallet(); const instructions = await this.getAdvanceDelegationRecordInstructions( stakeAccountPositions, ); @@ -674,7 +650,6 @@ export class PythStakingClient { stakeAccountPositions: PublicKey, newStakeAccountPositions: PublicKey | undefined, ) { - this.assertWallet(); const instruction = await this.integrityPoolProgram.methods .setPublisherStakeAccount() .accounts({