"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.previewJoinTournamentCost = previewJoinTournamentCost; exports.JoinTournament = JoinTournament; exports.AddTournament = AddTournament; const web3_js_1 = require("@solana/web3.js"); const shared_1 = require("./shared"); const anchor_1 = require("@coral-xyz/anchor"); const bs58_1 = __importDefault(require("bs58")); const rpc_1 = require("@coral-xyz/anchor/dist/cjs/utils/rpc"); function previewJoinTournamentCost(privateKey, id) { return __awaiter(this, void 0, void 0, function* () { var _a; const keypair = web3_js_1.Keypair.fromSecretKey(bs58_1.default.decode(privateKey)); const provider = new anchor_1.AnchorProvider(shared_1.connection, new anchor_1.Wallet(keypair)); const program = new anchor_1.Program(shared_1.IDL, provider); const tourneyId = new anchor_1.BN(id); const tourneyIdBuffer = tourneyId.toArrayLike(Buffer, 'le', 8); const [tourneyPda] = yield web3_js_1.PublicKey.findProgramAddressSync([shared_1.TOURNAMENT_SEED, tourneyIdBuffer], program.programId); const tourneyAcc = yield program.account.tournament.fetch(tourneyPda); // Simulate the transaction to preview cost const transaction = new web3_js_1.Transaction().add(yield program.methods.joinTournament(tourneyId).instruction()); transaction.recentBlockhash = (yield shared_1.connection.getLatestBlockhash('finalized')).blockhash; transaction.feePayer = keypair.publicKey; const { value } = yield (0, rpc_1.simulateTransaction)(shared_1.connection, transaction); // The 'value' contains the fee and log messages; you can access the fee estimate here if (value.err) { console.error("Simulation error:", value.err); return; } // Estimate the fee using getFeeForMessage const message = transaction.compileMessage(); const feeEstimate = yield shared_1.connection.getFeeForMessage(message); if (feeEstimate) { console.log(`Estimated transaction fee: ${feeEstimate} lamports`); const entryFee = tourneyAcc.entryFee; return ((_a = feeEstimate.value) !== null && _a !== void 0 ? _a : 0) + parseInt(entryFee.toString()); } else { console.error("Could not retrieve fee estimate."); return null; } }); } function JoinTournament(privateKey, id) { return __awaiter(this, void 0, void 0, function* () { const keypair = web3_js_1.Keypair.fromSecretKey(bs58_1.default.decode(privateKey)); const provider = new anchor_1.AnchorProvider(shared_1.connection, new anchor_1.Wallet(keypair)); const program = new anchor_1.Program(shared_1.IDL, provider); const solBalance = yield shared_1.connection.getBalance(keypair.publicKey); const tourneyId = new anchor_1.BN(id); const tourneyIdBuffer = tourneyId.toArrayLike(Buffer, 'le', 8); const [tourneyPda] = yield web3_js_1.PublicKey.findProgramAddressSync([shared_1.TOURNAMENT_SEED, tourneyIdBuffer], program.programId); const tourneyAcc = yield program.account.tournament.fetch(tourneyPda); const tx = yield program.methods.joinTournament(tourneyId).rpc(); return tx; }); } function AddTournament(privateKey, id, name, start_time, entry_fee_lamports, rewards1, rewards2) { return __awaiter(this, void 0, void 0, function* () { const keypair = web3_js_1.Keypair.fromSecretKey(bs58_1.default.decode(privateKey)); const provider = new anchor_1.AnchorProvider(shared_1.connection, new anchor_1.Wallet(keypair)); const program = new anchor_1.Program(shared_1.IDL, provider); const tx = yield program.methods.addTournament(new anchor_1.BN(id), new anchor_1.BN(entry_fee_lamports), start_time, name, new anchor_1.BN(rewards1), new anchor_1.BN(rewards2)).rpc(); return tx; }); } // export async function PurchaseTicket(privateKey:string, amount:number){ // const totalPrice = amount * TICKET_PRICE; // if(solBalance < totalPrice){ // console.log(`${keypair.publicKey} tried to buy ${amount} tickets (${totalPrice} SOL). But they had only ${solBalance}`); // return false; // } // const [buyerAta] = await PublicKey.findProgramAddress([keypair.publicKey.toBuffer(), TOKEN_PROGRAM_ID.toBuffer(), TICKETS_MINT.toBuffer()], ASSOCIATED_TOKEN_PROGRAM_ID); // let ticketsBalanceBefore = 0; // console.log(`buyers ATA is: ${buyerAta.toBase58()}`); // try{ // ticketsBalanceBefore = (await connection.getTokenAccountBalance(buyerAta)).value.uiAmount ?? 0; // console.log(`buyer has ${ticketsBalanceBefore} tickets already`); // }catch{ // const buyerAta = await getAssociatedTokenAddress(TICKETS_MINT, keypair.publicKey, false, TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID); // console.log("Buyer doesn't even has an ATA for this token, creating one"); // const createAtaInstruction = createAssociatedTokenAccountInstruction( // keypair.publicKey, // Payer // buyerAta, // Associated Token Account to create // keypair.publicKey, // Owner of the account // TICKETS_MINT, // Token mint address // TOKEN_PROGRAM_ID, // Token program ID // ASSOCIATED_TOKEN_PROGRAM_ID // Associated token program ID // ); // const transaction = new Transaction().add(createAtaInstruction); // await provider.sendAndConfirm(transaction, [keypair]); // console.log(`Created ATA: ${buyerAta.toBase58()}`); // console.log(`Buyer ATA Created: ${buyerAta}`); // } // const [sellers_reg_pda] = await PublicKey.findProgramAddress([Buffer.from("sales_reg")], program.programId); // console.log(`Sellers Reg PDA: ${sellers_reg_pda}`); // const seller = new PublicKey('cocD4r4yNpHxPq7CzUebxEMyLki3X4d2Y3HcTX5ptUc'); // // Make the transaction to purchase tickets // try{ // console.log(`Purchasing ${amount} tickets from ${seller.toBase58()}`); // const tx = await program.methods.purchaseTickets(new BN(1)).accounts({ // seller:seller, // mint: TICKETS_MINT, // tokenProgram: TOKEN_PROGRAM_ID, // }).rpc(); // console.log(`Ticket purchase transaction successful: ${tx}`); // return tx; // }catch(e){ // console.log("Failed to purchase tickets") // console.log(e); // return null; // } // }