From 8553acd829f04c90f8fcfee08b109139dace4b3c Mon Sep 17 00:00:00 2001 From: omar Date: Sat, 6 Jan 2024 21:27:28 -0800 Subject: [PATCH] fix: linting --- .vscode/extensions.json | 5 +--- scripts/sanitize-db.ts | 12 ++++---- src/constants.ts | 62 +++++++++++++++++++-------------------- src/database/index.ts | 12 ++++++-- src/database/seed.ts | 33 ++++++++++----------- src/erc20-methods.ts | 34 ++++++++++----------- src/index.ts | 38 ++++++++++++------------ src/landing.tsx | 2 +- src/price/ankr.ts | 12 ++++---- src/types.ts | 2 -- src/utilities/fetchers.ts | 12 ++++---- src/utilities/index.ts | 2 +- 12 files changed, 114 insertions(+), 112 deletions(-) diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 162f7a4..b166f50 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -1,6 +1,3 @@ { - "recommendations": [ - "biomejs.biome", - "github.vscode-github-actions" - ] + "recommendations": ["biomejs.biome", "github.vscode-github-actions"] } diff --git a/scripts/sanitize-db.ts b/scripts/sanitize-db.ts index ff4d572..44db442 100644 --- a/scripts/sanitize-db.ts +++ b/scripts/sanitize-db.ts @@ -26,7 +26,7 @@ export default { const results = [] const errors = [] for (const chunk of chunks) { - const addresses = chunk.map((row) => row.address) + const addresses = chunk.map(row => row.address) try { const hasBalanceOfs = await hasBalanceOfBatch(pathParam as Chain, addresses, env) @@ -39,7 +39,7 @@ export default { }) } catch (error) { console.error(error) - errors.push(...chunk.map((_) => _.address)) + errors.push(...chunk.map(_ => _.address)) } finally { sleep(5) } @@ -53,7 +53,7 @@ export default { const deleteRows = await removeRows({ database: env.DB, chain: pathParam, - addresses: chunk, + addresses: chunk }) console.log(JSON.stringify(deleteRows, undefined, 2)) } catch (error) { @@ -64,11 +64,11 @@ export default { return new Response( JSON.stringify({ errors, - results, + results }), { - headers: { 'content-type': 'application/json' }, + headers: { 'content-type': 'application/json' } } ) - }, + } } diff --git a/src/constants.ts b/src/constants.ts index d24ec2e..2f612d1 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -8,68 +8,68 @@ export const chains = { name: 'Ethereum Mainnet', id: 1, currency: 'ETH', - explorer: 'https://etherscan.io', + explorer: 'https://etherscan.io' }, optimism: { name: 'Optimism Mainnet', id: 10, currency: 'ETH', - explorer: 'https://optimistic.etherscan.io', + explorer: 'https://optimistic.etherscan.io' }, gnosis: { name: 'Gnosis Chain', id: 100, currency: 'xDai', - explorer: 'https://gnosischain.com', + explorer: 'https://gnosischain.com' }, arbitrum: { name: 'Arbitrum One', id: 42_161, currency: 'ETH', - explorer: 'https://arbiscan.io', + explorer: 'https://arbiscan.io' }, polygon: { name: 'Polygon (Matic) Mainnet', id: 137, currency: 'MATIC', - explorer: 'https://explorer-mainnet.maticvigil.com', + explorer: 'https://explorer-mainnet.maticvigil.com' }, celo: { name: 'Celo Mainnet', id: 42_220, currency: 'CELO', - explorer: 'https://explorer.celo.org', + explorer: 'https://explorer.celo.org' }, moonbeam: { name: 'Moonbeam', id: 1284, currency: 'GLMR', - explorer: 'https://blockscout.moonbeam.network', + explorer: 'https://blockscout.moonbeam.network' }, avalanche: { name: 'Avalanche Mainnet', id: 43_114, currency: 'AVAX', - explorer: 'https://explorer.avax.network', + explorer: 'https://explorer.avax.network' }, fantom: { name: 'Fantom Opera', id: 250, currency: 'FTM', - explorer: 'https://ftmscan.com', + explorer: 'https://ftmscan.com' }, bsc: { name: 'Binance Smart Chain', id: 56, currency: 'BNB', - explorer: 'https://bscscan.com', + explorer: 'https://bscscan.com' }, harmony: { name: 'Harmony One', id: 1_666_600_000, currency: 'ONE', - explorer: 'https://explorer.harmony.one', - }, + explorer: 'https://explorer.harmony.one' + } } as const satisfies Chains export const unsupportedChains = { @@ -77,93 +77,93 @@ export const unsupportedChains = { name: 'Aurora', id: 1_313_161_554, currency: 'NEAR', - explorer: 'https://explorer.aurora.dev', + explorer: 'https://explorer.aurora.dev' }, goerli: { name: 'Görli Testnet', id: 5, currency: 'ETH', - explorer: 'https://goerli.etherscan.io', + explorer: 'https://goerli.etherscan.io' }, optimismGoerli: { name: 'Optimism Görli Testnet', id: 420, currency: 'ETH', - explorer: 'https://goerli.optimism.io', + explorer: 'https://goerli.optimism.io' }, baseGoerli: { name: 'Base Görli Testnet', id: 84_531, currency: 'ETH', - explorer: 'https://goerli.etherscan.io', + explorer: 'https://goerli.etherscan.io' }, arbitrumNova: { name: 'Arbitrum Nova', id: 42_170, currency: 'ARB', - explorer: 'https://explorer.arbitrum.io', + explorer: 'https://explorer.arbitrum.io' }, bitorrnet: { name: 'BitTorrent', id: 199, currency: 'BTT', - explorer: 'https://bttcscan.com', + explorer: 'https://bttcscan.com' }, moonriver: { name: 'Moonriver', id: 1285, currency: 'MOVR', - explorer: 'https://blockscout.moonriver.moonbeam.network', + explorer: 'https://blockscout.moonriver.moonbeam.network' }, evmos: { name: 'Evmos', id: 9001, currency: 'EVM', - explorer: 'https://explorer.evmos.org', + explorer: 'https://explorer.evmos.org' }, cento: { name: 'Cento', id: 7700, currency: 'CENTO', - explorer: 'https://explorer.cento.org', + explorer: 'https://explorer.cento.org' }, sepolia: { name: 'Sepolia Testnet', id: 11_155_111, currency: 'ETH', - explorer: 'https://sepolia.etherscan.io', + explorer: 'https://sepolia.etherscan.io' }, polygonMumbai: { name: 'Polygon (Matic) Mumbai Testnet', id: 80_001, currency: 'MATIC', - explorer: 'https://explorer-mumbai.maticvigil.com', + explorer: 'https://explorer-mumbai.maticvigil.com' }, centoTestnet: { name: 'Cento Testnet', id: 740, currency: 'CENTO', - explorer: 'https://testnet.explorer.cento.org', + explorer: 'https://testnet.explorer.cento.org' }, avalancheFuji: { name: 'Avalanche Fuji Testnet', id: 43_113, currency: 'AVAX', - explorer: 'https://explorer.fuji.avax.network', + explorer: 'https://explorer.fuji.avax.network' }, fantomTestnet: { name: 'Fantom Testnet', id: 4002, currency: 'FTM', - explorer: 'https://testnet.ftmscan.com', + explorer: 'https://testnet.ftmscan.com' }, arbitrumGoerli: { name: 'Arbitrum Görli Testnet', id: 421_613, currency: 'ETH', - explorer: 'https://goerli.arbiscan.io', - }, + explorer: 'https://goerli.arbiscan.io' + } } satisfies UnsupportedChains export const chainNames = Object.keys(chains) as Array @@ -171,10 +171,10 @@ export const chainNames = Object.keys(chains) as Array export const invalidResponse = { chain: { success: false, - data: `Invalid chain. Available chains: ${Object.keys(chains).join(', ')}`, + data: `Invalid chain. Available chains: ${Object.keys(chains).join(', ')}` }, token: { success: false, - data: 'Invalid token.', - }, + data: 'Invalid token.' + } } diff --git a/src/database/index.ts b/src/database/index.ts index 16a8b27..4d65924 100644 --- a/src/database/index.ts +++ b/src/database/index.ts @@ -4,11 +4,19 @@ import type { Chain, Pretty, Token } from '#/types' import { Kysely } from 'kysely' import { D1Dialect } from 'kysely-d1' +interface KvTable { + key: string + value: string +} + export type Database = { [K in Chain]: Pretty } type GetDatabase = (database: Env['DB']) => Promise> -export const getDatabase: GetDatabase = async (database) => - new Kysely({ dialect: new D1Dialect({ database }) }) +export const getDatabase: GetDatabase = async database => + new Kysely({ + // @ts-expect-error + dialect: new D1Dialect({ database }) + }) diff --git a/src/database/seed.ts b/src/database/seed.ts index 1b5791f..7c2e48e 100644 --- a/src/database/seed.ts +++ b/src/database/seed.ts @@ -16,11 +16,11 @@ export default { await insertNewTokens({ chain: chain as Chain, tokens: seedResult[chain as Chain], - database: env.DB, + database: env.DB }) } return new Response('ok', { status: 200 }) - }, + } } type SeedDataShape = { @@ -65,7 +65,7 @@ export async function seed({ DB: database, TOKEN_LIST_URLS }: Env) { symbol, name, decimals, - logoURI, + logoURI }) } const payloadsByChain = {} as Record> @@ -83,7 +83,7 @@ export async function seed({ DB: database, TOKEN_LIST_URLS }: Env) { async function fetchTokenList(url: string) { const response = await fetch(url) if (!response.ok) throw new Error(`Failed to fetch tokenlist.json from ${url}`) - const json = await response.json() + const json = (await response.json()) as SeedDataShape console.log(`DONE -- fetched ${json.tokens.length} tokens from ${url}`) return json.tokens } @@ -100,14 +100,14 @@ const folioChains = [ 'moonbeam', 'optimism', // 'polygon', - 'harmony', + 'harmony' ] as const -type FolioChain = typeof folioChains[number] +type FolioChain = (typeof folioChains)[number] async function getTokensList(urls: Array) { - const tokenLists = await Promise.all([...urls].map((url) => fetchTokenList(url))) + const tokenLists = await Promise.all([...urls].map(url => fetchTokenList(url))) const reshapedTokenLists = await Promise.all( - folioChains.map((chain) => reshapeTokenList(chain as FolioChain)) + folioChains.map(chain => reshapeTokenList(chain as FolioChain)) ) tokenLists.push(...reshapedTokenLists) return tokenLists @@ -119,21 +119,20 @@ async function reshapeTokenList(chain: FolioChain) { `https://raw.githubusercontent.com/llamafolio/llamafolio-tokens/master/${chain}/tokenlist.json` ) if (!response.ok) throw new Error('Failed to fetch token list') - const json = await response.json< - Array<{ - address: string - name: string - symbol: string - decimals: number - }> - >() + const json = (await response.json()) as Array<{ + address: string + name: string + symbol: string + decimals: number + }> + const tokens = json.map(({ address, ...token }) => ({ chainId: chains[chain].id, address: address.toLowerCase(), name: token.name, symbol: token.symbol, decimals: token.decimals, - logoURI: `https://raw.githubusercontent.com/llamafolio/llamafolio-tokens/master/${chain}/logos/${address.toLowerCase()}.png`, + logoURI: `https://raw.githubusercontent.com/llamafolio/llamafolio-tokens/master/${chain}/logos/${address.toLowerCase()}.png` })) console.log(`DONE -- fetched ${tokens.length} tokens from ${chain}`) return tokens diff --git a/src/erc20-methods.ts b/src/erc20-methods.ts index 0893b53..4b1c5d8 100644 --- a/src/erc20-methods.ts +++ b/src/erc20-methods.ts @@ -6,7 +6,7 @@ export async function erc20Method({ chain, address, method, - env, + env }: { chain: Chain address: string @@ -35,15 +35,15 @@ export async function erc20Method({ decimals: '313ce567', symbol: '95d89b41', name: '06fdde03', - totalSupply: '18160ddd', + totalSupply: '18160ddd' }[method] - }`, + }` }, - 'latest', - ], - }), + 'latest' + ] + }) }) - const data = await response.json() + const data = (await response.json()) as RPC_Response if (data.error) throw new Error(data.error.message) const { result } = data if (result === '0x') return '0' @@ -67,13 +67,13 @@ export async function hasBalanceOf(chain: Chain, address: string, env?: Env) { params: [ { to: address, - data: `0x70a08231`, + data: `0x70a08231` }, - 'latest', - ], - }), + 'latest' + ] + }) }) - const data = await response.json() + const data = (await response.json()) as RPC_Response if (data.error) throw new Error(data.error.message) const { result } = data if (result === '0x') return false @@ -96,13 +96,13 @@ export async function hasBalanceOfBatch(chain: Chain, addresses: string[], env?: params: [ { to: address, - data: `0x70a08231`, + data: `0x70a08231` }, - 'latest', - ], + 'latest' + ] })) - ), + ) }) - const data = await response.json>() + const data = (await response.json()) as Array return data.map(({ result }) => (result === '0x' ? false : true)) } diff --git a/src/index.ts b/src/index.ts index 1709ff5..9859232 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,8 +20,8 @@ app.use( cacheControl: cacheHeader({ maxAge: '1week', staleIfError: '1.5week', - staleWhileRevalidate: '1year', - }), + staleWhileRevalidate: '1year' + }) }) ) @@ -41,7 +41,7 @@ app.onError((error, context) => { return context.json({ message: error.message }, 500) }) -app.get('/env', (context) => { +app.get('/env', context => { if (context.env['NODE_ENV'] !== 'development') { return new Response(JSON.stringify({ NODE_ENV: 'production' }, undefined, 2), { status: 418 }) } @@ -49,7 +49,7 @@ app.get('/env', (context) => { return context.text(JSON.stringify(context.env, undefined, 2)) }) -app.get('/error', (context) => { +app.get('/error', context => { console.log({ path: context.req.path, url: context.req.url, error: context.error }) return context.json({ message: 'ok' }) }) @@ -59,28 +59,28 @@ app.post('/auth', async (_, next) => { const authorized = true if (!authorized) throw new HTTPException(401, { - res: new Response('Unauthorized', { status: 401 }), + res: new Response('Unauthorized', { status: 401 }) }) await next() }) -app.get('/routes', (context) => { +app.get('/routes', context => { app.showRoutes() // <-- this logs routes to console return context.json(app.routes) }) -app.get('/', (context) => context.html(IndexPage({ baseURL: context.req.url, chains }))) +app.get('/', context => context.html(IndexPage({ baseURL: context.req.url, chains }))) // available chains -app.get('/chains', (context) => context.json(chains)) -app.get('/supported-chains', (context) => context.json(Object.keys(chains))) +app.get('/chains', context => context.json(chains)) +app.get('/supported-chains', context => context.json(Object.keys(chains))) // everything -app.get('/all', async (context) => context.json(await getAllTokens(context.env.DB))) -app.get('/everything', async (context) => context.json(await getAllTokens(context.env.DB))) +app.get('/all', async context => context.json(await getAllTokens(context.env.DB))) +app.get('/everything', async context => context.json(await getAllTokens(context.env.DB))) // all tokens for :chain -app.get('/:chain', async (context) => { +app.get('/:chain', async context => { const chain = context.req.param('chain') if (!chains.includes(chain)) return context.json(invalidResponse.chain) const tokens = await getAllChainTokens({ chain, database: context.env.DB }) @@ -88,7 +88,7 @@ app.get('/:chain', async (context) => { }) // also all tokens for :chain -app.get('/:chain/tokens', async (context) => { +app.get('/:chain/tokens', async context => { const chain = context.req.param('chain') if (!chains.includes(chain)) return context.json(invalidResponse.chain) const tokens = await getAllChainTokens({ chain, database: context.env.DB }) @@ -96,21 +96,21 @@ app.get('/:chain/tokens', async (context) => { }) // with address as path parameter -app.get('/:chain/token/:address', async (context) => { +app.get('/:chain/token/:address', async context => { const { chain, address } = <{ chain: Chain; address: string }>context.req.param() if (!chains.includes(chain)) return context.json(invalidResponse.chain) const [token] = await getToken({ database: context.env.DB, chain, by: 'address', - value: address, + value: address }) if (!token) return context.json(invalidResponse.token) return context.json(token) }) // with address as query parameter -app.get('/:chain/token', async (context) => { +app.get('/:chain/token', async context => { const chain = context.req.param('chain') const { address } = context.req.query() if (!chains.includes(chain)) return context.json(invalidResponse.chain) @@ -118,21 +118,21 @@ app.get('/:chain/token', async (context) => { database: context.env.DB, chain, by: 'address', - value: address, + value: address }) if (!token) return context.json(invalidResponse.token) return context.json(token) }) // token logo -app.get('/:chain/logo/:address', async (context) => { +app.get('/:chain/logo/:address', async context => { const { chain, address } = <{ chain: Chain; address: string }>context.req.param() if (!chains.includes(chain)) return context.json(invalidResponse.chain) const [token] = await getToken({ database: context.env.DB, chain, by: 'address', - value: address, + value: address }) if (!token) return context.json(invalidResponse.token) if (!token.logoURI) return context.json({ message: 'Not found' }, 404) diff --git a/src/landing.tsx b/src/landing.tsx index 91940ef..b42fa9a 100644 --- a/src/landing.tsx +++ b/src/landing.tsx @@ -16,7 +16,7 @@ export function IndexPage({ baseURL, chains }: { baseURL: string; chains: Readon

Supported Chains

    - {chains.map((chain) => ( + {chains.map(chain => (
  • diff --git a/src/price/ankr.ts b/src/price/ankr.ts index a0fd331..5836b79 100644 --- a/src/price/ankr.ts +++ b/src/price/ankr.ts @@ -12,10 +12,10 @@ export async function ankrGetPrice(chain: Chain, addresses?: string[]) { >({ url: `https://rpc.ankr.com/multichain/${apiKey('ANKR_API_KEY')}/?ankr_getPrice=`, method: 'ankr_getTokenPrice', - params: addresses.map((address) => ({ + params: addresses.map(address => ({ blockchain: chain === 'ethereum' ? 'eth' : chain, - address, - })), + address + })) }) return result } @@ -35,9 +35,9 @@ async function getAnkrTokens(chain: Chain) { method: 'ankr_getCurrencies', params: [ { - blockchain: chain === 'ethereum' ? 'eth' : chain, - }, - ], + blockchain: chain === 'ethereum' ? 'eth' : chain + } + ] }) return result } diff --git a/src/types.ts b/src/types.ts index 572e5cd..4b80806 100644 --- a/src/types.ts +++ b/src/types.ts @@ -9,7 +9,6 @@ export interface Token { logoURI: string } - export type Chain = | 'gnosis' | 'polygon' @@ -43,7 +42,6 @@ export type UnsupportedChain = | 'aurora' | 'goerli' - export type RPC_Response = { jsonrpc: '2.0' id: number | null diff --git a/src/utilities/fetchers.ts b/src/utilities/fetchers.ts index da11246..eec46b5 100644 --- a/src/utilities/fetchers.ts +++ b/src/utilities/fetchers.ts @@ -3,18 +3,18 @@ import type { RPC_Response } from '#/types' export async function jsonRpcFetch({ url, method, - params, + params }: { url: string; method: string; params: T[] }) { // generic json rpc fetch const response = await fetch(url, { method: 'POST', headers: { accept: 'application/json', 'content-type': 'application/json' }, body: Array.isArray(params) - ? JSON.stringify(params.map((params) => ({ jsonrpc: '2.0', method, params, id: 1 }))) - : JSON.stringify({ jsonrpc: '2.0', method, params, id: 1 }), + ? JSON.stringify(params.map(params => ({ jsonrpc: '2.0', method, params, id: 1 }))) + : JSON.stringify({ jsonrpc: '2.0', method, params, id: 1 }) }) if (!response.ok) throw new Error('invalid response') - const data = await response.json[]>() - if (Array.isArray(data) && data.some((datum) => datum.error)) throw new Error('invalid response') - return data.map((datum: typeof data[0]) => datum.result) //: data.result + const data = (await response.json()) as RPC_Response[] + if (Array.isArray(data) && data.some(datum => datum.error)) throw new Error('invalid response') + return data.map((datum: (typeof data)[0]) => datum.result) //: data.result } diff --git a/src/utilities/index.ts b/src/utilities/index.ts index ad4c917..e2337ae 100644 --- a/src/utilities/index.ts +++ b/src/utilities/index.ts @@ -10,7 +10,7 @@ export const isChain = (chain: string): chain is Chain => chain in chains export function sleep(milliseconds: number): void { typeof Atomics === 'undefined' - ? new Promise((resolve) => setTimeout(resolve, milliseconds)) + ? new Promise(resolve => setTimeout(resolve, milliseconds)) : Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, Math.max(1, milliseconds | 0)) }