Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transfer pda authority #256

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
})
);
});
});
Loading