"use strict";var l=Object.defineProperty;var d=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames;var w=Object.prototype.hasOwnProperty;var x=(e,r)=>{for(var n in r)l(e,n,{get:r[n],enumerable:!0})},h=(e,r,n,t)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of G(r))!w.call(e,o)&&o!==n&&l(e,o,{get:()=>r[o],enumerable:!(t=d(r,o))||t.enumerable});return e};var Q=e=>h(l({},"__esModule",{value:!0}),e);var I={};x(I,{buildCoinGeckoInfoURL:()=>T,buildCoinGeckoPricesURL:()=>m,buildCoinGeckoTokenListURL:()=>k,makeCoinGeckoPricesQuery:()=>f,makeTokenInfoQuery:()=>P,makeTokenListQuery:()=>C,useCoinGeckoPrices:()=>b,useTokenInfo:()=>D,useTokenList:()=>U});module.exports=Q(I);var c=async(e,r)=>{let n=await fetch(e,{signal:r});return n.status===404?null:await n.json()};var a=require("@tanstack/react-query"),p=0,y=0,m=e=>`https://api.coingecko.com/api/v3/simple/price?ids=${e.join("%2C")}&vs_currencies=usd`,T=e=>`https://api.coingecko.com/api/v3/coins/${e}`,k=()=>"https://api.coingecko.com/api/v3/coins/list",g=e=>{let r={};return e.forEach(n=>{r[n]=null}),r},L=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 g(e);if(console.log("coinGeckoPrices cache",r),r){let i=await L(e,r);if(console.log("makeCoinGeckoPricesQuery cachedTokenPrices",i),i)return i}let t=m(e),o=await c(t,n);if(p++,console.log("priceCallcount",p),!o)return g(e);let s={};return e.forEach(async i=>{let u=o[i];s[i]=u?u.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=T(e),t=await c(n,r);return y++,console.log("infoCallcount",y),t},staleTime:1e3*60*30}),C=()=>({queryKey:["tokenList"],queryFn:async({signal:e})=>{let r=k();return await c(r,e)},staleTime:1e3*60*30}),b=(e,r={},n)=>(0,a.useQuery)({...r,...f(e,n)}),D=e=>(0,a.useQuery)(P(e)),U=()=>(0,a.useQuery)(C());0&&(module.exports={buildCoinGeckoInfoURL,buildCoinGeckoPricesURL,buildCoinGeckoTokenListURL,makeCoinGeckoPricesQuery,makeTokenInfoQuery,makeTokenListQuery,useCoinGeckoPrices,useTokenInfo,useTokenList});
//# sourceMappingURL=index.js.map