Skip to content

Commit

Permalink
Cache in db
Browse files Browse the repository at this point in the history
  • Loading branch information
ezynda3 committed Mar 9, 2024
1 parent e6a16b2 commit ed0970a
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 4 deletions.
10 changes: 10 additions & 0 deletions drizzle/0001_high_mockingbird.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE `contracts` (
`id` text(36) PRIMARY KEY NOT NULL,
`chainId` integer NOT NULL,
`address` text NOT NULL,
`name` text NOT NULL,
`abi` text NOT NULL,
`createdAt` text DEFAULT CURRENT_TIMESTAMP
);
--> statement-breakpoint
CREATE UNIQUE INDEX `contracts_id_unique` ON `contracts` (`id`);
135 changes: 135 additions & 0 deletions drizzle/meta/0001_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
{
"version": "5",
"dialect": "sqlite",
"id": "cf4dbe5e-514e-4a76-abd6-ca938cb705c1",
"prevId": "292cb595-d7c0-4e69-8a8b-9a599b7a69ca",
"tables": {
"contracts": {
"name": "contracts",
"columns": {
"id": {
"name": "id",
"type": "text(36)",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"chainId": {
"name": "chainId",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"address": {
"name": "address",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"abi": {
"name": "abi",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"createdAt": {
"name": "createdAt",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": "CURRENT_TIMESTAMP"
}
},
"indexes": {
"contracts_id_unique": {
"name": "contracts_id_unique",
"columns": [
"id"
],
"isUnique": true
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"diamonds": {
"name": "diamonds",
"columns": {
"id": {
"name": "id",
"type": "text(36)",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"network": {
"name": "network",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"address": {
"name": "address",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"visits": {
"name": "visits",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": 0
},
"createdAt": {
"name": "createdAt",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": "CURRENT_TIMESTAMP"
}
},
"indexes": {
"diamonds_id_unique": {
"name": "diamonds_id_unique",
"columns": [
"id"
],
"isUnique": true
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}
9 changes: 8 additions & 1 deletion drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
"when": 1706192961433,
"tag": "0000_lyrical_multiple_man",
"breakpoints": true
},
{
"idx": 1,
"version": "5",
"when": 1710003868501,
"tag": "0001_high_mockingbird",
"breakpoints": true
}
]
}
}
35 changes: 34 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import { type ClassValue, clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'
import { cubicOut } from 'svelte/easing'
import type { TransitionConfig } from 'svelte/transition'
import type { Address } from 'viem'
import { parseAbi, type Address } from 'viem'
import type { Contract } from './types'
import toast from 'svelte-french-toast'
import Database from 'bun:sqlite'
import { drizzle } from 'drizzle-orm/bun-sqlite'
import { contracts } from '../schema'
import { and, eq } from 'drizzle-orm'

export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
Expand Down Expand Up @@ -63,14 +67,43 @@ export const getContractInformation = async (
chainId: number,
): Promise<Contract> => {
try {
const sqlite = new Database('./data/louper.db')
const db = drizzle(sqlite)

const result = await db
.select()
.from(contracts)
.where(and(eq(contracts.address, address), eq(contracts.chainId, chainId)))

if (result.length) {
console.log('Found in db cache!')
return {
name: result[0].name,
abi: [...JSON.parse(result[0].abi)],
address,
}
}

const response = await fetch(`https://anyabi.xyz/api/get-abi/${chainId}/${address}`)
if (!response.ok) return { name: 'Unverified', address, abi: [] }
const contractData = await response.json()

// Update the database
console.log('Adding to db cache...')
await db.insert(contracts).values({
id: `${chainId}:${address}`,
name: contractData.name,
address,
abi: JSON.stringify(contractData.abi),
chainId,
})

return {
...contractData,
address,
}
} catch (e) {
console.error(e)
throw new Error('Contract not found')
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/routes/diamond/[address]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import { Database } from 'bun:sqlite'
import { diamonds } from '../../../schema'
import { sql } from 'drizzle-orm'

const sqlite = new Database('./data/louper.db')
const db = drizzle(sqlite)

export const load: PageServerLoad = async ({ params, url }) => {
const { address } = params
const network = <string>url.searchParams.get('network') ?? 'mainnet'
Expand Down Expand Up @@ -55,8 +58,6 @@ export const load: PageServerLoad = async ({ params, url }) => {
}

// Udate the database
const sqlite = new Database('./data/louper.db')
const db = drizzle(sqlite)
await db
.insert(diamonds)
.values({
Expand Down
9 changes: 9 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ export const diamonds = sqliteTable('diamonds', {
visits: integer('visits').notNull().default(0),
createdAt: text('createdAt').default(sql`CURRENT_TIMESTAMP`),
})

export const contracts = sqliteTable('contracts', {
id: text('id', { length: 36 }).primaryKey().unique(),
chainId: integer('chainId').notNull(),
address: text('address').notNull(),
name: text('name').notNull(),
abi: text('abi').notNull(),
createdAt: text('createdAt').default(sql`CURRENT_TIMESTAMP`),
})

0 comments on commit ed0970a

Please sign in to comment.