Skip to content

Commit ee3249d

Browse files
feat(staking): make wallet optional in staking sdk (#1932)
* feat(staking): make wallet optional in staking sdk * fix * fix * fix * fix
1 parent 8baf597 commit ee3249d

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

apps/staking/src/api.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ export type AccountHistory = {
150150

151151
export const getAllStakeAccountAddresses = async (
152152
client: PythStakingClient,
153-
): Promise<PublicKey[]> =>
154-
client.getAllStakeAccountPositions(client.wallet.publicKey);
153+
): Promise<PublicKey[]> => client.getAllStakeAccountPositions();
155154

156155
export const getStakeAccount = async (
157156
client: PythStakingClient,

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: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ import type { Staking } from "../types/staking";
5454

5555
export type PythStakingClientConfig = {
5656
connection: Connection;
57-
wallet: AnchorWallet;
57+
wallet: AnchorWallet | undefined;
5858
};
5959

6060
export class PythStakingClient {
6161
connection: Connection;
62-
wallet: AnchorWallet;
62+
wallet: AnchorWallet | undefined;
6363
provider: AnchorProvider;
6464
stakingProgram: Program<Staking>;
6565
integrityPoolProgram: Program<IntegrityPool>;
@@ -68,9 +68,15 @@ export class PythStakingClient {
6868
constructor(config: PythStakingClientConfig) {
6969
this.connection = config.connection;
7070
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+
);
7480
this.stakingProgram = new Program(StakingIdl as Staking, this.provider);
7581
this.integrityPoolProgram = new Program(
7682
IntegrityPoolIdl as IntegrityPool,
@@ -82,7 +88,14 @@ export class PythStakingClient {
8288
);
8389
}
8490

91+
private assertWallet(): asserts this is { wallet: AnchorWallet } {
92+
if (this.wallet === undefined) {
93+
throw new Error("Wallet not set");
94+
}
95+
}
96+
8597
async initGlobalConfig(config: GlobalConfig) {
98+
this.assertWallet();
8699
const globalConfigAnchor = convertBigIntToBN(config);
87100
const instruction = await this.stakingProgram.methods
88101
.initConfig(globalConfigAnchor)
@@ -100,9 +113,8 @@ export class PythStakingClient {
100113
}
101114

102115
/** Gets a users stake accounts */
103-
public async getAllStakeAccountPositions(
104-
user: PublicKey,
105-
): Promise<PublicKey[]> {
116+
public async getAllStakeAccountPositions(): Promise<PublicKey[]> {
117+
this.assertWallet();
106118
const positionDataMemcmp = this.stakingProgram.coder.accounts.memcmp(
107119
"positionData",
108120
) as {
@@ -121,7 +133,7 @@ export class PythStakingClient {
121133
{
122134
memcmp: {
123135
offset: 8,
124-
bytes: user.toBase58(),
136+
bytes: this.wallet.publicKey.toBase58(),
125137
},
126138
},
127139
],
@@ -176,6 +188,7 @@ export class PythStakingClient {
176188
poolData: PublicKey;
177189
y: bigint;
178190
}) {
191+
this.assertWallet();
179192
const yAnchor = convertBigIntToBN(y);
180193
const instruction = await this.integrityPoolProgram.methods
181194
.initializePool(rewardProgramAuthority, yAnchor)
@@ -189,6 +202,7 @@ export class PythStakingClient {
189202
}
190203

191204
public async getOwnerPythAtaAccount(): Promise<Account> {
205+
this.assertWallet();
192206
const globalConfig = await this.getGlobalConfig();
193207
return getAccount(
194208
this.connection,
@@ -228,6 +242,7 @@ export class PythStakingClient {
228242
stakeAccountPositions: PublicKey,
229243
amount: bigint,
230244
) {
245+
this.assertWallet();
231246
const instruction = await this.stakingProgram.methods
232247
.createPosition(
233248
{
@@ -248,6 +263,7 @@ export class PythStakingClient {
248263
positionState: PositionState.LOCKED | PositionState.LOCKING,
249264
amount: bigint,
250265
) {
266+
this.assertWallet();
251267
const stakeAccountPositionsData = await this.getStakeAccountPositions(
252268
stakeAccountPositions,
253269
);
@@ -303,6 +319,7 @@ export class PythStakingClient {
303319
positionState: PositionState.LOCKED | PositionState.LOCKING,
304320
amount: bigint,
305321
) {
322+
this.assertWallet();
306323
const stakeAccountPositionsData = await this.getStakeAccountPositions(
307324
stakeAccountPositions,
308325
);
@@ -355,6 +372,7 @@ export class PythStakingClient {
355372
}
356373

357374
public async hasGovernanceRecord(config: GlobalConfig): Promise<boolean> {
375+
this.assertWallet();
358376
const tokenOwnerRecordAddress = await getTokenOwnerRecordAddress(
359377
GOVERNANCE_ADDRESS,
360378
config.pythGovernanceRealm,
@@ -370,6 +388,7 @@ export class PythStakingClient {
370388
}
371389

372390
public async createStakeAccountAndDeposit(amount: bigint) {
391+
this.assertWallet();
373392
const globalConfig = await this.getGlobalConfig();
374393

375394
const senderTokenAccount = await getAssociatedTokenAddress(
@@ -451,6 +470,7 @@ export class PythStakingClient {
451470
stakeAccountPositions: PublicKey,
452471
amount: bigint,
453472
) {
473+
this.assertWallet();
454474
const globalConfig = await this.getGlobalConfig();
455475
const mint = globalConfig.pythTokenMint;
456476

@@ -473,6 +493,7 @@ export class PythStakingClient {
473493
stakeAccountPositions: PublicKey,
474494
amount: bigint,
475495
) {
496+
this.assertWallet();
476497
const globalConfig = await this.getGlobalConfig();
477498
const mint = globalConfig.pythTokenMint;
478499

@@ -497,6 +518,7 @@ export class PythStakingClient {
497518
publisher: PublicKey,
498519
amount: bigint,
499520
) {
521+
this.assertWallet();
500522
const instruction = await this.integrityPoolProgram.methods
501523
.delegate(convertBigIntToBN(amount))
502524
.accounts({
@@ -534,6 +556,7 @@ export class PythStakingClient {
534556
async getAdvanceDelegationRecordInstructions(
535557
stakeAccountPositions: PublicKey,
536558
) {
559+
this.assertWallet();
537560
const poolData = await this.getPoolDataAccount();
538561
const stakeAccountPositionsData = await this.getStakeAccountPositions(
539562
stakeAccountPositions,
@@ -589,6 +612,7 @@ export class PythStakingClient {
589612
}
590613

591614
public async advanceDelegationRecord(stakeAccountPositions: PublicKey) {
615+
this.assertWallet();
592616
const instructions = await this.getAdvanceDelegationRecordInstructions(
593617
stakeAccountPositions,
594618
);
@@ -612,7 +636,7 @@ export class PythStakingClient {
612636

613637
for (const instruction of instructions.advanceDelegationRecordInstructions) {
614638
const tx = new Transaction().add(instruction);
615-
tx.feePayer = this.wallet.publicKey;
639+
tx.feePayer = PublicKey.default;
616640
const res = await this.connection.simulateTransaction(tx);
617641
const val = res.value.returnData?.data[0];
618642
if (val === undefined) {
@@ -650,6 +674,7 @@ export class PythStakingClient {
650674
stakeAccountPositions: PublicKey,
651675
newStakeAccountPositions: PublicKey | undefined,
652676
) {
677+
this.assertWallet();
653678
const instruction = await this.integrityPoolProgram.methods
654679
.setPublisherStakeAccount()
655680
.accounts({

0 commit comments

Comments
 (0)