read tx history from db cache instead of alchemy api
This commit is contained in:
@@ -14,6 +14,7 @@ import { tryCatch } from '@koins/utils'
|
||||
import {
|
||||
getTransactionHistory,
|
||||
getTransactionDetails,
|
||||
getCachedTransactionHistory,
|
||||
} from './lib/transactions'
|
||||
import { migrate } from 'drizzle-orm/bun-sqlite/migrator'
|
||||
import { eq } from 'drizzle-orm'
|
||||
@@ -177,6 +178,13 @@ type RPC = {
|
||||
}
|
||||
response: TxEntry[]
|
||||
}
|
||||
fetchCachedTxHistory: {
|
||||
params: {
|
||||
address: string
|
||||
chainid: string
|
||||
}
|
||||
response: TxEntry[]
|
||||
}
|
||||
fetchTokenBalances: {
|
||||
params: {
|
||||
address: string
|
||||
@@ -561,6 +569,10 @@ const rpc = BrowserView.defineRPC<RPC>({
|
||||
|
||||
return combined
|
||||
},
|
||||
fetchCachedTxHistory: async ({ address, chainid }) => {
|
||||
const transfers = getCachedTransactionHistory(chainid, address)
|
||||
return transfers.map(mapTransfer)
|
||||
},
|
||||
fetchTokenBalances: async ({ address, chainid }) => {
|
||||
const key = await Bun.secrets.get({
|
||||
service: 'koins',
|
||||
|
||||
@@ -64,6 +64,29 @@ export const getTransactionHistory = async (
|
||||
return rows.map(toTransfer)
|
||||
}
|
||||
|
||||
export const getCachedTransactionHistory = (
|
||||
chainid: string,
|
||||
address: string,
|
||||
) => {
|
||||
const addrLower = address.toLowerCase()
|
||||
const rows = db
|
||||
.select()
|
||||
.from(txHistory)
|
||||
.where(
|
||||
and(
|
||||
eq(txHistory.chainId, chainid),
|
||||
or(
|
||||
eq(txHistory.from, addrLower),
|
||||
eq(txHistory.to, addrLower),
|
||||
),
|
||||
),
|
||||
)
|
||||
.orderBy(desc(txHistory.blockNum))
|
||||
.all()
|
||||
|
||||
return rows.map(toTransfer)
|
||||
}
|
||||
|
||||
function toTransfer(row: typeof txHistory.$inferSelect): Transaction {
|
||||
if (row.raw) {
|
||||
return JSON.parse(row.raw) as Transaction
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
export { getTransactionHistory } from './get-history'
|
||||
export { getTransactionHistory, getCachedTransactionHistory } from './get-history'
|
||||
export { getTransactionDetails } from './get-details'
|
||||
|
||||
@@ -134,6 +134,13 @@ type RPC = {
|
||||
}
|
||||
response: TxEntry[]
|
||||
}
|
||||
fetchCachedTxHistory: {
|
||||
params: {
|
||||
address: string
|
||||
chainid: string
|
||||
}
|
||||
response: TxEntry[]
|
||||
}
|
||||
fetchTokenBalances: {
|
||||
params: {
|
||||
address: string
|
||||
|
||||
@@ -313,11 +313,11 @@ export const EvmWallet = () => {
|
||||
}
|
||||
|
||||
const fetchTxHistory = async () => {
|
||||
if (!address || !apiKey) {
|
||||
if (!address) {
|
||||
transactions = []
|
||||
return
|
||||
}
|
||||
const txs = await electrobun.rpc?.request.fetchTxHistory({
|
||||
const txs = await electrobun.rpc?.request.fetchCachedTxHistory({
|
||||
address, chainid: net().chainid,
|
||||
})
|
||||
transactions = txs ?? []
|
||||
|
||||
Reference in New Issue
Block a user