diff --git a/Anchor.toml b/Anchor.toml index 7f0a662..9d5e86e 100644 --- a/Anchor.toml +++ b/Anchor.toml @@ -5,7 +5,7 @@ resolution = true skip-lint = false [programs.localnet] -tournaments = "3owEKMMjFuuYN2HjT2J2GiVauDivNBeYqaUymyX7Q8Lw" +tournaments = "F5ECXfrZwMyWv5WusMJD7bYSRjEEXYLLPy6TN3XqQBPA" [registry] url = "https://api.apr.dev" diff --git a/programs/tournaments/src/error.rs b/programs/tournaments/src/error.rs index 6ac4378..4797a64 100644 --- a/programs/tournaments/src/error.rs +++ b/programs/tournaments/src/error.rs @@ -7,5 +7,7 @@ pub enum CustomErrors { #[msg("Insufficient funds to purchase a ticket, Recharge your wallet and try again")] InsufficientFunds, #[msg("Only the owner can perform this action")] - Unauthorized + Unauthorized, + #[msg("This tournament ID already exists, use a different one")] + ExistingTournament } diff --git a/programs/tournaments/src/instructions/add_tournament.rs b/programs/tournaments/src/instructions/add_tournament.rs index 529c223..4249bbd 100644 --- a/programs/tournaments/src/instructions/add_tournament.rs +++ b/programs/tournaments/src/instructions/add_tournament.rs @@ -1,17 +1,19 @@ use anchor_lang::prelude::*; -use anchor_spl::associated_token::spl_associated_token_account::instruction; use crate::{error::CustomErrors, DataRegistry, Tournament, DATA_REGISTRY_SEED}; pub fn handler(ctx:Context, 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 tournamentAccount = &mut ctx.accounts.tournament_account; - - tournamentAccount.id = id; - tournamentAccount.start_time = start_time; + 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(()) } diff --git a/programs/tournaments/src/instructions/initialize.rs b/programs/tournaments/src/instructions/initialize.rs index eb553de..aa154b0 100644 --- a/programs/tournaments/src/instructions/initialize.rs +++ b/programs/tournaments/src/instructions/initialize.rs @@ -1,6 +1,6 @@ use anchor_lang::prelude::*; use anchor_spl::{ - associated_token::AssociatedToken, token_interface::{Mint, TokenAccount, TokenInterface, close_account, transfer_checked, CloseAccount, TransferChecked} + associated_token::AssociatedToken, token_interface::{Mint, TokenAccount, TokenInterface} }; use crate::{ DataRegistry, DATA_REGISTRY_SEED}; diff --git a/programs/tournaments/src/instructions/join_tournament.rs b/programs/tournaments/src/instructions/join_tournament.rs index 9012723..f8d24d5 100644 --- a/programs/tournaments/src/instructions/join_tournament.rs +++ b/programs/tournaments/src/instructions/join_tournament.rs @@ -1,35 +1,30 @@ -use anchor_lang::prelude::*; +use anchor_lang::{prelude::*, solana_program::native_token::LAMPORTS_PER_SOL}; use crate::{DataRegistry,Tournament, DATA_REGISTRY_SEED}; use anchor_spl::{associated_token::AssociatedToken, token_interface::{transfer_checked, Mint, TokenAccount, TokenInterface, TransferChecked}}; -// pub fn transfer_ticket_to_vault(ctx:&Context, id:u64) -> Result<()>{ - -// let seeds = &[ -// b"sales", -// ctx.accounts.seller.to_account_info().key.as_ref(), -// &[ctx.accounts.sales.bump] -// ]; +pub fn transfer_ticket_to_vault(ctx:&Context, _id:u64) -> Result<()>{ + let accounts = TransferChecked { + from: ctx.accounts.user_token_account.to_account_info(), + to: ctx.accounts.vault.to_account_info(), + authority: ctx.accounts.user.to_account_info(), + mint: ctx.accounts.mint.to_account_info() + }; -// let signer_seeds = [&seeds[..]]; + let seeds = &[]; -// let accounts = TransferChecked{ -// from: ctx.accounts.vault.to_account_info(), -// to: ctx.accounts.buyer_token_account.to_account_info(), -// mint: ctx.accounts.mint.to_account_info(), -// authority: ctx.accounts.sales.to_account_info() -// }; + let signer_seeds = &[&seeds[..]]; -// let cpi_context = CpiContext::new_with_signer( -// ctx.accounts.token_program.to_account_info(), -// accounts, -// &signer_seeds -// ); + let ctx = CpiContext::new_with_signer( + ctx.accounts.token_program.to_account_info(), + accounts, + signer_seeds + ); -// transfer_checked(cpi_context, amount*LAMPORTS_PER_SOL, ctx.accounts.mint.decimals) -// } + transfer_checked(ctx, 1 *LAMPORTS_PER_SOL, 9) +} -pub fn add_to_participants_list(ctx:Context, id:u64) -> Result<()>{ +pub fn add_to_participants_list(ctx:Context, _id:u64) -> Result<()>{ let tournament_account = &mut ctx.accounts.tournament_account; tournament_account.participants.push(*ctx.accounts.user.key); @@ -42,6 +37,14 @@ pub struct JoinTorunament<'info>{ #[account(mut)] pub user: Signer<'info>, + #[account( + mut, + associated_token::mint = mint, + associated_token::authority = user, + associated_token::token_program = token_program + )] + pub user_token_account: Box>, + #[account( mut, seeds = [b"Tournament", id.to_le_bytes().as_ref()], diff --git a/programs/tournaments/src/instructions/purchase.rs b/programs/tournaments/src/instructions/purchase.rs index e1fe474..9662869 100644 --- a/programs/tournaments/src/instructions/purchase.rs +++ b/programs/tournaments/src/instructions/purchase.rs @@ -4,9 +4,8 @@ use anchor_spl::{ associated_token::AssociatedToken,token_interface::{transfer_checked, Mint, TokenAccount, TokenInterface, TransferChecked} }; -use crate::{error::CustomErrors, DataRegistry, DATA_REGISTRY_SEED}; +use crate::{DataRegistry, DATA_REGISTRY_SEED}; -use super::transfer_tokens; pub fn pay_seller(ctx:&Context, amount:u64)->Result<()>{ let from_account = &ctx.accounts.buyer; diff --git a/programs/tournaments/src/instructions/set_data.rs b/programs/tournaments/src/instructions/set_data.rs index c5148a8..3096e9d 100644 --- a/programs/tournaments/src/instructions/set_data.rs +++ b/programs/tournaments/src/instructions/set_data.rs @@ -1,5 +1,4 @@ use anchor_lang::prelude::*; -use anchor_spl:: token::{Mint, Token, TokenAccount}; use crate::{error::CustomErrors, DataRegistry, DATA_REGISTRY_SEED}; diff --git a/programs/tournaments/src/lib.rs b/programs/tournaments/src/lib.rs index 0cc2db6..94ca00e 100644 --- a/programs/tournaments/src/lib.rs +++ b/programs/tournaments/src/lib.rs @@ -9,7 +9,7 @@ pub use constants::*; pub use instructions::*; pub use state::*; -declare_id!("3owEKMMjFuuYN2HjT2J2GiVauDivNBeYqaUymyX7Q8Lw"); +declare_id!("F5ECXfrZwMyWv5WusMJD7bYSRjEEXYLLPy6TN3XqQBPA"); #[program] pub mod tournaments { use super::*; @@ -29,7 +29,7 @@ pub mod tournaments { } pub fn join_tournament(ctx:Context, id:u64)-> Result<()>{ - // join_tournament::transfer_ticket_to_vault(&ctx, id); + join_tournament::transfer_ticket_to_vault(&ctx, id)?; join_tournament::add_to_participants_list(ctx, id) } } diff --git a/programs/tournaments/src/state/data_registry.rs b/programs/tournaments/src/state/data_registry.rs index 8779dd4..f688932 100644 --- a/programs/tournaments/src/state/data_registry.rs +++ b/programs/tournaments/src/state/data_registry.rs @@ -1,8 +1,10 @@ -use anchor_lang::{prelude::*, Bump}; +use anchor_lang::prelude::*; #[account] #[derive(InitSpace)] pub struct DataRegistry{ pub sales_pda: Pubkey, - pub authority: Pubkey + pub authority: Pubkey, + #[max_len(1000)] + pub tournament_ids: Vec } \ No newline at end of file diff --git a/tournaments.json b/tournaments.json index 193aad8..27bf874 100644 --- a/tournaments.json +++ b/tournaments.json @@ -1,5 +1,5 @@ { - "address": "3owEKMMjFuuYN2HjT2J2GiVauDivNBeYqaUymyX7Q8Lw", + "address": "F5ECXfrZwMyWv5WusMJD7bYSRjEEXYLLPy6TN3XqQBPA", "metadata": { "name": "tournaments", "version": "0.1.0", @@ -222,6 +222,63 @@ "writable": true, "signer": true }, + { + "name": "user_token_account", + "writable": true, + "pda": { + "seeds": [ + { + "kind": "account", + "path": "user" + }, + { + "kind": "account", + "path": "token_program" + }, + { + "kind": "account", + "path": "mint" + } + ], + "program": { + "kind": "const", + "value": [ + 140, + 151, + 37, + 143, + 78, + 36, + 137, + 241, + 187, + 61, + 16, + 41, + 20, + 142, + 13, + 131, + 11, + 90, + 19, + 153, + 218, + 255, + 16, + 132, + 4, + 142, + 123, + 216, + 219, + 233, + 248, + 89 + ] + } + } + }, { "name": "tournament_account", "writable": true, @@ -574,6 +631,11 @@ "code": 6002, "name": "Unauthorized", "msg": "Only the owner can perform this action" + }, + { + "code": 6003, + "name": "ExistingTournament", + "msg": "This tournament ID already exists, use a different one" } ], "types": [ @@ -589,6 +651,12 @@ { "name": "authority", "type": "pubkey" + }, + { + "name": "tournament_ids", + "type": { + "vec": "u64" + } } ] }