From 8eaa28b27de7d956fcbc133a7791e15ad1a2d5aa Mon Sep 17 00:00:00 2001 From: Guillermo Bescos Date: Thu, 9 Nov 2023 16:17:59 +0000 Subject: [PATCH] Do it --- staking/programs/staking/src/context.rs | 9 ++++ staking/programs/staking/src/lib.rs | 10 +++- staking/target/idl/staking.json | 35 +++++++++++++ staking/target/types/staking.ts | 70 +++++++++++++++++++++++++ staking/tests/config.ts | 39 ++++++++++++++ staking/tests/utils/before.ts | 4 ++ 6 files changed, 166 insertions(+), 1 deletion(-) diff --git a/staking/programs/staking/src/context.rs b/staking/programs/staking/src/context.rs index 33f9599f..cf9c05c7 100644 --- a/staking/programs/staking/src/context.rs +++ b/staking/programs/staking/src/context.rs @@ -95,6 +95,15 @@ pub struct UpdateTokenListTime<'info> { pub config: Account<'info, global_config::GlobalConfig>, } +#[derive(Accounts)] +#[instruction(agreement_hash : [u8; 32])] +pub struct UpdateAgreementHash<'info> { + #[account(address = config.governance_authority)] + pub governance_signer: Signer<'info>, + #[account(mut, seeds = [CONFIG_SEED.as_bytes()], bump = config.bump)] + pub config: Account<'info, global_config::GlobalConfig>, +} + #[derive(Accounts)] #[instruction(owner : Pubkey, lock : vesting::VestingSchedule)] pub struct CreateStakeAccount<'info> { diff --git a/staking/programs/staking/src/lib.rs b/staking/programs/staking/src/lib.rs index 44b55b0a..46a83009 100644 --- a/staking/programs/staking/src/lib.rs +++ b/staking/programs/staking/src/lib.rs @@ -50,7 +50,6 @@ pub mod wasm; declare_id!("pytS9TjG1qyAZypk7n8rw8gfW9sUaqqYyMhJQ4E7JCQ"); #[program] pub mod staking { - /// Creates a global config for the program use super::*; pub fn init_config(ctx: Context, global_config: GlobalConfig) -> Result<()> { @@ -111,6 +110,15 @@ pub mod staking { Ok(()) } + pub fn update_agreement_hash( + ctx: Context, + agreement_hash: [u8; 32], + ) -> Result<()> { + let config = &mut ctx.accounts.config; + config.agreement_hash = agreement_hash; + Ok(()) + } + /// Trustless instruction that creates a stake account for a user /// The main account i.e. the position accounts needs to be initialized outside of the program /// otherwise we run into stack limits diff --git a/staking/target/idl/staking.json b/staking/target/idl/staking.json index 716d63a8..ac66f1f9 100644 --- a/staking/target/idl/staking.json +++ b/staking/target/idl/staking.json @@ -166,6 +166,41 @@ } ] }, + { + "name": "updateAgreementHash", + "accounts": [ + { + "name": "governanceSigner", + "isMut": false, + "isSigner": true + }, + { + "name": "config", + "isMut": true, + "isSigner": false, + "pda": { + "seeds": [ + { + "kind": "const", + "type": "string", + "value": "config" + } + ] + } + } + ], + "args": [ + { + "name": "agreementHash", + "type": { + "array": [ + "u8", + 32 + ] + } + } + ] + }, { "name": "createStakeAccount", "docs": [ diff --git a/staking/target/types/staking.ts b/staking/target/types/staking.ts index 8afd5133..1a77aeb7 100644 --- a/staking/target/types/staking.ts +++ b/staking/target/types/staking.ts @@ -166,6 +166,41 @@ export type Staking = { } ] }, + { + "name": "updateAgreementHash", + "accounts": [ + { + "name": "governanceSigner", + "isMut": false, + "isSigner": true + }, + { + "name": "config", + "isMut": true, + "isSigner": false, + "pda": { + "seeds": [ + { + "kind": "const", + "type": "string", + "value": "config" + } + ] + } + } + ], + "args": [ + { + "name": "agreementHash", + "type": { + "array": [ + "u8", + 32 + ] + } + } + ] + }, { "name": "createStakeAccount", "docs": [ @@ -2103,6 +2138,41 @@ export const IDL: Staking = { } ] }, + { + "name": "updateAgreementHash", + "accounts": [ + { + "name": "governanceSigner", + "isMut": false, + "isSigner": true + }, + { + "name": "config", + "isMut": true, + "isSigner": false, + "pda": { + "seeds": [ + { + "kind": "const", + "type": "string", + "value": "config" + } + ] + } + } + ], + "args": [ + { + "name": "agreementHash", + "type": { + "array": [ + "u8", + 32 + ] + } + } + ] + }, { "name": "createStakeAccount", "docs": [ diff --git a/staking/tests/config.ts b/staking/tests/config.ts index 6b5a9ab8..07be3053 100644 --- a/staking/tests/config.ts +++ b/staking/tests/config.ts @@ -12,6 +12,7 @@ import { ANCHOR_CONFIG_PATH, requestPythAirdrop, getDummyAgreementHash, + getDummyAgreementHash2, } from "./utils/before"; import { expectFail, createMint, getTargetAccount } from "./utils/utils"; import BN from "bn.js"; @@ -416,6 +417,14 @@ describe("config", async () => { "An address constraint was violated", errMap ); + + await expectFail( + samConnection.program.methods.updateAgreementHash( + Array.from(Buffer.alloc(32)) + ), + "An address constraint was violated", + errMap + ); }); it("updates pda authority", async () => { @@ -490,4 +499,34 @@ describe("config", async () => { }) ); }); + + it("updates agreement hash", async () => { + assert.notEqual( + JSON.stringify(getDummyAgreementHash()), + JSON.stringify(getDummyAgreementHash2()) + ); + + await program.methods.updateAgreementHash(getDummyAgreementHash2()).rpc(); + + let configAccountData = await program.account.globalConfig.fetch( + configAccount + ); + assert.equal( + JSON.stringify(configAccountData), + JSON.stringify({ + bump, + governanceAuthority: program.provider.wallet.publicKey, + pythTokenMint: pythMintAccount.publicKey, + pythGovernanceRealm: zeroPubkey, + unlockingDuration: 2, + epochDuration: new BN(3600), + freeze: true, + pdaAuthority: pdaAuthority, + governanceProgram, + pythTokenListTime: null, + agreementHash: getDummyAgreementHash2(), + mockClockTime: new BN(30), + }) + ); + }); }); diff --git a/staking/tests/utils/before.ts b/staking/tests/utils/before.ts index 0823d215..c454713d 100644 --- a/staking/tests/utils/before.ts +++ b/staking/tests/utils/before.ts @@ -75,6 +75,10 @@ export function getDummyAgreementHash(): number[] { return Array.from({ length: 32 }, (_, i) => i); } +export function getDummyAgreementHash2(): number[] { + return Array.from({ length: 32 }, (_, i) => 2); +} + /** * Deterministically determines the port for deploying the validator basing of the index of the testfile in the sorted * list of all testsfiles.