Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(staking): add reassign/opt out publisher rewards #1916

Merged
merged 5 commits into from
Sep 13, 2024
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
29 changes: 13 additions & 16 deletions apps/staking/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,20 +430,24 @@ export const unstakeIntegrityStaking = async (
};

export const reassignPublisherAccount = async (
_client: PythStakingClient,
_stakeAccount: PublicKey,
_targetAccount: PublicKey,
client: PythStakingClient,
stakeAccount: PublicKey,
targetAccount: PublicKey,
publisherKey: PublicKey,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I feel like this should be derived in the SDK -- it's not valid to call setPublisherStakeAccount if the stakeAccount isn't the one assigned to the publisherKey so the stake account should be all we need right? Or in other words the stake account implies a specific publisher key, so it doesn't really make sense to pass both.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason I did that was to avoid fetching this data. I can fetch this data from the sdk by making an rpc call but I wanted to avoid increasing the number of rpc call. Do you still think it's better to do it this way?

Copy link
Collaborator

Choose a reason for hiding this comment

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

hm, I'm not sure if I really have a strong opinion, let's leave it as is and revisit later.

): Promise<void> => {
await new Promise((resolve) => setTimeout(resolve, MOCK_DELAY));
throw new NotImplementedError();
return client.reassignPublisherStakeAccount(
publisherKey,
stakeAccount,
targetAccount,
);
};

export const optPublisherOut = async (
_client: PythStakingClient,
_stakeAccount: PublicKey,
client: PythStakingClient,
stakeAccount: PublicKey,
publisherKey: PublicKey,
): Promise<void> => {
await new Promise((resolve) => setTimeout(resolve, MOCK_DELAY));
throw new NotImplementedError();
await client.removePublisherStakeAccount(stakeAccount, publisherKey);
};

const MOCK_DELAY = 500;
Expand Down Expand Up @@ -486,10 +490,3 @@ const mkMockHistory = (): AccountHistory => [
locked: 0n,
},
];

