import { Connection, PublicKey, Keypair, clusterApiUrl, Transaction, sendAndConfirmTransaction } from "@solana/web3.js"; import {createAssociatedTokenAccountInstruction, getAssociatedTokenAddressSync} from "@solana/spl-token"; import { TicketStore } from "./ticket_store"; import { AnchorProvider, BN, Program, Wallet } from "@coral-xyz/anchor"; import { clusterUrl, cocSk, ticketsMint, TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID, ticketsSk } from "./keys"; const IDL = require('./ticket_store.json'); async function Init(){ const keypair = Keypair.fromSecretKey(Uint8Array.from(cocSk)); const mint = Keypair.fromSecretKey(Uint8Array.from(ticketsSk)); const connection = new Connection(clusterUrl); const provider = new AnchorProvider(connection, new Wallet(keypair)); const program: Program = new Program(IDL, provider); console.log(`Adding seller ${keypair.publicKey} with mint of ${ticketsMint}`); // const [sellerTicketATA] = await PublicKey.findProgramAddressSync([keypair.publicKey.toBuffer(), TOKEN_PROGRAM_ID.toBuffer(), ticketsMint.toBuffer()], ASSOCIATED_TOKEN_PROGRAM_ID); const sellerTicketATA = await getAssociatedTokenAddressSync(mint.publicKey, keypair.publicKey, false, TOKEN_PROGRAM_ID); console.log(`seller tickets ATA: ${sellerTicketATA.toBase58()}`); try{ const sellerTicketsBalance = await connection.getTokenAccountBalance(sellerTicketATA); console.log(`Seller has ${sellerTicketsBalance.value.uiAmount} Tickets, Putting 100 to sale`); }catch{ console.log("No ATA for seller found, Lets create one"); let tx = new Transaction(); tx.add( createAssociatedTokenAccountInstruction( keypair.publicKey, // payer sellerTicketATA, // ata keypair.publicKey, // owner ticketsMint, // mint TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID ) ); const tx_hash = await sendAndConfirmTransaction(connection, tx, [keypair,mint], { commitment: "finalized", }) console.log(`create ata txhash: ${tx_hash}`); } const tx = await program.methods.addSeller(new BN(100)).accounts({ mint:ticketsMint, tokenProgram: TOKEN_PROGRAM_ID }).rpc(); console.log(tx); } Init();