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
7 changes: 3 additions & 4 deletions apps/staking/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,10 @@ export const loadAccountHistory = async (
};

export const createStakeAccountAndDeposit = async (
_client: PythStakingClient,
_amount: bigint,
client: PythStakingClient,
amount: bigint,
): Promise<PublicKey> => {
await new Promise((resolve) => setTimeout(resolve, MOCK_DELAY));
throw new NotImplementedError();
return client.createStakeAccountAndDeposit(amount);
};

export const deposit = async (
Expand Down
1 change: 1 addition & 0 deletions governance/pyth_staking_sdk/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default [
rules: {
"n/no-unpublished-import": "off",
"unicorn/no-null": "off",
"unicorn/prefer-node-protocol": "off",
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand this

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 asking to import node:crypto instead of crypto which doesn't work for our setup.

},
},
];
1 change: 1 addition & 0 deletions governance/pyth_staking_sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"dependencies": {
"@coral-xyz/anchor": "^0.30.1",
"@pythnetwork/solana-utils": "workspace:*",
"@solana/spl-governance": "^0.3.28",
"@solana/spl-token": "^0.3.7",
"@solana/web3.js": "^1.95.3"
}
Expand Down
5 changes: 5 additions & 0 deletions governance/pyth_staking_sdk/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const FRACTION_PRECISION_N = 1_000_000n;

export const POSITION_BUFFER_SIZE = 200;
export const POSITIONS_ACCOUNT_HEADER_SIZE = DISCRIMINATOR_SIZE + 32;
export const POSITIONS_ACCOUNT_SIZE = DISCRIMINATOR_SIZE + 32;

export const STAKING_PROGRAM_ADDRESS = new PublicKey(
"pytS9TjG1qyAZypk7n8rw8gfW9sUaqqYyMhJQ4E7JCQ",
Expand All @@ -25,3 +26,7 @@ export const INTEGRITY_POOL_PROGRAM_ADDRESS = new PublicKey(
export const PUBLISHER_CAPS_PROGRAM_ADDRESS = new PublicKey(
"pytcD8uUjPxSLMsNqoVnm9dXQw9tKJJf3CQnGwa8oL7",
);

export const GOVERNANCE_ADDRESS = new PublicKey(
"pytGY6tWRgGinSCvRLnSv4fHfBTMoiDGiCsesmHWM6U",
);
102 changes: 102 additions & 0 deletions governance/pyth_staking_sdk/src/pyth-staking-client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import * as crypto from "crypto";

import { AnchorProvider, BN, Program } from "@coral-xyz/anchor";
import {
getTokenOwnerRecordAddress,
PROGRAM_VERSION_V2,
withCreateTokenOwnerRecord,
} from "@solana/spl-governance";
import {
type Account,
createTransferInstruction,
Expand All @@ -9,10 +16,12 @@ import type { AnchorWallet } from "@solana/wallet-adapter-react";
import {
Connection,
PublicKey,
SystemProgram,
Transaction,
TransactionInstruction,
} from "@solana/web3.js";

import { GOVERNANCE_ADDRESS, POSITIONS_ACCOUNT_SIZE } from "./constants";
import {
getConfigAddress,
getPoolConfigAddress,
Expand Down Expand Up @@ -334,6 +343,99 @@ export class PythStakingClient {
return sendTransaction(instructions, this.connection, this.wallet);
}

public async hasGovernanceRecord(config: GlobalConfig): Promise<boolean> {
const tokenOwnerRecordAddress = await getTokenOwnerRecordAddress(
GOVERNANCE_ADDRESS,
config.pythGovernanceRealm,
config.pythTokenMint,
this.wallet.publicKey,
);
const voterAccountInfo =
await this.stakingProgram.provider.connection.getAccountInfo(
tokenOwnerRecordAddress,
);

return Boolean(voterAccountInfo);
}

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

const senderTokenAccount = await getAssociatedTokenAddress(
globalConfig.pythTokenMint,
this.wallet.publicKey,
);

const nonce = crypto.randomBytes(16).toString("hex");
const stakeAccountPositions = await PublicKey.createWithSeed(
this.wallet.publicKey,
nonce,
this.stakingProgram.programId,
);

const minimumBalance =
await this.stakingProgram.provider.connection.getMinimumBalanceForRentExemption(
POSITIONS_ACCOUNT_SIZE,
);

const instructions = [];

instructions.push(
SystemProgram.createAccountWithSeed({
fromPubkey: this.wallet.publicKey,
newAccountPubkey: stakeAccountPositions,
basePubkey: this.wallet.publicKey,
seed: nonce,
lamports: minimumBalance,
space: POSITIONS_ACCOUNT_SIZE,
programId: this.stakingProgram.programId,
}),
await this.stakingProgram.methods
.createStakeAccount(this.wallet.publicKey, { fullyVested: {} })
.accounts({
stakeAccountPositions,
})
.instruction(),
await this.stakingProgram.methods
.createVoterRecord()
.accounts({
stakeAccountPositions,
})
.instruction(),
);

if (!(await this.hasGovernanceRecord(globalConfig))) {
await withCreateTokenOwnerRecord(
instructions,
GOVERNANCE_ADDRESS,
PROGRAM_VERSION_V2,
globalConfig.pythGovernanceRealm,
this.wallet.publicKey,
globalConfig.pythTokenMint,
this.wallet.publicKey,
);
}

instructions.push(
await this.stakingProgram.methods
.joinDaoLlc(globalConfig.agreementHash)
.accounts({
stakeAccountPositions,
})
.instruction(),
createTransferInstruction(
senderTokenAccount,
getStakeAccountCustodyAddress(stakeAccountPositions),
this.wallet.publicKey,
amount,
),
);

await sendTransaction(instructions, this.connection, this.wallet);

return stakeAccountPositions;
}

public async depositTokensToStakeAccountCustody(
stakeAccountPositions: PublicKey,
amount: bigint,
Expand Down
Loading
Loading