UNPKG

@mojito-inc/secondary-market

Version:

Mojito secondary market is the platform to purchase NFT.

2 lines (1 loc) 10.1 kB
"use strict";var e=require("ethers"),i=require("@solana/web3.js"),n=require("@solana/spl-token"),r=require("bn.js"),t=require("bs58"),s=require("buffer"),a=require("../constant/contractABI/ListitemERC115.js"),o=require("../constant/contractABI/ListitemERC721.js"),l=require("../constant/index.js"),u=require("./getEtherScanURL.utils.js"),c=require("./getVinTokenAddress.utils.js"),d=require("./getOrInitializeAccount.utils.js"),v=require("./customErrorMessage.utils.js"),g=require("./getSolanaBalance.utils.js"),m=require("./fetchSolanaPriorityFee.utils.js");exports.approveToken=async({marketPlaceContractAddress:i,tokenContractAddress:n,contractName:r,provider:t})=>{var s,u;try{const s=t.getSigner();if(r.includes("ERC721")){const r=new e.ethers.Contract(n,o.default.abi,s);return{status:!0,message:await r.setApprovalForAll(i,!0)}}const l=new e.ethers.Contract(n,a.default.abi,s);return{status:!0,message:await l.setApprovalForAll(i,!0)}}catch(e){return(null===(s=e.message)||void 0===s?void 0:s.includes(l.ErrorsType.USER_REJECTED))||(null===(u=e.message)||void 0===u?void 0:u.includes(l.ErrorsType.USER_DENIED))?{status:!1,message:"Transaction has been cancelled by user"}:{status:!1,message:null==e?void 0:e.message}}},exports.checkApproval=async({marketPlaceContractAddress:i,tokenContractAddress:n,contractName:r,provider:t})=>{try{const s=t.getSigner(),l=await s.getAddress();if(r.includes("ERC721")){const r=new e.ethers.Contract(n,o.default.abi,s);return await r.isApprovedForAll(l,i)}const u=new e.ethers.Contract(n,a.default.abi,s);return await u.isApprovedForAll(l,i)}catch(e){return!1}},exports.listItemSolana=async(e,a,o,b,p,f,y,h,w,E=.00228288)=>{var S,A,k,P,T,L,C,R,I,B,D,_,U,K,O,q,W,F,j,M,x,N,V,H,J,z,Y,G,Q,X,Z,$,ee,ie,ne,re,te,se,ae,oe,le,ue,ce,de;try{if(!b)return{success:!1,error:"Your wallet session has been closed. Please reconnect your wallet to continue."};if(e){const a=new i.PublicKey(e),ue=u.getSolanaEnvByChainId(f),ce=new i.Connection(h||i.clusterApiUrl(ue),{confirmTransactionInitialTimeout:3e5,commitment:"confirmed"}),de=await g.getSolanaBalance(e,ue,h),ve=c.getVinTokenAddress(ue,h),ge=new i.PublicKey(ve),me=await n.getAssociatedTokenAddress(ge,a);p("Checking token approval.");const be=await d.getOrInitializeAccount(ce,me,a,ge,a,p,b,o,de.native,h);if(!be.isAvailable)throw new Error("Insufficient SOL for initiating the transaction.");const pe=new i.PublicKey(null!==(S=null==y?void 0:y.bubblegumProgram)&&void 0!==S?S:""),fe=new i.PublicKey(null!==(A=null==y?void 0:y.compressionProgram)&&void 0!==A?A:""),ye=new i.PublicKey(null!==(k=null==y?void 0:y.newLeafDelegate)&&void 0!==k?k:""),he=new i.PublicKey(null!==(P=null==y?void 0:y.mintAccount)&&void 0!==P?P:""),we=new i.PublicKey(null!==(T=null==y?void 0:y.newLeafDelegate)&&void 0!==T?T:""),Ee=new r(null===(L=null==y?void 0:y.bubblegumListArg)||void 0===L?void 0:L.nonce).toArray("le",8),Se=[s.Buffer.from("marketplace")],[Ae,ke]=i.PublicKey.findProgramAddressSync(Se,we),[Pe,Te]=i.PublicKey.findProgramAddressSync([s.Buffer.from("listing_account"),a.toBuffer(),he.toBuffer(),Ee],we),Le=t.decode(null!==(R=null===(C=null==y?void 0:y.bubblegumListArg)||void 0===C?void 0:C.root)&&void 0!==R?R:""),Ce=t.decode(null!==(B=null===(I=null==y?void 0:y.bubblegumListArg)||void 0===I?void 0:I.dataHash)&&void 0!==B?B:""),Re=t.decode(null!==(_=null===(D=null==y?void 0:y.bubblegumListArg)||void 0===D?void 0:D.creatorHash)&&void 0!==_?_:"");if(32!==Ce.length||32!==Re.length)throw new Error("Hash length is not 32 bytes");const Ie=new i.PublicKey(null!==(U=null==y?void 0:y.systemProgram)&&void 0!==U?U:""),Be=new i.PublicKey(null!==(K=null==y?void 0:y.tokenProgram)&&void 0!==K?K:""),De=new i.PublicKey(null!==(O=null==y?void 0:y.rent)&&void 0!==O?O:""),_e=Array.from(Array(10).keys()).map((e=>{var n,r,t;return{pubkey:new i.PublicKey(null!==(t=null===(r=null===(n=null==y?void 0:y.bubblegumListArg)||void 0===n?void 0:n.proof)||void 0===r?void 0:r[e])&&void 0!==t?t:""),isSigner:!1,isWritable:!1}}));let Ue;Ue=(null==y?void 0:y.previousLeafDelegate)?new i.PublicKey(null==y?void 0:y.previousLeafDelegate):a;const Ke=new i.TransactionInstruction({programId:ye,keys:[{pubkey:a,isSigner:!0,isWritable:!0},{pubkey:he,isSigner:!1,isWritable:!0},{pubkey:Pe,isSigner:!1,isWritable:!0},{pubkey:Ie,isSigner:!1,isWritable:!0},{pubkey:Be,isSigner:!1,isWritable:!0},{pubkey:De,isSigner:!1,isWritable:!0},{pubkey:pe,isSigner:!1,isWritable:!0},{pubkey:fe,isSigner:!1,isWritable:!0},{pubkey:new i.PublicKey(null!==(q=null==y?void 0:y.treeAuthority)&&void 0!==q?q:""),isSigner:!1,isWritable:!0},{pubkey:a,isSigner:!1,isWritable:!0},{pubkey:Ue,isSigner:!1,isWritable:!0},{pubkey:Ae,isSigner:!1,isWritable:!0},{pubkey:new i.PublicKey(null!==(W=null==y?void 0:y.logWrapper)&&void 0!==W?W:""),isSigner:!1,isWritable:!0},{pubkey:new i.PublicKey(null!==(F=null==y?void 0:y.merkleTree)&&void 0!==F?F:""),isSigner:!1,isWritable:!0},..._e],data:s.Buffer.concat([s.Buffer.from(null!==(j=null==y?void 0:y.listDiscriminator)&&void 0!==j?j:[]),s.Buffer.from(Uint8Array.of(...new r(null!==(x=null===(M=null==y?void 0:y.bubblegumListArg)||void 0===M?void 0:M.price)&&void 0!==x?x:0).toArray("le",8),...Le,...Ce,...Re,...Ee,...new r(null!==(V=null===(N=null==y?void 0:y.bubblegumListArg)||void 0===N?void 0:N.index)&&void 0!==V?V:0).toArray("le",4),+(null!==(J=null===(H=null==y?void 0:y.bubblegumListArg)||void 0===H?void 0:H.isUpdate)&&void 0!==J&&J)))])});try{const n={nonNative:de.nonNative,native:de.native-be.usedBalance/i.LAMPORTS_PER_SOL};if("backpack"===(null==o?void 0:o.toLowerCase())||"phantom"===(null==o?void 0:o.toLowerCase())){const r=new i.Transaction({feePayer:a}).add(Ke),t=await ce.getLatestBlockhash("confirmed");r.recentBlockhash=t.blockhash,r.lastValidBlockHeight=t.lastValidBlockHeight,r.add(i.ComputeBudgetProgram.setComputeUnitLimit({units:14e5}));const s=await m.fetchSolanaPriorityFee(h,r.serialize({verifySignatures:!1})),u=i.ComputeBudgetProgram.setComputeUnitPrice({microLamports:s});r.add(u);const c=await ce.getFeeForMessage(r.compileMessage(),"confirmed"),d=n.native*i.LAMPORTS_PER_SOL,g=(null!==(z=null==c?void 0:c.value)&&void 0!==z?z:0)+E*i.LAMPORTS_PER_SOL;if(d<=0||d<g)return{success:!1,error:"Insufficient SOL for initiating the transaction."};p("Initiating item listing transaction.");try{const e=await(null===(G=null===(Y=null==b?void 0:b.adapter)||void 0===Y?void 0:Y.sendTransaction)||void 0===G?void 0:G.call(Y,r,ce,{skipPreflight:!0,preflightCommitment:"confirmed"}));await ce.confirmTransaction(Object.assign({signature:e},t),"confirmed");const i=await ce.getSignatureStatus(e);return null===(null===(Q=null==i?void 0:i.value)||void 0===Q?void 0:Q.err)?{success:!0,response:{hash:e,listingReceipt:Pe.toString()}}:{success:!1,error:"Transaction Failed"}}catch(i){return(null===(Z=null===(X=null==i?void 0:i.message)||void 0===X?void 0:X.includes)||void 0===Z?void 0:Z.call(X,l.ErrorsType.USER_REJECTED))||(null===($=null==i?void 0:i.includes)||void 0===$?void 0:$.call(i,l.ErrorsType.USER_REJECTED))||(null===(ee=null==i?void 0:i.includes)||void 0===ee?void 0:ee.call(i,l.ErrorsType.USER_DENIED))||(null===(ne=null===(ie=null==i?void 0:i.message)||void 0===ie?void 0:ie.includes)||void 0===ne?void 0:ne.call(ie,l.ErrorsType.USER_DENIED))?{success:!1,error:"Transaction has been cancelled by user"}:(null==w||w({key:"Additional Data",value:{address:e,balance:de,providerType:o,chainId:f,path:"Inside an list item solana contract function - 322"}},{exception:i}),{success:!1,error:v.customErrorMessage(i)})}}if("magic"===(null==o?void 0:o.toLowerCase())){const e=await ce.getLatestBlockhash("confirmed"),r=new i.Transaction({feePayer:a}).add(Ke);r.recentBlockhash=e.blockhash,r.lastValidBlockHeight=e.lastValidBlockHeight,r.add(i.ComputeBudgetProgram.setComputeUnitLimit({units:14e5}));const t=await m.fetchSolanaPriorityFee(h,r.serialize({verifySignatures:!1})),o=i.ComputeBudgetProgram.setComputeUnitPrice({microLamports:t});r.add(o);const l=await ce.getFeeForMessage(r.compileMessage(),"confirmed"),u=n.native*i.LAMPORTS_PER_SOL,c=(null!==(re=null==l?void 0:l.value)&&void 0!==re?re:0)+E*i.LAMPORTS_PER_SOL;if(u<=0||u<c)return{success:!1,error:"Insufficient SOL for initiating the transaction."};p("Initiating item listing transaction.");const d=await(null===(te=null==b?void 0:b.solana)||void 0===te?void 0:te.signTransaction(r,{requireAllSignatures:!1,verifySignatures:!0})),v=await ce.sendRawTransaction(s.Buffer.from(d.rawTransaction,"base64"),{skipPreflight:!0,preflightCommitment:"confirmed"});await ce.confirmTransaction(Object.assign({signature:v},e),"confirmed");const g=await ce.getSignatureStatus(v);return null===(null===(se=null==g?void 0:g.value)||void 0===se?void 0:se.err)?{success:!0,response:{hash:v,listingReceipt:Pe.toString()}}:{success:!1,error:"Transaction Failed"}}return null}catch(i){return(null===(ae=i.message)||void 0===ae?void 0:ae.includes(l.ErrorsType.USER_REJECTED))||(null===(oe=i.message)||void 0===oe?void 0:oe.includes(l.ErrorsType.USER_DENIED))?{success:!1,error:"Transaction has been cancelled by user"}:(null===(le=null==i?void 0:i.message)||void 0===le?void 0:le.includes("Assertion failed"))?{success:!1,error:"A maximum of 9,000,000 VIN can be processed per transaction."}:(null==w||w({key:"Additional Data",value:{address:e,balance:de,providerType:o,chainId:f,path:"Inside an list item solana contract function - 411"}},{exception:i}),{success:!1,error:v.customErrorMessage(i)})}}return null}catch(i){return(null===(ue=i.message)||void 0===ue?void 0:ue.includes(l.ErrorsType.USER_REJECTED))||(null===(ce=i.message)||void 0===ce?void 0:ce.includes(l.ErrorsType.USER_DENIED))?{success:!1,error:"Transaction has been cancelled by user"}:(null===(de=null==i?void 0:i.message)||void 0===de?void 0:de.includes("Assertion failed"))?{success:!1,error:"A maximum of 9,000,000 VIN can be processed per transaction."}:(null==w||w({key:"Additional Data",value:{address:e,providerType:o,chainId:f,path:"Inside an list item solana contract function - 445"}},{exception:i}),{success:!1,error:v.customErrorMessage(i)})}},exports.waitForApproval=async e=>!!e&&(await e.wait(),!!(null==e?void 0:e.hash));