import * as anchor from "@project-serum/anchor"; import { getAssociatedTokenAddress, getAccount, createAssociatedTokenAccountInstruction, } from "@solana/spl-token"; import { TOKEN_METADATA_PROGRAM_ID, STRIKR_COLLECTION_MINT, STRIKR_TREASURY, } from "../constants"; import { getStrikr, getStrikrAuthority } from "../utils"; import { requestComputeUnits } from "../actions/request-compute-units"; /** * Create a put option request as an option buyer * @param provider Anchor provider * @param assetNftMint Asset NFT which underlies the option * @param assetNftTokenAccount Token account containing the asset NFT * @returns Promise resolving to the transaction signature */ export const createPutOption = async ( provider: anchor.AnchorProvider, assetNftMint: anchor.web3.PublicKey, strikePrice: anchor.BN, duration: anchor.BN, premium: anchor.BN, isGraceIncluded: boolean = false, assetNftTokenAccount?: anchor.web3.PublicKey ) => { const strikr = getStrikr(provider); const transaction = new anchor.web3.Transaction(); const buyerPositionNftMintKeypair = new anchor.web3.Keypair(); const buyerPositionNftMint = buyerPositionNftMintKeypair.publicKey; // Request additional compute units and attach the instruction const computeUnitsIx = requestComputeUnits(provider, 400_000, 0); transaction.add(computeUnitsIx); // Signer's position NFT ATA let buyerPositionNftTokenAccount = await getAssociatedTokenAddress( buyerPositionNftMint, provider.wallet.publicKey, true ); // Use wallet's ATA as the NFT token account if not passed as argument if (!assetNftTokenAccount) { assetNftTokenAccount = await getAssociatedTokenAddress( assetNftMint, provider.wallet.publicKey ); // Check if ATA exists, otherwise initialize it try { await getAccount(provider.connection, assetNftTokenAccount); } catch { transaction.add( createAssociatedTokenAccountInstruction( provider.wallet.publicKey, assetNftTokenAccount, provider.wallet.publicKey, assetNftMint ) ); } } const strikrAuthority = await getStrikrAuthority(); const strikrAssetNftTokenAccount = await getAssociatedTokenAddress( assetNftMint, strikrAuthority, true ); transaction.add( await strikr.methods .createPutOption(strikePrice, duration, premium, isGraceIncluded) .accounts({ signer: provider.wallet.publicKey, assetNftMint, strikrTreasury: STRIKR_TREASURY, signerAssetNftTokenAccount: assetNftTokenAccount, strikrAssetNftTokenAccount, buyerPositionNftMint, buyerPositionNftTokenAccount, strikrCollectionMint: STRIKR_COLLECTION_MINT, tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID, }) .instruction() ); return await provider.sendAndConfirm(transaction, [ buyerPositionNftMintKeypair, ]); };