UNPKG

@deserialize/auction-sdk

Version:

An SDK for Auction on SVMs

1 lines 58.6 kB
import{PublicKey as e,SystemProgram as t,Transaction as r,Connection as a,Keypair as n}from"@solana/web3.js";import*as o from"@coral-xyz/anchor";import{getAssociatedTokenAddress as i,createAssociatedTokenAccountInstruction as s,ASSOCIATED_TOKEN_PROGRAM_ID as c}from"@solana/spl-token";import*as u from"@project-serum/anchor";import{Program as m}from"@project-serum/anchor";function g(e){var t,r,a,n=2;for("undefined"!=typeof Symbol&&(r=Symbol.asyncIterator,a=Symbol.iterator);n--;){if(r&&null!=(t=e[r]))return t.call(e);if(a&&null!=(t=e[a]))return new d(t.call(e));r="@@asyncIterator",a="@@iterator"}throw new TypeError("Object is not async iterable")}function d(e){function t(e){if(Object(e)!==e)return Promise.reject(new TypeError(e+" is not an object."));var t=e.done;return Promise.resolve(e.value).then(function(e){return{value:e,done:t}})}return d=function(e){this.s=e,this.n=e.next},d.prototype={s:null,n:null,next:function(){return t(this.n.apply(this.s,arguments))},return:function(e){var r=this.s.return;return void 0===r?Promise.resolve({value:e,done:!0}):t(r.apply(this.s,arguments))},throw:function(e){var r=this.s.return;return void 0===r?Promise.reject(e):t(r.apply(this.s,arguments))}},new d(e)}var l,p;function f(e,t){if(e.hasOwnProperty(t))return!0}!function(e){e.Active="active",e.Cancelled="cancelled",e.Refunded="refunded",e.Executed="executed"}(l||(l={}));class y{constructor(e){this.programLoader=void 0,this.programLoader=e}getListingProgram(){return new w(this.programLoader)}async create(r,a,n,u,m){try{var g=this.getListingProgram();const d=await g.get(r);let l=await this.programLoader.program.account.applicationState.fetch(this.programLoader.applicationState);const p=[o.utils.bytes.utf8.encode("auction_manager"),Uint8Array.from(d.nft.toBuffer()),Uint8Array.from(d.authority.toBuffer())],[f,y]=e.findProgramAddressSync(p,this.programLoader.program.programId),h=[o.utils.bytes.utf8.encode("listing"),Uint8Array.from(d.nft.toBuffer()),Uint8Array.from(d.authority.toBuffer()),Uint8Array.from(f.toBuffer()),Uint8Array.from(d.tokenMint.toBuffer())],[w,A]=e.findProgramAddressSync(h,this.programLoader.program.programId);console.log("[*] Listing: ",w),console.log("[*] Listing bump: ",A);const S=[o.utils.bytes.utf8.encode("bid_receipt"),Uint8Array.from(w.toBuffer()),Uint8Array.from(a.toBuffer()),Uint8Array.from(d.tokenMint.toBuffer())],[b,M]=e.findProgramAddressSync(S,this.programLoader.program.programId);console.log("[*] Bid receipt: ",b),console.log("[*] Bid receipt bump: ",M);const B=[o.utils.bytes.utf8.encode("user_account"),Uint8Array.from(a.toBuffer()),Uint8Array.from(l.listingAuthority.toBuffer()),o.utils.bytes.utf8.encode("deserialize")],[L,k]=e.findProgramAddressSync(B,this.programLoader.program.programId);console.log("[*] Userdata: ",L),console.log("[*] Userdata bump: ",k);const P=await i(d.nft,f,!0),U=await i(d.tokenMint,d.authority,!0,u),R=await i(d.tokenMint,a,!0,u),T=await i(d.nft,a,!0),I=await i(d.tokenMint,f,!0,u),E=await i(d.tokenMint,l.feeAccount,!0,u);console.log("[*] BIDDER TOKEN account: ",R),console.log("[*] BIDDER NFT account: ",T),console.log("[*] AUCTION MANAGER TOKEN escrow: ",I),console.log("[*] FEE TOKEN account: ",E);const N=[],v=await this.programLoader.connection.getAccountInfo(R),q=await this.programLoader.connection.getAccountInfo(T),K=await this.programLoader.connection.getAccountInfo(I);if(!await this.programLoader.connection.getAccountInfo(E)){const e=s(a,E,l.feeAccount,d.tokenMint,u);N.push(e)}if(!v){const e=s(a,R,a,d.tokenMint,u);N.push(e)}if(!q){const e=s(a,T,a,d.nft);N.push(e)}if(!K){const e=s(a,I,f,d.tokenMint,u);N.push(e)}const x=Math.floor(Date.now()/1e3),F=await this.programLoader.program.methods.bidOnListing(new o.BN(n),new o.BN(x)).accounts({applicationState:this.programLoader.applicationState,userdata:L,bidReceipt:b,user:a,listing:w,nft:d.nft,auctionManager:f,proceedsWallet:U,nftEscrow:P,bidderNftAccount:T,bidderAccount:R,tokenEscrow:I,tokenMint:d.tokenMint,tokenProgram:u,nftTokenProgram:m,associatedTokenProgram:c,systemProgram:t.programId,feeAccount:l.feeAccount,feeTokenAccount:E}).instruction();return N.push(F),N}catch(e){throw console.error(e),new Error(`Failed to create bid receipt: ${e.message}`)}}async createTransaction(e,t,a,n,o){try{let i=await this.create(e,t,a,n,o),s=new r;for(let e of i)s.add(e);return s.recentBlockhash=(await this.programLoader.getRecentBlockHash()).blockhash,s.feePayer=t,s}catch(e){throw console.error(e),new Error(`Failed to create bid receipt: ${e.message}`)}}async get(e){try{return await this.programLoader.program.account.bidReceipt.fetch(e)}catch(e){throw console.error(e),new Error(`Failed to get bid receipt: ${e.message}`)}}async fetchMany(e){try{const t=await this.programLoader.program.account.bidReceipt.fetchMultiple(e);return console.log("[*] Bid receipts: ",JSON.stringify(t,null,4)),t}catch(e){throw console.error(e),new Error(`Failed to fetch many bid receipts: ${e.message}`)}}async getAll(){try{return await this.programLoader.program.account.bidReceipt.all()}catch(e){throw console.error(e),new Error(`Failed to get all bid receipts: ${e.message}`)}}async getByState(e){try{return(await this.getAll()).filter(t=>t.state===e)}catch(e){throw console.error(e),new Error(`Failed to get bid receipts by state: ${e.message}`)}}async getUserBidOnListing(e,t){try{var r=this.getListingProgram();let a=await r.get(e);return(await this.fetchMany(a.bidReceipts)).find(e=>e.bidder.equals(t))}catch(e){throw console.error(e),new Error(`Failed to get user bid on listing: ${e.message}`)}}async cancel(r,a,n){var s=this;try{const m=await this.get(r);var u=this.getListingProgram();const g=await u.get(m.listing);if(!m.bidder.equals(a))throw new Error("You cannot cancel another user's bid");let d=await this.programLoader.program.account.applicationState.fetch(this.programLoader.applicationState);const l=[o.utils.bytes.utf8.encode("auction_manager"),Uint8Array.from(g.nft.toBuffer()),Uint8Array.from(g.authority.toBuffer())],[p,f]=e.findProgramAddressSync(l,this.programLoader.program.programId),y=[o.utils.bytes.utf8.encode("listing"),Uint8Array.from(g.nft.toBuffer()),Uint8Array.from(g.authority.toBuffer()),Uint8Array.from(p.toBuffer()),Uint8Array.from(g.tokenMint.toBuffer())],[h,w]=e.findProgramAddressSync(y,this.programLoader.program.programId);console.log("[*] Listing: ",h),console.log("[*] Listing bump: ",w);const A=[o.utils.bytes.utf8.encode("bid_receipt"),Uint8Array.from(h.toBuffer()),Uint8Array.from(a.toBuffer()),Uint8Array.from(g.tokenMint.toBuffer())],[S,b]=e.findProgramAddressSync(A,this.programLoader.program.programId);console.log("[*] Bid receipt: ",S),console.log("[*] Bid receipt bump: ",b);const M=[o.utils.bytes.utf8.encode("user_account"),Uint8Array.from(a.toBuffer()),Uint8Array.from(d.listingAuthority.toBuffer()),o.utils.bytes.utf8.encode("deserialize")],[B,L]=e.findProgramAddressSync(M,this.programLoader.program.programId);console.log("[*] Userdata: ",B),console.log("[*] Userdata bump: ",L);const k=await i(g.nft,p,!0),P=await i(g.nft,g.authority,!0);console.log("[*] AUCTION MANAGER NFT escrow: ",k),console.log("[*] LISTER NFT owner: ",P);const U=await i(g.tokenMint,a,!0,n),R=await i(g.nft,a,!0),T=await i(g.tokenMint,p,!0,n);console.log("[*] BIDDER TOKEN account: ",U),console.log("[*] BIDDER NFT account: ",R),console.log("[*] AUCTION MANAGER TOKEN escrow: ",T);let I=await Promise.all(g.bidReceipts.map(async function(e){return{amount:(await s.get(e)).amount,key:e}}));const E=Math.floor(Date.now()/1e3),N=await this.programLoader.program.methods.cancelBid(I,new o.BN(E)).accounts({applicationState:this.programLoader.applicationState,userdata:B,bidReceipt:S,user:a,listing:h,nft:g.nft,auctionManager:p,cancellerTokenAccount:U,tokenEscrow:T,tokenMint:g.tokenMint,tokenProgram:n,associatedTokenProgram:c,systemProgram:t.programId,feeAccount:d.feeAccount}).transaction();return N.recentBlockhash=(await this.programLoader.getRecentBlockHash()).blockhash,N.feePayer=a,N}catch(e){throw console.error(e),new Error(`Failed to cancel bid receipt: ${e.message}`)}}async refund(r,a,n){try{const u=await this.get(r);console.log("[*] Bid receipt data: ",JSON.stringify(u,null,4));var s=this.getListingProgram();const m=await s.get(u.listing);if(console.log("[*] Listing data: ",JSON.stringify(m,null,4)),!u.bidder.equals(a))throw new Error("You cannot cancel another user's bid");if(!f(u.state,l.Active))throw new Error("You cannot refund a bid that is not active");let g=await this.programLoader.program.account.applicationState.fetch(this.programLoader.applicationState);console.log("[*] Application state: ",JSON.stringify(g,null,4));const d=[o.utils.bytes.utf8.encode("auction_manager"),Uint8Array.from(m.nft.toBuffer()),Uint8Array.from(m.authority.toBuffer())],[p,y]=e.findProgramAddressSync(d,this.programLoader.program.programId);console.log("[*] AUCTION MANAGER",p);const h=[o.utils.bytes.utf8.encode("listing"),Uint8Array.from(m.nft.toBuffer()),Uint8Array.from(m.authority.toBuffer()),Uint8Array.from(p.toBuffer()),Uint8Array.from(m.tokenMint.toBuffer())],[w,A]=e.findProgramAddressSync(h,this.programLoader.program.programId);console.log("[*] Listing: ",w),console.log("[*] Listing bump: ",A);const S=[o.utils.bytes.utf8.encode("bid_receipt"),Uint8Array.from(w.toBuffer()),Uint8Array.from(a.toBuffer()),Uint8Array.from(m.tokenMint.toBuffer())],[b,M]=e.findProgramAddressSync(S,this.programLoader.program.programId);console.log("[*] Bid receipt: ",b),console.log("[*] Bid receipt bump: ",M);const B=[o.utils.bytes.utf8.encode("user_account"),Uint8Array.from(a.toBuffer()),Uint8Array.from(g.listingAuthority.toBuffer()),o.utils.bytes.utf8.encode("deserialize")],[L,k]=e.findProgramAddressSync(B,this.programLoader.program.programId);console.log("[*] Userdata: ",L),console.log("[*] Userdata bump: ",k);const P=await i(m.nft,p,!0),U=await i(m.nft,m.authority,!0);console.log("[*] AUCTION MANAGER NFT escrow: ",P),console.log("[*] LISTER NFT owner: ",U);const R=await i(m.tokenMint,a,!0,n),T=await i(m.nft,a,!0),I=await i(m.tokenMint,p,!0,n);console.log("[*] BIDDER TOKEN account: ",R),console.log("[*] BIDDER NFT account: ",T),console.log("[*] AUCTION MANAGER TOKEN escrow: ",I);const E=Math.floor(Date.now()/1e3);return await this.programLoader.program.methods.refundBid(new o.BN(E)).accounts({applicationState:this.programLoader.applicationState,user:m.authority,auctionManager:p,listing:w,nft:m.nft,userdata:L,userRefundAccount:R,refundBidReceipt:b,tokenEscrow:I,tokenMint:m.tokenMint,tokenProgram:n,associatedTokenProgram:c,systemProgram:t.programId}).instruction()}catch(e){throw console.error(e),new Error(`Failed to refund bid receipt: ${e.message}`)}}async increase(r,a,n,s){try{const m=await this.get(r);console.log("bidReceiptData: ",m.state);var u=this.getListingProgram();const g=await u.get(m.listing);if(!m.bidder.equals(a))throw new Error("You cannot increase another user's bid");if(!f(m.state,l.Active))throw new Error("You cannot increase a bid that is not active");let d=await this.programLoader.program.account.applicationState.fetch(this.programLoader.applicationState);const p=[o.utils.bytes.utf8.encode("auction_manager"),Uint8Array.from(g.nft.toBuffer()),Uint8Array.from(g.authority.toBuffer())],[y,h]=e.findProgramAddressSync(p,this.programLoader.program.programId),w=[o.utils.bytes.utf8.encode("listing"),Uint8Array.from(g.nft.toBuffer()),Uint8Array.from(g.authority.toBuffer()),Uint8Array.from(y.toBuffer()),Uint8Array.from(g.tokenMint.toBuffer())],[A,S]=e.findProgramAddressSync(w,this.programLoader.program.programId);console.log("[*] Listing: ",A),console.log("[*] Listing bump: ",S);const b=[o.utils.bytes.utf8.encode("bid_receipt"),Uint8Array.from(A.toBuffer()),Uint8Array.from(a.toBuffer()),Uint8Array.from(g.tokenMint.toBuffer())],[M,B]=e.findProgramAddressSync(b,this.programLoader.program.programId);console.log("[*] Bid receipt: ",M),console.log("[*] Bid receipt bump: ",B);const L=[o.utils.bytes.utf8.encode("user_account"),Uint8Array.from(a.toBuffer()),Uint8Array.from(d.listingAuthority.toBuffer()),o.utils.bytes.utf8.encode("deserialize")],[k,P]=e.findProgramAddressSync(L,this.programLoader.program.programId);console.log("[*] Userdata: ",k),console.log("[*] Userdata bump: ",P);const U=await i(g.nft,y,!0),R=await i(g.nft,g.authority,!0);console.log("[*] AUCTION MANAGER NFT escrow: ",U),console.log("[*] LISTER NFT owner: ",R);const T=await i(g.tokenMint,a,!0,s),I=await i(g.nft,a,!0),E=await i(g.tokenMint,y,!0,s);console.log("[*] BIDDER TOKEN account: ",T),console.log("[*] BIDDER NFT account: ",I),console.log("[*] AUCTION MANAGER TOKEN escrow: ",E);const N=Math.floor(Date.now()/1e3),v=await this.programLoader.program.methods.increaseBid(new o.BN(n),new o.BN(N)).accounts({applicationState:this.programLoader.applicationState,userdata:k,bidReceipt:M,user:a,listing:A,nft:g.nft,auctionManager:y,bidderAccount:T,tokenEscrow:E,tokenMint:g.tokenMint,tokenProgram:s,associatedTokenProgram:c,systemProgram:t.programId,feeAccount:d.feeAccount}).transaction();return v.recentBlockhash=(await this.programLoader.getRecentBlockHash()).blockhash,v.feePayer=a,v}catch(e){throw console.error(e),new Error(`Failed to increase bid receipt: ${e.message}`)}}}class h{constructor(e){this.programLoader=void 0,this.programLoader=e}async create(r,a,n){try{const i=[o.utils.bytes.utf8.encode("auction_manager"),Uint8Array.from(r.toBuffer()),Uint8Array.from(a.toBuffer())],[s,c]=e.findProgramAddressSync(i,this.programLoader.program.programId);return console.log("[*] Auction manager: ",s),console.log("[*] Auction manager bump: ",c),await this.programLoader.program.methods.createAuctionManager().accounts({applicationState:this.programLoader.applicationState,nft:r,systemProgram:t.programId,auctionManager:s,tokenProgram:n,rent:o.web3.SYSVAR_RENT_PUBKEY,user:a}).instruction()}catch(e){throw console.error(e),new Error(`Failed to create auction: ${e.message}`)}}async get(e){try{const t=await this.programLoader.program.account.auctionManager.fetch(e);return console.log("[*] Auction manager account: ",JSON.stringify(t,null,2)),t}catch(e){throw console.error(e),new Error(`Failed to get auction manager: ${e.message}`)}}async getAll(){let e=await this.programLoader.program.account.auctionManager.all();return console.log("[*] Auction manager accounts: ",JSON.stringify(e,null,2)),e}async fetchMany(e){let t=await this.programLoader.program.account.auctionManager.fetchMultiple(e);return console.log("[*] Auction manager accounts: ",JSON.stringify(t,null,2)),t}}class w{constructor(e){this.programLoader=void 0,this.auctionManagerProgram=void 0,this.programLoader=e,this.auctionManagerProgram=new h(e)}async create(r,a,n,c,u,m,g,d,l){try{const p=[o.utils.bytes.utf8.encode("auction_manager"),Uint8Array.from(r.toBuffer()),Uint8Array.from(u.toBuffer())],[f,y]=e.findProgramAddressSync(p,this.programLoader.program.programId);console.log("[*] Auction manager: ",f),console.log("[*] Auction manager bump: ",y);const h=[o.utils.bytes.utf8.encode("listing"),Uint8Array.from(r.toBuffer()),Uint8Array.from(u.toBuffer()),Uint8Array.from(f.toBuffer()),Uint8Array.from(n.toBuffer())],[w,A]=e.findProgramAddressSync(h,this.programLoader.program.programId);console.log("[*] Listing: ",w),console.log("[*] Listing bump: ",A);let S=await this.programLoader.program.account.applicationState.fetch(this.programLoader.applicationState);const b=[o.utils.bytes.utf8.encode("user_account"),Uint8Array.from(u.toBuffer()),Uint8Array.from(S.listingAuthority.toBuffer()),o.utils.bytes.utf8.encode("deserialize")],[M,B]=e.findProgramAddressSync(b,this.programLoader.program.programId);console.log("[*] Userdata: ",M),console.log("[*] Userdata bump: ",B);let L=[],k=await i(r,f,!0),P=await i(r,u,!0),U=await i(n,u,!0,c),R=await this.programLoader.connection.getAccountInfo(k),T=await this.programLoader.connection.getAccountInfo(P),I=await this.programLoader.connection.getAccountInfo(U);if(!await this.programLoader.connection.getAccountInfo(f)){let e=await this.auctionManagerProgram.create(r,u,a);L.push(e)}if(!R){let e=s(u,k,f,r);L.push(e)}if(!T){let e=s(u,P,u,r);L.push(e)}if(!I){let e=s(u,U,u,n,c);L.push(e)}const E=Math.floor(Date.now()/1e3);let N=null;d&&(N=E+86400*d),console.log("tokenMintProgramId: ",c);const v=await this.programLoader.program.methods.createListing(new o.BN(m),new o.BN(g),new o.BN(E),N?new o.BN(N):N,new o.BN(l)).accounts({applicationState:this.programLoader.applicationState,auctionManager:f,listing:w,owner:u,userdata:M,nft:r,nftEscrow:k,proceedsWallet:U,tokenMint:n,feeAccount:S.feeAccount,nftOwner:P,tokenProgram:a,systemProgram:t.programId,rent:o.web3.SYSVAR_RENT_PUBKEY}).instruction();return L.push(v),L}catch(e){throw console.error(e),new Error(`Failed to create listing: ${e.message}`)}}async createTransaction(e,t,a,n,o,i,s,c,u){console.log("tokenMintProgramId 1: ",n);try{let m=await this.create(e,t,a,n,o,i,s,c,u),g=(new r).add(...m);return g.recentBlockhash=(await this.programLoader.getRecentBlockHash()).blockhash,g.feePayer=o,g}catch(e){throw console.error(e),new Error(`Failed to create listing transaction: ${e.message}`)}}async get(e){try{console.log("[*] Listing publicKey is "+e);const t=await this.programLoader.program.account.listing.fetch(e);return console.log(JSON.stringify(t,null,4)),t}catch(e){throw console.error(e),new Error(`Failed to get listing: ${e.message}`)}}async fetchMany(e){try{return await this.programLoader.program.account.listing.fetchMultiple(e)}catch(e){throw console.error(e),new Error(`Failed to fetch listings: ${e.message}`)}}async getAll(){try{const e=await this.programLoader.program.account.listing.all();return console.log("[*] Listings",e),e}catch(e){throw console.error(e),new Error(`Failed to get all listings: ${e.message}`)}}async cancel(r,a){try{let n=await this.get(r);const s=[o.utils.bytes.utf8.encode("auction_manager"),Uint8Array.from(n.nft.toBuffer()),Uint8Array.from(n.authority.toBuffer())],[c,u]=e.findProgramAddressSync(s,this.programLoader.program.programId),m=[o.utils.bytes.utf8.encode("listing"),Uint8Array.from(n.nft.toBuffer()),Uint8Array.from(n.authority.toBuffer()),Uint8Array.from(c.toBuffer()),Uint8Array.from(n.tokenMint.toBuffer())],[g,d]=e.findProgramAddressSync(m,this.programLoader.program.programId);console.log("[*] Listing: ",g),console.log("[*] Listing bump: ",d);let l=await this.programLoader.program.account.applicationState.fetch(this.programLoader.applicationState);const p=[o.utils.bytes.utf8.encode("user_account"),Uint8Array.from(n.authority.toBuffer()),Uint8Array.from(l.listingAuthority.toBuffer()),o.utils.bytes.utf8.encode("deserialize")],[f,y]=e.findProgramAddressSync(p,this.programLoader.program.programId);console.log("[*] Userdata: ",f),console.log("[*] Userdata bump: ",y);let h=await i(n.nft,c,!0),w=await i(n.nft,n.authority,!0);const A=Math.floor(Date.now()/1e3),S=await this.programLoader.program.methods.cancelListing(new o.BN(A)).accounts({applicationState:this.programLoader.applicationState,auctionManager:c,listing:g,userdata:f,nft:n.nft,nftEscrow:h,originalNftHolder:w,tokenMint:n.tokenMint,feeAccount:l.feeAccount,user:n.authority,tokenProgram:a,systemProgram:t.programId}).transaction();return S.recentBlockhash=(await this.programLoader.getRecentBlockHash()).blockhash,S.feePayer=n.authority,S}catch(e){throw console.error(e),new Error(`Failed to cancel listing (${r}): ${e.message}`)}}async getBids(e){try{const t=new y(this.programLoader),r=await this.programLoader.program.account.listing.fetch(e);return await t.fetchMany(r.bidReceipts)}catch(t){throw console.error(t),new Error(`Failed to get bids for listing (${e}): ${t.message}`)}}async getByState(e){try{return(await this.getAll()).filter(t=>t.state===e)}catch(t){throw console.error(t),new Error(`Failed to get listings by state (${e}): ${t.message}`)}}async getOneTimeSales(){try{return(await this.getAll()).filter(e=>null===e.endTime)}catch(e){throw console.error(e),new Error(`Failed to get one-time sale listings: ${e.message}`)}}async getAuctions(){try{return(await this.getAll()).filter(e=>null!==e.endTime)}catch(e){throw console.error(e),new Error(`Failed to get auction listings: ${e.message}`)}}async getHighestBidInfo(e){try{const t=new y(this.programLoader),r=await this.get(e);if(!r.highestBidReceipt)throw new Error("No bids on this listing");const a=await t.get(r.highestBidReceipt);return{bidder:a.bidder,receipt:r.highestBidReceipt,amount:a.amount}}catch(e){throw console.error(e),new Error(`Failed to get highest bid info: ${e.message}`)}}async getBidsInfoToRefund(e){try{const i=await this.get(e),s=i.bidReceipts.filter(e=>e!==i.highestBidReceipt),c=new y(this.programLoader);if(0===i.bidReceipts.length)return[];const u=[];var t,r=!1,a=!1;try{for(var n,o=g(s);r=!(n=await o.next()).done;r=!1){let e=n.value;if(!e.equals(i.highestBidReceipt)){let t=await c.get(e);u.push({bidder:t.bidder,receipt:e,amount:t.amount})}}}catch(e){a=!0,t=e}finally{try{r&&null!=o.return&&await o.return()}finally{if(a)throw t}}return u}catch(e){throw console.error(e),new Error(`Failed to get bids info to refund: ${e.message}`)}}}!function(e){e.Active="active",e.Sold="sold",e.Cancelled="cancelled"}(p||(p={}));class A{constructor(e){this.programLoader=void 0,this.listingProgram=void 0,this.bidReceiptProgram=void 0,this.programLoader=e,this.listingProgram=new w(e),this.bidReceiptProgram=new y(e)}async create(r){try{const a=[o.utils.bytes.utf8.encode("deserialize"),Uint8Array.from(this.programLoader.wallet.publicKey.toBuffer())],[n,i]=e.findProgramAddressSync(a,this.programLoader.program.programId);console.log("[*] NEW APPLICATION STATE:- "+n);let s=await this.programLoader.program.methods.initialize().accounts({applicationState:n,user:this.programLoader.wallet.publicKey,feeAccount:r,systemProgram:t.programId}).signers([this.programLoader.wallet]).rpc();return console.log("[*] Transaction signature:- ",s),await this.programLoader.program.account.applicationState.fetch(n)}catch(e){throw console.error(e),new Error(`Failed to create application state: ${e.message}`)}}async getAll(){try{const e=await this.programLoader.program.account.applicationState.all();return console.log("[*] Application states: ",e),e}catch(e){throw console.error(e),new Error(`Failed to get all application states: ${e.message}`)}}async get(e){try{return await this.programLoader.program.account.applicationState.fetch(e)}catch(e){throw console.error(e),new Error(`Failed to get application state: ${e.message}`)}}async update(e,t){try{return await this.programLoader.program.methods.updateApplicationState(t).accounts({applicationState:e,user:this.programLoader.wallet.publicKey}).signers([this.programLoader.wallet]).rpc(),await this.get(e)}catch(e){throw console.error(e),new Error(`Failed to update application state: ${e.message}`)}}async executeSale(a,n,s,u){try{const S=await this.listingProgram.get(a);console.log("[*] Listing data: ",JSON.stringify(S,null,4));let b=await this.programLoader.program.account.applicationState.fetch(this.programLoader.applicationState);console.log("[*] Application state: ",JSON.stringify(b,null,4));const M=[o.utils.bytes.utf8.encode("auction_manager"),Uint8Array.from(S.nft.toBuffer()),Uint8Array.from(S.authority.toBuffer())],[B,L]=e.findProgramAddressSync(M,this.programLoader.program.programId);var m=await this.programLoader.program.account.auctionManager.fetch(B);console.log("[*] Auction manager state: ",JSON.stringify(m,null,4));const k=[o.utils.bytes.utf8.encode("listing"),Uint8Array.from(S.nft.toBuffer()),Uint8Array.from(S.authority.toBuffer()),Uint8Array.from(B.toBuffer()),Uint8Array.from(S.tokenMint.toBuffer())],[P,U]=e.findProgramAddressSync(k,this.programLoader.program.programId);var d=await this.programLoader.program.account.listing.fetch(P);if(console.log("[*] Listing: ",JSON.stringify(d,null,4)),!a.equals(P))throw new Error("Invalid Listing");if(console.log("[*] Listing state: ",S.state),!f(S.state,p.Active))throw new Error("Listing is not active");let R=await this.listingProgram.getHighestBidInfo(a);console.log("[*] Highest bid info: ",JSON.stringify(R,null,4));let T=await this.listingProgram.getBidsInfoToRefund(a);console.log("[*] Bids info to refund: ",JSON.stringify(T,null,4));let I=new r;var l,y=!1,h=!1;try{for(var w,A=g(T);y=!(w=await A.next()).done;y=!1){let e=w.value;{let t=await this.bidReceiptProgram.refund(e.receipt,e.bidder,s);I.add(t)}}}catch(e){h=!0,l=e}finally{try{y&&null!=A.return&&await A.return()}finally{if(h)throw l}}const E=await i(S.nft,B,!0),N=await i(S.nft,R.bidder,!0,u),v=await i(S.tokenMint,B,!0,s),q=await i(S.tokenMint,b.feeAccount,!0,s),K=[o.utils.bytes.utf8.encode("user_account"),Uint8Array.from(R.bidder.toBuffer()),Uint8Array.from(b.listingAuthority.toBuffer()),o.utils.bytes.utf8.encode("deserialize")],[x,F]=e.findProgramAddressSync(K,this.programLoader.program.programId);console.log("[*] WINNER NFT account: ",N),console.log("[*] AUCTION MANAGER Token escrow: ",v);const O=Math.floor(Date.now()/1e3),$=await this.programLoader.program.methods.executeSale(new o.BN(O)).accounts({applicationState:this.programLoader.applicationState,userdata:x,bidReceipt:R.receipt,user:n,listing:P,nft:S.nft,auctionManager:B,nftEscrow:E,winnerNftAccount:N,proceedsWallet:S.auctionProceedsWallet,tokenEscrow:v,tokenMint:S.tokenMint,tokenProgram:s,nftTokenProgram:u,associatedTokenProgram:c,systemProgram:t.programId,feeTokenAccount:q}).instruction();return I.add($),I.recentBlockhash=(await this.programLoader.getRecentBlockHash()).blockhash,I.feePayer=n,I}catch(e){throw console.error(e),new Error(`Failed to execute sale: ${e.message}`)}}}const S={version:"0.1.0",name:"auction",instructions:[{name:"initialize",accounts:[{name:"applicationState",isMut:!0,isSigner:!1},{name:"user",isMut:!0,isSigner:!0},{name:"feeAccount",isMut:!1,isSigner:!1},{name:"systemProgram",isMut:!1,isSigner:!1}],args:[]},{name:"initializeAccount",accounts:[{name:"applicationState",isMut:!1,isSigner:!1},{name:"userdata",isMut:!0,isSigner:!1},{name:"user",isMut:!0,isSigner:!0},{name:"systemProgram",isMut:!1,isSigner:!1}],args:[]},{name:"updateApplicationState",accounts:[{name:"applicationState",isMut:!0,isSigner:!1},{name:"user",isMut:!0,isSigner:!0}],args:[{name:"feeAccount",type:"publicKey"}]},{name:"createAuctionManager",accounts:[{name:"applicationState",isMut:!0,isSigner:!1},{name:"user",isMut:!0,isSigner:!0},{name:"auctionManager",isMut:!0,isSigner:!1},{name:"nft",isMut:!1,isSigner:!1},{name:"tokenProgram",isMut:!1,isSigner:!1},{name:"systemProgram",isMut:!1,isSigner:!1},{name:"rent",isMut:!1,isSigner:!1}],args:[]},{name:"createListing",accounts:[{name:"applicationState",isMut:!1,isSigner:!1},{name:"owner",isMut:!0,isSigner:!0},{name:"userdata",isMut:!0,isSigner:!1},{name:"auctionManager",isMut:!0,isSigner:!1},{name:"listing",isMut:!0,isSigner:!1},{name:"nft",isMut:!1,isSigner:!1},{name:"nftEscrow",isMut:!0,isSigner:!1},{name:"proceedsWallet",isMut:!0,isSigner:!1},{name:"nftOwner",isMut:!0,isSigner:!1},{name:"feeAccount",isMut:!0,isSigner:!1},{name:"tokenMint",isMut:!1,isSigner:!1},{name:"tokenProgram",isMut:!1,isSigner:!1},{name:"systemProgram",isMut:!1,isSigner:!1},{name:"rent",isMut:!1,isSigner:!1}],args:[{name:"timeExtension",type:"u64"},{name:"startingPrice",type:"u128"},{name:"startTime",type:"u64"},{name:"endTime",type:{option:"u64"}},{name:"minimumBidThreshold",type:"u128"}]},{name:"bidOnListing",accounts:[{name:"applicationState",isMut:!1,isSigner:!1},{name:"bidReceipt",isMut:!0,isSigner:!1},{name:"userdata",isMut:!0,isSigner:!1},{name:"user",isMut:!0,isSigner:!0},{name:"listing",isMut:!0,isSigner:!1},{name:"auctionManager",isMut:!0,isSigner:!1},{name:"bidderAccount",isMut:!0,isSigner:!1},{name:"bidderNftAccount",isMut:!0,isSigner:!1},{name:"tokenEscrow",isMut:!0,isSigner:!1},{name:"nftEscrow",isMut:!0,isSigner:!1},{name:"proceedsWallet",isMut:!0,isSigner:!1},{name:"feeAccount",isMut:!0,isSigner:!1},{name:"feeTokenAccount",isMut:!0,isSigner:!1},{name:"nft",isMut:!1,isSigner:!1},{name:"tokenMint",isMut:!1,isSigner:!1},{name:"nftTokenProgram",isMut:!1,isSigner:!1},{name:"tokenProgram",isMut:!1,isSigner:!1},{name:"associatedTokenProgram",isMut:!1,isSigner:!1},{name:"systemProgram",isMut:!1,isSigner:!1}],args:[{name:"amount",type:"u128"},{name:"submittedAt",type:"u64"}]},{name:"cancelListing",accounts:[{name:"applicationState",isMut:!1,isSigner:!1},{name:"userdata",isMut:!1,isSigner:!1},{name:"auctionManager",isMut:!0,isSigner:!1},{name:"nft",isMut:!1,isSigner:!1},{name:"tokenMint",isMut:!1,isSigner:!1},{name:"nftEscrow",isMut:!0,isSigner:!1},{name:"originalNftHolder",isMut:!0,isSigner:!1},{name:"user",isMut:!0,isSigner:!0},{name:"feeAccount",isMut:!0,isSigner:!1},{name:"listing",isMut:!0,isSigner:!1},{name:"tokenProgram",isMut:!1,isSigner:!1},{name:"systemProgram",isMut:!1,isSigner:!1}],args:[{name:"cancelledAt",type:"u64"}]},{name:"increaseBid",accounts:[{name:"applicationState",isMut:!1,isSigner:!1},{name:"auctionManager",isMut:!0,isSigner:!1},{name:"bidReceipt",isMut:!0,isSigner:!1},{name:"userdata",isMut:!1,isSigner:!1},{name:"user",isMut:!0,isSigner:!0},{name:"listing",isMut:!0,isSigner:!1},{name:"bidderAccount",isMut:!0,isSigner:!1},{name:"tokenEscrow",isMut:!0,isSigner:!1},{name:"feeAccount",isMut:!0,isSigner:!1},{name:"nft",isMut:!1,isSigner:!1},{name:"tokenMint",isMut:!1,isSigner:!1},{name:"tokenProgram",isMut:!1,isSigner:!1},{name:"associatedTokenProgram",isMut:!1,isSigner:!1},{name:"systemProgram",isMut:!1,isSigner:!1}],args:[{name:"newAmount",type:"u128"},{name:"updatedAt",type:"u64"}]},{name:"cancelBid",accounts:[{name:"applicationState",isMut:!1,isSigner:!1},{name:"auctionManager",isMut:!0,isSigner:!1},{name:"user",isMut:!0,isSigner:!0},{name:"listing",isMut:!0,isSigner:!1},{name:"userdata",isMut:!1,isSigner:!1},{name:"bidReceipt",isMut:!0,isSigner:!1},{name:"cancellerTokenAccount",isMut:!0,isSigner:!1},{name:"tokenEscrow",isMut:!0,isSigner:!1},{name:"feeAccount",isMut:!0,isSigner:!1},{name:"nft",isMut:!1,isSigner:!1},{name:"tokenMint",isMut:!1,isSigner:!1},{name:"tokenProgram",isMut:!1,isSigner:!1},{name:"associatedTokenProgram",isMut:!1,isSigner:!1},{name:"systemProgram",isMut:!1,isSigner:!1}],args:[{name:"bidReceipts",type:{vec:{defined:"ReceiptInfo"}}},{name:"submittedAt",type:"u64"}]},{name:"refundBid",accounts:[{name:"applicationState",isMut:!1,isSigner:!1},{name:"user",isMut:!0,isSigner:!0},{name:"auctionManager",isMut:!0,isSigner:!1},{name:"listing",isMut:!0,isSigner:!1},{name:"userdata",isMut:!1,isSigner:!1},{name:"userRefundAccount",isMut:!0,isSigner:!1},{name:"refundBidReceipt",isMut:!0,isSigner:!1},{name:"tokenEscrow",isMut:!0,isSigner:!1},{name:"tokenMint",isMut:!1,isSigner:!1},{name:"nft",isMut:!1,isSigner:!1},{name:"tokenProgram",isMut:!1,isSigner:!1},{name:"associatedTokenProgram",isMut:!1,isSigner:!1},{name:"systemProgram",isMut:!1,isSigner:!1}],args:[{name:"refundedAt",type:"u64"}]},{name:"executeSale",accounts:[{name:"applicationState",isMut:!1,isSigner:!1},{name:"auctionManager",isMut:!0,isSigner:!1},{name:"user",isMut:!0,isSigner:!0},{name:"listing",isMut:!0,isSigner:!1},{name:"userdata",isMut:!1,isSigner:!1},{name:"winnerNftAccount",isMut:!0,isSigner:!1},{name:"bidReceipt",isMut:!0,isSigner:!1},{name:"tokenEscrow",isMut:!0,isSigner:!1},{name:"nftEscrow",isMut:!0,isSigner:!1},{name:"proceedsWallet",isMut:!0,isSigner:!1},{name:"feeTokenAccount",isMut:!0,isSigner:!1},{name:"nft",isMut:!1,isSigner:!1},{name:"tokenMint",isMut:!1,isSigner:!1},{name:"nftTokenProgram",isMut:!1,isSigner:!1},{name:"tokenProgram",isMut:!1,isSigner:!1},{name:"associatedTokenProgram",isMut:!1,isSigner:!1},{name:"systemProgram",isMut:!1,isSigner:!1}],args:[{name:"submittedAt",type:"u64"}]},{name:"createBidRequest",accounts:[{name:"applicationState",isMut:!1,isSigner:!1},{name:"userdata",isMut:!0,isSigner:!1},{name:"bidRequest",isMut:!0,isSigner:!1},{name:"nftOwner",isMut:!1,isSigner:!1},{name:"feeAccount",isMut:!1,isSigner:!1},{name:"escrowTokenAccount",isMut:!0,isSigner:!1},{name:"userTokenAccount",isMut:!0,isSigner:!1},{name:"user",isMut:!0,isSigner:!0},{name:"nft",isMut:!1,isSigner:!1},{name:"tokenMint",isMut:!1,isSigner:!1},{name:"tokenProgram",isMut:!1,isSigner:!1},{name:"systemProgram",isMut:!1,isSigner:!1}],args:[{name:"amount",type:"u128"},{name:"createdAt",type:"u64"}]},{name:"acceptBidRequest",accounts:[{name:"applicationState",isMut:!1,isSigner:!1},{name:"bidRequest",isMut:!1,isSigner:!1},{name:"user",isMut:!0,isSigner:!0},{name:"sellerNftTokenAccount",isMut:!0,isSigner:!1},{name:"buyerNftTokenAccount",isMut:!0,isSigner:!1},{name:"escrowTokenAccount",isMut:!0,isSigner:!1},{name:"sellerTokenAccount",isMut:!0,isSigner:!1},{name:"feeTokenAccount",isMut:!0,isSigner:!1},{name:"nft",isMut:!1,isSigner:!1},{name:"tokenMint",isMut:!1,isSigner:!1},{name:"nftTokenProgram",isMut:!1,isSigner:!1},{name:"tokenProgram",isMut:!1,isSigner:!1},{name:"associatedTokenProgram",isMut:!1,isSigner:!1},{name:"systemProgram",isMut:!1,isSigner:!1}],args:[{name:"acceptedAt",type:"u64"}]},{name:"rejectBidRequest",accounts:[{name:"bidRequest",isMut:!1,isSigner:!1},{name:"user",isMut:!0,isSigner:!0},{name:"escrowTokenAccount",isMut:!0,isSigner:!1},{name:"requesterTokenAccount",isMut:!0,isSigner:!1},{name:"tokenMint",isMut:!1,isSigner:!1},{name:"tokenProgram",isMut:!1,isSigner:!1},{name:"associatedTokenProgram",isMut:!1,isSigner:!1},{name:"systemProgram",isMut:!1,isSigner:!1}],args:[{name:"rejectedAt",type:"u64"}]},{name:"cancelBidRequest",accounts:[{name:"bidRequest",isMut:!1,isSigner:!1},{name:"user",isMut:!0,isSigner:!0},{name:"escrowTokenAccount",isMut:!0,isSigner:!1},{name:"cancellerTokenAccount",isMut:!0,isSigner:!1},{name:"tokenMint",isMut:!1,isSigner:!1},{name:"tokenProgram",isMut:!1,isSigner:!1},{name:"associatedTokenProgram",isMut:!1,isSigner:!1},{name:"systemProgram",isMut:!1,isSigner:!1}],args:[{name:"cancelledAt",type:"u64"}]},{name:"increaseBidRequest",accounts:[{name:"bidRequest",isMut:!1,isSigner:!1},{name:"user",isMut:!0,isSigner:!0},{name:"escrowTokenAccount",isMut:!0,isSigner:!1},{name:"ownerTokenAccount",isMut:!0,isSigner:!1},{name:"tokenMint",isMut:!1,isSigner:!1},{name:"tokenProgram",isMut:!1,isSigner:!1},{name:"associatedTokenProgram",isMut:!1,isSigner:!1},{name:"systemProgram",isMut:!1,isSigner:!1}],args:[{name:"newAmount",type:"u128"},{name:"submittedAt",type:"u64"}]},{name:"makeBidRequestPublic",accounts:[{name:"applicationState",isMut:!1,isSigner:!1},{name:"creatorUserdata",isMut:!0,isSigner:!1},{name:"bidderUserdata",isMut:!0,isSigner:!1},{name:"user",isMut:!0,isSigner:!0},{name:"bidder",isMut:!1,isSigner:!1},{name:"listing",isMut:!0,isSigner:!1},{name:"auctionManager",isMut:!0,isSigner:!1},{name:"bidReceipt",isMut:!0,isSigner:!1},{name:"bidRequest",isMut:!1,isSigner:!1},{name:"bidRequestTokenAccount",isMut:!0,isSigner:!1},{name:"escrowTokenAccount",isMut:!0,isSigner:!1},{name:"escrowNftAccount",isMut:!0,isSigner:!1},{name:"creatorNftAccount",isMut:!0,isSigner:!1},{name:"creatorTokenAccount",isMut:!0,isSigner:!1},{name:"feeAccount",isMut:!1,isSigner:!1},{name:"nft",isMut:!1,isSigner:!1},{name:"tokenMint",isMut:!1,isSigner:!1},{name:"nftTokenProgram",isMut:!1,isSigner:!1},{name:"tokenProgram",isMut:!1,isSigner:!1},{name:"associatedTokenProgram",isMut:!1,isSigner:!1},{name:"systemProgram",isMut:!1,isSigner:!1}],args:[{name:"createdAt",type:"u64"},{name:"endTime",type:{option:"u64"}},{name:"timeExtension",type:"u64"}]}],accounts:[{name:"applicationState",docs:["* The different states we need to keep track of are the \n * \n * 1. General Application State\n * 2. Auction Manager State\n * 3. Listing State\n * 4. BidReceipt State\n *"],type:{kind:"struct",fields:[{name:"key",type:"publicKey"},{name:"listingAuthority",type:"publicKey"},{name:"feeAccount",type:"publicKey"}]}},{name:"auctionManager",type:{kind:"struct",fields:[{name:"key",type:"publicKey"},{name:"nft",type:"publicKey"},{name:"listingAuthority",type:"publicKey"},{name:"updateAuthority",type:"publicKey"}]}},{name:"userData",type:{kind:"struct",fields:[{name:"key",type:"publicKey"},{name:"listings",type:{vec:"publicKey"}},{name:"bids",type:{vec:"publicKey"}},{name:"bidRequests",type:{vec:"publicKey"}}]}},{name:"listing",type:{kind:"struct",fields:[{name:"key",type:"publicKey"},{name:"auctionManager",type:"publicKey"},{name:"nft",type:"publicKey"},{name:"tokenMint",type:"publicKey"},{name:"startingPrice",type:"u128"},{name:"startTime",type:"u64"},{name:"endTime",type:{option:"u64"}},{name:"highestBidReceipt",type:{option:"publicKey"}},{name:"bidReceipts",type:{vec:"publicKey"}},{name:"highestBidAmount",type:{option:"u128"}},{name:"nftEscrow",type:"publicKey"},{name:"auctionProceedsWallet",type:"publicKey"},{name:"authority",type:"publicKey"},{name:"rewardPercentage",type:"u8"},{name:"timeExtension",type:"u64"},{name:"state",type:{defined:"ListingState"}},{name:"cancelledAt",type:{option:"u64"}}]}},{name:"bidReceipt",type:{kind:"struct",fields:[{name:"key",type:"publicKey"},{name:"listing",type:"publicKey"},{name:"bidder",type:"publicKey"},{name:"amount",type:"u128"},{name:"createdAt",type:"u64"},{name:"updatedAt",type:{option:"u64"}},{name:"state",type:{defined:"BidReceiptState"}},{name:"cancelledAt",type:{option:"u64"}},{name:"refundedAt",type:{option:"u64"}}]}},{name:"bidRequest",type:{kind:"struct",fields:[{name:"key",type:"publicKey"},{name:"nft",type:"publicKey"},{name:"authority",type:"publicKey"},{name:"amount",type:"u128"},{name:"createdAt",type:"u64"},{name:"updatedAt",type:{option:"u64"}},{name:"status",type:{defined:"BidRequestStatus"}},{name:"owner",type:"publicKey"}]}}],types:[{name:"ReceiptInfo",type:{kind:"struct",fields:[{name:"amount",type:"u128"},{name:"key",type:"publicKey"}]}},{name:"ListingState",type:{kind:"enum",variants:[{name:"Active"},{name:"Sold"},{name:"Cancelled"}]}},{name:"BidReceiptState",type:{kind:"enum",variants:[{name:"Executed"},{name:"Cancelled"},{name:"Active"},{name:"Refunded"}]}},{name:"BidRequestStatus",type:{kind:"enum",variants:[{name:"Accepted"},{name:"Rejected"},{name:"Pending"},{name:"Cancelled"},{name:"Public"}]}}],events:[{name:"AuctionManagerCreated",fields:[{name:"auctionManager",type:"publicKey",index:!1},{name:"nft",type:"publicKey",index:!1}]},{name:"AuctionListingCreated",fields:[{name:"auctionManager",type:"publicKey",index:!1},{name:"nft",type:"publicKey",index:!1},{name:"listing",type:"publicKey",index:!1},{name:"tokenMint",type:"publicKey",index:!1},{name:"endTime",type:{option:"u64"},index:!1}]},{name:"BidOnListingCreated",fields:[{name:"listing",type:"publicKey",index:!1},{name:"bidder",type:"publicKey",index:!1},{name:"amount",type:"u128",index:!1},{name:"receipt",type:"publicKey",index:!1},{name:"createdAt",type:"u64",index:!1}]},{name:"ListingSold",fields:[{name:"listing",type:"publicKey",index:!1},{name:"buyer",type:"publicKey",index:!1},{name:"amount",type:"u128",index:!1},{name:"createdAt",type:"u64",index:!1}]},{name:"ListingCancelled",fields:[{name:"by",type:"publicKey",index:!1},{name:"nft",type:"publicKey",index:!1},{name:"listing",type:"publicKey",index:!1},{name:"cancelledAt",type:"u64",index:!1}]},{name:"BidCancelled",fields:[{name:"bidReceipt",type:"publicKey",index:!1},{name:"receiver",type:"publicKey",index:!1},{name:"amount",type:"u128",index:!1},{name:"refundedAt",type:"u64",index:!1}]},{name:"BidIncreased",fields:[{name:"bidReceipt",type:"publicKey",index:!1},{name:"bidder",type:"publicKey",index:!1},{name:"oldAmount",type:"u128",index:!1},{name:"newAmount",type:"u128",index:!1},{name:"updatedAt",type:"u64",index:!1}]},{name:"BidExecuted",fields:[{name:"bidReceipt",type:"publicKey",index:!1},{name:"receiver",type:"publicKey",index:!1},{name:"amount",type:"u128",index:!1},{name:"createdAt",type:"u64",index:!1}]},{name:"BidRefunded",fields:[{name:"bidReceipt",type:"publicKey",index:!1},{name:"receiver",type:"publicKey",index:!1},{name:"amount",type:"u128",index:!1},{name:"refundedAt",type:"u64",index:!1}]},{name:"BidRequestCreated",fields:[{name:"bidRequest",type:"publicKey",index:!1},{name:"creator",type:"publicKey",index:!1},{name:"nft",type:"publicKey",index:!1},{name:"createdAt",type:"u64",index:!1}]},{name:"BidRequestAccepted",fields:[{name:"bidRequest",type:"publicKey",index:!1},{name:"nft",type:"publicKey",index:!1},{name:"acceptedAt",type:"u64",index:!1},{name:"newOwner",type:"publicKey",index:!1}]},{name:"BidRequestRejected",fields:[{name:"bidRequest",type:"publicKey",index:!1},{name:"nft",type:"publicKey",index:!1},{name:"rejectedAt",type:"u64",index:!1}]},{name:"BidRequestCancelled",fields:[{name:"bidRequest",type:"publicKey",index:!1},{name:"nft",type:"publicKey",index:!1},{name:"cancelledAt",type:"u64",index:!1}]},{name:"BidRequestMadePublic",fields:[{name:"listing",type:"publicKey",index:!1},{name:"bidRequest",type:"publicKey",index:!1},{name:"nft",type:"publicKey",index:!1},{name:"createdAt",type:"u64",index:!1}]},{name:"BidRequestIncreased",fields:[{name:"bidRequest",type:"publicKey",index:!1},{name:"nft",type:"publicKey",index:!1},{name:"oldAmount",type:"u128",index:!1},{name:"newAmount",type:"u128",index:!1},{name:"increasedAt",type:"u64",index:!1}]}],errors:[{code:6e3,name:"AuctionNotActive",msg:"Auction is not active"},{code:6001,name:"InvalidListingAuthority",msg:"Invalid listing authority"},{code:6002,name:"UnauthorizedAuctionManager",msg:"Auction manager is not authorized"},{code:6003,name:"ListingSold",msg:"Listing has already been sold"},{code:6004,name:"InvalidBidAmount",msg:"Invalid bid amount"},{code:6005,name:"BidAmountLessThanStartingPrice",msg:"Bid amount is less than the starting price"},{code:6006,name:"BidAmountLessThanHighestBid",msg:"Bid amount is less than the highest bid"},{code:6007,name:"OneTimeSaleListing",msg:"Listing is a one-time sale"},{code:6008,name:"DuplicateListingState",msg:"Duplicate listing state"},{code:6009,name:"BidAlreadyCancelled",msg:"Bid already cancelled"},{code:6010,name:"BidAlreadyExeceuted",msg:"Bid already executed"},{code:6011,name:"UnauthorizedListingCanceller",msg:"You are not authorized to cancel this listing"},{code:6012,name:"ListingAlreadyCancelled",msg:"Listing is alredy cancelled"},{code:6013,name:"CannotCancelListing",msg:"Cannot cancel listing that already has active bids"},{code:6014,name:"InvalidBidReceipt",msg:"This receipt does not belong to this listing"},{code:6015,name:"NewBidNotGreaterThanOldBid",msg:"New bid is lesser than the old bid"},{code:6016,name:"UserNotAuthorized",msg:"User is not authorized to take this action"},{code:6017,name:"MintMismatch",msg:"Token mint mismatch"},{code:6018,name:"FeeAccountNotAuthorized",msg:"Fee account not authorized"},{code:6019,name:"ListingHasNoBids",msg:"Listing has no bids"},{code:6020,name:"ListingNotActive",msg:"Listing is not active"},{code:6021,name:"InvalidAuctionWinner",msg:"Invalid auction winner"},{code:6022,name:"SaleNotEnded",msg:"Listing sale has not ended"},{code:6023,name:"BidRefunded",msg:"Bid has been refunded"},{code:6024,name:"BidNotActive",msg:"Bid is not active"},{code:6025,name:"StartingPriceLessThanMinimumBidThreshold",msg:"Starting price is less than the minimum bid threshold"},{code:6026,name:"BidRequestActionInvalid",msg:"Bid request action invalid"}]};class b{constructor(e,t,r,o){this.connection=void 0,this.program=void 0,this.wallet=void 0,this.applicationState=void 0,this.connection=new a(t),u.setProvider(new u.AnchorProvider(this.connection,new u.Wallet(o||n.generate()),{})),this.program=new m(S,e),this.wallet=o,this.applicationState=r||null}async getRecentBlockHash(){return await this.connection.getLatestBlockhash()}}class M{constructor(e){this.programLoader=void 0,this.auctionManagerProgram=void 0,this.programLoader=e,this.auctionManagerProgram=new h(e)}async create(r,a,n,c,u,m){try{var g=await this.programLoader.program.account.applicationState.fetch(this.programLoader.applicationState);const d=[o.utils.bytes.utf8.encode("user_account"),Uint8Array.from(a.toBuffer()),Uint8Array.from(g.listingAuthority.toBuffer()),o.utils.bytes.utf8.encode("deserialize")],[l,p]=e.findProgramAddressSync(d,this.programLoader.program.programId);console.log("[*] Userdata: ",l),console.log("[*] Userdata bump: ",p);const f=[o.utils.bytes.utf8.encode("bid_request"),Uint8Array.from(r.toBuffer()),Uint8Array.from(n.toBuffer()),Uint8Array.from(a.toBuffer()),Uint8Array.from(c.toBuffer())],[y,h]=e.findProgramAddressSync(f,this.programLoader.program.programId);console.log("[*] Bid request: ",y),console.log("[*] Bid request bump: ",h);let w=[],A=await i(c,y,!0,u),S=await i(c,a,!0,u),b=await this.programLoader.connection.getAccountInfo(A),M=await this.programLoader.connection.getAccountInfo(S);if(!b){let e=s(a,A,y,c,u);w.push(e)}if(!M){let e=s(a,S,a,c,u);w.push(e)}const B=Math.floor(Date.now()/1e3),L=await this.programLoader.program.methods.createBidRequest(new o.BN(m),new o.BN(B)).accounts({applicationState:this.programLoader.applicationState,userdata:l,bidRequest:y,nftOwner:n,nft:r,tokenMint:c,feeAccount:g.feeAccount,escrowTokenAccount:A,userTokenAccount:S,user:a,tokenProgram:u,systemProgram:t.programId}).instruction();return w.push(L),w}catch(e){throw console.error(e),new Error(`Failed to create bid request: ${e.message}`)}}async createTransaction(e,t,a,n,o,i){try{const c=await this.create(e,t,a,n,o,i);var s=(new r).add(...c);return s.recentBlockhash=(await this.programLoader.getRecentBlockHash()).blockhash,s.feePayer=t,s}catch(e){throw console.error(e),new Error(`Failed to create bid request transaction: ${e.message}`)}}async get(e){try{return await this.programLoader.program.account.bidRequest.fetch(e)}catch(e){throw console.error(e),new Error(`Failed to get bid request: ${e.message}`)}}async fetchMany(e){try{return await this.programLoader.program.account.bidRequest.fetchMultiple(e)}catch(e){throw console.error(e),new Error(`Failed to fetch many bid requests: ${e.message}`)}}async getAll(){try{return await this.programLoader.program.account.bidRequest.all()}catch(e){throw console.error(e),new Error(`Failed to get all bid requests: ${e.message}`)}}async getByNft(e){try{return(await this.getAll()).filter(t=>t.nft.equals(e))}catch(e){throw console.error(e),new Error(`Failed to get bid requests by nft: ${e.message}`)}}async getByAuthority(e){try{return(await this.getAll()).filter(t=>t.authority.equals(e))}catch(e){throw console.error(e),new Error(`Failed to get bid requests by authority: ${e.message}`)}}async getByNftOwner(e){try{return(await this.getAll()).filter(t=>t.owner.equals(e))}catch(e){throw console.error(e),new Error(`Failed to get bid requests by nft owner: ${e.message}`)}}async accept(a,n,u,m,g,d){try{const P=[o.utils.bytes.utf8.encode("deserialize"),Uint8Array.from(this.programLoader.wallet.publicKey.toBuffer())],[U,R]=e.findProgramAddressSync(P,this.programLoader.program.programId);console.log("[*] Application state: ",U);var l=await this.programLoader.program.account.applicationState.fetch(U);const T=[o.utils.bytes.utf8.encode("bid_request"),Uint8Array.from(a.toBuffer()),Uint8Array.from(u.toBuffer()),Uint8Array.from(d.toBuffer()),Uint8Array.from(m.toBuffer())],[I,E]=e.findProgramAddressSync(T,this.programLoader.program.programId);console.log("[*] Bid request: ",I),console.log("[*] Bid request bump: ",E);var p=new r,f=await i(a,u,!0,n),y=await i(a,d,!0,n),h=await i(m,I,!0,g),w=await i(m,u,!0,g),A=await i(m,l.feeAccount,!0,g),S=await this.programLoader.connection.getAccountInfo(f),b=await this.programLoader.connection.getAccountInfo(y),M=await this.programLoader.connection.getAccountInfo(h),B=await this.programLoader.connection.getAccountInfo(w),L=await this.programLoader.connection.getAccountInfo(A);if(!S){let e=await s(u,f,u,a,n);p.add(e)}if(!b){let e=await s(u,y,d,a,n);p.add(e)}if(!M){let e=await s(u,h,I,m,g);p.add(e)}if(!B){let e=await s(u,w,u,m,g);p.add(e)}if(!L){let e=await s(u,A,l.feeAccount,m,g);p.add(e)}var k=Math.floor(Date.now()/1e3);let N=await this.programLoader.program.methods.acceptBidRequest(new o.BN(k)).accounts({applicationState:U,bidRequest:I,user:u,sellerNftTokenAccount:f,buyerNftTokenAccount:y,escrowTokenAccount:h,sellerTokenAccount:w,feeTokenAccount:A,nft:a,tokenMint:m,tokenProgram:g,nftTokenProgram:n,associatedTokenProgram:c,systemProgram:t.programId}).instruction();return p.add(N),p.recentBlockhash=(await this.programLoader.getRecentBlockHash()).blockhash,p.feePayer=u,p}catch(e){throw console.error(e),new Error(`Failed to accept bid request: ${e.message}`)}}async reject(a,n,u,m,g){try{const w=[o.utils.bytes.utf8.encode("bid_request"),Uint8Array.from(a.toBuffer()),Uint8Array.from(n.toBuffer()),Uint8Array.from(u.toBuffer()),Uint8Array.from(m.toBuffer())],[A,S]=e.findProgramAddressSync(w,this.programLoader.program.programId);console.log("[*] Bid request: ",A),console.log("[*] Bid request bump: ",S);var d=new r,l=await i(m,A,!0,g),p=await i(m,u,!0,g),f=await this.programLoader.connection.getAccountInfo(l),y=await this.programLoader.connection.getAccountInfo(p);if(!f){let e=await s(n,l,A,m,g);d.add(e)}if(!y){let e=await s(n,p,u,m,g);d.add(e)}var h=Math.floor(Date.now()/1e3);let b=await this.programLoader.program.methods.rejectBidRequest(new o.BN(h)).accounts({bidRequest:A,user:n,escrowTokenAccount:l,requesterTokenAccount:p,tokenMint:m,tokenProgram:g,associatedTokenProgram:c,systemProgram:t.programId}).instruction();return d.add(b),d.recentBlockhash=(await this.programLoader.getRecentBlockHash()).blockhash,d.feePayer=n,d}catch(e){throw console.error(e),new Error(`Failed to reject bid request: ${e.message}`)}}async cancel(a,n,u,m,g){try{const w=[o.utils.bytes.utf8.encode("bid_request"),Uint8Array.from(a.toBuffer()),Uint8Array.from(n.toBuffer()),Uint8Array.from(u.toBuffer()),Uint8Array.from(m.toBuffer())],[A,S]=e.findProgramAddressSync(w,this.programLoader.program.programId);console.log("[*] Bid request: ",A),console.log("[*] Bid request bump: ",S);var d=new r,l=await i(m,A,!0,g),p=await i(m,u,!0,g),f=await this.programLoader.connection.getAccountInfo(l),y=await this.programLoader.connection.getAccountInfo(p);if(!f){let e=await s(u,l,A,m,g);d.add(e)}if(!y){let e=await s(u,p,u,m,g);d.add(e)}var h=Math.floor(Date.now()/1e3);let b=await this.programLoader.program.m