@mojito-inc/secondary-market
Version:
Mojito secondary market is the platform to purchase NFT.
2 lines (1 loc) • 12.8 kB
JavaScript
import{ethers as e,utils as i}from"ethers";import{PublicKey as n,LAMPORTS_PER_SOL as t,Transaction as o,ComputeBudgetProgram as a,Connection as r,clusterApiUrl as l,Keypair as s,TransactionInstruction as c}from"@solana/web3.js";import{getAccount as u,getAssociatedTokenAddress as d,createApproveInstruction as v}from"@solana/spl-token";import f from"bn.js";import{roundUp as m}from"./truncateDecimal.utils.js";import{getABIFile as g}from"./getABIFile.utils.js";import{ErrorsType as p}from"../constant/index.js";import{getSolanaEnvByChainId as h}from"./getEtherScanURL.utils.js";import{customErrorMessage as k}from"./customErrorMessage.utils.js";import{getVinTokenAddress as b}from"./getVinTokenAddress.utils.js";import{getSolanaBalance as w}from"./getSolanaBalance.utils.js";import{fetchSolanaPriorityFee as y}from"./fetchSolanaPriorityFee.utils.js";async function A({price:i,status:n,marketPlaceContractAddress:t,currencyContractAddress:o,currency:a,provider:r}){try{const l=g(a),s=m(i,2),c=r.getSigner(),u=await c.getAddress(),d=new e.Contract(o,l,c),v=await d.allowance(u,t);return Number(v)/1e18>=s?!n||"success":!!n&&"error"}catch(e){return!!n&&"error"}}async function S({marketPlaceContractAddress:n,currencyContractAddress:t,currency:o,provider:a}){var r,l;try{const r=g(o),l=a.getSigner(),s=i.parseEther("999999999"),c=new e.Contract(t,r,l),u=await c.approve(n,s);return await a.waitForTransaction(u.hash,1),{status:!0,message:"Success"}}catch(e){return(null===(r=e.message)||void 0===r?void 0:r.includes(p.USER_REJECTED))||(null===(l=e.message)||void 0===l?void 0:l.includes(p.USER_DENIED))?{status:!1,message:"Reject"}:{status:!1,message:"Error"}}}const T=async(e,i,m,g,A,S,T,I,E,C,O=.00228288)=>{var B,P,D,L,M,R,U,V,x,N,j,F,H,_,W,z,J,K,q,$,Y,G,Q,X,Z,ee,ie,ne,te,oe,ae,re,le,se,ce,ue,de,ve,fe,me,ge,pe,he,ke,be,we;try{if(!m)return{success:!1,error:"Your wallet session has been closed. Please reconnect your wallet to continue."};const i=h(S),be=await w(e,i,E),{instruction:we,offerAccount:ye,connection:Ae,err:Se,approveInstruction:Te,buyerVinTokenAccount:Ie,pda:Ee}=await(async(e,i,t,o,a,u,m,g,p)=>{var w,y,A,S,T,I,E,C,O,B,P,D,L,M,R;try{const t=new n(e),o=h(i),a=new r(p||l(o),{confirmTransactionInitialTimeout:3e5,commitment:"confirmed"}),u=new n(null!==(w=null==g?void 0:g.mintAccount)&&void 0!==w?w:""),k=new n(null!==(y=null==g?void 0:g.tokenProgram)&&void 0!==y?y:""),U=new n(null!==(A=null==g?void 0:g.systemProgram)&&void 0!==A?A:""),V=new n(null!==(S=null==g?void 0:g.offerEscrow)&&void 0!==S?S:""),x=s.generate(),N=await d(new n(b(o,p)),t),j=new n(null!==(T=null==g?void 0:g.offerEscrow)&&void 0!==T?T:""),F=[Buffer.from("marketplace")],[H,_]=n.findProgramAddressSync(F,j),W=new n((null==g?void 0:g.seller)||m),z=new c({programId:V,keys:[{pubkey:t,isSigner:!0,isWritable:!0},{pubkey:W,isSigner:!1,isWritable:!0},{pubkey:u,isSigner:!1,isWritable:!0},{pubkey:x.publicKey,isSigner:!0,isWritable:!0},{pubkey:k,isSigner:!1,isWritable:!0},{pubkey:U,isSigner:!1,isWritable:!0}],data:Buffer.concat([Buffer.from(null!==(I=null==g?void 0:g.makeOfferDiscriminator)&&void 0!==I?I:[]),Buffer.from(Uint8Array.of(...new f(null!==(C=null===(E=null==g?void 0:g.bubblegumMakeOfferArg)||void 0===E?void 0:E.offerAmount)&&void 0!==C?C:0).toArray("le",8),...new f(null!==(B=null===(O=null==g?void 0:g.bubblegumMakeOfferArg)||void 0===O?void 0:O.expirationTime)&&void 0!==B?B:0).toArray("le",8),...new f(null!==(D=null===(P=null==g?void 0:g.bubblegumMakeOfferArg)||void 0===P?void 0:P.index)&&void 0!==D?D:0).toArray("le",8)))])}),J=(null!==(M=null===(L=null==g?void 0:g.bubblegumMakeOfferArg)||void 0===L?void 0:L.offerAmount)&&void 0!==M?M:0)+(null!==(R=null==g?void 0:g.totalActiveOfferAmount)&&void 0!==R?R:0);return{instruction:z,offerAccount:x,approveInstruction:v(N,H,t,J),buyerVinTokenAccount:N,pda:H,connection:a}}catch(e){return{err:k(e)}}})(e,S,0,0,0,be.native,T,I,E);let Ce;if(!Se&&we){const i=new n(e);if("phantom"===g.toLowerCase()||"backpack"===g.toLowerCase()){if(be.nonNative*t<(null!==(P=null===(B=null==I?void 0:I.bubblegumMakeOfferArg)||void 0===B?void 0:B.offerAmount)&&void 0!==P?P:0))return{success:!1,error:"Insufficient VIN for initiating the transaction."};try{A("Verifying token approval for the transaction.");const n=await u(Ae,Ie),r=(null!==(L=null===(D=n.delegate)||void 0===D?void 0:D.toString())&&void 0!==L?L:"")===Ee.toString();if(!n.delegate||!r||n.delegatedAmount<(null!==(R=null===(M=null==I?void 0:I.bubblegumMakeOfferArg)||void 0===M?void 0:M.offerAmount)&&void 0!==R?R:0)+(null!==(U=null==I?void 0:I.totalActiveOfferAmount)&&void 0!==U?U:0)){const n=new o({feePayer:i}).add(Te),r=await Ae.getLatestBlockhash("confirmed");n.recentBlockhash=r.blockhash,n.lastValidBlockHeight=r.lastValidBlockHeight,n.add(a.setComputeUnitLimit({units:14e5}));const l=await y(E,n.serialize({verifySignatures:!1})),s=a.setComputeUnitPrice({microLamports:l});n.add(s);const c=await Ae.getFeeForMessage(n.compileMessage(),"confirmed"),u=be.native*t,d=(null!==(V=null==c?void 0:c.value)&&void 0!==V?V:0)+O*t;if(u<=0||u<d)return{success:!1,error:"Insufficient SOL for initiating the transaction."};A(0===(null!==(x=null==I?void 0:I.totalActiveOfferAmount)&&void 0!==x?x:0)?"Processing token approval.":`Processing token approval for ${((null!==(N=null==I?void 0:I.totalActiveOfferAmount)&&void 0!==N?N:0)+(null!==(F=null===(j=null==I?void 0:I.bubblegumMakeOfferArg)||void 0===j?void 0:j.offerAmount)&&void 0!==F?F:0))/t} VIN. This includes your current offer and extra tokens to cover your existing active offers.`);const v=await(null===(_=null===(H=null==m?void 0:m.adapter)||void 0===H?void 0:H.sendTransaction)||void 0===_?void 0:_.call(H,n,Ae,{skipPreflight:!0,preflightCommitment:"confirmed"}));await Ae.confirmTransaction(Object.assign({signature:v},r),"confirmed");const f=await Ae.getSignatureStatus(v);if(null!==(null===(W=null==f?void 0:f.value)||void 0===W?void 0:W.err))return null==C||C({key:"Additional Data",value:{address:e,balance:be,providerType:g,chainId:S,path:"Inside an make offer solana contract function - 268"}},{exception:null===(z=null==f?void 0:f.value)||void 0===z?void 0:z.err}),{success:!1,error:"Transaction Failed"};Ce={nonNative:be.nonNative,native:be.native-d/t}}else Ce=be;const l=new o({feePayer:i}).add(we),s=await Ae.getLatestBlockhash("confirmed");l.recentBlockhash=s.blockhash,l.lastValidBlockHeight=s.lastValidBlockHeight,l.add(a.setComputeUnitLimit({units:14e5}));const c=await y(E,l.serialize({verifySignatures:!1})),d=a.setComputeUnitPrice({microLamports:c});l.add(d);const v=await Ae.getFeeForMessage(l.compileMessage(),"confirmed"),f=Ce.native*t,p=(null!==(J=null==v?void 0:v.value)&&void 0!==J?J:0)+O*t;if(f<=0||f<p)return{success:!1,error:"Insufficient SOL for initiating the transaction."};A("Initiating make offer transaction.");const h=await(null===(q=null===(K=null==m?void 0:m.adapter)||void 0===K?void 0:K.sendTransaction)||void 0===q?void 0:q.call(K,l,Ae,{signers:[ye],skipPreflight:!0,preflightCommitment:"confirmed"}));await Ae.confirmTransaction(Object.assign({signature:h},s),"confirmed");const k=await Ae.getSignatureStatus(h);return null===(null===($=null==k?void 0:k.value)||void 0===$?void 0:$.err)?{success:!0,response:{hash:h,offerReceipt:ye.publicKey.toString()}}:(null==C||C({key:"Additional Data",value:{address:e,balance:be,providerType:g,chainId:S,path:"Inside an make offer solana contract function - 338"}},{exception:null===(Y=null==k?void 0:k.value)||void 0===Y?void 0:Y.err}),{success:!1,error:"Transaction Failed"})}catch(i){return(null===(Q=null===(G=null==i?void 0:i.message)||void 0===G?void 0:G.includes)||void 0===Q?void 0:Q.call(G,p.USER_REJECTED))||(null===(X=null==i?void 0:i.includes)||void 0===X?void 0:X.call(i,p.USER_REJECTED))||(null===(Z=null==i?void 0:i.includes)||void 0===Z?void 0:Z.call(i,p.USER_DENIED))||(null===(ie=null===(ee=null==i?void 0:i.message)||void 0===ee?void 0:ee.includes)||void 0===ie?void 0:ie.call(ee,p.USER_DENIED))?{success:!1,error:"Transaction has been cancelled by user"}:(null==C||C({key:"Additional Data",value:{address:e,balance:be,providerType:g,chainId:S,path:"Inside an make offer solana contract function - 366"}},{exception:i}),{success:!1,error:k(i)})}}if("magic"===(null==g?void 0:g.toLowerCase())){if(be.nonNative*t<(null!==(te=null===(ne=null==I?void 0:I.bubblegumMakeOfferArg)||void 0===ne?void 0:ne.offerAmount)&&void 0!==te?te:0))return{success:!1,error:"Insufficient VIN for initiating the transaction."};A("Verifying token approval for the transaction.");const n=await u(Ae,Ie),r=(null!==(ae=null===(oe=n.delegate)||void 0===oe?void 0:oe.toString())&&void 0!==ae?ae:"")===Ee.toString();if(!n.delegate||!r||n.delegatedAmount<(null!==(le=null===(re=null==I?void 0:I.bubblegumMakeOfferArg)||void 0===re?void 0:re.offerAmount)&&void 0!==le?le:0)+(null!==(se=null==I?void 0:I.totalActiveOfferAmount)&&void 0!==se?se:0)){const n=new o({feePayer:i}).add(Te),r=await Ae.getLatestBlockhash("confirmed");n.recentBlockhash=r.blockhash,n.lastValidBlockHeight=r.lastValidBlockHeight,n.add(a.setComputeUnitLimit({units:14e5}));const l=await y(E,n.serialize({verifySignatures:!1})),s=a.setComputeUnitPrice({microLamports:l});n.add(s);const c=await Ae.getFeeForMessage(n.compileMessage(),"confirmed"),u=(null!==(ce=null==c?void 0:c.value)&&void 0!==ce?ce:0)+O*t,d=be.native*t;if(d<=0||d<u)return{success:!1,error:"Insufficient SOL for initiating the transaction."};A(0===(null!==(ue=null==I?void 0:I.totalActiveOfferAmount)&&void 0!==ue?ue:0)?"Processing token approval.":`Processing token approval for ${(null!==(de=null==I?void 0:I.totalActiveOfferAmount)&&void 0!==de?de:0)/t} VIN. This includes your current offer and extra tokens to cover your existing active offers.`);const v=await(null===(ve=null==m?void 0:m.solana)||void 0===ve?void 0:ve.signTransaction(n,{requireAllSignatures:!1,verifySignatures:!0})),f=await Ae.sendRawTransaction(Buffer.from(v.rawTransaction,"base64"),{skipPreflight:!0,preflightCommitment:"confirmed"});await Ae.confirmTransaction(Object.assign({signature:f},r),"confirmed");const p=await Ae.getSignatureStatus(f);if(null!==(null===(fe=null==p?void 0:p.value)||void 0===fe?void 0:fe.err))return null==C||C({key:"Additional Data",value:{address:e,balance:be,providerType:g,chainId:S,path:"Inside an make offer solana contract function - 444"}},{exception:null===(me=null==p?void 0:p.value)||void 0===me?void 0:me.err}),{success:!1,error:"Transaction Failed"};Ce={nonNative:be.nonNative,native:be.native-u/t}}else Ce=be;const l=new o({feePayer:i}).add(we),s=await Ae.getLatestBlockhash("confirmed");l.recentBlockhash=s.blockhash,l.lastValidBlockHeight=s.lastValidBlockHeight,l.add(a.setComputeUnitLimit({units:14e5}));const c=await y(E,l.serialize({verifySignatures:!1})),d=a.setComputeUnitPrice({microLamports:c});l.add(d);const v=await Ae.getFeeForMessage(l.compileMessage(),"confirmed"),f=(null!==(ge=null==v?void 0:v.value)&&void 0!==ge?ge:0)+O*t,p=Ce.native*t;if(p<=0||p<f)return{success:!1,error:"Insufficient SOL for initiating the transaction."};A("Initiating make offer transaction.");const h=await(null===(pe=null==m?void 0:m.solana)||void 0===pe?void 0:pe.signTransaction(l,{requireAllSignatures:!1,verifySignatures:!0})),k=o.from(Buffer.from(h.rawTransaction,"base64"));k.partialSign(ye);const b=k.serialize(),w=await Ae.sendRawTransaction(b,{skipPreflight:!0,preflightCommitment:"confirmed"});await Ae.confirmTransaction(Object.assign({signature:w},s),"confirmed");const T=await Ae.getSignatureStatus(w);return null===(null===(he=null==T?void 0:T.value)||void 0===he?void 0:he.err)?{success:!0,response:{hash:w,offerReceipt:ye.publicKey.toString()}}:(null==C||C({key:"Additional Data",value:{address:e,balance:be,providerType:g,chainId:S,path:"Inside an make offer solana contract function - 525"}},{exception:null===(ke=null==T?void 0:T.value)||void 0===ke?void 0:ke.err}),{success:!1,error:"Transaction Failed"})}return{success:!1,error:"Invalid provider. Please try again later."}}return Se?(null==Se?void 0:Se.includes("Assertion failed"))?{success:!1,error:"A maximum of 9,000,000 VIN can be processed per transaction."}:(null==C||C({key:"Additional Data",value:{address:e,balance:be,providerType:g,chainId:S,path:"Inside an make offer solana contract function - 554"}},{exception:Se}),{success:!1,error:Se}):null}catch(i){return(null===(be=i.message)||void 0===be?void 0:be.includes(p.USER_REJECTED))||(null===(we=i.message)||void 0===we?void 0:we.includes(p.USER_DENIED))?{success:!1,error:"Transaction has been cancelled by user"}:(null==C||C({key:"Additional Data",value:{address:e,providerType:g,chainId:S,path:"Inside an make offer solana contract function - 581"}},{exception:i}),{success:!1,error:k(i)})}};export{A as checkApproval,T as makeOfferSolanaItem,S as unlockApproval};