Skip to content

Commit 52bbbb4

Browse files
feat(staking): reload stake account positions properly (#1914)
1 parent 83ce7a5 commit 52bbbb4

File tree

7 files changed

+65
-62
lines changed

7 files changed

+65
-62
lines changed

apps/staking/src/api.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,21 @@ export type AccountHistory = {
151151
locked: bigint;
152152
}[];
153153

154-
export const getStakeAccounts = async (
154+
export const getAllStakeAccountAddresses = async (
155155
client: PythStakingClient,
156-
): Promise<StakeAccountPositions[]> =>
156+
): Promise<PublicKey[]> =>
157157
client.getAllStakeAccountPositions(client.wallet.publicKey);
158158

159+
export const getStakeAccount = async (
160+
client: PythStakingClient,
161+
stakeAccountAddress: PublicKey,
162+
): Promise<StakeAccountPositions> =>
163+
client.getStakeAccountPositions(stakeAccountAddress);
164+
159165
export const loadData = async (
160166
client: PythStakingClient,
161167
hermesClient: HermesClient,
162-
stakeAccount?: StakeAccountPositions | undefined,
168+
stakeAccount?: PublicKey | undefined,
163169
): Promise<Data> =>
164170
stakeAccount === undefined
165171
? loadDataNoStakeAccount(client, hermesClient)
@@ -191,23 +197,25 @@ const loadDataNoStakeAccount = async (
191197
const loadDataForStakeAccount = async (
192198
client: PythStakingClient,
193199
hermesClient: HermesClient,
194-
stakeAccount: StakeAccountPositions,
200+
stakeAccount: PublicKey,
195201
) => {
196202
const [
197203
{ publishers, ...baseInfo },
198204
stakeAccountCustody,
199205
unlockSchedule,
200206
claimableRewards,
207+
stakeAccountPositions,
201208
] = await Promise.all([
202209
loadBaseInfo(client, hermesClient),
203-
client.getStakeAccountCustody(stakeAccount.address),
204-
client.getUnlockSchedule(stakeAccount.address),
205-
client.getClaimableRewards(stakeAccount.address),
210+
client.getStakeAccountCustody(stakeAccount),
211+
client.getUnlockSchedule(stakeAccount),
212+
client.getClaimableRewards(stakeAccount),
213+
client.getStakeAccountPositions(stakeAccount),
206214
]);
207215

208216
const filterGovernancePositions = (positionState: PositionState) =>
209217
getAmountByTargetAndState({
210-
stakeAccountPositions: stakeAccount,
218+
stakeAccountPositions,
211219
targetWithParameters: { voting: {} },
212220
positionState,
213221
epoch: baseInfo.currentEpoch,
@@ -218,7 +226,7 @@ const loadDataForStakeAccount = async (
218226
positionState: PositionState,
219227
) =>
220228
getAmountByTargetAndState({
221-
stakeAccountPositions: stakeAccount,
229+
stakeAccountPositions,
222230
targetWithParameters: { integrityPool: { publisher } },
223231
positionState,
224232
epoch: baseInfo.currentEpoch,
@@ -330,7 +338,7 @@ export const loadAccountHistory = async (
330338
export const createStakeAccountAndDeposit = async (
331339
_client: PythStakingClient,
332340
_amount: bigint,
333-
): Promise<StakeAccountPositions> => {
341+
): Promise<PublicKey> => {
334342
await new Promise((resolve) => setTimeout(resolve, MOCK_DELAY));
335343
throw new NotImplementedError();
336344
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Tokens } from "../Tokens";
1313
type Props = { api: States[ApiStateType.Loaded] };
1414

1515
export const AccountHistory = ({ api }: Props) => {
16-
const history = useData(api.accountHisoryCacheKey, api.loadAccountHistory);
16+
const history = useData(api.accountHistoryCacheKey, api.loadAccountHistory);
1717

1818
switch (history.type) {
1919
case StateType.NotLoaded:

apps/staking/src/components/Header/current-stake-account.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ export const CurrentStakeAccount = ({
1818
<div className="flex flex-col items-end text-xs md:flex-row md:items-baseline md:text-sm">
1919
<div className="font-semibold">Stake account:</div>
2020
<CopyButton
21-
text={api.account.address.toBase58()}
21+
text={api.account.toBase58()}
2222
className="-mr-2 ml-2 text-pythpurple-400 md:mr-0"
2323
>
24-
<TruncatedKey>{api.account.address}</TruncatedKey>
24+
<TruncatedKey>{api.account}</TruncatedKey>
2525
</CopyButton>
2626
</div>
2727
</div>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const OracleIntegrityStaking = ({
7676
() =>
7777
api.type === ApiStateType.Loaded &&
7878
publishers.find((publisher) =>
79-
publisher.stakeAccount?.equals(api.account.address),
79+
publisher.stakeAccount?.equals(api.account),
8080
),
8181
[publishers, api],
8282
);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ const ConnectedButton = ({
136136
<StyledMenu
137137
items={api.allAccounts.map((account) => ({
138138
account,
139-
id: account.address.toBase58(),
139+
id: account.toBase58(),
140140
}))}
141141
>
142142
{(item) => (
@@ -154,7 +154,7 @@ const ConnectedButton = ({
154154
invisible: item.account !== api.account,
155155
})}
156156
/>
157-
<TruncatedKey>{item.account.address}</TruncatedKey>
157+
<TruncatedKey>{item.account}</TruncatedKey>
158158
</WalletMenuItem>
159159
)}
160160
</StyledMenu>

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
"use client";
22

33
import { HermesClient } from "@pythnetwork/hermes-client";
4-
import {
5-
type StakeAccountPositions,
6-
PythStakingClient,
7-
} from "@pythnetwork/staking-sdk";
4+
import { PythStakingClient } from "@pythnetwork/staking-sdk";
85
import { useConnection, useWallet } from "@solana/wallet-adapter-react";
96
import type { PublicKey } from "@solana/web3.js";
107
import {
@@ -46,7 +43,7 @@ const State = {
4643
[StateType.LoadedNoStakeAccount]: (
4744
client: PythStakingClient,
4845
hermesClient: HermesClient,
49-
onCreateAccount: (newAccount: StakeAccountPositions) => void,
46+
onCreateAccount: (newAccount: PublicKey) => void,
5047
) => ({
5148
type: StateType.LoadedNoStakeAccount as const,
5249
client,
@@ -62,18 +59,18 @@ const State = {
6259
[StateType.Loaded]: (
6360
client: PythStakingClient,
6461
hermesClient: HermesClient,
65-
account: StakeAccountPositions,
66-
allAccounts: [StakeAccountPositions, ...StakeAccountPositions[]],
67-
selectAccount: (account: StakeAccountPositions) => void,
62+
account: PublicKey,
63+
allAccounts: [PublicKey, ...PublicKey[]],
64+
selectAccount: (account: PublicKey) => void,
6865
mutate: ReturnType<typeof useSWRConfig>["mutate"],
6966
) => {
70-
const dashboardDataCacheKey = account.address.toBase58();
71-
const accountHisoryCacheKey = `${account.address.toBase58()}/history`;
67+
const dashboardDataCacheKey = account.toBase58();
68+
const accountHistoryCacheKey = `${account.toBase58()}/history`;
7269

7370
const reload = async () => {
7471
await Promise.all([
7572
mutate(dashboardDataCacheKey),
76-
mutate(accountHisoryCacheKey),
73+
mutate(accountHistoryCacheKey),
7774
]);
7875
};
7976

@@ -86,7 +83,7 @@ const State = {
8683
) => Promise<void>,
8784
) =>
8885
async (...args: T) => {
89-
await fn(client, account.address, ...args);
86+
await fn(client, account, ...args);
9087
await reload();
9188
};
9289

@@ -98,10 +95,10 @@ const State = {
9895
allAccounts,
9996
selectAccount,
10097
dashboardDataCacheKey,
101-
accountHisoryCacheKey,
98+
accountHistoryCacheKey,
10299

103100
loadData: () => api.loadData(client, hermesClient, account),
104-
loadAccountHistory: () => api.loadAccountHistory(client, account.address),
101+
loadAccountHistory: () => api.loadAccountHistory(client, account),
105102

106103
claim: bindApi(api.claim),
107104
deposit: bindApi(api.deposit),
@@ -157,7 +154,7 @@ const useApiContext = (hermesUrl: string) => {
157154
const { mutate } = useSWRConfig();
158155

159156
const setAccount = useCallback(
160-
(account: StakeAccountPositions) => {
157+
(account: PublicKey) => {
161158
setState((cur) =>
162159
cur.type === StateType.Loaded
163160
? State[StateType.Loaded](
@@ -195,7 +192,7 @@ const useApiContext = (hermesUrl: string) => {
195192
const hermesClient = new HermesClient(hermesUrl);
196193
setState(State[StateType.LoadingStakeAccounts](client, hermesClient));
197194
api
198-
.getStakeAccounts(client)
195+
.getAllStakeAccountAddresses(client)
199196
.then((accounts) => {
200197
const [firstAccount, ...otherAccounts] = accounts;
201198
if (firstAccount) {

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

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class PythStakingClient {
9292
/** Gets a users stake accounts */
9393
public async getAllStakeAccountPositions(
9494
user: PublicKey,
95-
): Promise<StakeAccountPositions[]> {
95+
): Promise<PublicKey[]> {
9696
const positionDataMemcmp = this.stakingProgram.coder.accounts.memcmp(
9797
"positionData",
9898
) as {
@@ -117,13 +117,7 @@ export class PythStakingClient {
117117
],
118118
},
119119
);
120-
return res.map((account) =>
121-
deserializeStakeAccountPositions(
122-
account.pubkey,
123-
account.account.data,
124-
this.stakingProgram.idl,
125-
),
126-
);
120+
return res.map((account) => account.pubkey);
127121
}
128122

129123
public async getStakeAccountPositions(
@@ -458,37 +452,41 @@ export class PythStakingClient {
458452
),
459453
})
460454
.instruction(),
461-
));
455+
),
456+
);
462457

463-
const mergePositionsInstruction = await Promise.all(
464-
publishers.map(({ pubkey }) =>
465-
this.integrityPoolProgram.methods
466-
.mergeDelegationPositions()
467-
.accounts({
468-
owner: this.wallet.publicKey,
469-
publisher: pubkey,
470-
stakeAccountPositions,
471-
})
472-
.instruction(),
473-
),
474-
);
458+
const mergePositionsInstruction = await Promise.all(
459+
publishers.map(({ pubkey }) =>
460+
this.integrityPoolProgram.methods
461+
.mergeDelegationPositions()
462+
.accounts({
463+
owner: this.wallet.publicKey,
464+
publisher: pubkey,
465+
stakeAccountPositions,
466+
})
467+
.instruction(),
468+
),
469+
);
475470

476-
return {
477-
advanceDelegationRecordInstructions,
478-
mergePositionsInstruction,
479-
};
471+
return {
472+
advanceDelegationRecordInstructions,
473+
mergePositionsInstruction,
474+
};
480475
}
481476

482477
public async advanceDelegationRecord(stakeAccountPositions: PublicKey) {
483478
const instructions = await this.getAdvanceDelegationRecordInstructions(
484479
stakeAccountPositions,
485480
);
486481

487-
488-
return sendTransaction([
489-
...instructions.advanceDelegationRecordInstructions,
490-
...instructions.mergePositionsInstruction,
491-
], this.connection, this.wallet);
482+
return sendTransaction(
483+
[
484+
...instructions.advanceDelegationRecordInstructions,
485+
...instructions.mergePositionsInstruction,
486+
],
487+
this.connection,
488+
this.wallet,
489+
);
492490
}
493491

494492
public async getClaimableRewards(stakeAccountPositions: PublicKey) {

0 commit comments

Comments
 (0)