init and add working
This commit is contained in:
parent
4e17f2f443
commit
24f62ee65d
|
|
@ -5,7 +5,7 @@ resolution = true
|
||||||
skip-lint = false
|
skip-lint = false
|
||||||
|
|
||||||
[programs.localnet]
|
[programs.localnet]
|
||||||
ticket_store = "6T3kQi1i8fHpeqWYcBDrxqv7TBSqW63YasajrnuLZRsf"
|
ticket_store = "BX8z8nGWybFRW3rs6Novhp7oERFqek1QqfESXRB1GPWr"
|
||||||
|
|
||||||
[registry]
|
[registry]
|
||||||
url = "https://api.apr.dev"
|
url = "https://api.apr.dev"
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ pub fn handler(ctx: Context<AddSeller>) -> Result<()> {
|
||||||
|
|
||||||
sales_account.owner = ctx.accounts.signer.key();
|
sales_account.owner = ctx.accounts.signer.key();
|
||||||
sales_account.seller_ata = ctx.accounts.signer_token_account.key();
|
sales_account.seller_ata = ctx.accounts.signer_token_account.key();
|
||||||
|
sales_account.bump = ctx.bumps.sales_account;
|
||||||
ctx.accounts.sellers_registry.sales_pdas.push(sales_account.key());
|
ctx.accounts.sellers_registry.sales_pdas.push(sales_account.key());
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,19 +17,37 @@ pub fn purchase_ticket(ctx: Context<PurchaseTickets>, amount:u64)->Result<()>{
|
||||||
ctx.accounts.system_program.to_account_info(),
|
ctx.accounts.system_program.to_account_info(),
|
||||||
anchor_lang::system_program::Transfer {
|
anchor_lang::system_program::Transfer {
|
||||||
from: ctx.accounts.payer.to_account_info(),
|
from: ctx.accounts.payer.to_account_info(),
|
||||||
to: ctx.accounts.owner.to_account_info(),
|
to: ctx.accounts.owner.to_account_info()
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
total_price,
|
total_price,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
// Transfer tokens from seller_ata to buyer_ata
|
// Transfer tokens from seller_ata to buyer_ata
|
||||||
|
|
||||||
|
let owner_key = ctx.accounts.owner.key();
|
||||||
|
let mint_key = ctx.accounts.mint.key();
|
||||||
|
|
||||||
|
let seeds = &[
|
||||||
|
b"ticket_seller".as_ref(),
|
||||||
|
owner_key.as_ref(),
|
||||||
|
mint_key.as_ref(),
|
||||||
|
&[ctx.accounts.sales.bump],
|
||||||
|
];
|
||||||
|
|
||||||
|
let signer_seeds = [&seeds[..]];
|
||||||
|
|
||||||
let cpi_accounts = anchor_spl::token::Transfer {
|
let cpi_accounts = anchor_spl::token::Transfer {
|
||||||
from: ctx.accounts.seller_ata.to_account_info(),
|
from: ctx.accounts.seller_ata.to_account_info(),
|
||||||
to: ctx.accounts.buyer_ata.to_account_info(),
|
to: ctx.accounts.buyer_ata.to_account_info(),
|
||||||
authority: sales_account.to_account_info(),
|
authority: ctx.accounts.sales.to_account_info(),
|
||||||
};
|
};
|
||||||
let cpi_context = CpiContext::new(ctx.accounts.token_program.to_account_info(), cpi_accounts);
|
|
||||||
|
let cpi_context = CpiContext::new_with_signer(
|
||||||
|
ctx.accounts.token_program.to_account_info(),
|
||||||
|
cpi_accounts,
|
||||||
|
&signer_seeds
|
||||||
|
);
|
||||||
token::transfer(cpi_context, amount)?;
|
token::transfer(cpi_context, amount)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
@ -42,7 +60,7 @@ pub struct PurchaseTickets<'info>{
|
||||||
pub payer:Signer<'info>,
|
pub payer:Signer<'info>,
|
||||||
|
|
||||||
#[account(mut)]
|
#[account(mut)]
|
||||||
pub owner:Signer<'info>,
|
pub owner:SystemAccount<'info>,
|
||||||
|
|
||||||
#[account(
|
#[account(
|
||||||
init_if_needed,
|
init_if_needed,
|
||||||
|
|
@ -53,9 +71,21 @@ pub struct PurchaseTickets<'info>{
|
||||||
pub buyer_ata:Account<'info, TokenAccount>,
|
pub buyer_ata:Account<'info, TokenAccount>,
|
||||||
pub mint: Account<'info, Mint>,
|
pub mint: Account<'info, Mint>,
|
||||||
|
|
||||||
#[account(mut, has_one=owner, has_one=seller_ata)]
|
#[account(
|
||||||
|
mut,
|
||||||
|
has_one=seller_ata,
|
||||||
|
seeds= [b"sales", owner.key().as_ref()],
|
||||||
|
bump = sales.bump
|
||||||
|
)]
|
||||||
pub sales: Account<'info, Sales>,
|
pub sales: Account<'info, Sales>,
|
||||||
|
|
||||||
|
#[account(
|
||||||
|
mut,
|
||||||
|
seeds = [b"ticket_seller".as_ref(), owner.key().as_ref(), mint.key().as_ref()],
|
||||||
|
bump,
|
||||||
|
token::mint = mint,
|
||||||
|
token::authority = sales,
|
||||||
|
)]
|
||||||
pub seller_ata: Account<'info, TokenAccount>,
|
pub seller_ata: Account<'info, TokenAccount>,
|
||||||
|
|
||||||
// /// CHECK:
|
// /// CHECK:
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ pub use constants::*;
|
||||||
pub use instructions::*;
|
pub use instructions::*;
|
||||||
pub use state::*;
|
pub use state::*;
|
||||||
|
|
||||||
declare_id!("5kgwVNdKDndFYNkUhZ5TvkNXxEJngMSczfS61qNALhzJ");
|
declare_id!("BX8z8nGWybFRW3rs6Novhp7oERFqek1QqfESXRB1GPWr");
|
||||||
|
|
||||||
#[program]
|
#[program]
|
||||||
pub mod ticket_store {
|
pub mod ticket_store {
|
||||||
|
|
|
||||||
|
|
@ -6,5 +6,6 @@ pub struct Sales{
|
||||||
pub owner: Pubkey,
|
pub owner: Pubkey,
|
||||||
pub seller_ata: Pubkey,
|
pub seller_ata: Pubkey,
|
||||||
pub sales_count:u64,
|
pub sales_count:u64,
|
||||||
pub last_buyer: Pubkey
|
pub last_buyer: Pubkey,
|
||||||
|
pub bump:u8
|
||||||
}
|
}
|
||||||
|
|
@ -55,6 +55,7 @@ describe("TicketStore - Sales Initialization", () => {
|
||||||
program.programId
|
program.programId
|
||||||
);
|
);
|
||||||
|
|
||||||
|
await program.methods.initialize().rpc();
|
||||||
// Call the initialize function
|
// Call the initialize function
|
||||||
await program.methods
|
await program.methods
|
||||||
.initialize()
|
.initialize()
|
||||||
|
|
|
||||||
96
tests/ticket_store.ts.working.bkp
Normal file
96
tests/ticket_store.ts.working.bkp
Normal file
|
|
@ -0,0 +1,96 @@
|
||||||
|
import * as anchor from "@coral-xyz/anchor";
|
||||||
|
import { Program } from "@coral-xyz/anchor";
|
||||||
|
import { 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 } from "@solana/spl-token";
|
||||||
|
import { expect } from "chai";
|
||||||
|
|
||||||
|
describe("TicketStore - Sales Initialization", () => {
|
||||||
|
|
||||||
|
const signer = Keypair.generate();
|
||||||
|
const buyer = Keypair.generate();
|
||||||
|
|
||||||
|
// Set up the Anchor provider
|
||||||
|
const provider = new anchor.AnchorProvider(anchor.AnchorProvider.env().connection, new anchor.Wallet(signer));
|
||||||
|
anchor.setProvider(provider);
|
||||||
|
|
||||||
|
// Get the program using its IDL and TypeScript type
|
||||||
|
const program = anchor.workspace.TicketStore as Program<TicketStore>;
|
||||||
|
|
||||||
|
|
||||||
|
let salesAccountPda = null;
|
||||||
|
let signerTokenAccountPda = null;
|
||||||
|
let mintAccount = null;
|
||||||
|
|
||||||
|
before(async()=>{
|
||||||
|
const connection = provider.connection;
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
it("Initializes the sales account and token account", async () => {
|
||||||
|
console.log(program.programId);
|
||||||
|
// Request an airdrop to fund the signer
|
||||||
|
const connection = provider.connection;
|
||||||
|
await connection.confirmTransaction(
|
||||||
|
await connection.requestAirdrop(signer.publicKey, anchor.web3.LAMPORTS_PER_SOL * 100)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Create a new mint account for tokens
|
||||||
|
mintAccount = await createMint(
|
||||||
|
provider.connection,
|
||||||
|
signer, // Fee payer
|
||||||
|
signer.publicKey, // Mint authority
|
||||||
|
null, // Freeze authority
|
||||||
|
9 // Decimals
|
||||||
|
);
|
||||||
|
|
||||||
|
// Derive PDAs for the sales and token accounts
|
||||||
|
[salesAccountPda] = await PublicKey.findProgramAddress(
|
||||||
|
[Buffer.from("sales"), signer.publicKey.toBuffer()],
|
||||||
|
program.programId
|
||||||
|
);
|
||||||
|
|
||||||
|
[signerTokenAccountPda] = await PublicKey.findProgramAddress(
|
||||||
|
[Buffer.from("ticket_seller"), signer.publicKey.toBuffer(), mintAccount.toBuffer()],
|
||||||
|
program.programId
|
||||||
|
);
|
||||||
|
|
||||||
|
await program.methods.initialize().rpc();
|
||||||
|
// Call the initialize function
|
||||||
|
await program.methods
|
||||||
|
.initialize()
|
||||||
|
.accounts({
|
||||||
|
mint: mintAccount,
|
||||||
|
})
|
||||||
|
.signers([signer])
|
||||||
|
.rpc();
|
||||||
|
|
||||||
|
// Fetch account data to verify initialization
|
||||||
|
const salesAccount = await program.account.sales.fetch(salesAccountPda);
|
||||||
|
const signerTokenAccount = await getAccount(provider.connection, signerTokenAccountPda);
|
||||||
|
console.log(salesAccount);
|
||||||
|
await mintTo(provider.connection, signer, mintAccount, signerTokenAccountPda, signer, LAMPORTS_PER_SOL);
|
||||||
|
// Assertions
|
||||||
|
expect(salesAccount.owner.toBase58()).to.equal(signer.publicKey.toBase58());
|
||||||
|
expect(salesAccount.sellerAta.toBase58()).to.equal(signerTokenAccountPda.toBase58());
|
||||||
|
expect(signerTokenAccount.mint.toBase58()).to.equal(mintAccount.toBase58());
|
||||||
|
expect(signerTokenAccount.owner.toBase58()).to.equal(salesAccountPda.toBase58());
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Purchases a ticket", async()=>{
|
||||||
|
const provider = new anchor.AnchorProvider(anchor.AnchorProvider.env().connection, new anchor.Wallet(buyer));
|
||||||
|
anchor.setProvider(provider);
|
||||||
|
console.log(`requesting airdrop to ${buyer.publicKey}`);
|
||||||
|
const airdropTx = await provider.connection.requestAirdrop(buyer.publicKey, LAMPORTS_PER_SOL * 100);
|
||||||
|
await provider.connection.confirmTransaction(airdropTx);
|
||||||
|
|
||||||
|
console.log(await provider.connection.getBalance(buyer.publicKey));
|
||||||
|
|
||||||
|
await program.methods.purchaseTickets(new anchor.BN(1)).accounts(
|
||||||
|
{signer:buyer.publicKey,sales: salesAccountPda,owner:signer.publicKey,mint:mintAccount, sellerAta:signerTokenAccountPda}
|
||||||
|
).rpc();
|
||||||
|
|
||||||
|
const salesAccount = await program.account.sales.fetch(salesAccountPda);
|
||||||
|
console.log(salesAccount);
|
||||||
|
})
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue
Block a user