Skip to content

Commit

Permalink
solana: refactor tbtc program
Browse files Browse the repository at this point in the history
state: tbtc -> config

make config pda (remove generated keypair)

fix tests
  • Loading branch information
a5-pickle committed Jul 31, 2023
1 parent 5c18024 commit 89f4a0b
Show file tree
Hide file tree
Showing 16 changed files with 380 additions and 346 deletions.
8 changes: 4 additions & 4 deletions cross-chain/solana/programs/tbtc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ pub mod tbtc {
processor::add_minter(ctx)
}

pub fn remove_minter(ctx: Context<RemoveMinter>, minter: Pubkey) -> Result<()> {
processor::remove_minter(ctx, minter)
pub fn remove_minter(ctx: Context<RemoveMinter>) -> Result<()> {
processor::remove_minter(ctx)
}

pub fn add_guardian(ctx: Context<AddGuardian>) -> Result<()> {
processor::add_guardian(ctx)
}

pub fn remove_guardian(ctx: Context<RemoveGuardian>, guardian: Pubkey) -> Result<()> {
processor::remove_guardian(ctx, guardian)
pub fn remove_guardian(ctx: Context<RemoveGuardian>) -> Result<()> {
processor::remove_guardian(ctx)
}

pub fn pause(ctx: Context<Pause>) -> Result<()> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
use crate::{
error::TbtcError,
state::{GuardianInfo, Tbtc},
state::{Config, GuardianInfo},
};
use anchor_lang::prelude::*;

#[derive(Accounts)]
pub struct AddGuardian<'info> {
#[account(
mut,
seeds = [Config::SEED_PREFIX],
bump,
has_one = authority @ TbtcError::IsNotAuthority
)]
pub tbtc: Account<'info, Tbtc>,
pub authority: Signer<'info>,
/// CHECK: the guardian does not need to sign
pub guardian: UncheckedAccount<'info>,
config: Account<'info, Config>,

#[account(mut)]
pub payer: Signer<'info>,
authority: Signer<'info>,

#[account(
init,
payer = payer,
space = GuardianInfo::MAXIMUM_SIZE,
seeds = [GuardianInfo::SEED_PREFIX, tbtc.key().as_ref(), guardian.key().as_ref()], bump
payer = authority,
space = 8 + GuardianInfo::INIT_SPACE,
seeds = [GuardianInfo::SEED_PREFIX, guardian.key().as_ref()],
bump
)]
pub guardian_info: Account<'info, GuardianInfo>,
pub system_program: Program<'info, System>,
guardian_info: Account<'info, GuardianInfo>,

/// CHECK: Required authority to pause contract. This pubkey lives in `GuardianInfo`.
guardian: AccountInfo<'info>,

system_program: Program<'info, System>,
}

pub fn add_guardian(ctx: Context<AddGuardian>) -> Result<()> {
ctx.accounts.guardian_info.set_inner(GuardianInfo {
guardian: ctx.accounts.guardian.key(),
bump: *ctx.bumps.get("guardian_info").unwrap(),
bump: ctx.bumps["guardian_info"],
});

ctx.accounts.tbtc.guardians += 1;
ctx.accounts.config.num_guardians += 1;
Ok(())
}
32 changes: 19 additions & 13 deletions cross-chain/solana/programs/tbtc/src/processor/admin/add_minter.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
use crate::{
error::TbtcError,
state::{MinterInfo, Tbtc},
state::{Config, MinterInfo},
};
use anchor_lang::prelude::*;

#[derive(Accounts)]
pub struct AddMinter<'info> {
#[account(
mut,
seeds = [Config::SEED_PREFIX],
bump,
has_one = authority @ TbtcError::IsNotAuthority
)]
pub tbtc: Account<'info, Tbtc>,
pub authority: Signer<'info>,
/// CHECK: the minter does not need to sign
pub minter: UncheckedAccount<'info>,
config: Account<'info, Config>,

#[account(mut)]
pub payer: Signer<'info>,
authority: Signer<'info>,

#[account(
init,
payer = payer,
space = MinterInfo::MAXIMUM_SIZE,
seeds = [MinterInfo::SEED_PREFIX, tbtc.key().as_ref(), minter.key().as_ref()], bump
payer = authority,
space = 8 + MinterInfo::INIT_SPACE,
seeds = [MinterInfo::SEED_PREFIX, minter.key().as_ref()],
bump
)]
pub minter_info: Account<'info, MinterInfo>,
pub system_program: Program<'info, System>,
minter_info: Account<'info, MinterInfo>,

