Skip to content

Commit

Permalink
Do it
Browse files Browse the repository at this point in the history
  • Loading branch information
guibescos committed Nov 9, 2023
1 parent deb1121 commit 8eaa28b
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 1 deletion.
9 changes: 9 additions & 0 deletions staking/programs/staking/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
10 changes: 9 additions & 1 deletion staking/programs/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<InitConfig>, global_config: GlobalConfig) -> Result<()> {
Expand Down Expand Up @@ -111,6 +110,15 @@ pub mod staking {
Ok(())
}

pub fn update_agreement_hash(
ctx: Context<UpdateAgreementHash>,
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
Expand Down
35 changes: 35 additions & 0 deletions staking/target/idl/staking.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
70 changes: 70 additions & 0 deletions staking/target/types/staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down
39 changes: 39 additions & 0 deletions staking/tests/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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),
})
);
});
});
4 changes: 4 additions & 0 deletions staking/tests/utils/before.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 8eaa28b

Please sign in to comment.