const e=require(`../../_virtual/_rolldown/runtime.cjs`),t=require(`../../types/ethereum-network.types.cjs`),n=require(`../../constants/chains.constants.cjs`),r=require(`../../constants/protocol.constants.cjs`),i=require(`../../types/cache.types.cjs`),a=require(`../../functions/utils/caseInsensitive.utils.cjs`),o=require(`../../API/API.cjs`),s=require(`../../error-handling/logger.cjs`),c=require(`../../functions/utils/string.utils.cjs`),l=require(`./token-visibility-db.cjs`),u=require(`../../functions/web3/getContractMetadata.cjs`);let d=require(`axios`);d=e.__toESM(d);let f=require(`idb-keyval`);var p=class{API_BASE=`https://testnet.arcscan.app/api`;fetchedTokens={};async getPublicTokens(e){let t=c.getStateKey(e,n.chainIds.arcTestnet);return(await(0,f.get)(i.StorageKeys.ARC_PUBLIC_TOKENS))?.[t]||[]}getFetchedTokens(e){let t=c.getStateKey(e,n.chainIds.arcTestnet);return this.fetchedTokens[t]||[]}async fetchAndUpdatePublicTokens(e){let t=c.getStateKey(e,n.chainIds.arcTestnet),s=await this.getERC20TokenAddresses(e),u=await this.getNFTTokens(e),d=u.map(e=>e.contractAddress),p=[...s,...d];if(!p.length)return;let m=(await o.API.tokensInfoCall(n.chainIds.arcTestnet,p)).map((e,t)=>{if(e)return e;let i=p[t],o=u.find(e=>a.caseInsensitiveEqual(e.contractAddress,i));return o&&o.tokenIds&&o.tokenIds.length>0?{chainId:n.chainIds.arcTestnet,name:`Unknown NFT`,symbol:`NFT`,erc20TokenAddress:i,decimals:0,nftTokenType:r.TokenType.ERC721,tokenIds:o.tokenIds}:null}).filter(e=>!!e),h=await l.tokenVisibilityStorage.hideSpamsAndFetchHiddenTokensAddresses(n.chainIds.arcTestnet,m),g=m.filter(e=>!a.lowerCaseIncludes(h,e.erc20TokenAddress)).map(e=>({...e,tokenIds:u.find(t=>a.caseInsensitiveEqual(t.contractAddress,e.erc20TokenAddress))?.tokenIds}));await(0,f.update)(i.StorageKeys.ARC_PUBLIC_TOKENS,(e={})=>{let n={...e,[t]:g};return this.fetchedTokens=n,n})}async getERC20TokenAddresses(e){let r=[];try{let i=(await d.default.post(`${this.API_BASE}?module=account&action=tokentx&address=${e}`)).data;if(i.status===`1`&&i.result){let a=[...new Set(i.result.map(({contractAddress:e})=>e))];for(let i of a)try{let a=u.getContractWithFetcherByChainId(n.chainIds.arcTestnet,t.ContractType.ERC20Contract,i),o=BigInt(await a.balanceOf(e));o>0n&&r.push({contractAddress:i,tokenBalance:o.toString()})}catch(e){s.Logger.error(`Error fetching ARC testnet token balance:`,e)}}}catch(e){s.Logger.error(`Error fetching ARC testnet token transactions:`,e)}return r.filter(({tokenBalance:e})=>Number(e)!==0).map(({contractAddress:e})=>e)}async getNFTTokens(e){let t=`${this.API_BASE}/v2/addresses/${e}/nft?type=ERC-721,ERC-1155`,n=[],r;do{let e=r?`&${new URLSearchParams(r)}`:``,{data:i}=await d.default.get(`${t}${e}`),{items:a=[],next_page_params:o}=i;n.push(...a),r=o?{token_contract_address_hash:o.token_contract_address_hash,token_id:o.token_id,token_type:o.token_type}:void 0}while(r);let i=n.reduce((e,t)=>{let n=t.token.address_hash.toLowerCase(),r=t.id;return e.has(n)||e.set(n,[]),e.get(n)?.push(r),e},new Map);return Array.from(i.entries()).map(([e,t])=>({contractAddress:e,tokenIds:t}))}},m=new p;exports.ArcPublicTokensDB=p,exports.arcPublicTokensDB=m;