Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvankhademi committed Sep 5, 2024
1 parent 949d707 commit d956e2e
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ export async function startValidatorRaw() {
const internalController: AbortController = new AbortController();
const { signal } = internalController;

const user = loadKeypair("integration-tests/keypairs/localnet-authority.json");
const user = loadKeypair(
"integration-tests/keypairs/localnet-authority.json",
);

const command = `solana-test-validator \
--ledger ${ledgerDir} \
Expand Down
4 changes: 2 additions & 2 deletions governance/pyth_staking_sdk/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export default base({
global: {
config: {
testTimeout: 10_000,
}
}
},
},
});
1 change: 0 additions & 1 deletion governance/pyth_staking_sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./staking/accounts";
export * from "./pyth-staking-client";
export * from "./utils/clock";
export * from "./utils/position";
Expand Down
24 changes: 12 additions & 12 deletions governance/pyth_staking_sdk/src/pyth-staking-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import {
getStakeAccountCustodyAddress,
getStakeAccountMetadataAddress,
} from "./pdas";
import {
type StakeAccountPositions,
StakeAccountPositionsAnchor,
} from "./staking/accounts";
import type { GlobalConfig, PoolConfig, PoolDataAccount } from "./types";
import type {
GlobalConfig,
PoolConfig,
PoolDataAccount,
StakeAccountPositions,
} from "./types";
import { convertBigIntToBN, convertBNToBigInt } from "./utils/bn";
import { deserializeStakeAccountPositions } from "./utils/position";
import { getUnlockSchedule } from "./utils/vesting";
import * as IntegrityPoolIdl from "../idl/integrity-pool.json";
import * as PublisherCapsIdl from "../idl/publisher-caps.json";
Expand Down Expand Up @@ -120,14 +122,13 @@ export class PythStakingClient {
],
},
);
return res.map((account) => {
const stakeAccountPositionsAnchor = new StakeAccountPositionsAnchor(
return res.map((account) =>
deserializeStakeAccountPositions(
account.pubkey,
account.account.data,
this.stakingProgram.idl,
);
return stakeAccountPositionsAnchor.toStakeAccountPositions();
});
),
);
}

public async getStakeAccountPositions(
Expand All @@ -142,12 +143,11 @@ export class PythStakingClient {
throw new Error("Stake account not found");
}

const stakeAccountPositionsAnchor = new StakeAccountPositionsAnchor(
return deserializeStakeAccountPositions(
stakeAccountPositions,
account.data,
this.stakingProgram.idl,
);
return stakeAccountPositionsAnchor.toStakeAccountPositions();
}

public async getStakeAccountCustody(
Expand Down
58 changes: 0 additions & 58 deletions governance/pyth_staking_sdk/src/staking/accounts.ts

This file was deleted.

19 changes: 14 additions & 5 deletions governance/pyth_staking_sdk/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { BN, IdlAccounts, IdlTypes } from "@coral-xyz/anchor";
import { PublicKey } from "@solana/web3.js";

import type { IntegrityPool } from "../types/integrity-pool";
import type { Staking } from "../types/staking";
Expand Down Expand Up @@ -45,10 +46,18 @@ export type UnlockSchedule = {
amount: bigint;
}[];

export type StakeAccountPositions = {
address: PublicKey;
data: {
owner: PublicKey;
positions: Position[];
};
};

export enum PositionState {
UNLOCKED = 0,
LOCKING = 1,
LOCKED = 2,
PREUNLOCKING = 3,
UNLOCKING = 4,
UNLOCKED,
LOCKING,
LOCKED,
PREUNLOCKING,
UNLOCKING,
}
38 changes: 37 additions & 1 deletion governance/pyth_staking_sdk/src/utils/position.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import type { StakeAccountPositions } from "../staking/accounts";
import { BorshCoder } from "@coral-xyz/anchor";
import { PublicKey } from "@solana/web3.js";

import { convertBNToBigInt } from "./bn";
import type { Staking } from "../../types/staking";
import { POSITION_BUFFER_SIZE, POSITIONS_ACCOUNT_SIZE } from "../constants";
import {
type Position,
type PositionAnchor,
PositionState,
type StakeAccountPositions,
type TargetWithParameters,
} from "../types";

Expand Down Expand Up @@ -53,3 +60,32 @@ export const getAmountByTargetAndState = (options: {
.map((p) => p.amount)
.reduce((sum, amount) => sum + amount, 0n);
};

export const deserializeStakeAccountPositions = (
address: PublicKey,
data: Buffer,
idl: Staking,
) => {
const coder = new BorshCoder(idl);
let i = 8; // Skip discriminator
const owner = new PublicKey(data.slice(i, i + 32));
const numberOfPositions = Math.floor(
(data.length - POSITIONS_ACCOUNT_SIZE) / POSITION_BUFFER_SIZE,
);
i += 32;
const positions: PositionAnchor[] = [];
for (let j = 0; j < numberOfPositions; j++) {
if (data[i] === 1) {
positions.push(coder.types.decode("position", data.subarray(i + 1)));
}
i += POSITION_BUFFER_SIZE;
}

return {
address,
data: {
owner,
positions: positions.map((p) => convertBNToBigInt(p)),
},
};
};

0 comments on commit d956e2e

Please sign in to comment.