var c=(s,a,e)=>new Promise((l,m)=>{var h=n=>{try{w(e.next(n))}catch(i){m(i)}},p=n=>{try{w(e.throw(n))}catch(i){m(i)}},w=n=>n.done?l(n.value):Promise.resolve(n.value).then(h,p);w((e=e.apply(s,a)).next())});import{useEffect as P,useRef as R,useState as q}from"react";import{fetchQuote as z,swapFromSolana as M}from"@mayanfinance/swap-sdk";import{useConnection as N,useWallet as O}from"@solana/wallet-adapter-react";import{useToast as _}from"@chakra-ui/react";import F from"bignumber.js";import Q from"axios";var u=s=>{switch(s){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}},Y=({amountIn:s,fromCurrency:a,toCurrency:e})=>{var A;let[l,m]=q({}),[h,p]=q(!1),[w,n]=q(0),{publicKey:i,signTransaction:k}=O(),{connection:T}=N(),S=_(),B=R(null),E=((A=u(a))==null?void 0:A.quoteAmount)||1;P(()=>{let o=()=>c(void 0,null,function*(){try{p(!0);let t=yield x(E);m(t)}catch(t){S({title:"Failed to fetch price",description:t.reason||t.message,status:"error",duration:1e3})}finally{p(!1)}});return a&&e&&a!==e&&(o(),B.current=setInterval(o,3e4)),()=>{clearInterval(B.current)}},[a,e]);let x=o=>c(void 0,null,function*(){return yield z({amount:o,fromToken:u(a).token,toToken:u(e).token,fromChain:u(a).chain,toChain:u(e).chain,slippage:3})}),d=(o,t,f="finalized")=>c(void 0,null,function*(){try{let r=yield Q.get(`https://explorer-api.mayan.finance/v1/swap/trx/${o}`);return r.data.status==="REDEEMED_ON_EVM"||f==="processed"?r.data:(yield new Promise(g=>setTimeout(g,1e4)),d(o,0))}catch(r){if(console.log(r),t>0)return yield new Promise(g=>setTimeout(g,1e4)),d(o,t-1);throw new Error("Failed to swap")}}),v=o=>c(void 0,null,function*(){try{let t=yield x(o),f=yield M(t,i.toBase58(),i.toBase58(),1e5,k,T);return yield d(f,100,"processed"),f}catch(t){throw console.log(t),new Error(t)}}),D=a!==e&&l.expectedAmountOut?new F(s).multipliedBy(E).dividedBy(l.minAmountOut).toFixed(4):new F(s).toFixed(2);return{initSwap:v,verifySwapComplete:d,requiredAmount:D,isFetchingQuote:h}},W=Y;export{W as default};
