Skip to content

Commit d235d0b

Browse files
committed
fix: use key with sol for simulations
1 parent b895fb4 commit d235d0b

File tree

5 files changed

+23
-10
lines changed

5 files changed

+23
-10
lines changed

apps/staking/src/api.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export const loadData = async (
106106
pythnetClient: PythnetClient,
107107
hermesClient: HermesClient,
108108
stakeAccount?: PublicKey | undefined,
109+
simulationPayer?: PublicKey | undefined,
109110
): Promise<Data> =>
110111
stakeAccount === undefined
111112
? loadDataNoStakeAccount(client, pythnetClient, hermesClient)
@@ -114,6 +115,7 @@ export const loadData = async (
114115
pythnetClient,
115116
hermesClient,
116117
stakeAccount,
118+
simulationPayer,
117119
);
118120

119121
const loadDataNoStakeAccount = async (
@@ -149,6 +151,7 @@ const loadDataForStakeAccount = async (
149151
pythnetClient: PythnetClient,
150152
hermesClient: HermesClient,
151153
stakeAccount: PublicKey,
154+
simulationPayer?: PublicKey,
152155
): Promise<Data> => {
153156
const [
154157
{ publishers, ...baseInfo },
@@ -160,7 +163,7 @@ const loadDataForStakeAccount = async (
160163
loadBaseInfo(client, pythnetClient, hermesClient),
161164
client.getStakeAccountCustody(stakeAccount),
162165
client.getUnlockSchedule(stakeAccount),
163-
client.getClaimableRewards(stakeAccount),
166+
client.getClaimableRewards(stakeAccount, simulationPayer),
164167
client.getStakeAccountPositions(stakeAccount),
165168
]);
166169

apps/staking/src/components/Root/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
MAINNET_RPC,
1515
HERMES_URL,
1616
PYTHNET_RPC,
17+
SIMULATION_PAYER,
1718
} from "../../config/server";
1819
import { ApiProvider } from "../../hooks/use-api";
1920
import { LoggerProvider } from "../../hooks/use-logger";
@@ -27,6 +28,7 @@ import { MaxWidth } from "../MaxWidth";
2728
import { ReportAccessibility } from "../ReportAccessibility";
2829
import { RouterProvider } from "../RouterProvider";
2930
import { WalletProvider } from "../WalletProvider";
31+
import { PublicKey } from "@solana/web3.js";
3032

3133
const redHatText = Red_Hat_Text({
3234
subsets: ["latin"],
@@ -82,7 +84,7 @@ const HtmlWithProviders = ({ lang, ...props }: HTMLProps<HTMLHtmlElement>) => (
8284
walletConnectProjectId={WALLETCONNECT_PROJECT_ID}
8385
mainnetRpc={MAINNET_RPC}
8486
>
85-
<ApiProvider hermesUrl={HERMES_URL} pythnetRpcUrl={PYTHNET_RPC}>
87+
<ApiProvider hermesUrl={HERMES_URL} pythnetRpcUrl={PYTHNET_RPC} simulationPayer={SIMULATION_PAYER}>
8688
<ToastProvider>
8789
<html lang={lang} {...props} />
8890
</ToastProvider>

apps/staking/src/config/server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ export const GOVERNANCE_ONLY_REGIONS = transformOr(
7474
[],
7575
);
7676
export const PROXYCHECK_API_KEY = demandInProduction("PROXYCHECK_API_KEY");
77-
77+
// 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
78+
// such simulation fails when the payer has no funds
79+
export const SIMULATION_PAYER = process.env.SIMULATION_PAYER
7880
class MissingEnvironmentError extends Error {
7981
constructor(name: string) {
8082
super(`Missing environment variable: ${name}!`);

apps/staking/src/hooks/use-api.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { HermesClient } from "@pythnetwork/hermes-client";
44
import { PythnetClient, PythStakingClient } from "@pythnetwork/staking-sdk";
55
import { useLocalStorageValue } from "@react-hookz/web";
66
import { useConnection, useWallet } from "@solana/wallet-adapter-react";
7-
import { Connection, type PublicKey } from "@solana/web3.js";
7+
import { Connection, PublicKey } from "@solana/web3.js";
88
import { type ComponentProps, createContext, useContext, useMemo } from "react";
99
import { useSWRConfig } from "swr";
1010

@@ -65,6 +65,7 @@ const State = {
6565
pythnetClient: PythnetClient,
6666
hermesClient: HermesClient,
6767
account: PublicKey,
68+
simulationPayer: string | undefined,
6869
allAccounts: [PublicKey, ...PublicKey[]],
6970
selectAccount: (account: PublicKey) => void,
7071
mutate: ReturnType<typeof useSWRConfig>["mutate"],
@@ -95,7 +96,7 @@ const State = {
9596
dashboardDataCacheKey,
9697

9798
loadData: () =>
98-
api.loadData(client, pythnetClient, hermesClient, account),
99+
api.loadData(client, pythnetClient, hermesClient, account, simulationPayer ? new PublicKey(simulationPayer) : undefined),
99100

100101
claim: bindApi(api.claim),
101102
deposit: bindApi(api.deposit),
@@ -131,19 +132,21 @@ type ApiProviderProps = Omit<
131132
> & {
132133
pythnetRpcUrl: string;
133134
hermesUrl: string;
135+
simulationPayer: string | undefined;
134136
};
135137

136138
export const ApiProvider = ({
137139
hermesUrl,
138140
pythnetRpcUrl,
141+
simulationPayer,
139142
...props
140143
}: ApiProviderProps) => {
141-
const state = useApiContext(hermesUrl, pythnetRpcUrl);
144+
const state = useApiContext(hermesUrl, pythnetRpcUrl, simulationPayer);
142145

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

146-
const useApiContext = (hermesUrl: string, pythnetRpcUrl: string) => {
149+
const useApiContext = (hermesUrl: string, pythnetRpcUrl: string, simulationPayer: string | undefined) => {
147150
const wallet = useWallet();
148151
const { connection } = useConnection();
149152
const { isMainnet } = useNetwork();
@@ -235,6 +238,7 @@ const useApiContext = (hermesUrl: string, pythnetRpcUrl: string) => {
235238
pythnetClient,
236239
hermesClient,
237240
selectedAccount ?? firstAccount,
241+
simulationPayer,
238242
[firstAccount, ...otherAccounts],
239243
(account: PublicKey) => {
240244
localStorageValue.set(account.toBase58());

governance/pyth_staking_sdk/src/pyth-staking-client.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@ export class PythStakingClient {
688688

689689
async getAdvanceDelegationRecordInstructions(
690690
stakeAccountPositions: PublicKey,
691+
payer?: PublicKey
691692
) {
692693
const poolData = await this.getPoolDataAccount();
693694
const stakeAccountPositionsData = await this.getStakeAccountPositions(
@@ -745,7 +746,7 @@ export class PythStakingClient {
745746
this.integrityPoolProgram.methods
746747
.advanceDelegationRecord()
747748
.accountsPartial({
748-
payer: this.wallet.publicKey,
749+
payer: payer ?? this.wallet.publicKey,
749750
publisher: pubkey,
750751
publisherStakeAccountPositions: stakeAccount,
751752
publisherStakeAccountCustody: stakeAccount
@@ -795,16 +796,17 @@ export class PythStakingClient {
795796
);
796797
}
797798

798-
public async getClaimableRewards(stakeAccountPositions: PublicKey) {
799+
public async getClaimableRewards(stakeAccountPositions: PublicKey, simulationPayer?: PublicKey) {
799800
const instructions = await this.getAdvanceDelegationRecordInstructions(
800801
stakeAccountPositions,
802+
simulationPayer
801803
);
802804

803805
let totalRewards = 0n;
804806

805807
for (const instruction of instructions.advanceDelegationRecordInstructions) {
806808
const tx = new Transaction().add(instruction);
807-
tx.feePayer = this.wallet.publicKey;
809+
tx.feePayer = simulationPayer ?? this.wallet.publicKey;
808810
const res = await this.connection.simulateTransaction(tx);
809811
const val = res.value.returnData?.data[0];
810812
if (val === undefined) {

0 commit comments

Comments
 (0)