class NotImplementedError extends Error {
constructor() {
super("Not yet implemented!");
this.name = "NotImplementedError";
}
}
11 changes: 8 additions & 3 deletions apps/staking/src/components/OracleIntegrityStaking/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ const ReassignStakeAccountButton = ({
{({ close }) => (
<ReassignStakeAccountForm
api={api}
publisherPubkey={self.publicKey}
close={close}
setCloseDisabled={setCloseDisabled}
/>
Expand All @@ -223,12 +224,14 @@ const ReassignStakeAccountButton = ({

type ReassignStakeAccountFormProps = {
api: States[ApiStateType.Loaded];
publisherPubkey: PublicKey;
close: () => void;
setCloseDisabled: (value: boolean) => void;
};

const ReassignStakeAccountForm = ({
api,
publisherPubkey,
close,
setCloseDisabled,
}: ReassignStakeAccountFormProps) => {
Expand All @@ -246,8 +249,8 @@ const ReassignStakeAccountForm = ({
() =>
key === undefined
? Promise.reject(new InvalidKeyError())
: api.reassignPublisherAccount(key),
[api, key],
: api.reassignPublisherAccount(key, publisherPubkey),
[api, key, publisherPubkey],
);

const { state, execute } = useAsync(doReassign);
Expand Down Expand Up @@ -332,7 +335,9 @@ type OptOutButtonProps = {
};

const OptOutButton = ({ api, self }: OptOutButtonProps) => {
const { state, execute } = useAsync(api.optPublisherOut);
const { state, execute } = useAsync(() =>
api.optPublisherOut(self.publicKey),
);

const doOptOut = useCallback(() => {
execute().catch(() => {
Expand Down
22 changes: 18 additions & 4 deletions governance/pyth_staking_sdk/idl/integrity-pool.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@
"address": "11111111111111111111111111111111"
}
],
"args": []
"args": [],
"returns": "u64"
},
{
"name": "create_slash_event",
Expand All @@ -223,7 +224,8 @@
"relations": ["pool_config"]
},
{
"name": "slash_custody"
"name": "slash_custody",
"relations": ["pool_config"]
},
{
"name": "pool_data",
Expand Down Expand Up @@ -448,6 +450,9 @@
]
}
},
{
"name": "slash_custody"
},
{
"name": "system_program",
"address": "11111111111111111111111111111111"
Expand Down Expand Up @@ -608,7 +613,8 @@
}
},
{
"name": "new_stake_account_positions"
"name": "new_stake_account_positions_option",
"optional": true
},
{
"name": "current_stake_account_positions_option",
Expand Down Expand Up @@ -1083,7 +1089,7 @@
},
{
"code": 6001,
"name": "PublisherNeedsToSign"
"name": "PublisherOrRewardAuthorityNeedsToSign"
},
{
"code": 6002,
Expand Down Expand Up @@ -1178,6 +1184,10 @@
"code": 6023,
"name": "InvalidY",
"msg": "Y should not be greater than 1%"
},
{
"code": 6024,
"name": "InvalidSlashCustodyAccount"
}
],
"types": [
Expand Down Expand Up @@ -1343,6 +1353,10 @@
{
"name": "y",
"type": "u64"
},
{
"name": "slash_custody",
"type": "pubkey"
}
]
}
Expand Down
16 changes: 16 additions & 0 deletions governance/pyth_staking_sdk/idl/publisher-caps.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@
"description": "Created with Anchor"
},
"instructions": [
{
"name": "close_publisher_caps",
"discriminator": [36, 234, 184, 214, 76, 203, 153, 144],
"accounts": [
{
"name": "write_authority",
"signer": true,
"relations": ["publisher_caps"]
},
{
"name": "publisher_caps",
"writable": true
}
],
"args": []
},
{
"name": "init_publisher_caps",
"discriminator": [193, 208, 32, 97, 144, 247, 246, 168],
Expand Down
4 changes: 2 additions & 2 deletions governance/pyth_staking_sdk/idl/staking.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"address": "pytS9TjG1qyAZypk7n8rw8gfW9sUaqqYyMhJQ4E7JCQ",
"metadata": {
"name": "staking",
"version": "1.2.0",
"version": "2.0.0",
"spec": "0.1.0",
"description": "Created with Anchor"
},
Expand Down Expand Up @@ -1975,7 +1975,7 @@
"type": "u8"
},
{
"name": "transfer_epoch",
"name": "_deprecated",
"type": {
"option": "u64"
}
Expand Down
42 changes: 42 additions & 0 deletions governance/pyth_staking_sdk/src/pyth-staking-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export class PythStakingClient {
.initializePool(rewardProgramAuthority, yAnchor)
.accounts({
poolData,
slashCustody: getStakeAccountCustodyAddress(poolData),
})
.instruction();

Expand Down Expand Up @@ -642,4 +643,45 @@ export class PythStakingClient {
lowestEpoch === undefined ? undefined : epochToDate(lowestEpoch + 52n),
};
}

async setPublisherStakeAccount(
publisher: PublicKey,
stakeAccountPositions: PublicKey,
newStakeAccountPositions: PublicKey | undefined,
) {
const instruction = await this.integrityPoolProgram.methods
.setPublisherStakeAccount()
.accounts({
currentStakeAccountPositionsOption: stakeAccountPositions,
newStakeAccountPositionsOption: newStakeAccountPositions ?? null,
publisher,
})
.instruction();

await sendTransaction([instruction], this.connection, this.wallet);
return;
}

public async reassignPublisherStakeAccount(
publisher: PublicKey,
stakeAccountPositions: PublicKey,
newStakeAccountPositions: PublicKey,
) {
return this.setPublisherStakeAccount(
publisher,
stakeAccountPositions,
newStakeAccountPositions,
);
}

public async removePublisherStakeAccount(
publisher: PublicKey,
stakeAccountPositions: PublicKey,
) {
return this.setPublisherStakeAccount(
publisher,
stakeAccountPositions,
undefined,
);
}
}
18 changes: 16 additions & 2 deletions governance/pyth_staking_sdk/types/integrity-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ export type IntegrityPool = {
},
];
args: [];
returns: "u64";
},
{
name: "createSlashEvent";
Expand All @@ -361,6 +362,7 @@ export type IntegrityPool = {
},
{
name: "slashCustody";
relations: ["poolConfig"];
},
{
name: "poolData";
Expand Down Expand Up @@ -620,6 +622,9 @@ export type IntegrityPool = {
];
};
},
{
name: "slashCustody";
},
{
name: "systemProgram";
address: "11111111111111111111111111111111";
Expand Down Expand Up @@ -803,7 +808,8 @@ export type IntegrityPool = {
};
},
{
name: "newStakeAccountPositions";
name: "newStakeAccountPositionsOption";
optional: true;
},
{
name: "currentStakeAccountPositionsOption";
Expand Down Expand Up @@ -1363,7 +1369,7 @@ export type IntegrityPool = {
},
{
code: 6001;
name: "publisherNeedsToSign";
name: "publisherOrRewardAuthorityNeedsToSign";
},
{
code: 6002;
Expand Down Expand Up @@ -1459,6 +1465,10 @@ export type IntegrityPool = {
name: "invalidY";
msg: "Y should not be greater than 1%";
},
{
code: 6024;
name: "invalidSlashCustodyAccount";
},
];
types: [
{
Expand Down Expand Up @@ -1624,6 +1634,10 @@ export type IntegrityPool = {
name: "y";
type: "u64";
},
{
name: "slashCustody";
type: "pubkey";
},
];
};
},
Expand Down
16 changes: 16 additions & 0 deletions governance/pyth_staking_sdk/types/publisher-caps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ export type PublisherCaps = {
description: "Created with Anchor";
};
instructions: [
{
name: "closePublisherCaps";
discriminator: [36, 234, 184, 214, 76, 203, 153, 144];
accounts: [
{
name: "writeAuthority";
signer: true;
relations: ["publisherCaps"];
},
{
name: "publisherCaps";
writable: true;
},
];
args: [];
},
{
name: "initPublisherCaps";
discriminator: [193, 208, 32, 97, 144, 247, 246, 168];
Expand Down
4 changes: 2 additions & 2 deletions governance/pyth_staking_sdk/types/staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type Staking = {
address: "pytS9TjG1qyAZypk7n8rw8gfW9sUaqqYyMhJQ4E7JCQ";
metadata: {
name: "staking";
version: "1.2.0";
version: "2.0.0";
spec: "0.1.0";
description: "Created with Anchor";
};
Expand Down Expand Up @@ -2184,7 +2184,7 @@ export type Staking = {
type: "u8";
},
{
name: "transferEpoch";
name: "deprecated";
type: {
option: "u64";
};
Expand Down
Loading