Skip to content
Merged
Changes from 1 commit
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
29 changes: 26 additions & 3 deletions governance/pyth_staking_sdk/src/pyth-staking-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Staking>;
integrityPoolProgram: Program<IntegrityPool>;
Expand All @@ -68,7 +68,9 @@ export class PythStakingClient {
constructor(config: PythStakingClientConfig) {
this.connection = config.connection;
this.wallet = config.wallet;
this.provider = new AnchorProvider(this.connection, this.wallet, {

// {} as AnchorWallet is a workaround for AnchorProvider requiring a wallet
this.provider = new AnchorProvider(this.connection, this.wallet ?? {} as AnchorWallet, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does {} === undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, but the check is for this.wallet not the {} in the provider.

skipPreflight: true,
});
this.stakingProgram = new Program(StakingIdl as Staking, this.provider);
Expand All @@ -82,7 +84,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)
Expand Down Expand Up @@ -176,6 +185,7 @@ export class PythStakingClient {
poolData: PublicKey;
y: bigint;
}) {
this.assertWallet();
const yAnchor = convertBigIntToBN(y);
const instruction = await this.integrityPoolProgram.methods
.initializePool(rewardProgramAuthority, yAnchor)
Expand All @@ -189,6 +199,7 @@ export class PythStakingClient {
}

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

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

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

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

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

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

public async advanceDelegationRecord(stakeAccountPositions: PublicKey) {
this.assertWallet();
const instructions = await this.getAdvanceDelegationRecordInstructions(
stakeAccountPositions,
);
Expand All @@ -604,6 +625,7 @@ export class PythStakingClient {
}

public async getClaimableRewards(stakeAccountPositions: PublicKey) {
this.assertWallet();
Copy link
Contributor

@guibescos guibescos Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this one require a wallet?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's using this.wallet.publicKey but since it's only the simulation i think we can replace this with any pubkey.

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