Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/staking/src/hooks/use-api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const State = {
onCreateAccount: (newAccount: PublicKey) => Promise<void>,
) => ({
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);
Expand Down
39 changes: 7 additions & 32 deletions governance/pyth_staking_sdk/src/pyth-staking-client.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -15,6 +15,7 @@ import {
import type { AnchorWallet } from "@solana/wallet-adapter-react";
import {
Connection,
Keypair,
PublicKey,
SystemProgram,
Transaction,
Expand Down Expand Up @@ -59,24 +60,19 @@ export type PythStakingClientConfig = {

export class PythStakingClient {
connection: Connection;
wallet: AnchorWallet | undefined;
wallet: AnchorWallet;
provider: AnchorProvider;
stakingProgram: Program<Staking>;
integrityPoolProgram: Program<IntegrityPool>;
publisherCapsProgram: Program<PublisherCaps>;

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,
Expand All @@ -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)
Expand All @@ -114,7 +103,6 @@ export class PythStakingClient {

/** Gets a users stake accounts */
public async getAllStakeAccountPositions(): Promise<PublicKey[]> {
this.assertWallet();
const positionDataMemcmp = this.stakingProgram.coder.accounts.memcmp(
"positionData",
) as {
Expand Down Expand Up @@ -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)
Expand All @@ -202,7 +189,6 @@ export class PythStakingClient {
}

public async getOwnerPythAtaAccount(): Promise<Account> {
this.assertWallet();
const globalConfig = await this.getGlobalConfig();
return getAccount(
this.connection,
Expand Down Expand Up @@ -242,7 +228,6 @@ export class PythStakingClient {
stakeAccountPositions: PublicKey,
amount: bigint,
) {
this.assertWallet();
const instruction = await this.stakingProgram.methods
.createPosition(
{
Expand All @@ -263,7 +248,6 @@ export class PythStakingClient {
positionState: PositionState.LOCKED | PositionState.LOCKING,
amount: bigint,
) {
this.assertWallet();
const stakeAccountPositionsData = await this.getStakeAccountPositions(
stakeAccountPositions,
);
Expand Down Expand Up @@ -319,7 +303,6 @@ export class PythStakingClient {
positionState: PositionState.LOCKED | PositionState.LOCKING,
amount: bigint,
) {
this.assertWallet();
const stakeAccountPositionsData = await this.getStakeAccountPositions(
stakeAccountPositions,
);
Expand Down Expand Up @@ -372,7 +355,6 @@ export class PythStakingClient {
}

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

public async createStakeAccountAndDeposit(amount: bigint) {
this.assertWallet();
const globalConfig = await this.getGlobalConfig();

const senderTokenAccount = await getAssociatedTokenAddress(
Expand Down Expand Up @@ -470,7 +451,6 @@ export class PythStakingClient {
stakeAccountPositions: PublicKey,
amount: bigint,
) {
this.assertWallet();
const globalConfig = await this.getGlobalConfig();
const mint = globalConfig.pythTokenMint;

Expand All @@ -493,7 +473,6 @@ export class PythStakingClient {
stakeAccountPositions: PublicKey,
amount: bigint,
) {
this.assertWallet();
const globalConfig = await this.getGlobalConfig();
const mint = globalConfig.pythTokenMint;

Expand All @@ -518,7 +497,6 @@ export class PythStakingClient {
publisher: PublicKey,
amount: bigint,
) {
this.assertWallet();
const instruction = await this.integrityPoolProgram.methods
.delegate(convertBigIntToBN(amount))
.accounts({
Expand Down Expand Up @@ -556,7 +534,6 @@ export class PythStakingClient {
async getAdvanceDelegationRecordInstructions(
stakeAccountPositions: PublicKey,
) {
this.assertWallet();
const poolData = await this.getPoolDataAccount();
const stakeAccountPositionsData = await this.getStakeAccountPositions(
stakeAccountPositions,
Expand Down Expand Up @@ -612,7 +589,6 @@ export class PythStakingClient {
}

public async advanceDelegationRecord(stakeAccountPositions: PublicKey) {
this.assertWallet();
const instructions = await this.getAdvanceDelegationRecordInstructions(
stakeAccountPositions,
);
Expand Down Expand Up @@ -674,7 +650,6 @@ export class PythStakingClient {
stakeAccountPositions: PublicKey,
newStakeAccountPositions: PublicKey | undefined,
) {
this.assertWallet();
const instruction = await this.integrityPoolProgram.methods
.setPublisherStakeAccount()
.accounts({
Expand Down
Loading