From 4e17f2f4433fa5bd7d9daa97a0ae6338e96be297 Mon Sep 17 00:00:00 2001 From: Sewmina Dilshan Date: Sat, 26 Oct 2024 21:43:04 +0530 Subject: [PATCH] init purchase sep --- .gitignore | 1 + Anchor.toml | 2 +- programs/ticket_store/src/constants.rs | 5 +- .../src/instructions/add_seller.rs | 55 +++++++++++++++++++ .../src/instructions/initialize.rs | 30 ++-------- programs/ticket_store/src/instructions/mod.rs | 3 + programs/ticket_store/src/lib.rs | 6 +- programs/ticket_store/src/state/mod.rs | 5 +- .../src/state/sellers_registry.rs | 8 +++ tests/init.ts | 36 ++++++++++++ 10 files changed, 123 insertions(+), 28 deletions(-) create mode 100644 programs/ticket_store/src/instructions/add_seller.rs create mode 100644 programs/ticket_store/src/state/sellers_registry.rs create mode 100644 tests/init.ts diff --git a/.gitignore b/.gitignore index 2e0446b..6dd3a37 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ target node_modules test-ledger .yarn +.env diff --git a/Anchor.toml b/Anchor.toml index a1d4cbf..4eb6419 100644 --- a/Anchor.toml +++ b/Anchor.toml @@ -5,7 +5,7 @@ resolution = true skip-lint = false [programs.localnet] -ticket_store = "8w2kJrGZyPrhd2cN68x16wwjwaAmngZZcgudDz2k9C9Z" +ticket_store = "6T3kQi1i8fHpeqWYcBDrxqv7TBSqW63YasajrnuLZRsf" [registry] url = "https://api.apr.dev" diff --git a/programs/ticket_store/src/constants.rs b/programs/ticket_store/src/constants.rs index a8ff21e..b476eb6 100644 --- a/programs/ticket_store/src/constants.rs +++ b/programs/ticket_store/src/constants.rs @@ -1,4 +1,7 @@ use anchor_lang::prelude::*; #[constant] -pub const ID: &str = ""; \ No newline at end of file +pub const ID: &str = ""; + +#[constant] +pub const SELLERS_REGISTRY_SEED:&[u8; 9] = b"sales_reg"; \ No newline at end of file diff --git a/programs/ticket_store/src/instructions/add_seller.rs b/programs/ticket_store/src/instructions/add_seller.rs new file mode 100644 index 0000000..7769fea --- /dev/null +++ b/programs/ticket_store/src/instructions/add_seller.rs @@ -0,0 +1,55 @@ +use anchor_lang::prelude::*; +use anchor_spl:: token::{Mint, Token, TokenAccount}; + + +use crate::{Sales, SellersRegistry, SELLERS_REGISTRY_SEED}; + +#[derive(Accounts)] +pub struct AddSeller <'info>{ + #[account(mut)] + pub signer: Signer<'info>, + + #[account( + init, + payer = signer, + space = 8 + Sales::INIT_SPACE, + seeds= [b"sales", signer.key().as_ref()], + bump + )] + pub sales_account: Account<'info, Sales>, + + #[account( + init, + payer = signer, + seeds = [b"ticket_seller".as_ref(), signer.key().as_ref(), mint.key().as_ref()], + bump, + token::mint = mint, + token::authority = sales_account, + )] + pub signer_token_account: Account<'info, TokenAccount>, + + #[account( + mut, + seeds=[SELLERS_REGISTRY_SEED], + bump + )] + pub sellers_registry: Account<'info, SellersRegistry>, + + pub mint: Account<'info, Mint>, + + pub system_program: Program<'info, System>, + pub token_program: Program<'info, Token>, + +} + +pub fn handler(ctx: Context) -> Result<()> { + let sales_account = &mut ctx.accounts.sales_account; + + sales_account.owner = ctx.accounts.signer.key(); + sales_account.seller_ata = ctx.accounts.signer_token_account.key(); + + ctx.accounts.sellers_registry.sales_pdas.push(sales_account.key()); + + + Ok(()) +} diff --git a/programs/ticket_store/src/instructions/initialize.rs b/programs/ticket_store/src/instructions/initialize.rs index 20b461d..be36c1c 100644 --- a/programs/ticket_store/src/instructions/initialize.rs +++ b/programs/ticket_store/src/instructions/initialize.rs @@ -2,7 +2,7 @@ use anchor_lang::prelude::*; use anchor_spl:: token::{Mint, Token, TokenAccount}; -use crate::Sales; +use crate::{Sales, SellersRegistry, SELLERS_REGISTRY_SEED}; #[derive(Accounts)] pub struct Initialize <'info>{ @@ -11,36 +11,18 @@ pub struct Initialize <'info>{ #[account( init, - payer = signer, - space = 8 + Sales::INIT_SPACE, - seeds= [b"sales", signer.key().as_ref()], + payer=signer, + space= 8 + SellersRegistry::INIT_SPACE, + seeds = [SELLERS_REGISTRY_SEED], bump )] - pub sales_account: Account<'info, Sales>, - - #[account( - init, - payer = signer, - seeds = [b"ticket_seller".as_ref(), signer.key().as_ref(), mint.key().as_ref()], - bump, - token::mint = mint, - token::authority = sales_account, - )] - pub signer_token_account: Account<'info, TokenAccount>, - - pub mint: Account<'info, Mint>, + pub sellers_registry: Account<'info, SellersRegistry>, pub system_program: Program<'info, System>, - pub token_program: Program<'info, Token>, } pub fn handler(ctx: Context) -> Result<()> { - let sales_account = &mut ctx.accounts.sales_account; - - sales_account.owner = ctx.accounts.signer.key(); - sales_account.seller_ata = ctx.accounts.signer_token_account.key(); - - + msg!("Initiating Ticket store"); Ok(()) } diff --git a/programs/ticket_store/src/instructions/mod.rs b/programs/ticket_store/src/instructions/mod.rs index bd3c8b5..74c4fd6 100644 --- a/programs/ticket_store/src/instructions/mod.rs +++ b/programs/ticket_store/src/instructions/mod.rs @@ -1,5 +1,8 @@ pub mod initialize; pub use initialize::*; +pub mod add_seller; +pub use add_seller::*; + pub mod purchase; pub use purchase::*; \ No newline at end of file diff --git a/programs/ticket_store/src/lib.rs b/programs/ticket_store/src/lib.rs index 2914cc6..c038bf8 100644 --- a/programs/ticket_store/src/lib.rs +++ b/programs/ticket_store/src/lib.rs @@ -9,7 +9,7 @@ pub use constants::*; pub use instructions::*; pub use state::*; -declare_id!("8w2kJrGZyPrhd2cN68x16wwjwaAmngZZcgudDz2k9C9Z"); +declare_id!("5kgwVNdKDndFYNkUhZ5TvkNXxEJngMSczfS61qNALhzJ"); #[program] pub mod ticket_store { @@ -19,6 +19,10 @@ pub mod ticket_store { initialize::handler(ctx) } + pub fn add_seller(ctx: Context) -> Result<()> { + add_seller::handler(ctx) + } + pub fn purchase_tickets(ctx:Context, amount:u64)->Result<()>{ purchase::purchase_ticket(ctx, amount) } diff --git a/programs/ticket_store/src/state/mod.rs b/programs/ticket_store/src/state/mod.rs index de5133b..bf6f847 100644 --- a/programs/ticket_store/src/state/mod.rs +++ b/programs/ticket_store/src/state/mod.rs @@ -1,2 +1,5 @@ pub mod sales; -pub use sales::Sales; \ No newline at end of file +pub use sales::Sales; + +pub mod sellers_registry; +pub use sellers_registry::SellersRegistry; \ No newline at end of file diff --git a/programs/ticket_store/src/state/sellers_registry.rs b/programs/ticket_store/src/state/sellers_registry.rs new file mode 100644 index 0000000..0d9d59f --- /dev/null +++ b/programs/ticket_store/src/state/sellers_registry.rs @@ -0,0 +1,8 @@ +use anchor_lang::prelude::*; + +#[account] +#[derive(InitSpace)] +pub struct SellersRegistry{ + #[max_len(100)] + pub sales_pdas: Vec +} \ No newline at end of file diff --git a/tests/init.ts b/tests/init.ts new file mode 100644 index 0000000..b9ab966 --- /dev/null +++ b/tests/init.ts @@ -0,0 +1,36 @@ +import * as anchor from "@coral-xyz/anchor"; +import { Program } from "@coral-xyz/anchor"; +import { clusterApiUrl, Connection, Keypair, LAMPORTS_PER_SOL, PublicKey, SystemProgram } from "@solana/web3.js"; +import { TicketStore } from "../target/types/ticket_store"; +import { createMint, createAccount, getAccount, TOKEN_PROGRAM_ID, mintTo, getMint } from "@solana/spl-token"; + + +const mintPubkey = new PublicKey("tktUDLZhFGb9VW9zDxZ7HYDFuBooEf8daZEvPbBY7at"); +const programId = new PublicKey("5kgwVNdKDndFYNkUhZ5TvkNXxEJngMSczfS61qNALhzJ"); +const signer = Keypair.fromSecretKey(Uint8Array.from([202,150,67,41,155,133,176,172,9,100,150,190,239,37,69,73,18,16,76,65,164,197,99,134,240,151,112,65,61,122,95,41,9,44,6,237,108,123,86,90,144,27,1,160,101,95,239,35,53,91,195,220,22,214,2,84,132,37,20,236,133,242,104,197])); + +const IDL = require('../target/idl/ticket_store.json'); + +// Call the initialize function +Initialize().then(()=>{ + console.log("Init complete"); +}); + +async function Initialize(){ + const connection = new Connection(clusterApiUrl("devnet")); + const provider = new anchor.AnchorProvider(connection, new anchor.Wallet(signer)); + + anchor.setProvider(provider); + const program = new Program(IDL,provider); + console.log(program.programId); + + + + await program.methods + .initialize() + .accounts({ + mint: mintPubkey, + }) + .signers([signer]) + .rpc(); +}