From d7edd31b9c069e5808435d8b1ae61295e8e8ce46 Mon Sep 17 00:00:00 2001 From: Ed Zynda Date: Wed, 18 Sep 2024 17:08:44 +0200 Subject: [PATCH] Various fixes (#29) * Update config stuff * Add more logging * move hooks to only run on the server * move sqlite deps out of client * use older version of bun... * bump version --- Dockerfile | 2 +- bun.lockb | Bin 586324 -> 586300 bytes src/{hooks.ts => hooks.server.ts} | 0 src/lib/utils.server.ts | 88 ++++++++++++++++++ src/lib/utils.ts | 91 ++++--------------- src/routes/diamond/[address]/+page.server.ts | 4 +- src/routes/diamond/[address]/json/+server.ts | 24 ++--- vite.config.ts | 12 ++- 8 files changed, 130 insertions(+), 91 deletions(-) rename src/{hooks.ts => hooks.server.ts} (100%) create mode 100644 src/lib/utils.server.ts diff --git a/Dockerfile b/Dockerfile index 4eb4996..e85f7dd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM oven/bun:1.1-slim +FROM oven/bun:1.1.20-slim WORKDIR /app COPY package.json package.json diff --git a/bun.lockb b/bun.lockb index 9e88507d583a015c803cc51b1059b54d66a7415c..4c2c8b1c8f70f9b8336cc4e39b871ddbc7a5cb80 100755 GIT binary patch delta 8528 zcmeI1X?RWN+Q-+*-fIsc6+>o;kf!vg-DD>uDuf=?Eap;ELaHHZ2&#%5R0TD4tA~UV zj&o`(f^C+dEvkm1B&ZmorBM-js;zSVck*ZJ^%df#vFb6xj-{?BjS&wM{??Y;K7 zn-TkNMl9^2Z`|7T*GPArSfQAh>t7 zu2t8x%!~=C>Elv0ZS<&&aYHgPw8uH_IrW=VxtF7BKDb$*F4WxNfAgo;9Pa-1HhmSyRQM$$*R)<&(!(wi9{ zL%voiFGIGkl#%W~9fm`K&eaoiwwIbD@wbCIwR<`AV&^7ODrga|=YVvm=q7h)oW4~p1N6GJ zTm@(8Ej$+&=|{S`Oq! z5`w`3kj9Wnc-F2Unl=_p!qN^q=3NQLsWa1zn$5@4UFb$$nm~qeY2(P=A;WE4YeqIv z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|;gO+Zj?MCROup+e|4Xn~-wx;f0G3I#4 z!W&>m>ccKNCtfu|OnRYnZISVw9w;;Fhi2l_9re9oybup73r3JL^QKW>cY9F`H>B=rXR*;NBP=wm?2W63re*q{3|_m&y~aGd-Y+85 zRZ}0H0jN28^3;95PW?hr(=aruAsV$uO;ywkDU93JtwGG4e`=CZQw}xH8*It!bhqeJ z)P#qHMumn)eT*76)Ql}1`rY)3Kh?79wtnFlC|XlvZy8~xh#0l;mf>4X`q!4gxiXyl zf9O{~CBMMUHL1ujRea0vGetzHSMc-?$A2f&l1Gh7Nlv@gFRaV9db_h+elek8D(toq zUfs0{d4g9&S3R~3vJ_8_*b0}hPz<6b+%|j~MukR}eGR(9rQM5Tsm8-qiExPwjSP*z zfBVu?QwE}gR?CXkt@PRZ!qtD2+EU(D84JL6xQ6&7Wt6O2t7}Q|Z0+`P3~HjJW?_jD zuZOGlB}UcGZ--*Ky>UT3F_1IuoAh4CADl3WV|Zvo{3b{lmEK@-bl=V+)2 z3vCn{gI^@6=@P$M*XzA+_b$qGM~&-zR9=bEL620%9V5pS9;H^?F{+rtV^sbmv(&LtYfJr0XN3pWtGcOLt}6Z6>T9Z#tE>;L?M;!nD)FH;LXT6^ z9$M?^@oM`+Yoy*s-Fs*qs%NT>kE~UVoLpV=S4ov^-o{SINLBocwX^f`BWn%a+2V=y zWj(j$HSpzzwA|UchON2T?Mz@qs|kDXfCxKpx2Z!1?BVL8 z?e^wsgR*<)?Xag<)s{lLMaB2DRZ+Y4*nd=;x7%Y?rB*0j`RIUsN`%|4$7R1%3)F => { + try { + const response = await fetch(`https://anyabi.xyz/api/get-abi/${chainId}/${address}`) + if (!response.ok) { + consola.info('ABI not found.') + return { name: 'Unverified', address, abi: [] } + } + const contractData = await response.json() + + return { + ...contractData, + address, + } + } catch (e) { + consola.error(e) + consola.info('ABI not found.') + return { name: 'Unverified', address, abi: [] } + } +} + +export const getCachedContractInformation = async ( + address: Address, + chainId: number, + db: BunSQLiteDatabase, +): Promise => { + try { + consola.info('Fetching contract information for', address, 'on chain', chainId) + consola.info('Checking for cached ABI...') + const result = await db + .select() + .from(contracts) + .where(and(eq(contracts.address, address), eq(contracts.chainId, chainId))) + + if (result.length) { + consola.info('Found in db cache') + return { + name: result[0].name, + abi: [...JSON.parse(result[0].abi)], + address, + } + } + consola.info('Not found in cache. Fetching from anyabi.xyz...') + const contract = await getContractInformation(address, chainId) + + // Don't cache unverified contracts + if (contract.name === 'Unverified') { + return contract + } + + // Update the database + consola.info('Updating db cache...') + await db.insert(contracts).values({ + id: `${chainId}:${address}`, + name: contract.name, + address, + abi: JSON.stringify(contract.abi), + chainId, + }) + + return contract + } catch (e) { + consola.error(e) + throw new Error('Failed to fetch contract information') + } +} + +export const getFuncSigBySelector = async (selector: string): Promise => { + const response = await fetch( + `https://api.openchain.xyz/signature-database/v1/lookup?function=${selector}&filter=true`, + ) + const data = await response.json() + + if (data && data.result && data.result.function && data.result.function[selector]) { + return data.result.function[selector][0].name + } + + return 'unknown()' +} diff --git a/src/lib/utils.ts b/src/lib/utils.ts index b34e5f2..64aac73 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -3,12 +3,8 @@ import { twMerge } from 'tailwind-merge' import { cubicOut } from 'svelte/easing' import type { TransitionConfig } from 'svelte/transition' import { type Abi, type AbiFunction, type Address } from 'viem' -import type { Contract } from './types' import toast from 'svelte-french-toast' -import Database from 'bun:sqlite' -import { drizzle, type BunSQLiteDatabase } from 'drizzle-orm/bun-sqlite' -import { contracts } from '../schema' -import { and, eq } from 'drizzle-orm' +import type { Contract } from './types' import consola from 'consola' export function cn(...inputs: ClassValue[]) { @@ -63,13 +59,26 @@ export const flyAndScale = ( } } +export const copyToClipboard = async (text: string) => { + await navigator.clipboard.writeText(text) + toast.success('Copied to clipboard!') +} + +export const abiMethods = (abi: Abi): AbiFunction[] => + abi.filter((i) => i.type === 'function') as AbiFunction[] + +export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) + export const getContractInformation = async ( address: Address, chainId: number, ): Promise => { try { const response = await fetch(`https://anyabi.xyz/api/get-abi/${chainId}/${address}`) - if (!response.ok) return { name: 'Unverified', address, abi: [] } + if (!response.ok) { + consola.info('ABI not found.') + return { name: 'Unverified', address, abi: [] } + } const contractData = await response.json() return { @@ -78,73 +87,7 @@ export const getContractInformation = async ( } } catch (e) { consola.error(e) - throw new Error('Contract not found') - } -} - -export const getCachedContractInformation = async ( - address: Address, - chainId: number, - db: BunSQLiteDatabase, -): Promise => { - try { - consola.info('Fetching contract information for', address, 'on chain', chainId) - const result = await db - .select() - .from(contracts) - .where(and(eq(contracts.address, address), eq(contracts.chainId, chainId))) - - if (result.length) { - consola.info('Found in db cache') - return { - name: result[0].name, - abi: [...JSON.parse(result[0].abi)], - address, - } - } - const contract = await getContractInformation(address, chainId) - - // Don't cache unverified contracts - if (contract.name === 'Unverified') { - return contract - } - - // Update the database - consola.info('Updating db cache') - await db.insert(contracts).values({ - id: `${chainId}:${address}`, - name: contract.name, - address, - abi: JSON.stringify(contract.abi), - chainId, - }) - - return contract - } catch (e) { - consola.error(e) - throw new Error('Contract not found') - } -} - -export const copyToClipboard = async (text: string) => { - await navigator.clipboard.writeText(text) - toast.success('Copied to clipboard!') -} - -export const getFuncSigBySelector = async (selector: string): Promise => { - const response = await fetch( - `https://api.openchain.xyz/signature-database/v1/lookup?function=${selector}&filter=true`, - ) - const data = await response.json() - - if (data && data.result && data.result.function && data.result.function[selector]) { - return data.result.function[selector][0].name + consola.info('ABI not found.') + return { name: 'Unverified', address, abi: [] } } - - return 'unknown()' } - -export const abiMethods = (abi: Abi): AbiFunction[] => - abi.filter((i) => i.type === 'function') as AbiFunction[] - -export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) diff --git a/src/routes/diamond/[address]/+page.server.ts b/src/routes/diamond/[address]/+page.server.ts index 60a0f16..84c13a5 100644 --- a/src/routes/diamond/[address]/+page.server.ts +++ b/src/routes/diamond/[address]/+page.server.ts @@ -1,5 +1,5 @@ import type { FacetData, Contract, Diamond } from '$lib/types' -import { getCachedContractInformation, getFuncSigBySelector } from '$lib/utils' +import { getCachedContractInformation, getFuncSigBySelector } from '$lib/utils.server' import { error } from '@sveltejs/kit' import type { PageServerLoad } from './$types' import { @@ -16,6 +16,7 @@ import { chainMap } from '$lib/chains' import { type BunSQLiteDatabase } from 'drizzle-orm/bun-sqlite' import { diamonds } from '../../../schema' import { sql } from 'drizzle-orm' +import consola from 'consola' export const load: PageServerLoad = async ({ params, url, locals }) => { const { address } = params @@ -54,6 +55,7 @@ export const load: PageServerLoad = async ({ params, url, locals }) => { } // Udate the database + consola.info('Updating stats...') await locals.db .insert(diamonds) .values({ diff --git a/src/routes/diamond/[address]/json/+server.ts b/src/routes/diamond/[address]/json/+server.ts index 6958855..3c5f7ca 100644 --- a/src/routes/diamond/[address]/json/+server.ts +++ b/src/routes/diamond/[address]/json/+server.ts @@ -1,9 +1,5 @@ import type { FacetData, Contract, Diamond } from '$lib/types' -import { - getCachedContractInformation, - getContractInformation, - getFuncSigBySelector, -} from '$lib/utils' +import { getCachedContractInformation, getFuncSigBySelector } from '$lib/utils.server' import { error, json } from '@sveltejs/kit' import type { RequestHandler } from './$types' import { @@ -17,10 +13,10 @@ import { } from 'viem' import type { Chain } from 'viem/chains' import { chainMap } from '$lib/chains' -import { drizzle, type BunSQLiteDatabase } from 'drizzle-orm/bun-sqlite' -import { Database } from 'bun:sqlite' +import { type BunSQLiteDatabase } from 'drizzle-orm/bun-sqlite' import { diamonds } from '../../../../schema' import { sql } from 'drizzle-orm' +import consola from 'consola' export const GET: RequestHandler = async ({ params, url, locals }) => { const { address } = params @@ -59,9 +55,8 @@ export const GET: RequestHandler = async ({ params, url, locals }) => { } // Udate the database - const sqlite = new Database('./data/louper.db') - const db = drizzle(sqlite) - await db + consola.info('Updating stats...') + await locals.db .insert(diamonds) .values({ id: `${network}:${address}`, @@ -76,14 +71,15 @@ export const GET: RequestHandler = async ({ params, url, locals }) => { visits: sql`${diamonds.visits} + 1`, }, }) + consola.info('Stats updated.') - sqlite.close() - - return json({ + const response = { chain: network, diamond, diamondAbi, - }) + } + + return json(response) } catch (e) { console.error(e) throw error(400, { message: 'Unable to fetch diamond details' }) diff --git a/vite.config.ts b/vite.config.ts index ec916a0..5fcc8d4 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -3,5 +3,15 @@ import { defineConfig } from 'vite' import { nodePolyfills } from 'vite-plugin-node-polyfills' export default defineConfig({ - plugins: [nodePolyfills(), sveltekit()], + plugins: [ + nodePolyfills({ + include: ['path', 'stream', 'util'], + globals: { + Buffer: true, + global: true, + process: true, + }, + }), + sveltekit(), + ], })