diff --git a/apps/staking/src/api.ts b/apps/staking/src/api.ts index d0a3b2b1f8..a1dbd269c6 100644 --- a/apps/staking/src/api.ts +++ b/apps/staking/src/api.ts @@ -150,8 +150,7 @@ export type AccountHistory = { export const getAllStakeAccountAddresses = async ( client: PythStakingClient, -): Promise => - client.getAllStakeAccountPositions(client.wallet.publicKey); +): Promise => client.getAllStakeAccountPositions(); export const getStakeAccount = async ( client: PythStakingClient, diff --git a/apps/staking/src/hooks/use-api.tsx b/apps/staking/src/hooks/use-api.tsx index 7e744c5114..19cfd51acb 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 4fc49d7c84..69e403726f 100644 --- a/governance/pyth_staking_sdk/src/pyth-staking-client.ts +++ b/governance/pyth_staking_sdk/src/pyth-staking-client.ts @@ -54,12 +54,12 @@ import type { Staking } from "../types/staking"; export type PythStakingClientConfig = { connection: Connection; - wallet: AnchorWallet; + wallet: AnchorWallet | undefined; }; export class PythStakingClient { connection: Connection; - wallet: AnchorWallet; + wallet: AnchorWallet | undefined; provider: AnchorProvider; stakingProgram: Program; integrityPoolProgram: Program; @@ -68,9 +68,15 @@ export class PythStakingClient { constructor(config: PythStakingClientConfig) { this.connection = config.connection; this.wallet = config.wallet; - this.provider = new AnchorProvider(this.connection, this.wallet, { - skipPreflight: true, - }); + + // {} as AnchorWallet is a workaround for AnchorProvider requiring a wallet + this.provider = new AnchorProvider( + this.connection, + this.wallet ?? ({} as AnchorWallet), + { + skipPreflight: true, + }, + ); this.stakingProgram = new Program(StakingIdl as Staking, this.provider); this.integrityPoolProgram = new Program( IntegrityPoolIdl as IntegrityPool, @@ -82,7 +88,14 @@ 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) @@ -100,9 +113,8 @@ export class PythStakingClient { } /** Gets a users stake accounts */ - public async getAllStakeAccountPositions( - user: PublicKey, - ): Promise { + public async getAllStakeAccountPositions(): Promise { + this.assertWallet(); const positionDataMemcmp = this.stakingProgram.coder.accounts.memcmp( "positionData", ) as { @@ -121,7 +133,7 @@ export class PythStakingClient { { memcmp: { offset: 8, - bytes: user.toBase58(), + bytes: this.wallet.publicKey.toBase58(), }, }, ], @@ -176,6 +188,7 @@ export class PythStakingClient { poolData: PublicKey; y: bigint; }) { + this.assertWallet(); const yAnchor = convertBigIntToBN(y); const instruction = await this.integrityPoolProgram.methods .initializePool(rewardProgramAuthority, yAnchor) @@ -189,6 +202,7 @@ export class PythStakingClient { } public async getOwnerPythAtaAccount(): Promise { + this.assertWallet(); const globalConfig = await this.getGlobalConfig(); return getAccount( this.connection, @@ -228,6 +242,7 @@ export class PythStakingClient { stakeAccountPositions: PublicKey, amount: bigint, ) { + this.assertWallet(); const instruction = await this.stakingProgram.methods .createPosition( { @@ -248,6 +263,7 @@ export class PythStakingClient { positionState: PositionState.LOCKED | PositionState.LOCKING, amount: bigint, ) { + this.assertWallet(); const stakeAccountPositionsData = await this.getStakeAccountPositions( stakeAccountPositions, ); @@ -303,6 +319,7 @@ export class PythStakingClient { positionState: PositionState.LOCKED | PositionState.LOCKING, amount: bigint, ) { + this.assertWallet(); const stakeAccountPositionsData = await this.getStakeAccountPositions( stakeAccountPositions, ); @@ -355,6 +372,7 @@ export class PythStakingClient { } public async hasGovernanceRecord(config: GlobalConfig): Promise { + this.assertWallet(); const tokenOwnerRecordAddress = await getTokenOwnerRecordAddress( GOVERNANCE_ADDRESS, config.pythGovernanceRealm, @@ -370,6 +388,7 @@ export class PythStakingClient { } public async createStakeAccountAndDeposit(amount: bigint) { + this.assertWallet(); const globalConfig = await this.getGlobalConfig(); const senderTokenAccount = await getAssociatedTokenAddress( @@ -451,6 +470,7 @@ export class PythStakingClient { stakeAccountPositions: PublicKey, amount: bigint, ) { + this.assertWallet(); const globalConfig = await this.getGlobalConfig(); const mint = globalConfig.pythTokenMint; @@ -473,6 +493,7 @@ export class PythStakingClient { stakeAccountPositions: PublicKey, amount: bigint, ) { + this.assertWallet(); const globalConfig = await this.getGlobalConfig(); const mint = globalConfig.pythTokenMint; @@ -497,6 +518,7 @@ export class PythStakingClient { publisher: PublicKey, amount: bigint, ) { + this.assertWallet(); const instruction = await this.integrityPoolProgram.methods .delegate(convertBigIntToBN(amount)) .accounts({ @@ -534,6 +556,7 @@ export class PythStakingClient { async getAdvanceDelegationRecordInstructions( stakeAccountPositions: PublicKey, ) { + this.assertWallet(); const poolData = await this.getPoolDataAccount(); const stakeAccountPositionsData = await this.getStakeAccountPositions( stakeAccountPositions, @@ -589,6 +612,7 @@ export class PythStakingClient { } public async advanceDelegationRecord(stakeAccountPositions: PublicKey) { + this.assertWallet(); const instructions = await this.getAdvanceDelegationRecordInstructions( stakeAccountPositions, ); @@ -612,7 +636,7 @@ export class PythStakingClient { for (const instruction of instructions.advanceDelegationRecordInstructions) { const tx = new Transaction().add(instruction); - tx.feePayer = this.wallet.publicKey; + tx.feePayer = PublicKey.default; const res = await this.connection.simulateTransaction(tx); const val = res.value.returnData?.data[0]; if (val === undefined) { @@ -650,6 +674,7 @@ export class PythStakingClient { stakeAccountPositions: PublicKey, newStakeAccountPositions: PublicKey | undefined, ) { + this.assertWallet(); const instruction = await this.integrityPoolProgram.methods .setPublisherStakeAccount() .accounts({