Skip to content

Commit

Permalink
Add more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ezynda3 committed Sep 11, 2024
1 parent 3bdeaec commit dbce4e2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
Binary file modified bun.lockb
Binary file not shown.
14 changes: 10 additions & 4 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ export const getContractInformation = async (
): Promise<Contract> => {
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 {
Expand All @@ -78,7 +81,8 @@ export const getContractInformation = async (
}
} catch (e) {
consola.error(e)
throw new Error('Contract not found')
consola.info('ABI not found.')
return { name: 'Unverified', address, abi: [] }
}
}

Expand All @@ -89,6 +93,7 @@ export const getCachedContractInformation = async (
): Promise<Contract> => {
try {
consola.info('Fetching contract information for', address, 'on chain', chainId)
consola.info('Checking for cached ABI...')
const result = await db
.select()
.from(contracts)
Expand All @@ -102,6 +107,7 @@ export const getCachedContractInformation = async (
address,
}
}
consola.info('Not found in cache. Fetching from anyabi.xyz...')
const contract = await getContractInformation(address, chainId)

// Don't cache unverified contracts
Expand All @@ -110,7 +116,7 @@ export const getCachedContractInformation = async (
}

// Update the database
consola.info('Updating db cache')
consola.info('Updating db cache...')
await db.insert(contracts).values({
id: `${chainId}:${address}`,
name: contract.name,
Expand All @@ -122,7 +128,7 @@ export const getCachedContractInformation = async (
return contract
} catch (e) {
consola.error(e)
throw new Error('Contract not found')
throw new Error('Failed to fetch contract information')
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/routes/diamond/[address]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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({
Expand Down
11 changes: 4 additions & 7 deletions src/routes/diamond/[address]/json/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,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
Expand Down Expand Up @@ -59,9 +59,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}`,
Expand All @@ -77,8 +76,6 @@ export const GET: RequestHandler = async ({ params, url, locals }) => {
},
})

sqlite.close()

return json({
chain: network,
diamond,
Expand Down

0 comments on commit dbce4e2

Please # to comment.