Skip to content

Commit

Permalink
fix: use key with sol for simulations
Browse files Browse the repository at this point in the history
  • Loading branch information
guibescos committed Jan 31, 2025
1 parent b895fb4 commit d235d0b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
5 changes: 4 additions & 1 deletion apps/staking/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export const loadData = async (
pythnetClient: PythnetClient,
hermesClient: HermesClient,
stakeAccount?: PublicKey | undefined,
simulationPayer?: PublicKey | undefined,
): Promise<Data> =>
stakeAccount === undefined
? loadDataNoStakeAccount(client, pythnetClient, hermesClient)
Expand All @@ -114,6 +115,7 @@ export const loadData = async (
pythnetClient,
hermesClient,
stakeAccount,
simulationPayer,
);

const loadDataNoStakeAccount = async (
Expand Down Expand Up @@ -149,6 +151,7 @@ const loadDataForStakeAccount = async (
pythnetClient: PythnetClient,
hermesClient: HermesClient,
stakeAccount: PublicKey,
simulationPayer?: PublicKey,
): Promise<Data> => {
const [
{ publishers, ...baseInfo },
Expand All @@ -160,7 +163,7 @@ const loadDataForStakeAccount = async (
loadBaseInfo(client, pythnetClient, hermesClient),
client.getStakeAccountCustody(stakeAccount),
client.getUnlockSchedule(stakeAccount),
client.getClaimableRewards(stakeAccount),
client.getClaimableRewards(stakeAccount, simulationPayer),
client.getStakeAccountPositions(stakeAccount),
]);

Expand Down
4 changes: 3 additions & 1 deletion apps/staking/src/components/Root/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
MAINNET_RPC,
HERMES_URL,
PYTHNET_RPC,
SIMULATION_PAYER,
} from "../../config/server";
import { ApiProvider } from "../../hooks/use-api";
import { LoggerProvider } from "../../hooks/use-logger";
Expand All @@ -27,6 +28,7 @@ import { MaxWidth } from "../MaxWidth";
import { ReportAccessibility } from "../ReportAccessibility";
import { RouterProvider } from "../RouterProvider";
import { WalletProvider } from "../WalletProvider";
import { PublicKey } from "@solana/web3.js";

const redHatText = Red_Hat_Text({
subsets: ["latin"],
Expand Down Expand Up @@ -82,7 +84,7 @@ const HtmlWithProviders = ({ lang, ...props }: HTMLProps<HTMLHtmlElement>) => (
walletConnectProjectId={WALLETCONNECT_PROJECT_ID}
mainnetRpc={MAINNET_RPC}
>
<ApiProvider hermesUrl={HERMES_URL} pythnetRpcUrl={PYTHNET_RPC}>
<ApiProvider hermesUrl={HERMES_URL} pythnetRpcUrl={PYTHNET_RPC} simulationPayer={SIMULATION_PAYER}>
<ToastProvider>
<html lang={lang} {...props} />
</ToastProvider>
Expand Down
4 changes: 3 additions & 1 deletion apps/staking/src/config/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ export const GOVERNANCE_ONLY_REGIONS = transformOr(
[],
);
export const PROXYCHECK_API_KEY = demandInProduction("PROXYCHECK_API_KEY");

// This needs to be a public key that has SOL in it all the time, it will be used in the simulation to compute the claimable rewards
// such simulation fails when the payer has no funds
export const SIMULATION_PAYER = process.env.SIMULATION_PAYER
class MissingEnvironmentError extends Error {
constructor(name: string) {
super(`Missing environment variable: ${name}!`);
Expand Down
12 changes: 8 additions & 4 deletions apps/staking/src/hooks/use-api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { HermesClient } from "@pythnetwork/hermes-client";
import { PythnetClient, PythStakingClient } from "@pythnetwork/staking-sdk";
import { useLocalStorageValue } from "@react-hookz/web";
import { useConnection, useWallet } from "@solana/wallet-adapter-react";
import { Connection, type PublicKey } from "@solana/web3.js";
import { Connection, PublicKey } from "@solana/web3.js";
import { type ComponentProps, createContext, useContext, useMemo } from "react";
import { useSWRConfig } from "swr";

Expand Down Expand Up @@ -65,6 +65,7 @@ const State = {
pythnetClient: PythnetClient,
hermesClient: HermesClient,
account: PublicKey,
simulationPayer: string | undefined,
allAccounts: [PublicKey, ...PublicKey[]],
selectAccount: (account: PublicKey) => void,
mutate: ReturnType<typeof useSWRConfig>["mutate"],
Expand Down Expand Up @@ -95,7 +96,7 @@ const State = {
dashboardDataCacheKey,

loadData: () =>
api.loadData(client, pythnetClient, hermesClient, account),
api.loadData(client, pythnetClient, hermesClient, account, simulationPayer ? new PublicKey(simulationPayer) : undefined),

claim: bindApi(api.claim),
deposit: bindApi(api.deposit),
Expand Down Expand Up @@ -131,19 +132,21 @@ type ApiProviderProps = Omit<
> & {
pythnetRpcUrl: string;
hermesUrl: string;
simulationPayer: string | undefined;
};

export const ApiProvider = ({
hermesUrl,
pythnetRpcUrl,
simulationPayer,
...props
}: ApiProviderProps) => {
const state = useApiContext(hermesUrl, pythnetRpcUrl);
const state = useApiContext(hermesUrl, pythnetRpcUrl, simulationPayer);

return <ApiContext.Provider value={state} {...props} />;
};

const useApiContext = (hermesUrl: string, pythnetRpcUrl: string) => {
const useApiContext = (hermesUrl: string, pythnetRpcUrl: string, simulationPayer: string | undefined) => {
const wallet = useWallet();
const { connection } = useConnection();
const { isMainnet } = useNetwork();
Expand Down Expand Up @@ -235,6 +238,7 @@ const useApiContext = (hermesUrl: string, pythnetRpcUrl: string) => {
pythnetClient,
hermesClient,
selectedAccount ?? firstAccount,
simulationPayer,
[firstAccount, ...otherAccounts],
(account: PublicKey) => {
localStorageValue.set(account.toBase58());
Expand Down
8 changes: 5 additions & 3 deletions governance/pyth_staking_sdk/src/pyth-staking-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ export class PythStakingClient {

async getAdvanceDelegationRecordInstructions(
stakeAccountPositions: PublicKey,
payer?: PublicKey
) {
const poolData = await this.getPoolDataAccount();
const stakeAccountPositionsData = await this.getStakeAccountPositions(
Expand Down Expand Up @@ -745,7 +746,7 @@ export class PythStakingClient {
this.integrityPoolProgram.methods
.advanceDelegationRecord()
.accountsPartial({
payer: this.wallet.publicKey,
payer: payer ?? this.wallet.publicKey,
publisher: pubkey,
publisherStakeAccountPositions: stakeAccount,
publisherStakeAccountCustody: stakeAccount
Expand Down Expand Up @@ -795,16 +796,17 @@ export class PythStakingClient {
);
}

public async getClaimableRewards(stakeAccountPositions: PublicKey) {
public async getClaimableRewards(stakeAccountPositions: PublicKey, simulationPayer?: PublicKey) {
const instructions = await this.getAdvanceDelegationRecordInstructions(
stakeAccountPositions,
simulationPayer
);

let totalRewards = 0n;

for (const instruction of instructions.advanceDelegationRecordInstructions) {
const tx = new Transaction().add(instruction);
tx.feePayer = this.wallet.publicKey;
tx.feePayer = simulationPayer ?? this.wallet.publicKey;
const res = await this.connection.simulateTransaction(tx);
const val = res.value.returnData?.data[0];
if (val === undefined) {
Expand Down

0 comments on commit d235d0b

Please sign in to comment.