UNPKG

@mojito-inc/secondary-market

Version:

Mojito secondary market is the platform to purchase NFT.

2 lines (1 loc) 10.8 kB
"use strict";var e=require("ethers"),i=require("moment"),n=require("@solana/web3.js"),r=require("bs58"),t=require("bn.js"),o=require("@solana/spl-token"),a=require("../constant/contractABI/BalanceWMATIC.js"),l=require("../constant/contractABI/BalanceWETH.js"),s=require("../constant/contractABI/BuyOrSell.js"),u=require("../constant/index.js"),c=require("./getEtherScanURL.utils.js"),d=require("./getOrInitializeAccount.utils.js"),v=require("./getVinTokenAddress.utils.js"),g=require("./customErrorMessage.utils.js"),b=require("./getSolanaBalance.utils.js"),f=require("./fetchSolanaPriorityFee.utils.js");exports.acceptOfferApproval=async({tokenId:n,price:r,buyerAddress:t,quantity:o,tax:a,contractAddress:l,marketPlaceContractAddress:u,random:c,expiryDate:d,proofOfApproval:v,networkContractAddress:g,provider:b})=>{try{const f=r*a/100,y=b.getSigner(),p=await y.getAddress(),m=new e.ethers.Contract(u,s.default.abi,y),h=await m.sell([c,n,l,o,p,e.utils.parseEther(String(r)),g,e.utils.parseEther(String(f)),e.ethers.constants.AddressZero,0,0,t],v,i(d).unix(),g);return await b.waitForTransaction(h.hash,1),h.hash}catch(e){return""}},exports.acceptOfferSolana=async(e,i,a,l,s,y,p,m,h,w=.00228288)=>{var S,A,k,P,E,T,B,I,K,L,O,C,W,R,_,q,D,M,V,j,U,x;try{if(!a)return{success:!1,error:"Your wallet session has been closed. Please reconnect your wallet to continue."};const i=c.getSolanaEnvByChainId(y),U=await b.getSolanaBalance(e,i,m),{err:x,instruction:F,connectedPublicKey:N,connection:H,usedBalance:z,buyerVinTokenAccount:J,pda:Y}=await(async(e,i,o,a,l,s,u,g)=>{var b,f,y,p,m,h,w,S,A,k,P,E,T,B,I,K,L,O,C,W,R,_,q,D,M,V,j,U,x,F;try{const N=new n.PublicKey(e),H=c.getSolanaEnvByChainId(l),z=new n.Connection(g||n.clusterApiUrl(H),{confirmTransactionInitialTimeout:3e5,commitment:"confirmed"}),J=new n.PublicKey(null!==(b=null==u?void 0:u.commissionVinTokenAccount)&&void 0!==b?b:""),Y=new n.PublicKey(null!==(f=null==u?void 0:u.platformVinTokenAccount)&&void 0!==f?f:""),Z=new n.PublicKey(null!==(y=null==u?void 0:u.initializeAccount)&&void 0!==y?y:""),G=new n.PublicKey(null!==(p=null==u?void 0:u.mintAccount)&&void 0!==p?p:""),Q=new n.PublicKey(null!==(m=null==u?void 0:u.sellerVinTokenAccount)&&void 0!==m?m:""),X=v.getVinTokenAddress(H,g),$=new n.PublicKey(X),ee=await d.getOrInitializeAccount(z,Q,N,$,N,i,o,a,s,g);if(!ee.isAvailable)throw new Error("Insufficient SOL for initiating the transaction.");const ie=new n.PublicKey(null!==(h=null==u?void 0:u.offer)&&void 0!==h?h:""),ne=new n.PublicKey(null!==(w=null==u?void 0:u.offerEscrow)&&void 0!==w?w:""),re=[Buffer.from("marketplace")],[te,oe]=n.PublicKey.findProgramAddressSync(re,ne),ae=new n.PublicKey(null!==(S=null==u?void 0:u.offerEscrow)&&void 0!==S?S:""),le=new n.PublicKey(null!==(A=null==u?void 0:u.tokenProgram)&&void 0!==A?A:""),se=new n.PublicKey(null!==(k=null==u?void 0:u.systemProgram)&&void 0!==k?k:""),ue=new n.PublicKey(null!==(P=null==u?void 0:u.buyer)&&void 0!==P?P:""),ce=r.decode(null!==(T=null===(E=null==u?void 0:u.bubblegumArg)||void 0===E?void 0:E.root)&&void 0!==T?T:""),de=Array.from(Array(10).keys()).map((e=>{var i,r,t;return{pubkey:new n.PublicKey(null!==(t=null===(r=null===(i=null==u?void 0:u.bubblegumArg)||void 0===i?void 0:i.proof)||void 0===r?void 0:r[e])&&void 0!==t?t:""),isSigner:!1,isWritable:!1}})),ve=r.decode(null!==(I=null===(B=null==u?void 0:u.bubblegumArg)||void 0===B?void 0:B.dataHash)&&void 0!==I?I:""),ge=r.decode(null!==(L=null===(K=null==u?void 0:u.bubblegumArg)||void 0===K?void 0:K.creatorHash)&&void 0!==L?L:"");if(32!==ve.length||32!==ge.length)throw new Error("Hash length is not 32 bytes");const be=new n.PublicKey(null!==(O=null==u?void 0:u.buyerVinTokenAccount)&&void 0!==O?O:""),fe=new n.PublicKey(null!==(C=null==u?void 0:u.bubblegumProgram)&&void 0!==C?C:""),ye=new n.PublicKey(null!==(W=null==u?void 0:u.compressionProgram)&&void 0!==W?W:""),pe=new n.PublicKey(null!==(R=null==u?void 0:u.treeAuthority)&&void 0!==R?R:"");let me;me=(null==u?void 0:u.leafDelegate)?new n.PublicKey(null!==(_=null==u?void 0:u.leafDelegate)&&void 0!==_?_:""):N;const he=new n.PublicKey(null!==(q=null==u?void 0:u.buyer)&&void 0!==q?q:""),we=new n.PublicKey(null!==(D=null==u?void 0:u.logWrapper)&&void 0!==D?D:""),Se=new n.PublicKey(null!==(M=null==u?void 0:u.merkleTree)&&void 0!==M?M:"");return{instruction:new n.TransactionInstruction({programId:ae,keys:[{pubkey:N,isSigner:!0,isWritable:!0},{pubkey:ue,isSigner:!1,isWritable:!0},{pubkey:ie,isSigner:!1,isWritable:!0},{pubkey:G,isSigner:!1,isWritable:!0},{pubkey:be,isSigner:!1,isWritable:!0},{pubkey:Q,isSigner:!1,isWritable:!0},{pubkey:J,isSigner:!1,isWritable:!0},{pubkey:Y,isSigner:!1,isWritable:!0},{pubkey:Z,isSigner:!1,isWritable:!0},{pubkey:te,isSigner:!1,isWritable:!0},{pubkey:le,isSigner:!1,isWritable:!0},{pubkey:se,isSigner:!1,isWritable:!0},{pubkey:fe,isSigner:!1,isWritable:!0},{pubkey:ye,isSigner:!1,isWritable:!0},{pubkey:pe,isSigner:!1,isWritable:!0},{pubkey:N,isSigner:!1,isWritable:!0},{pubkey:me,isSigner:!1,isWritable:!0},{pubkey:he,isSigner:!1,isWritable:!0},{pubkey:we,isSigner:!1,isWritable:!0},{pubkey:Se,isSigner:!1,isWritable:!0},...de],data:Buffer.concat([Buffer.from(null!==(V=null==u?void 0:u.acceptOfferDiscriminator)&&void 0!==V?V:[]),Buffer.from(Uint8Array.of(...ce,...ve,...ge,...new t(null!==(U=null===(j=null==u?void 0:u.bubblegumArg)||void 0===j?void 0:j.nonce)&&void 0!==U?U:0).toArray("le",8),...new t(null!==(F=null===(x=null==u?void 0:u.bubblegumArg)||void 0===x?void 0:x.index)&&void 0!==F?F:0).toArray("le",8)))])}),connectedPublicKey:N,connection:z,err:null,usedBalance:ee.usedBalance,buyerVinTokenAccount:be,pda:te}}catch(e){return{err:null==e?void 0:e.message}}})(e,s,a,l,y,U.native,p,m);if(!x&&F){const r={nonNative:U.nonNative,native:U.native-z/n.LAMPORTS_PER_SOL};s("Verifying token balance from the offer account");const t=await o.getAccount(H,J),c=(null!==(A=null===(S=t.delegate)||void 0===S?void 0:S.toString())&&void 0!==A?A:"")===Y.toString();if(!t.delegate||!c||t.delegatedAmount<(null!==(P=null===(k=null==p?void 0:p.bubblegumArg)||void 0===k?void 0:k.amount)&&void 0!==P?P:0))return{success:!1,error:"Not enough VIN tokens or approval in the offer account. Try again later or review other offers."};const d=await b.getSolanaBalance(null!==(E=null==p?void 0:p.buyer)&&void 0!==E?E:"",i,m);if(d.nonNative*n.LAMPORTS_PER_SOL<(null!==(B=null===(T=null==p?void 0:p.bubblegumArg)||void 0===T?void 0:T.amount)&&void 0!==B?B:0))return{success:!1,error:"Not enough VIN tokens or approval in the offer account. Try again later or review other offers."};if("phantom"===l.toLowerCase()||"backpack"===l.toLowerCase()){const i=new n.Transaction({feePayer:N}).add(F),t=await H.getLatestBlockhash("confirmed");i.recentBlockhash=t.blockhash,i.lastValidBlockHeight=t.lastValidBlockHeight,i.add(n.ComputeBudgetProgram.setComputeUnitLimit({units:14e5}));const o=await f.fetchSolanaPriorityFee(m,i.serialize({verifySignatures:!1})),c=n.ComputeBudgetProgram.setComputeUnitPrice({microLamports:o});i.add(c);const d=await H.getFeeForMessage(i.compileMessage(),"confirmed"),v=r.native*n.LAMPORTS_PER_SOL,b=(null!==(I=null==d?void 0:d.value)&&void 0!==I?I:0)+w*n.LAMPORTS_PER_SOL;if(v<=0||v<b)return{success:!1,error:"Insufficient SOL for initiating the transaction."};try{s("Initiating accept offer transaction.");const e=await(null===(L=null===(K=null==a?void 0:a.adapter)||void 0===K?void 0:K.sendTransaction)||void 0===L?void 0:L.call(K,i,H,{skipPreflight:!0,preflightCommitment:"confirmed"}));await H.confirmTransaction(Object.assign({signature:e},t),"confirmed");const n=await H.getSignatureStatus(e);return null===(null===(O=null==n?void 0:n.value)||void 0===O?void 0:O.err)?{success:!0,response:{hash:e}}:{success:!1,error:"Transaction Failed"}}catch(i){return(null===(W=null===(C=null==i?void 0:i.message)||void 0===C?void 0:C.includes)||void 0===W?void 0:W.call(C,u.ErrorsType.USER_REJECTED))||(null===(R=null==i?void 0:i.includes)||void 0===R?void 0:R.call(i,u.ErrorsType.USER_REJECTED))||(null===(_=null==i?void 0:i.includes)||void 0===_?void 0:_.call(i,u.ErrorsType.USER_DENIED))||(null===(D=null===(q=null==i?void 0:i.message)||void 0===q?void 0:q.includes)||void 0===D?void 0:D.call(q,u.ErrorsType.USER_DENIED))?{success:!1,error:"Transaction has been cancelled by user"}:(null==h||h({key:"Additional Data",value:{address:e,balance:U,newBalance:r,providerType:l,chainId:y,path:"Inside an Accept offer solana contract function - 351"}},{exception:i,hint:{data:{path:"Inside an Accept offer solana contract function"}}}),{success:!1,error:g.customErrorMessage(i)})}}if("magic"===l.toLowerCase()){const e=new n.Transaction({feePayer:N}).add(F),i=await H.getLatestBlockhash("confirmed");e.recentBlockhash=i.blockhash,e.lastValidBlockHeight=i.lastValidBlockHeight,e.add(n.ComputeBudgetProgram.setComputeUnitLimit({units:14e5}));const t=await f.fetchSolanaPriorityFee(m,e.serialize({verifySignatures:!1})),o=n.ComputeBudgetProgram.setComputeUnitPrice({microLamports:t});e.add(o);const l=await H.getFeeForMessage(e.compileMessage(),"confirmed"),u=r.native*n.LAMPORTS_PER_SOL,c=(null!==(M=null==l?void 0:l.value)&&void 0!==M?M:0)+w*n.LAMPORTS_PER_SOL;if(u<=0||u<c)return{success:!1,error:"Insufficient SOL for initiating the transaction."};s("Initiating accept offer transaction.");const d=await(null===(V=null==a?void 0:a.solana)||void 0===V?void 0:V.signTransaction(e,{requireAllSignatures:!1,verifySignatures:!0})),v=await H.sendRawTransaction(Buffer.from(d.rawTransaction,"base64"),{skipPreflight:!0,preflightCommitment:"confirmed"});await H.confirmTransaction(Object.assign({signature:v},i),"confirmed");const g=await H.getSignatureStatus(v);return null===(null===(j=null==g?void 0:g.value)||void 0===j?void 0:j.err)?{success:!0,response:{hash:v}}:{success:!1,error:"Transaction Failed"}}}return x?(null==h||h({key:"Additional Data",value:{address:e,balance:U,providerType:l,chainId:y,path:"Inside an Accept offer solana contract function - 432"}},{exception:x,hint:{data:{path:"Inside an Accept offer solana contract function - 438"}}}),{success:!1,error:g.customErrorMessage(x)}):null}catch(n){return(null===(U=n.message)||void 0===U?void 0:U.includes(u.ErrorsType.USER_REJECTED))||(null===(x=n.message)||void 0===x?void 0:x.includes(u.ErrorsType.USER_DENIED))?{success:!1,error:"Transaction has been cancelled by user"}:(null==h||h({key:"Additional Data",value:{address:e,_balance:i,providerType:l,chainId:y,path:"Inside an Accept offer solana contract function - 465"}},{exception:n,hint:{data:{path:"Inside an Accept offer solana contract function - 465"}}}),{success:!1,error:g.customErrorMessage(n)})}},exports.buyerBalance=async function(i,n,r,t){try{const o=r.getSigner(),s=80001===t||137===t?a.default.abi:l.default.abi,u=new e.ethers.Contract(n,s,o);return(await u.balanceOf(i)/1e18).toFixed(6)}catch(e){return"0"}};