UNPKG

@deserialize/auction-sdk

Version:

An SDK for Auction on SVMs

1 lines 58.1 kB
import{PublicKey as e,SystemProgram as t,Transaction as r,Connection as a,Keypair as n}from"@solana/web3.js";import*as i from"@coral-xyz/anchor";import{getAssociatedTokenAddress as o,createAssociatedTokenAccountInstruction as s,TOKEN_PROGRAM_ID as c,ASSOCIATED_TOKEN_PROGRAM_ID as u}from"@solana/spl-token";import*as m from"@project-serum/anchor";import{Program as g}from"@project-serum/anchor";function d(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 l(t.call(e));r="@@asyncIterator",a="@@iterator"}throw new TypeError("Object is not async iterable")}function l(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 l=function(e){this.s=e,this.n=e.next},l.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 l(e)}var p,f;function y(e,t){if(e.hasOwnProperty(t))return!0}!function(e){e.Active="active",e.Cancelled="cancelled",e.Refunded="refunded",e.Executed="executed"}(p||(p={}));class h{constructor(e){this.programLoader=void 0,this.programLoader=e}getListingProgram(){return new A(this.programLoader)}async create(r,a,n,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=[i.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=[i.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=[i.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=[i.utils.bytes.utf8.encode("user_account"),Uint8Array.from(a.toBuffer()),Uint8Array.from(l.listingAuthority.toBuffer()),i.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 o(d.nft,f,!0),U=await o(d.tokenMint,d.authority,!0,m),R=await o(d.tokenMint,a,!0,m),I=await o(d.nft,a,!0),T=await o(d.tokenMint,f,!0,m),E=await o(d.tokenMint,l.feeAccount,!0,m);console.log("[*] BIDDER TOKEN account: ",R),console.log("[*] BIDDER NFT account: ",I),console.log("[*] AUCTION MANAGER TOKEN escrow: ",T),console.log("[*] FEE TOKEN account: ",E);const N=[],v=await this.programLoader.connection.getAccountInfo(R),q=await this.programLoader.connection.getAccountInfo(I),K=await this.programLoader.connection.getAccountInfo(T);if(!await this.programLoader.connection.getAccountInfo(E)){const e=s(a,E,l.feeAccount,d.tokenMint,m);N.push(e)}if(!v){const e=s(a,R,a,d.tokenMint,m);N.push(e)}if(!q){const e=s(a,I,a,d.nft);N.push(e)}if(!K){const e=s(a,T,f,d.tokenMint,m);N.push(e)}const x=Math.floor(Date.now()/1e3),F=await this.programLoader.program.methods.bidOnListing(new i.BN(n),new i.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:I,bidderAccount:R,tokenEscrow:T,tokenMint:d.tokenMint,tokenProgram:c,associatedTokenProgram:u,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){try{let i=await this.create(e,t,a,n),o=new r;for(let e of i)o.add(e);return o.recentBlockhash=(await this.programLoader.getRecentBlockHash()).blockhash,o.feePayer=t,o}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 g=await this.get(r);var m=this.getListingProgram();const d=await m.get(g.listing);if(!g.bidder.equals(a))throw new Error("You cannot cancel another user's bid");let l=await this.programLoader.program.account.applicationState.fetch(this.programLoader.applicationState);const p=[i.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=[i.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=[i.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=[i.utils.bytes.utf8.encode("user_account"),Uint8Array.from(a.toBuffer()),Uint8Array.from(l.listingAuthority.toBuffer()),i.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 o(d.nft,f,!0),U=await o(d.nft,d.authority,!0);console.log("[*] AUCTION MANAGER NFT escrow: ",P),console.log("[*] LISTER NFT owner: ",U);const R=await o(d.tokenMint,a,!0,n),I=await o(d.nft,a,!0),T=await o(d.tokenMint,f,!0,n);console.log("[*] BIDDER TOKEN account: ",R),console.log("[*] BIDDER NFT account: ",I),console.log("[*] AUCTION MANAGER TOKEN escrow: ",T);let E=await Promise.all(d.bidReceipts.map(async function(e){return{amount:(await s.get(e)).amount,key:e}}));const N=Math.floor(Date.now()/1e3),v=await this.programLoader.program.methods.cancelBid(E,new i.BN(N)).accounts({applicationState:this.programLoader.applicationState,userdata:L,bidReceipt:b,user:a,listing:w,nft:d.nft,auctionManager:f,cancellerTokenAccount:R,tokenEscrow:T,tokenMint:d.tokenMint,tokenProgram:c,associatedTokenProgram:u,systemProgram:t.programId,feeAccount:l.feeAccount}).transaction();return v.recentBlockhash=(await this.programLoader.getRecentBlockHash()).blockhash,v.feePayer=a,v}catch(e){throw console.error(e),new Error(`Failed to cancel bid receipt: ${e.message}`)}}async refund(r,a,n){try{const m=await this.get(r);console.log("[*] Bid receipt data: ",JSON.stringify(m,null,4));var s=this.getListingProgram();const g=await s.get(m.listing);if(console.log("[*] Listing data: ",JSON.stringify(g,null,4)),!m.bidder.equals(a))throw new Error("You cannot cancel another user's bid");if(!y(m.state,p.Active))throw new Error("You cannot refund a bid that is not active");let d=await this.programLoader.program.account.applicationState.fetch(this.programLoader.applicationState);console.log("[*] Application state: ",JSON.stringify(d,null,4));const l=[i.utils.bytes.utf8.encode("auction_manager"),Uint8Array.from(g.nft.toBuffer()),Uint8Array.from(g.authority.toBuffer())],[f,h]=e.findProgramAddressSync(l,this.programLoader.program.programId);console.log("[*] AUCTION MANAGER",f);const w=[i.utils.bytes.utf8.encode("listing"),Uint8Array.from(g.nft.toBuffer()),Uint8Array.from(g.authority.toBuffer()),Uint8Array.from(f.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=[i.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=[i.utils.bytes.utf8.encode("user_account"),Uint8Array.from(a.toBuffer()),Uint8Array.from(d.listingAuthority.toBuffer()),i.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 o(g.nft,f,!0),R=await o(g.nft,g.authority,!0);console.log("[*] AUCTION MANAGER NFT escrow: ",U),console.log("[*] LISTER NFT owner: ",R);const I=await o(g.tokenMint,a,!0,n),T=await o(g.nft,a,!0),E=await o(g.tokenMint,f,!0,n);console.log("[*] BIDDER TOKEN account: ",I),console.log("[*] BIDDER NFT account: ",T),console.log("[*] AUCTION MANAGER TOKEN escrow: ",E);const N=Math.floor(Date.now()/1e3);return await this.programLoader.program.methods.refundBid(new i.BN(N)).accounts({applicationState:this.programLoader.applicationState,user:a,auctionManager:f,listing:A,nft:g.nft,userdata:k,userRefundAccount:I,refundBidReceipt:M,tokenEscrow:E,tokenMint:g.tokenMint,tokenProgram:c,associatedTokenProgram:u,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 g=await this.get(r);var m=this.getListingProgram();const d=await m.get(g.listing);if(!g.bidder.equals(a))throw new Error("You cannot increase another user's bid");if(g.state!==p.Active)throw new Error("You cannot increase a bid that is not active");let l=await this.programLoader.program.account.applicationState.fetch(this.programLoader.applicationState);const f=[i.utils.bytes.utf8.encode("auction_manager"),Uint8Array.from(d.nft.toBuffer()),Uint8Array.from(d.authority.toBuffer())],[y,h]=e.findProgramAddressSync(f,this.programLoader.program.programId),w=[i.utils.bytes.utf8.encode("listing"),Uint8Array.from(d.nft.toBuffer()),Uint8Array.from(d.authority.toBuffer()),Uint8Array.from(y.toBuffer()),Uint8Array.from(d.tokenMint.toBuffer())],[A,S]=e.findProgramAddressSync(w,this.programLoader.program.programId);console.log("[*] Listing: ",A),console.log("[*] Listing bump: ",S);const b=[i.utils.bytes.utf8.encode("bid_receipt"),Uint8Array.from(A.toBuffer()),Uint8Array.from(a.toBuffer()),Uint8Array.from(d.tokenMint.toBuffer())],[M,B]=e.findProgramAddressSync(b,this.programLoader.program.programId);console.log("[*] Bid receipt: ",M),console.log("[*] Bid receipt bump: ",B);const L=[i.utils.bytes.utf8.encode("user_account"),Uint8Array.from(a.toBuffer()),Uint8Array.from(l.listingAuthority.toBuffer()),i.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 o(d.nft,y,!0),R=await o(d.nft,d.authority,!0);console.log("[*] AUCTION MANAGER NFT escrow: ",U),console.log("[*] LISTER NFT owner: ",R);const I=await o(d.tokenMint,a,!0,s),T=await o(d.nft,a,!0),E=await o(d.tokenMint,y,!0,s);console.log("[*] BIDDER TOKEN account: ",I),console.log("[*] BIDDER NFT account: ",T),console.log("[*] AUCTION MANAGER TOKEN escrow: ",E);const N=Math.floor(Date.now()/1e3),v=await this.programLoader.program.methods.increaseBid(new i.BN(n),new i.BN(N)).accounts({applicationState:this.programLoader.applicationState,userdata:k,bidReceipt:M,user:a,listing:A,nft:d.nft,auctionManager:y,bidderAccount:I,tokenEscrow:E,tokenMint:d.tokenMint,tokenProgram:c,associatedTokenProgram:u,systemProgram:t.programId,feeAccount:l.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 w{constructor(e){this.programLoader=void 0,this.programLoader=e}async create(r,a){try{const n=[i.utils.bytes.utf8.encode("auction_manager"),Uint8Array.from(r.toBuffer()),Uint8Array.from(a.toBuffer())],[o,s]=e.findProgramAddressSync(n,this.programLoader.program.programId);return console.log("[*] Auction manager: ",o),console.log("[*] Auction manager bump: ",s),await this.programLoader.program.methods.createAuctionManager().accounts({applicationState:this.programLoader.applicationState,nft:r,systemProgram:t.programId,auctionManager:o,tokenProgram:c,rent:i.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 A{constructor(e){this.programLoader=void 0,this.auctionManagerProgram=void 0,this.programLoader=e,this.auctionManagerProgram=new w(e)}async create(r,a,n,u,m,g,d){try{const l=[i.utils.bytes.utf8.encode("auction_manager"),Uint8Array.from(r.toBuffer()),Uint8Array.from(u.toBuffer())],[p,f]=e.findProgramAddressSync(l,this.programLoader.program.programId);console.log("[*] Auction manager: ",p),console.log("[*] Auction manager bump: ",f);const y=[i.utils.bytes.utf8.encode("listing"),Uint8Array.from(r.toBuffer()),Uint8Array.from(u.toBuffer()),Uint8Array.from(p.toBuffer()),Uint8Array.from(a.toBuffer())],[h,w]=e.findProgramAddressSync(y,this.programLoader.program.programId);console.log("[*] Listing: ",h),console.log("[*] Listing bump: ",w);let A=await this.programLoader.program.account.applicationState.fetch(this.programLoader.applicationState);const S=[i.utils.bytes.utf8.encode("user_account"),Uint8Array.from(u.toBuffer()),Uint8Array.from(A.listingAuthority.toBuffer()),i.utils.bytes.utf8.encode("deserialize")],[b,M]=e.findProgramAddressSync(S,this.programLoader.program.programId);console.log("[*] Userdata: ",b),console.log("[*] Userdata bump: ",M);let B=[],L=await o(r,p,!0),k=await o(r,u,!0),P=await o(a,u,!0,n),U=await this.programLoader.connection.getAccountInfo(L),R=await this.programLoader.connection.getAccountInfo(k),I=await this.programLoader.connection.getAccountInfo(P);if(!await this.programLoader.connection.getAccountInfo(p)){let e=await this.auctionManagerProgram.create(r,u);B.push(e)}if(!U){let e=s(u,L,p,r);B.push(e)}if(!R){let e=s(u,k,u,r);B.push(e)}if(!I){let e=s(u,P,u,a,n);B.push(e)}const T=Math.floor(Date.now()/1e3);let E=null;d&&(E=T+86400*d);const N=await this.programLoader.program.methods.createListing(new i.BN(m),new i.BN(g),new i.BN(T),E?new i.BN(E):E).accounts({applicationState:this.programLoader.applicationState,auctionManager:p,listing:h,owner:u,userdata:b,nft:r,nftEscrow:L,proceedsWallet:P,tokenMint:a,feeAccount:A.feeAccount,nftOwner:k,tokenProgram:c,systemProgram:t.programId,rent:i.web3.SYSVAR_RENT_PUBKEY}).instruction();return B.push(N),B}catch(e){throw console.error(e),new Error(`Failed to create listing: ${e.message}`)}}async createTransaction(e,t,a,n,i,o,s){try{let c=await this.create(e,t,a,n,i,o,s),u=(new r).add(...c);return u.recentBlockhash=(await this.programLoader.getRecentBlockHash()).blockhash,u.feePayer=n,u}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){try{let a=await this.get(r);const n=[i.utils.bytes.utf8.encode("auction_manager"),Uint8Array.from(a.nft.toBuffer()),Uint8Array.from(a.authority.toBuffer())],[s,u]=e.findProgramAddressSync(n,this.programLoader.program.programId),m=[i.utils.bytes.utf8.encode("listing"),Uint8Array.from(a.nft.toBuffer()),Uint8Array.from(a.authority.toBuffer()),Uint8Array.from(s.toBuffer()),Uint8Array.from(a.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=[i.utils.bytes.utf8.encode("user_account"),Uint8Array.from(a.authority.toBuffer()),Uint8Array.from(l.listingAuthority.toBuffer()),i.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 o(a.nft,s,!0),w=await o(a.nft,a.authority,!0);const A=Math.floor(Date.now()/1e3),S=await this.programLoader.program.methods.cancelListing(new i.BN(A)).accounts({applicationState:this.programLoader.applicationState,auctionManager:s,listing:g,userdata:f,nft:a.nft,nftEscrow:h,originalNftHolder:w,feeAccount:l.feeAccount,user:a.authority,tokenProgram:c,systemProgram:t.programId}).transaction();return S.recentBlockhash=(await this.programLoader.getRecentBlockHash()).blockhash,S.feePayer=a.authority,S}catch(e){throw console.error(e),new Error(`Failed to cancel listing (${r}): ${e.message}`)}}async getBids(e){try{const t=new h(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 h(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 o=await this.get(e),s=o.bidReceipts.filter(e=>e!==o.highestBidReceipt),c=new h(this.programLoader);if(0===o.bidReceipts.length)return[];const u=[];var t,r=!1,a=!1;try{for(var n,i=d(s);r=!(n=await i.next()).done;r=!1){let e=n.value;if(!e.equals(o.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!=i.return&&await i.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"}(f||(f={}));class S{constructor(e){this.programLoader=void 0,this.listingProgram=void 0,this.bidReceiptProgram=void 0,this.programLoader=e,this.listingProgram=new A(e),this.bidReceiptProgram=new h(e)}async create(r){try{const a=[i.utils.bytes.utf8.encode("deserialize"),Uint8Array.from(this.programLoader.wallet.publicKey.toBuffer())],[n,o]=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){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=[i.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=[i.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 g=await this.programLoader.program.account.listing.fetch(P);if(console.log("[*] Listing: ",JSON.stringify(g,null,4)),!a.equals(P))throw new Error("Invalid Listing");if(console.log("[*] Listing state: ",S.state),!y(S.state,f.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 I=await this.listingProgram.getBidsInfoToRefund(a);console.log("[*] Bids info to refund: ",JSON.stringify(I,null,4));let T=new r;var l,p=!1,h=!1;try{for(var w,A=d(I);p=!(w=await A.next()).done;p=!1){let e=w.value;{let t=await this.bidReceiptProgram.refund(e.receipt,e.bidder,s);T.add(t)}}}catch(e){h=!0,l=e}finally{try{p&&null!=A.return&&await A.return()}finally{if(h)throw l}}const E=await o(S.nft,B,!0),N=await o(S.nft,R.bidder,!0),v=await o(S.tokenMint,B,!0),q=await o(S.tokenMint,b.feeAccount,!0),K=[i.utils.bytes.utf8.encode("user_account"),Uint8Array.from(R.bidder.toBuffer()),Uint8Array.from(b.listingAuthority.toBuffer()),i.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 i.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:c,associatedTokenProgram:u,systemProgram:t.programId,feeTokenAccount:q}).instruction();return T.add($),T.recentBlockhash=(await this.programLoader.getRecentBlockHash()).blockhash,T.feePayer=n,T}catch(e){throw console.error(e),new Error(`Failed to execute sale: ${e.message}`)}}}const b={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:"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:"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:"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:"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:"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 M{constructor(e,t,r,i){this.connection=void 0,this.program=void 0,this.wallet=void 0,this.applicationState=void 0,this.connection=new a(t),m.setProvider(new m.AnchorProvider(this.connection,new m.Wallet(i||n.generate()),{})),this.program=new g(b,e),this.wallet=i,this.applicationState=r||null}async getRecentBlockHash(){return await this.connection.getLatestBlockhash()}}class B{constructor(e){this.programLoader=void 0,this.auctionManagerProgram=void 0,this.programLoader=e,this.auctionManagerProgram=new w(e)}async create(r,a,n,u,m,g){try{var d=await this.programLoader.program.account.applicationState.fetch(this.programLoader.applicationState);const l=[i.utils.bytes.utf8.encode("user_account"),Uint8Array.from(a.toBuffer()),Uint8Array.from(d.listingAuthority.toBuffer()),i.utils.bytes.utf8.encode("deserialize")],[p,f]=e.findProgramAddressSync(l,this.programLoader.program.programId);console.log("[*] Userdata: ",p),console.log("[*] Userdata bump: ",f);const y=[i.utils.bytes.utf8.encode("bid_request"),Uint8Array.from(r.toBuffer()),Uint8Array.from(n.toBuffer()),Uint8Array.from(a.toBuffer()),Uint8Array.from(u.toBuffer())],[h,w]=e.findProgramAddressSync(y,this.programLoader.program.programId);console.log("[*] Bid request: ",h),console.log("[*] Bid request bump: ",w);let A=[],S=await o(u,h,!0,m),b=await o(u,a,!0,m),M=await this.programLoader.connection.getAccountInfo(S),B=await this.programLoader.connection.getAccountInfo(b);if(!M){let e=s(a,S,h,u,m);A.push(e)}if(!B){let e=s(a,b,a,u,m);A.push(e)}const L=Math.floor(Date.now()/1e3),k=await this.programLoader.program.methods.createBidRequest(new i.BN(g),new i.BN(L)).accounts({applicationState:this.programLoader.applicationState,userdata:p,bidRequest:h,nftOwner:n,nft:r,tokenMint:u,feeAccount:d.feeAccount,escrowTokenAccount:S,userTokenAccount:b,user:a,tokenProgram:c,systemProgram:t.programId}).instruction();return A.push(k),A}catch(e){throw console.error(e),new Error(`Failed to create bid request: ${e.message}`)}}async createTransaction(e,t,a,n,i,o){try{const c=await this.create(e,t,a,n,i,o);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,m,g,d){try{const P=[i.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 I=[i.utils.bytes.utf8.encode("bid_request"),Uint8Array.from(a.toBuffer()),Uint8Array.from(n.toBuffer()),Uint8Array.from(d.toBuffer()),Uint8Array.from(m.toBuffer())],[T,E]=e.findProgramAddressSync(I,this.programLoader.program.programId);console.log("[*] Bid request: ",T),console.log("[*] Bid request bump: ",E);var p=new r,f=await o(a,n,!0),y=await o(a,d,!0),h=await o(m,T,!0,g),w=await o(m,n,!0,g),A=await o(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(n,f,n,a);p.add(e)}if(!b){let e=await s(n,y,d,a);p.add(e)}if(!M){let e=await s(n,h,T,m,g);p.add(e)}if(!B){let e=await s(n,w,n,m,g);p.add(e)}if(!L){let e=await s(n,A,l.feeAccount,m,g);p.add(e)}var k=Math.floor(Date.now()/1e3);let N=await this.programLoader.program.methods.acceptBidRequest(new i.BN(k)).accounts({applicationState:U,bidRequest:T,user:n,sellerNftTokenAccount:f,buyerNftTokenAccount:y,escrowTokenAccount:h,sellerTokenAccount:w,feeTokenAccount:A,nft:a,tokenMint:m,tokenProgram:c,associatedTokenProgram:u,systemProgram:t.programId}).instruction();return p.add(N),p.recentBlockhash=(await this.programLoader.getRecentBlockHash()).blockhash,p.feePayer=n,p}catch(e){throw console.error(e),new Error(`Failed to accept bid request: ${e.message}`)}}async reject(a,n,m,g,d){try{const A=[i.utils.bytes.utf8.encode("bid_request"),Uint8Array.from(a.toBuffer()),Uint8Array.from(n.toBuffer()),Uint8Array.from(m.toBuffer()),Uint8Array.from(g.toBuffer())],[S,b]=e.findProgramAddressSync(A,this.programLoader.program.programId);console.log("[*] Bid request: ",S),console.log("[*] Bid request bump: ",b);var l=new r,p=await o(g,S,!0,d),f=await o(g,m,!0,d),y=await this.programLoader.connection.getAccountInfo(p),h=await this.programLoader.connection.getAccountInfo(f);if(!y){let e=await s(n,p,S,g,d);l.add(e)}if(!h){let e=await s(n,f,m,g,d);l.add(e)}var w=Math.floor(Date.now()/1e3);let M=await this.programLoader.program.methods.rejectBidRequest(new i.BN(w)).accounts({bidRequest:S,user:n,escrowTokenAccount:p,requesterTokenAccount:f,tokenMint:g,tokenProgram:c,associatedTokenProgram:u,systemProgram:t.programId}).instruction();return l.add(M),l.recentBlockhash=(await this.programLoader.getRecentBlockHash()).blockhash,l.feePayer=n,l}catch(e){throw console.error(e),new Error(`Failed to reject bid request: ${e.message}`)}}async cancel(a,n,m,g,d){try{const A=[i.utils.bytes.utf8.encode("bid_request"),Uint8Array.from(a.toBuffer()),Uint8Array.from(n.toBuffer()),Uint8Array.from(m.toBuffer()),Uint8Array.from(g.toBuffer())],[S,b]=e.findProgramAddressSync(A,this.programLoader.program.programId);console.log("[*] Bid request: ",S),console.log("[*] Bid request bump: ",b);var l=new r,p=await o(g,S,!0,d),f=await o(g,m,!0,d),y=await this.programLoader.connection.getAccountInfo(p),h=await this.programLoader.connection.getAccountInfo(f);if(!y){let e=await s(m,p,S,g,d);l.add(e)}if(!h){let e=await s(m,f,m,g,d);l.add(e)}var w=Math.floor(Date.now()/1e3);let M=await this.programLoader.program.methods.cancelBidRequest(new i.BN(w)).accounts({bidRequest:S,user:m,escrowTokenAccount:p,cancellerTokenAccount:f,tokenMint:g,tokenProgram:c,associatedTokenProgram:u,systemProgram:t.programId}).instruction();return l.add(M),l.recentBlockhash=(await this.programLoader.getRecentBlockHash()).blockhash,l.feePayer=m,l}catch(e){throw console.error(e),new Error(`Failed to cancel bid request: ${e.message}`)}}async increaseAmount(a,n,m,g,d,l){try{const S=[i.utils.bytes.