UNPKG

@mojito-inc/secondary-market

Version:

Mojito secondary market is the platform to purchase NFT.

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