Skip to content

Commit

Permalink
Do it
Browse files Browse the repository at this point in the history
  • Loading branch information
guibescos committed Nov 6, 2023
1 parent cb6583d commit 54ed376
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 1 deletion.
10 changes: 10 additions & 0 deletions staking/programs/staking/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ pub struct UpdateGovernanceAuthority<'info> {
pub config: Account<'info, global_config::GlobalConfig>,
}

#[derive(Accounts)]
#[instruction(new_authority : Pubkey)]
pub struct UpdatePdaAuthority<'info> {
#[account(address = config.pda_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(freeze : bool)]
pub struct UpdateFreeze<'info> {
Expand Down
9 changes: 9 additions & 0 deletions staking/programs/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ pub mod staking {
Ok(())
}

pub fn update_pda_authority(
ctx: Context<UpdatePdaAuthority>,
new_authority: Pubkey,
) -> Result<()> {
let config = &mut ctx.accounts.config;
config.pda_authority = new_authority;
Ok(())
}

pub fn update_freeze(ctx: Context<UpdateFreeze>, freeze: bool) -> Result<()> {
let config = &mut ctx.accounts.config;
config.freeze = freeze;
Expand Down
30 changes: 30 additions & 0 deletions staking/target/idl/staking.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,36 @@
}
]
},
{
"name": "updatePdaAuthority",
"accounts": [
{
"name": "governanceSigner",
"isMut": false,
"isSigner": true
},
{
"name": "config",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "const",
"type": "string",
"value": "config"
}
]
}
}
],
"args": [
{
"name": "newAuthority",
"type": "publicKey"
}
]
},
{
"name": "updateFreeze",
"accounts": [
Expand Down
60 changes: 60 additions & 0 deletions staking/target/types/staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,36 @@ export type Staking = {
}
]
},
{
"name": "updatePdaAuthority",
"accounts": [
{
"name": "governanceSigner",
"isMut": false,
"isSigner": true
},
{
"name": "config",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "const",
"type": "string",
"value": "config"
}
]
}
}
],
"args": [
{
"name": "newAuthority",
"type": "publicKey"
}
]
},
{
"name": "updateFreeze",
"accounts": [
Expand Down Expand Up @@ -1981,6 +2011,36 @@ export const IDL: Staking = {
}
]
},
{
"name": "updatePdaAuthority",
"accounts": [
{
"name": "governanceSigner",
"isMut": false,
"isSigner": true
},
{
"name": "config",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "const",
"type": "string",
"value": "config"
}
]
}
}
],
"args": [
{
"name": "newAuthority",
"type": "publicKey"
}
]
},
{
"name": "updateFreeze",
"accounts": [
Expand Down
73 changes: 72 additions & 1 deletion staking/tests/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ describe("config", async () => {
const pythMintAuthority = new Keypair();
const zeroPubkey = new PublicKey(0);

const pdaAuthorityKeypair = new Keypair();
const config = readAnchorConfig(ANCHOR_CONFIG_PATH);
const pdaAuthority = PublicKey.unique();
const pdaAuthority = pdaAuthorityKeypair.publicKey;
const governanceProgram = new PublicKey(config.programs.localnet.governance);

let errMap: Map<number, string>;
Expand Down Expand Up @@ -416,4 +417,74 @@ describe("config", async () => {
errMap
);
});

it("updates pda authority", async () => {
await expectFail(
program.methods.updatePdaAuthority(program.provider.wallet.publicKey),
"An address constraint was violated",
errMap
);

const pdaConnection = await StakeConnection.createStakeConnection(
program.provider.connection,
new Wallet(pdaAuthorityKeypair),
program.programId
);

await pdaConnection.program.provider.connection.requestAirdrop(
pdaAuthorityKeypair.publicKey,
1_000_000_000_000
);

// Airdrops are not instant unfortunately, wait
await new Promise((resolve) => setTimeout(resolve, 2000));

await pdaConnection.program.methods
.updatePdaAuthority(program.provider.wallet.publicKey)
.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: program.provider.wallet.publicKey,
governanceProgram,
pythTokenListTime: null,
agreementHash: getDummyAgreementHash(),
mockClockTime: new BN(30),
})
);

await program.methods.updatePdaAuthority(pdaAuthority).rpc();

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: getDummyAgreementHash(),
mockClockTime: new BN(30),
})
);
});
});

0 comments on commit 54ed376

Please sign in to comment.