var M=Object.create;var p=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var _=Object.getPrototypeOf,Q=Object.prototype.hasOwnProperty;var Y=(t,e)=>{for(var o in e)p(t,o,{get:e[o],enumerable:!0})},A=(t,e,o,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of O(e))!Q.call(t,i)&&i!==o&&p(t,i,{get:()=>e[i],enumerable:!(r=N(e,i))||r.enumerable});return t};var F=(t,e,o)=>(o=t!=null?M(_(t)):{},A(e||!t||!t.__esModule?p(o,"default",{value:t,enumerable:!0}):o,t)),K=t=>A(p({},"__esModule",{value:!0}),t);var j={};Y(j,{default:()=>b});module.exports=K(j);var s=require("react"),w=require("@mayanfinance/swap-sdk"),d=require("@solana/wallet-adapter-react"),k=require("@chakra-ui/react"),h=F(require("bignumber.js"),1),T=F(require("axios"),1),u=t=>{switch(t){case"BONK":return{token:"DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263",chain:"solana",quoteAmount:1e6};case"SOL":return{token:"0x0000000000000000000000000000000000000000",chain:"solana",quoteAmount:1};case"USDT":return{token:"Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB",chain:"solana",quoteAmount:1};case"ETH":return{token:"0x0000000000000000000000000000000000000000",chain:"ethereum",quoteAmount:1};default:return null}},L=({amountIn:t,fromCurrency:e,toCurrency:o})=>{let[r,i]=(0,s.useState)({}),[S,g]=(0,s.useState)(!1),[y,H]=(0,s.useState)(0),{publicKey:q,signTransaction:v}=(0,d.useWallet)(),{connection:D}=(0,d.useConnection)(),P=(0,k.useToast)(),B=(0,s.useRef)(null),E=u(e)?.quoteAmount||1;(0,s.useEffect)(()=>{let n=async()=>{try{g(!0);let a=await x(E);i(a)}catch(a){P({title:"Failed to fetch price",description:a.reason||a.message,status:"error",duration:1e3})}finally{g(!1)}};return e&&o&&e!==o&&(n(),B.current=setInterval(n,3e4)),()=>{clearInterval(B.current)}},[e,o]);let x=async n=>await(0,w.fetchQuote)({amount:n,fromToken:u(e).token,toToken:u(o).token,fromChain:u(e).chain,toChain:u(o).chain,slippage:3}),l=async(n,a,m="finalized")=>{try{let c=await T.default.get(`https://explorer-api.mayan.finance/v1/swap/trx/${n}`);return c.data.status==="REDEEMED_ON_EVM"||m==="processed"?c.data:(await new Promise(f=>setTimeout(f,1e4)),l(n,0))}catch(c){if(console.log(c),a>0)return await new Promise(f=>setTimeout(f,1e4)),l(n,a-1);throw new Error("Failed to swap")}},R=async n=>{try{let a=await x(n),m=await(0,w.swapFromSolana)(a,q.toBase58(),q.toBase58(),1e5,v,D);return await l(m,100,"processed"),m}catch(a){throw console.log(a),new Error(a)}},z=e!==o&&r.expectedAmountOut?new h.default(t).multipliedBy(E).dividedBy(r.minAmountOut).toFixed(4):new h.default(t).toFixed(2);return{initSwap:R,verifySwapComplete:l,requiredAmount:z,isFetchingQuote:S}},b=L;
