73 lines
3.2 KiB
TypeScript
73 lines
3.2 KiB
TypeScript
import { Connection, PublicKey, Keypair, clusterApiUrl } from "@solana/web3.js";
|
|
import { TicketStore } from "./ticket_store";
|
|
import { AnchorProvider, BN, Program, Wallet } from "@coral-xyz/anchor";
|
|
import { clusterUrl, testerSk, ticketsMint } from "./keys";
|
|
import { ASSOCIATED_PROGRAM_ID, TOKEN_PROGRAM_ID } from "@coral-xyz/anchor/dist/cjs/utils/token";
|
|
const IDL = require('./ticket_store.json');
|
|
|
|
async function Init(){
|
|
const keypair = Keypair.fromSecretKey(Uint8Array.from(testerSk));
|
|
const connection = new Connection(clusterUrl);
|
|
const provider = new AnchorProvider(connection, new Wallet(keypair));
|
|
const program: Program<TicketStore> = new Program<TicketStore>(IDL, provider);
|
|
|
|
let solBalance = await connection.getBalance(keypair.publicKey);
|
|
console.log(`Tester ${keypair.publicKey} has ${solBalance} SOL`);
|
|
if(solBalance <= 1){
|
|
console.log("Requesting airdrop due to insufficient funds");
|
|
try{
|
|
await connection.requestAirdrop(keypair.publicKey, 5);
|
|
}catch{
|
|
console.log("Airdrop failed");
|
|
}
|
|
solBalance = await connection.getBalance(keypair.publicKey);
|
|
console.log(`Tester now has ${solBalance} SOL`);
|
|
}
|
|
let ticketsBalanceBefore = 0;
|
|
const [buyerAta] = await PublicKey.findProgramAddress([keypair.publicKey.toBuffer(), TOKEN_PROGRAM_ID.toBuffer(), ticketsMint.toBuffer()], ASSOCIATED_PROGRAM_ID);
|
|
try{
|
|
console.log(`buyers ATA is: ${buyerAta.toBase58()}`);
|
|
ticketsBalanceBefore = (await connection.getTokenAccountBalance(buyerAta)).value.uiAmount;
|
|
console.log(`Tester has ${ticketsBalanceBefore} tickets already`);
|
|
}catch{
|
|
console.log("Tester doesn't even has an ATA for this token");
|
|
}
|
|
|
|
const [sellers_reg_pda] = await PublicKey.findProgramAddress([Buffer.from("sales_reg")], program.programId);
|
|
console.log(`Sellers Reg PDA: ${sellers_reg_pda}`);
|
|
|
|
const sellers_reg_acc = await program.account.sellersRegistry.fetch(sellers_reg_pda);
|
|
console.log(sellers_reg_acc);
|
|
|
|
let chosenSellerOwner;
|
|
for(let i =0; i < sellers_reg_acc.salesPdas.length; i++){
|
|
const salesPda = sellers_reg_acc.salesPdas[i];
|
|
const salesAcc = await program.account.sales.fetch(salesPda);
|
|
const sellerTokenHoldings = await connection.getTokenAccountBalance(salesAcc.vault);
|
|
|
|
const output = {
|
|
owner: salesAcc.seller.toBase58(),
|
|
seller_vault_balance: sellerTokenHoldings
|
|
}
|
|
|
|
if(sellerTokenHoldings.value.uiAmount > 10){
|
|
chosenSellerOwner = salesAcc.seller;
|
|
}
|
|
|
|
console.log("====Sales Account====")
|
|
console.log(output);
|
|
}
|
|
console.log(`Buying from seller account ${chosenSellerOwner}`);
|
|
const tx = await program.methods.purchaseTickets(new BN(1)).accounts({
|
|
seller:chosenSellerOwner,
|
|
mint: ticketsMint,
|
|
tokenProgram: TOKEN_PROGRAM_ID
|
|
}).rpc();
|
|
console.log(`purchase method called : ${tx}`)
|
|
await connection.confirmTransaction(tx);
|
|
const newTicketsBalance = (await connection.getTokenAccountBalance(buyerAta)).value.uiAmount;
|
|
console.log(`Tester had ${ticketsBalanceBefore} tickets, now he has ${newTicketsBalance} tickets.`);
|
|
}
|
|
|
|
|
|
Init(); |