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

Some fixes #676

Merged
merged 2 commits into from
Aug 1, 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
11 changes: 11 additions & 0 deletions cross-chain/solana/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions cross-chain/solana/programs/tbtc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ use anchor_lang::prelude::*;

declare_id!("HksEtDgsXJV1BqcuhzbLRTmXp5gHgHJktieJCtQd3pG");

#[derive(Clone)]
pub struct Tbtc;

impl Id for Tbtc {
fn id() -> Pubkey {
ID
}
}

#[program]
pub mod tbtc {
use super::*;
Expand All @@ -29,16 +38,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
26 changes: 19 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,38 @@
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,
)]
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>,
}

impl<'info> Pause<'info> {
fn constraints(ctx: &Context<Self>) -> Result<()> {
require!(!ctx.accounts.config.paused, TbtcError::IsPaused);

Ok(())
}
}

#[access_control(Pause::constraints(&ctx))]
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(())
}
Loading