init purchase sep

This commit is contained in:
Sewmina Dilshan 2024-10-26 21:43:04 +05:30
parent ff1856c5bd
commit 4e17f2f443
10 changed files with 123 additions and 28 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@ target
node_modules node_modules
test-ledger test-ledger
.yarn .yarn
.env

View File

@ -5,7 +5,7 @@ resolution = true
skip-lint = false skip-lint = false
[programs.localnet] [programs.localnet]
ticket_store = "8w2kJrGZyPrhd2cN68x16wwjwaAmngZZcgudDz2k9C9Z" ticket_store = "6T3kQi1i8fHpeqWYcBDrxqv7TBSqW63YasajrnuLZRsf"
[registry] [registry]
url = "https://api.apr.dev" url = "https://api.apr.dev"

View File

@ -2,3 +2,6 @@ use anchor_lang::prelude::*;
#[constant] #[constant]
pub const ID: &str = ""; pub const ID: &str = "";
#[constant]
pub const SELLERS_REGISTRY_SEED:&[u8; 9] = b"sales_reg";

View File

@ -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<AddSeller>) -> 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(())
}

View File

@ -2,7 +2,7 @@ use anchor_lang::prelude::*;
use anchor_spl:: token::{Mint, Token, TokenAccount}; use anchor_spl:: token::{Mint, Token, TokenAccount};
use crate::Sales; use crate::{Sales, SellersRegistry, SELLERS_REGISTRY_SEED};
#[derive(Accounts)] #[derive(Accounts)]
pub struct Initialize <'info>{ pub struct Initialize <'info>{
@ -12,35 +12,17 @@ pub struct Initialize <'info>{
#[account( #[account(
init, init,
payer=signer, payer=signer,
space = 8 + Sales::INIT_SPACE, space= 8 + SellersRegistry::INIT_SPACE,
seeds= [b"sales", signer.key().as_ref()], seeds = [SELLERS_REGISTRY_SEED],
bump bump
)] )]
pub sales_account: Account<'info, Sales>, pub sellers_registry: Account<'info, SellersRegistry>,
#[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 system_program: Program<'info, System>, pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>,
} }
pub fn handler(ctx: Context<Initialize>) -> Result<()> { pub fn handler(ctx: Context<Initialize>) -> Result<()> {
let sales_account = &mut ctx.accounts.sales_account; msg!("Initiating Ticket store");
sales_account.owner = ctx.accounts.signer.key();
sales_account.seller_ata = ctx.accounts.signer_token_account.key();
Ok(()) Ok(())
} }

View File

@ -1,5 +1,8 @@
pub mod initialize; pub mod initialize;
pub use initialize::*; pub use initialize::*;
pub mod add_seller;
pub use add_seller::*;
pub mod purchase; pub mod purchase;
pub use purchase::*; pub use purchase::*;

View File

@ -9,7 +9,7 @@ pub use constants::*;
pub use instructions::*; pub use instructions::*;
pub use state::*; pub use state::*;
declare_id!("8w2kJrGZyPrhd2cN68x16wwjwaAmngZZcgudDz2k9C9Z"); declare_id!("5kgwVNdKDndFYNkUhZ5TvkNXxEJngMSczfS61qNALhzJ");
#[program] #[program]
pub mod ticket_store { pub mod ticket_store {
@ -19,6 +19,10 @@ pub mod ticket_store {
initialize::handler(ctx) initialize::handler(ctx)
} }
pub fn add_seller(ctx: Context<AddSeller>) -> Result<()> {
add_seller::handler(ctx)
}
pub fn purchase_tickets(ctx:Context<PurchaseTickets>, amount:u64)->Result<()>{ pub fn purchase_tickets(ctx:Context<PurchaseTickets>, amount:u64)->Result<()>{
purchase::purchase_ticket(ctx, amount) purchase::purchase_ticket(ctx, amount)
} }

View File

@ -1,2 +1,5 @@
pub mod sales; pub mod sales;
pub use sales::Sales; pub use sales::Sales;
pub mod sellers_registry;
pub use sellers_registry::SellersRegistry;

View File

@ -0,0 +1,8 @@
use anchor_lang::prelude::*;
#[account]
#[derive(InitSpace)]
pub struct SellersRegistry{
#[max_len(100)]
pub sales_pdas: Vec<Pubkey>
}

36
tests/init.ts Normal file
View File

@ -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<TicketStore>(IDL,provider);
console.log(program.programId);
await program.methods
.initialize()
.accounts({
mint: mintPubkey,
})
.signers([signer])
.rpc();
}