Skip to content

Commit

Permalink
Transfer pda authority (#256)
Browse files Browse the repository at this point in the history
* Do it

* Let's go
  • Loading branch information
guibescos authored Nov 6, 2023
1 parent c5c9479 commit 6fb7c6a
Show file tree
Hide file tree
Showing 5 changed files with 184 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
76 changes: 75 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,77 @@ describe("config", async () => {
errMap
);
});

it("updates pda authority", async () => {
// governance authority can't update pda authority
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));

// pda_authority updates pda_authority to the holder of governance_authority
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),
})
);

// the authority gets returned to the original pda_authority
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),
})
);
});
});

1 comment on commit 6fb7c6a

@vercel
Copy link

@vercel vercel bot commented on 6fb7c6a Nov 6, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

staking-devnet – ./

governance-nu.vercel.app
staking-devnet-git-main-pyth-web.vercel.app
staking-devnet-pyth-web.vercel.app

Please sign in to comment.