96 lines
3.6 KiB
TypeScript
96 lines
3.6 KiB
TypeScript
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
|
|
);
|
|
|
|
// 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);
|
|
})
|
|
});
|