/// CHECK: Required authority to mint tokens. This pubkey lives in `MinterInfo`.
minter: AccountInfo<'info>,

system_program: Program<'info, System>,
}

pub fn add_minter(ctx: Context<AddMinter>) -> Result<()> {
ctx.accounts.minter_info.set_inner(MinterInfo {
minter: ctx.accounts.minter.key(),
bump: *ctx.bumps.get("minter_info").unwrap(),
bump: ctx.bumps["minter_info"],
});

ctx.accounts.tbtc.minters += 1;
ctx.accounts.config.num_minters += 1;
Ok(())
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
use crate::{error::TbtcError, state::Tbtc};
use crate::{error::TbtcError, state::Config};
use anchor_lang::prelude::*;

#[derive(Accounts)]
pub struct ChangeAuthority<'info> {
#[account(
mut,
seeds = [Config::SEED_PREFIX],
bump,
has_one = authority @ TbtcError::IsNotAuthority
)]
pub tbtc: Account<'info, Tbtc>,
pub authority: Signer<'info>,
#[account(mut)]
pub payer: Signer<'info>,
pub new_authority: Signer<'info>,
config: Account<'info, Config>,

authority: Signer<'info>,

new_authority: Signer<'info>,
}

pub fn change_authority(ctx: Context<ChangeAuthority>) -> Result<()> {
ctx.accounts.tbtc.authority = ctx.accounts.new_authority.key();
ctx.accounts.config.authority = ctx.accounts.new_authority.key();
Ok(())
}
35 changes: 21 additions & 14 deletions cross-chain/solana/programs/tbtc/src/processor/admin/initialize.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{constants::SEED_PREFIX_TBTC_MINT, state::Tbtc};
use crate::{constants::SEED_PREFIX_TBTC_MINT, state::Config};
use anchor_lang::prelude::*;
use anchor_spl::token;

Expand All @@ -8,31 +8,38 @@ pub struct Initialize<'info> {
// so we can sign for it from the program
#[account(
init,
seeds = [SEED_PREFIX_TBTC_MINT, tbtc.key().as_ref()],
seeds = [SEED_PREFIX_TBTC_MINT],
bump,
payer = authority,
mint::decimals = 9,
mint::authority = tbtc_mint,
mint::authority = config,
)]
pub tbtc_mint: Account<'info, token::Mint>,
mint: Account<'info, token::Mint>,

#[account(init, payer = authority, space = Tbtc::MAXIMUM_SIZE)]
pub tbtc: Account<'info, Tbtc>,
#[account(
init,
payer = authority,
space = 8 + Config::INIT_SPACE,
seeds = [Config::SEED_PREFIX],
bump,
)]
config: Account<'info, Config>,

#[account(mut)]
pub authority: Signer<'info>,
authority: Signer<'info>,

pub token_program: Program<'info, token::Token>,
pub system_program: Program<'info, System>,
token_program: Program<'info, token::Token>,
system_program: Program<'info, System>,
}

pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
ctx.accounts.tbtc.set_inner(Tbtc {
ctx.accounts.config.set_inner(Config {
bump: ctx.bumps["config"],
authority: ctx.accounts.authority.key(),
token_mint: ctx.accounts.tbtc_mint.key(),
token_bump: *ctx.bumps.get("tbtc_mint").unwrap(),
minters: 0,
guardians: 0,
mint: ctx.accounts.mint.key(),
mint_bump: ctx.bumps["mint"],
num_minters: 0,
num_guardians: 0,
paused: false,
});
Ok(())
Expand Down
18 changes: 11 additions & 7 deletions cross-chain/solana/programs/tbtc/src/processor/admin/pause.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
use crate::{
error::TbtcError,
state::{GuardianInfo, Tbtc},
state::{Config, GuardianInfo},
};
use anchor_lang::prelude::*;

#[derive(Accounts)]
pub struct Pause<'info> {
#[account(
mut,
constraint = !tbtc.paused @ TbtcError::IsPaused
seeds = [Config::SEED_PREFIX],
bump,
constraint = !config.paused @ TbtcError::IsPaused
)]
pub tbtc: Account<'info, Tbtc>,
config: Account<'info, Config>,

