Skip to content

Commit

Permalink
Attempt to run on load and usePendingBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
ditto-eth committed Oct 9, 2023
1 parent 8d3bc36 commit 891c9d9
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 44 deletions.
16 changes: 16 additions & 0 deletions src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ import {
useAccountStore,
useNetworkStore,
useSessionsStore,
useTokensStore,
} from '~/zustand'

import { type AppMeta, AppMetaContext } from './contexts'
import { useImportTransferredTokens } from './hooks/useImportTransferredTokens'
import Layout from './screens/_layout'
import AccountDetails from './screens/account-details'
import BlockConfig from './screens/block-config'
Expand Down Expand Up @@ -126,6 +128,7 @@ export function init({ type = 'standalone' }: { type?: AppMeta['type'] } = {}) {
<AccountsChangedEmitter />
<NetworkChangedEmitter />
<SyncBlockNumber />
<SyncTransferredTokens />
<SyncJsonRpcAccounts />
<SyncNetwork />
<RouterProvider router={router} />
Expand Down Expand Up @@ -197,6 +200,19 @@ function SyncBlockNumber() {
return null
}

/** Keeps Transfer event imported tokens in sync. */
function SyncTransferredTokens() {
const { account } = useAccountStore()
const { addToken } = useTokensStore()
const tokens = useImportTransferredTokens()
if (account?.address) {
tokens.forEach((addr) => {
addToken(addr, account.address)
})
}
return null
}

/** Keeps accounts in sync with network. */
function SyncJsonRpcAccounts() {
const { data: chainId } = useNetworkStatus()
Expand Down
10 changes: 10 additions & 0 deletions src/hooks/useGetLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ export function useGetLogsQueryOptions<

return queryOptions({
enabled: Boolean(parameters),
gcTime:
typeof parameters.fromBlock === 'bigint' &&
typeof parameters.toBlock === 'bigint'
? Infinity
: undefined,
staleTime:
typeof parameters.fromBlock === 'bigint' &&
typeof parameters.toBlock === 'bigint'
? Infinity
: undefined,
queryKey: getLogsQueryKey([client.key, stringify(parameters)]),
async queryFn() {
return await client.getLogs(parameters)
Expand Down
49 changes: 49 additions & 0 deletions src/hooks/useImportTransferredTokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { parseAbiItem } from 'abitype'
import type { BlockTag } from 'viem'
import { useAccountStore, useNetworkStore } from '~/zustand'
import { useGetLogs } from './useGetLogs'

type UseImportTransferredTokensParameters = {
fromBlock?: bigint | BlockTag
toBlock?: bigint | BlockTag
}

export function useImportTransferredTokens({
fromBlock,
toBlock,
}: UseImportTransferredTokensParameters = {}) {
const { account } = useAccountStore()
const { network } = useNetworkStore()

const { data: transfersFrom } = useGetLogs({
event: parseAbiItem(
'event Transfer(address indexed from, address indexed to, uint256)',
),
args: {
from: account?.address,
},
fromBlock: fromBlock || network.forkBlockNumber,
toBlock: toBlock || 'latest',
})
const { data: transfersTo } = useGetLogs({
event: parseAbiItem(
'event Transfer(address indexed from, address indexed to, uint256)',
),
args: {
to: account?.address,
},
fromBlock: fromBlock || network.forkBlockNumber,
toBlock: toBlock || 'latest',
})

if (account?.address) {
return [
...new Set([
...(transfersFrom?.map((t) => t.address) || []),
...(transfersTo?.map((t) => t.address) || []),
]),
]
}

return []
}
39 changes: 37 additions & 2 deletions src/hooks/usePendingBlock.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { type InfiniteData, useQuery } from '@tanstack/react-query'
import type { Block, Client, Transaction } from 'viem'
import { type Block, type Client, type Transaction, parseAbiItem } from 'viem'

import {
createQueryKey,
queryClient,
updateInfiniteQueryData,
} from '~/react-query'
import { useNetworkStore } from '~/zustand'
import { useAccountStore, useNetworkStore, useTokensStore } from '~/zustand'

import { getBalanceQueryKey } from './useBalance'
import { getBlockQueryKey } from './useBlock'
Expand All @@ -31,6 +31,8 @@ export function usePendingBlockQueryOptions({
}: UsePendingBlockParameters = {}) {
const { network } = useNetworkStore()
const { data: chainId } = useNetworkStatus()
const { addToken } = useTokensStore()
const { account } = useAccountStore()
const client = useClient()

return {
Expand All @@ -53,6 +55,39 @@ export function usePendingBlockQueryOptions({
)
return prevBlock || null

// Import any tokens from Transfer event
if (account && prevBlock.number) {
const transfersFrom = await client.getLogs({
event: parseAbiItem(
'event Transfer(address indexed from, address indexed to, uint256)',
),
args: {
from: account.address,
},
fromBlock: prevBlock.number,
toBlock: 'latest',
})
const transfersTo = await client.getLogs({
event: parseAbiItem(
'event Transfer(address indexed from, address indexed to, uint256)',
),
args: {
to: account.address,
},
fromBlock: prevBlock.number,
toBlock: 'latest',
})
;[
...new Set([
...(transfersFrom?.map((t) => t.address) || []),
...(transfersTo?.map((t) => t.address) || []),
]),
].forEach((addr) => {
console.log(addr)
addToken(addr, account.address)
})
}

queryClient.invalidateQueries({
queryKey: getBalanceQueryKey([client.key]),
})
Expand Down
42 changes: 0 additions & 42 deletions src/screens/account-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
BaseError,
formatUnits,
isAddress,
parseAbiItem,
parseUnits,
} from 'viem'

Expand All @@ -32,9 +31,7 @@ import {
} from '~/design-system'
import { useErc20Balance } from '~/hooks/useErc20Balance'
import { useErc20Metadata } from '~/hooks/useErc20Metadata'
import { useGetLogs } from '~/hooks/useGetLogs'
import { useSetErc20Balance } from '~/hooks/useSetErc20Balance'
import { useNetworkStore } from '~/zustand'
import { useTokensStore } from '~/zustand/tokens'

export default function AccountDetails() {
Expand Down Expand Up @@ -91,47 +88,8 @@ export default function AccountDetails() {
)
}

function useImportTransferredTokens() {
const { address: accountAddress } = useParams()
const { addToken } = useTokensStore()
const { network } = useNetworkStore()

const { data: transfersFrom } = useGetLogs({
event: parseAbiItem(
'event Transfer(address indexed from, address indexed to, uint256)',
),
args: {
from: accountAddress as Address,
},
fromBlock: network.forkBlockNumber,
toBlock: 'latest',
})
const { data: transfersTo } = useGetLogs({
event: parseAbiItem(
'event Transfer(address indexed from, address indexed to, uint256)',
),
args: {
to: accountAddress as Address,
},
fromBlock: network.forkBlockNumber,
toBlock: 'latest',
})

if (accountAddress) {
;[
...new Set([
...(transfersFrom?.map((t) => t.address) || []),
...(transfersTo?.map((t) => t.address) || []),
]),
].forEach((addr) => {
addToken(addr, accountAddress as Address)
})
}
}

function Tokens({ accountAddress }: { accountAddress: Address }) {
const { tokens } = useTokensStore()
useImportTransferredTokens()

if (!accountAddress) return null
return (
Expand Down
8 changes: 8 additions & 0 deletions src/zustand/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ export {
useSessionsStore,
} from './sessions'

export {
type TokensActions,
type TokensState,
type TokensStore,
tokensStore,
useTokensStore,
} from './tokens'

export { syncStores } from './utils'

0 comments on commit 891c9d9

Please sign in to comment.