var c=async(e,r)=>{let n=await fetch(e,{signal:r});return n.status===404?null:await n.json()};import{useQuery as a}from"@tanstack/react-query";var u=0,p=0,g=e=>`https://api.coingecko.com/api/v3/simple/price?ids=${e.join("%2C")}&vs_currencies=usd`,m=e=>`https://api.coingecko.com/api/v3/coins/${e}`,T=()=>"https://api.coingecko.com/api/v3/coins/list",y=e=>{let r={};return e.forEach(n=>{r[n]=null}),r},k=async(e,r)=>{let n=await Promise.all(e.map(async t=>{try{let o=await r.getTokenPrice(t),s=new Date(o.updated_at).getTime();return new Date().getTime()-s>36e5?{token:t,price:null}:{token:t,price:o.price}}catch(o){return console.log("tryGetAllPricesFromCache error",o),{token:t,price:null}}}));if(console.log("tryGetAllPricesFromCache prices ",n),!n.some(t=>t.price===null)){let t={};return n.forEach(o=>{t[o.token]=o.price}),t}return console.log("tryGetAllPricesFromCache returning null as some are empty "),null},f=(e,r)=>({staleTime:1e3*60*20,retry:!0,retryDelay:3e4,queryKey:["coinGeckoPrices",...e],queryFn:async({signal:n})=>{if(e.length===0)return y(e);if(console.log("coinGeckoPrices cache",r),r){let i=await k(e,r);if(console.log("makeCoinGeckoPricesQuery cachedTokenPrices",i),i)return i}let t=g(e),o=await c(t,n);if(u++,console.log("priceCallcount",u),!o)return y(e);let s={};return e.forEach(async i=>{let l=o[i];s[i]=l?l.usd:null,r&&await r.saveTokenPrice(i,s[i]||0)}),console.log("makeCoinGeckoPricesQuery cg ret",s),s}}),P=e=>({queryKey:["tokenInfo",e],queryFn:async({signal:r})=>{if(!e||e==="")return null;let n=m(e),t=await c(n,r);return p++,console.log("infoCallcount",p),t},staleTime:1e3*60*30}),C=()=>({queryKey:["tokenList"],queryFn:async({signal:e})=>{let r=T();return await c(r,e)},staleTime:1e3*60*30}),Q=(e,r={},n)=>a({...r,...f(e,n)}),L=e=>a(P(e)),b=()=>a(C());export{m as buildCoinGeckoInfoURL,g as buildCoinGeckoPricesURL,T as buildCoinGeckoTokenListURL,f as makeCoinGeckoPricesQuery,P as makeTokenInfoQuery,C as makeTokenListQuery,Q as useCoinGeckoPrices,L as useTokenInfo,b as useTokenList};
//# sourceMappingURL=index.mjs.map