#[account(
has_one = guardian,
seeds = [GuardianInfo::SEED_PREFIX, tbtc.key().as_ref(), guardian.key().as_ref()],
seeds = [GuardianInfo::SEED_PREFIX, guardian.key().as_ref()],
bump = guardian_info.bump
)]
pub guardian_info: Account<'info, GuardianInfo>,
pub guardian: Signer<'info>,
guardian_info: Account<'info, GuardianInfo>,

guardian: Signer<'info>,
}

pub fn pause(ctx: Context<Pause>) -> Result<()> {
ctx.accounts.tbtc.paused = true;
ctx.accounts.config.paused = true;
Ok(())
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
use crate::{
error::TbtcError,
state::{GuardianInfo, Tbtc},
state::{Config, GuardianInfo},
};
use anchor_lang::prelude::*;

#[derive(Accounts)]
#[instruction(guardian: Pubkey)]
pub struct RemoveGuardian<'info> {
#[account(
mut,
has_one = authority @ TbtcError::IsNotAuthority,
)]
pub tbtc: Account<'info, Tbtc>,
pub authority: Signer<'info>,
config: Account<'info, Config>,

authority: Signer<'info>,

#[account(
mut,
has_one = guardian,
close = authority,
seeds = [GuardianInfo::SEED_PREFIX, tbtc.key().as_ref(), guardian.as_ref()],
seeds = [GuardianInfo::SEED_PREFIX, guardian.key().as_ref()],
bump = guardian_info.bump,
)]
pub guardian_info: Account<'info, GuardianInfo>,
guardian_info: Account<'info, GuardianInfo>,

/// CHECK: Required authority to pause contract. This pubkey lives in `GuardianInfo`.
guardian: AccountInfo<'info>,
}

pub fn remove_guardian(ctx: Context<RemoveGuardian>, _guardian: Pubkey) -> Result<()> {
ctx.accounts.tbtc.guardians -= 1;
pub fn remove_guardian(ctx: Context<RemoveGuardian>) -> Result<()> {
ctx.accounts.config.num_guardians -= 1;
Ok(())
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
use crate::{
error::TbtcError,
state::{MinterInfo, Tbtc},
state::{Config, MinterInfo},
};
use anchor_lang::prelude::*;

#[derive(Accounts)]
#[instruction(minter: Pubkey)]
pub struct RemoveMinter<'info> {
#[account(
mut,
seeds = [Config::SEED_PREFIX],
bump,
has_one = authority @ TbtcError::IsNotAuthority
)]
pub tbtc: Account<'info, Tbtc>,
pub authority: Signer<'info>,
config: Account<'info, Config>,

authority: Signer<'info>,

#[account(
mut,
constraint = minter_info.minter == minter,
has_one = minter,
close = authority,
seeds = [MinterInfo::SEED_PREFIX, tbtc.key().as_ref(), minter.as_ref()],
seeds = [MinterInfo::SEED_PREFIX, minter.key().as_ref()],
bump = minter_info.bump,
)]
pub minter_info: Account<'info, MinterInfo>,
minter_info: Account<'info, MinterInfo>,

/// CHECK: Required authority to mint tokens. This pubkey lives in `MinterInfo`.
minter: AccountInfo<'info>,
}

pub fn remove_minter(ctx: Context<RemoveMinter>, _minter: Pubkey) -> Result<()> {
ctx.accounts.tbtc.minters -= 1;
pub fn remove_minter(ctx: Context<RemoveMinter>) -> Result<()> {
ctx.accounts.config.num_minters -= 1;
Ok(())
}
15 changes: 9 additions & 6 deletions cross-chain/solana/programs/tbtc/src/processor/admin/unpause.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
use crate::{error::TbtcError, state::Tbtc};
use crate::{error::TbtcError, state::Config};
use anchor_lang::prelude::*;

#[derive(Accounts)]
pub struct Unpause<'info> {
#[account(
mut,
constraint = tbtc.paused @ TbtcError::IsNotPaused,
has_one = authority @ TbtcError::IsNotAuthority
seeds = [Config::SEED_PREFIX],
bump,
has_one = authority @ TbtcError::IsNotAuthority,
constraint = config.paused @ TbtcError::IsNotPaused
)]
pub tbtc: Account<'info, Tbtc>,
pub authority: Signer<'info>,
config: Account<'info, Config>,

authority: Signer<'info>,
}

pub fn unpause(ctx: Context<Unpause>) -> Result<()> {
ctx.accounts.tbtc.paused = false;
ctx.accounts.config.paused = false;
Ok(())
}
Loading

0 comments on commit 89f4a0b

Please sign in to comment.