Skip to content

Commit

Permalink
feat(staking): add reassign/opt out publisher rewards (#1916)
Browse files Browse the repository at this point in the history
* go

* feat: add opt out

* go

* go
  • Loading branch information
keyvankhademi authored Sep 13, 2024
1 parent faf2a1b commit b3fe3d4
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 29 deletions.
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,
): 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

0 comments on commit b3fe3d4

Please sign in to comment.