init purchase sep
This commit is contained in:
parent
ff1856c5bd
commit
4e17f2f443
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -5,3 +5,4 @@ target
|
|||
node_modules
|
||||
test-ledger
|
||||
.yarn
|
||||
.env
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ resolution = true
|
|||
skip-lint = false
|
||||
|
||||
[programs.localnet]
|
||||
ticket_store = "8w2kJrGZyPrhd2cN68x16wwjwaAmngZZcgudDz2k9C9Z"
|
||||
ticket_store = "6T3kQi1i8fHpeqWYcBDrxqv7TBSqW63YasajrnuLZRsf"
|
||||
|
||||
[registry]
|
||||
url = "https://api.apr.dev"
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
use anchor_lang::prelude::*;
|
||||
|
||||
#[constant]
|
||||
pub const ID: &str = "";
|
||||
pub const ID: &str = "";
|
||||
|
||||
#[constant]
|
||||
pub const SELLERS_REGISTRY_SEED:&[u8; 9] = b"sales_reg";
|
||||
55
programs/ticket_store/src/instructions/add_seller.rs
Normal file
55
programs/ticket_store/src/instructions/add_seller.rs
Normal 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(())
|
||||
}
|
||||
|
|
@ -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<Initialize>) -> 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(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
pub mod initialize;
|
||||
pub use initialize::*;
|
||||
|
||||
pub mod add_seller;
|
||||
pub use add_seller::*;
|
||||
|
||||
pub mod purchase;
|
||||
pub use purchase::*;
|
||||
|
|
@ -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<AddSeller>) -> Result<()> {
|
||||
add_seller::handler(ctx)
|
||||
}
|
||||
|
||||
pub fn purchase_tickets(ctx:Context<PurchaseTickets>, amount:u64)->Result<()>{
|
||||
purchase::purchase_ticket(ctx, amount)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,2 +1,5 @@
|
|||
pub mod sales;
|
||||
pub use sales::Sales;
|
||||
pub use sales::Sales;
|
||||
|
||||
pub mod sellers_registry;
|
||||
pub use sellers_registry::SellersRegistry;
|
||||
8
programs/ticket_store/src/state/sellers_registry.rs
Normal file
8
programs/ticket_store/src/state/sellers_registry.rs
Normal 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
36
tests/init.ts
Normal 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();
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user