diff --git a/Anchor.toml b/Anchor.toml index 5560db7..7f0a662 100644 --- a/Anchor.toml +++ b/Anchor.toml @@ -5,7 +5,7 @@ resolution = true skip-lint = false [programs.localnet] -tournaments = "88dRZraTvmpqMs8GqtgCc321Foo5bWtDAsXJ1kqoc6nC" +tournaments = "3owEKMMjFuuYN2HjT2J2GiVauDivNBeYqaUymyX7Q8Lw" [registry] url = "https://api.apr.dev" diff --git a/programs/tournaments/src/instructions/add_seller.rs b/programs/tournaments/src/instructions/add_seller.rs deleted file mode 100644 index 11a1906..0000000 --- a/programs/tournaments/src/instructions/add_seller.rs +++ /dev/null @@ -1,81 +0,0 @@ -use anchor_lang::prelude::*; -use anchor_spl::{ - associated_token::AssociatedToken, token_interface::{Mint, TokenAccount, TokenInterface, close_account, transfer_checked, CloseAccount, TransferChecked} -}; - -use crate::{Sales, DataRegistry, DATA_REGISTRY_SEED}; - -use super::transfer_tokens; - -#[derive(Accounts)] -pub struct AddSeller <'info>{ - #[account(mut)] - pub seller: Signer<'info>, - - // #[account( - // mut, - // associated_token::mint=mint, - // associated_token::authority = seller, - // associated_token::token_program= token_program, - // )] - // pub seller_token_account: InterfaceAccount<'info, TokenAccount>, - - #[account( - init, - payer = seller, - space = 8 + Sales::INIT_SPACE, - seeds= [b"sales", seller.key().as_ref()], - bump - )] - pub sales: Account<'info, Sales>, - - #[account(mint::token_program = token_program)] - pub mint: InterfaceAccount<'info, Mint>, - - #[account( - init, - payer =seller, - associated_token::mint=mint, - associated_token::authority = sales, - associated_token::token_program= token_program, - )] - pub vault: InterfaceAccount<'info, TokenAccount>, - - #[account( - mut, - seeds=[DATA_REGISTRY_SEED], - bump - )] - pub data_registry: Account<'info, DataRegistry>, - - pub system_program: Program<'info, System>, - pub token_program: Interface<'info, TokenInterface>, - pub associated_token_program: Program<'info, AssociatedToken> - -} - -// pub fn send_tickets_to_vault(ctx: &Context, tickets_count: u64) -> Result<()>{ -// transfer_tokens( -// &ctx.accounts.seller_token_account, -// &ctx.accounts.vault, -// &tickets_count, -// &ctx.accounts.mint, -// &ctx.accounts.seller, -// &ctx.accounts.token_program -// ) -// } - -pub fn save_sales_account(ctx: Context) -> Result<()> { - ctx.accounts.sales.set_inner( - Sales{ - seller: ctx.accounts.seller.key(), - vault: ctx.accounts.vault.key(), - mint: ctx.accounts.mint.key(), - bump: ctx.bumps.sales - } - ); - - ctx.accounts.data_registry.sales_pdas.push(ctx.accounts.sales.key()); - - Ok(()) -} diff --git a/programs/tournaments/src/instructions/initialize.rs b/programs/tournaments/src/instructions/initialize.rs index 47076d4..eb553de 100644 --- a/programs/tournaments/src/instructions/initialize.rs +++ b/programs/tournaments/src/instructions/initialize.rs @@ -1,8 +1,9 @@ use anchor_lang::prelude::*; -use anchor_spl:: token::{Mint, Token, TokenAccount}; +use anchor_spl::{ + associated_token::AssociatedToken, token_interface::{Mint, TokenAccount, TokenInterface, close_account, transfer_checked, CloseAccount, TransferChecked} +}; - -use crate::{Sales, DataRegistry, DATA_REGISTRY_SEED}; +use crate::{ DataRegistry, DATA_REGISTRY_SEED}; #[derive(Accounts)] pub struct Initialize <'info>{ @@ -18,12 +19,26 @@ pub struct Initialize <'info>{ )] pub data_registry: Account<'info, DataRegistry>, + #[account(mint::token_program = token_program)] + pub mint: InterfaceAccount<'info, Mint>, + #[account( + init, + payer =signer, + associated_token::mint=mint, + associated_token::authority = data_registry, + associated_token::token_program= token_program, + )] + pub vault: InterfaceAccount<'info, TokenAccount>, + pub system_program: Program<'info, System>, + pub token_program: Interface<'info, TokenInterface>, + pub associated_token_program: Program<'info, AssociatedToken> } pub fn handler(ctx: Context) -> Result<()> { let reg_acc = &mut ctx.accounts.data_registry; reg_acc.authority = ctx.accounts.signer.key(); + reg_acc.sales_pda = ctx.accounts.vault.key(); msg!("Initiated Ticket store"); Ok(()) } diff --git a/programs/tournaments/src/instructions/join_tournament.rs b/programs/tournaments/src/instructions/join_tournament.rs index 02912f6..9012723 100644 --- a/programs/tournaments/src/instructions/join_tournament.rs +++ b/programs/tournaments/src/instructions/join_tournament.rs @@ -1,5 +1,5 @@ use anchor_lang::prelude::*; -use crate::{DataRegistry, Sales, Tournament, DATA_REGISTRY_SEED}; +use crate::{DataRegistry,Tournament, DATA_REGISTRY_SEED}; use anchor_spl::{associated_token::AssociatedToken, token_interface::{transfer_checked, Mint, TokenAccount, TokenInterface, TransferChecked}}; @@ -56,22 +56,13 @@ pub struct JoinTorunament<'info>{ )] pub data_registry: Account<'info, DataRegistry>, - #[account(mut)] - pub seller:SystemAccount<'info>, - #[account(mint::token_program = token_program)] pub mint: InterfaceAccount<'info, Mint>, - #[account( - mut, - seeds= [b"sales", seller.key().as_ref()], - bump = sales.bump - )] - pub sales: Account<'info, Sales>, #[account( mut, associated_token::mint= mint, - associated_token::authority = sales, + associated_token::authority = data_registry, associated_token::token_program = token_program )] vault: InterfaceAccount<'info, TokenAccount>, diff --git a/programs/tournaments/src/instructions/mod.rs b/programs/tournaments/src/instructions/mod.rs index 14a30ff..4d93a6e 100644 --- a/programs/tournaments/src/instructions/mod.rs +++ b/programs/tournaments/src/instructions/mod.rs @@ -1,9 +1,6 @@ pub mod initialize; pub use initialize::*; -pub mod add_seller; -pub use add_seller::*; - pub mod add_tournament; pub use add_tournament::*; diff --git a/programs/tournaments/src/instructions/purchase.rs b/programs/tournaments/src/instructions/purchase.rs index 15064a5..e1fe474 100644 --- a/programs/tournaments/src/instructions/purchase.rs +++ b/programs/tournaments/src/instructions/purchase.rs @@ -1,16 +1,16 @@ use anchor_lang::{prelude::*, solana_program::{native_token::LAMPORTS_PER_SOL, system_instruction}}; use anchor_spl::{ - associated_token::AssociatedToken, token_interface::{Mint, TokenAccount, TokenInterface, close_account, transfer_checked, CloseAccount, TransferChecked} + associated_token::AssociatedToken,token_interface::{transfer_checked, Mint, TokenAccount, TokenInterface, TransferChecked} }; -use crate::{error::CustomErrors, Sales}; +use crate::{error::CustomErrors, DataRegistry, DATA_REGISTRY_SEED}; use super::transfer_tokens; pub fn pay_seller(ctx:&Context, amount:u64)->Result<()>{ let from_account = &ctx.accounts.buyer; - let to_account = &ctx.accounts.seller; + let to_account = &ctx.accounts.author; let transfer_instruction = system_instruction::transfer( from_account.key, @@ -31,28 +31,27 @@ pub fn pay_seller(ctx:&Context, amount:u64)->Result<()>{ } pub fn handover_tickets(ctx:Context, amount:u64)->Result<()>{ - let seeds = &[ - b"sales", - ctx.accounts.seller.to_account_info().key.as_ref(), - &[ctx.accounts.sales.bump] - ]; - - let signer_seeds = [&seeds[..]]; - - let accounts = TransferChecked{ + 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() + authority: ctx.accounts.data_registry.to_account_info(), + mint: ctx.accounts.mint.to_account_info() }; + let bump = ctx.bumps.data_registry; + let seeds = &[ + &DATA_REGISTRY_SEED[..], + &[bump] + ]; - let cpi_context = CpiContext::new_with_signer( - ctx.accounts.token_program.to_account_info(), - accounts, - &signer_seeds + let signer_seeds = &[&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, amount *LAMPORTS_PER_SOL, 9) } @@ -62,12 +61,15 @@ pub struct PurchaseTickets<'info>{ #[account(mut)] pub buyer:Signer<'info>, - #[account(mut)] - pub seller:SystemAccount<'info>, - #[account(mint::token_program = token_program)] pub mint: InterfaceAccount<'info, Mint>, + #[account( + mut, + address = data_registry.authority + )] + pub author: SystemAccount<'info>, + #[account( init_if_needed, payer= buyer, @@ -76,16 +78,18 @@ pub struct PurchaseTickets<'info>{ associated_token::token_program = token_program )] pub buyer_token_account: Box>, + #[account( mut, - seeds= [b"sales", seller.key().as_ref()], - bump = sales.bump + seeds = [DATA_REGISTRY_SEED], + bump )] - pub sales: Account<'info, Sales>, + pub data_registry: Account<'info, DataRegistry>, + #[account( mut, associated_token::mint= mint, - associated_token::authority = sales, + associated_token::authority = data_registry, associated_token::token_program = token_program )] vault: InterfaceAccount<'info, TokenAccount>, diff --git a/programs/tournaments/src/instructions/set_data.rs b/programs/tournaments/src/instructions/set_data.rs index 5b5df55..c5148a8 100644 --- a/programs/tournaments/src/instructions/set_data.rs +++ b/programs/tournaments/src/instructions/set_data.rs @@ -2,7 +2,7 @@ use anchor_lang::prelude::*; use anchor_spl:: token::{Mint, Token, TokenAccount}; -use crate::{error::CustomErrors, DataRegistry, Sales, DATA_REGISTRY_SEED}; +use crate::{error::CustomErrors, DataRegistry, DATA_REGISTRY_SEED}; pub fn handler(ctx: Context, new_auth:Pubkey)->Result<()>{ require!(ctx.accounts.signer.key() == ctx.accounts.data_registry.authority, CustomErrors::Unauthorized); diff --git a/programs/tournaments/src/lib.rs b/programs/tournaments/src/lib.rs index 77ab60b..0cc2db6 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!("88dRZraTvmpqMs8GqtgCc321Foo5bWtDAsXJ1kqoc6nC"); +declare_id!("3owEKMMjFuuYN2HjT2J2GiVauDivNBeYqaUymyX7Q8Lw"); #[program] pub mod tournaments { use super::*; @@ -18,12 +18,6 @@ pub mod tournaments { initialize::handler(ctx) } - pub fn add_seller(ctx: Context, tickets_count:u64) -> Result<()> { - // msg!("seller ata: {}", ctx.accounts.seller_token_account.key()); - // add_seller::send_tickets_to_vault(&ctx, tickets_count)?; - add_seller::save_sales_account(ctx) - } - pub fn purchase_tickets(ctx:Context, amount:u64)->Result<()>{ let ticket_price: u64 =( LAMPORTS_PER_SOL / 100) * 3; purchase::pay_seller(&ctx, ticket_price)?; @@ -35,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 b75e9e7..8779dd4 100644 --- a/programs/tournaments/src/state/data_registry.rs +++ b/programs/tournaments/src/state/data_registry.rs @@ -1,9 +1,8 @@ -use anchor_lang::prelude::*; +use anchor_lang::{prelude::*, Bump}; #[account] #[derive(InitSpace)] pub struct DataRegistry{ - #[max_len(100)] - pub sales_pdas: Vec, + pub sales_pda: Pubkey, pub authority: Pubkey } \ No newline at end of file diff --git a/programs/tournaments/src/state/mod.rs b/programs/tournaments/src/state/mod.rs index b443ef0..3694262 100644 --- a/programs/tournaments/src/state/mod.rs +++ b/programs/tournaments/src/state/mod.rs @@ -1,6 +1,3 @@ -pub mod sales; -pub use sales::Sales; - pub mod data_registry; pub use data_registry::DataRegistry; diff --git a/programs/tournaments/src/state/sales.rs b/programs/tournaments/src/state/sales.rs deleted file mode 100644 index 7762b45..0000000 --- a/programs/tournaments/src/state/sales.rs +++ /dev/null @@ -1,10 +0,0 @@ -use anchor_lang::prelude::*; - -#[account] -#[derive(InitSpace)] -pub struct Sales{ - pub seller: Pubkey, - pub vault: Pubkey, - pub mint : Pubkey, - pub bump:u8 -} \ No newline at end of file diff --git a/tournaments.json b/tournaments.json new file mode 100644 index 0000000..193aad8 --- /dev/null +++ b/tournaments.json @@ -0,0 +1,636 @@ +{ + "address": "3owEKMMjFuuYN2HjT2J2GiVauDivNBeYqaUymyX7Q8Lw", + "metadata": { + "name": "tournaments", + "version": "0.1.0", + "spec": "0.1.0", + "description": "Created with Anchor" + }, + "instructions": [ + { + "name": "add_tournament", + "discriminator": [ + 86, + 126, + 171, + 66, + 224, + 148, + 167, + 201 + ], + "accounts": [ + { + "name": "payer", + "writable": true, + "signer": true + }, + { + "name": "tournament_account", + "writable": true, + "pda": { + "seeds": [ + { + "kind": "const", + "value": [ + 116, + 111, + 117, + 114, + 110, + 97, + 109, + 101, + 110, + 116 + ] + }, + { + "kind": "arg", + "path": "id" + } + ] + } + }, + { + "name": "data_registry", + "writable": true, + "pda": { + "seeds": [ + { + "kind": "const", + "value": [ + 115, + 97, + 108, + 101, + 115, + 95, + 114, + 101, + 103 + ] + } + ] + } + }, + { + "name": "system_program", + "address": "11111111111111111111111111111111" + } + ], + "args": [ + { + "name": "id", + "type": "u64" + }, + { + "name": "start_time", + "type": "string" + } + ] + }, + { + "name": "initialize", + "discriminator": [ + 175, + 175, + 109, + 31, + 13, + 152, + 155, + 237 + ], + "accounts": [ + { + "name": "signer", + "writable": true, + "signer": true + }, + { + "name": "data_registry", + "writable": true, + "pda": { + "seeds": [ + { + "kind": "const", + "value": [ + 115, + 97, + 108, + 101, + 115, + 95, + 114, + 101, + 103 + ] + } + ] + } + }, + { + "name": "mint" + }, + { + "name": "vault", + "writable": true, + "pda": { + "seeds": [ + { + "kind": "account", + "path": "data_registry" + }, + { + "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": "system_program", + "address": "11111111111111111111111111111111" + }, + { + "name": "token_program" + }, + { + "name": "associated_token_program", + "address": "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL" + } + ], + "args": [] + }, + { + "name": "join_tournament", + "discriminator": [ + 77, + 21, + 212, + 206, + 77, + 82, + 124, + 31 + ], + "accounts": [ + { + "name": "user", + "writable": true, + "signer": true + }, + { + "name": "tournament_account", + "writable": true, + "pda": { + "seeds": [ + { + "kind": "const", + "value": [ + 84, + 111, + 117, + 114, + 110, + 97, + 109, + 101, + 110, + 116 + ] + }, + { + "kind": "arg", + "path": "id" + } + ] + } + }, + { + "name": "data_registry", + "writable": true, + "pda": { + "seeds": [ + { + "kind": "const", + "value": [ + 115, + 97, + 108, + 101, + 115, + 95, + 114, + 101, + 103 + ] + } + ] + } + }, + { + "name": "mint" + }, + { + "name": "vault", + "writable": true, + "pda": { + "seeds": [ + { + "kind": "account", + "path": "data_registry" + }, + { + "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": "system_program", + "address": "11111111111111111111111111111111" + }, + { + "name": "token_program" + }, + { + "name": "associated_token_program", + "address": "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL" + } + ], + "args": [ + { + "name": "id", + "type": "u64" + } + ] + }, + { + "name": "purchase_tickets", + "discriminator": [ + 146, + 121, + 85, + 207, + 182, + 70, + 169, + 155 + ], + "accounts": [ + { + "name": "buyer", + "writable": true, + "signer": true + }, + { + "name": "mint" + }, + { + "name": "author", + "writable": true + }, + { + "name": "buyer_token_account", + "writable": true, + "pda": { + "seeds": [ + { + "kind": "account", + "path": "buyer" + }, + { + "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": "data_registry", + "writable": true, + "pda": { + "seeds": [ + { + "kind": "const", + "value": [ + 115, + 97, + 108, + 101, + 115, + 95, + 114, + 101, + 103 + ] + } + ] + } + }, + { + "name": "vault", + "writable": true, + "pda": { + "seeds": [ + { + "kind": "account", + "path": "data_registry" + }, + { + "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": "system_program", + "address": "11111111111111111111111111111111" + }, + { + "name": "token_program" + }, + { + "name": "associated_token_program", + "address": "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL" + } + ], + "args": [ + { + "name": "amount", + "type": "u64" + } + ] + } + ], + "accounts": [ + { + "name": "DataRegistry", + "discriminator": [ + 169, + 50, + 35, + 17, + 11, + 106, + 49, + 193 + ] + }, + { + "name": "Tournament", + "discriminator": [ + 175, + 139, + 119, + 242, + 115, + 194, + 57, + 92 + ] + } + ], + "errors": [ + { + "code": 6000, + "name": "CustomError", + "msg": "Custom error message" + }, + { + "code": 6001, + "name": "InsufficientFunds", + "msg": "Insufficient funds to purchase a ticket, Recharge your wallet and try again" + }, + { + "code": 6002, + "name": "Unauthorized", + "msg": "Only the owner can perform this action" + } + ], + "types": [ + { + "name": "DataRegistry", + "type": { + "kind": "struct", + "fields": [ + { + "name": "sales_pda", + "type": "pubkey" + }, + { + "name": "authority", + "type": "pubkey" + } + ] + } + }, + { + "name": "Tournament", + "type": { + "kind": "struct", + "fields": [ + { + "name": "id", + "type": "u64" + }, + { + "name": "participants", + "type": { + "vec": "pubkey" + } + }, + { + "name": "start_time", + "type": "string" + } + ] + } + } + ], + "constants": [ + { + "name": "DATA_REGISTRY_SEED", + "type": { + "array": [ + "u8", + 9 + ] + }, + "value": "[115, 97, 108, 101, 115, 95, 114, 101, 103]" + }, + { + "name": "ID", + "type": "string", + "value": "\"\"" + } + ] +} \ No newline at end of file