mhunt_tournaments_anchor/programs/tournaments/src/instructions/add_tournament.rs
2024-11-12 12:31:22 +05:30

44 lines
1.2 KiB
Rust

use anchor_lang::prelude::*;
use crate::{error::CustomErrors, DataRegistry, Tournament, DATA_REGISTRY_SEED};
pub fn handler(ctx:Context<AddTournament>, id:u64, start_time:String)->Result<()>{
require!(ctx.accounts.payer.key() == ctx.accounts.data_registry.authority, CustomErrors::Unauthorized);
require!(!ctx.accounts.data_registry.tournament_ids.contains(&id), CustomErrors::ExistingTournament);
let tournament_account = &mut ctx.accounts.tournament_account;
let data_reg = &mut ctx.accounts.data_registry;
tournament_account.id = id;
tournament_account.start_time = start_time;
data_reg.tournament_ids.push(id);
Ok(())
}
#[derive(Accounts)]
#[instruction(id:u64)]
pub struct AddTournament<'info>{
#[account(mut)]
pub payer: Signer<'info>,
#[account(
init,
payer= payer,
space = Tournament::INIT_SPACE,
seeds = [b"tournament",id.to_le_bytes().as_ref()],
bump
)]
pub tournament_account: Account<'info,Tournament>,
#[account(
mut,
seeds = [DATA_REGISTRY_SEED],
bump
)]
pub data_registry: Account<'info, DataRegistry>,
pub system_program: Program<'info, System>
}