-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
- Loading branch information
Showing
8 changed files
with
130 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM oven/bun:1.1-slim | ||
FROM oven/bun:1.1.20-slim | ||
|
||
WORKDIR /app | ||
COPY package.json package.json | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import type { Address } from 'viem' | ||
import type { Contract } from './types' | ||
import consola from 'consola' | ||
import type { BunSQLiteDatabase } from 'drizzle-orm/bun-sqlite' | ||
import { contracts } from '../schema' | ||
import { and, eq } from 'drizzle-orm' | ||
|
||
export const getContractInformation = async ( | ||
address: Address, | ||
chainId: number, | ||
): Promise<Contract> => { | ||
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<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) | ||
.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<string> => { | ||
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()' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters