-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
26,878 additions
and
789 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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { type Address, parseAbiItem } from 'abitype' | ||
import type { GetLogsParameters } from 'viem' | ||
|
||
import { getLogsQueryOptions } from '~/hooks/useGetLogs' | ||
import { queryClient } from '~/react-query' | ||
import type { Client } from '~/viem' | ||
|
||
export async function getAccountTokens( | ||
client: Client, | ||
{ | ||
address, | ||
fromBlock, | ||
toBlock, | ||
}: { | ||
address: Address | ||
fromBlock: GetLogsParameters['fromBlock'] | ||
toBlock: GetLogsParameters['toBlock'] | ||
}, | ||
) { | ||
const [transfersFrom, transfersTo] = await Promise.all([ | ||
queryClient.fetchQuery( | ||
getLogsQueryOptions(client, { | ||
event: parseAbiItem( | ||
'event Transfer(address indexed from, address indexed to, uint256)', | ||
), | ||
args: { | ||
from: address, | ||
}, | ||
fromBlock, | ||
toBlock, | ||
}), | ||
), | ||
queryClient.fetchQuery( | ||
getLogsQueryOptions(client, { | ||
event: parseAbiItem( | ||
'event Transfer(address indexed from, address indexed to, uint256)', | ||
), | ||
args: { | ||
to: address, | ||
}, | ||
fromBlock, | ||
toBlock, | ||
}), | ||
), | ||
]) | ||
|
||
// TODO: Check if log addresses are ERC20 tokens. | ||
|
||
return [ | ||
...new Set([ | ||
...(transfersFrom?.map((t) => t.address) || []), | ||
...(transfersTo?.map((t) => t.address) || []), | ||
]), | ||
] | ||
} |
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,2 +1,3 @@ | ||
export { connect } from './connect' | ||
export { disconnect } from './disconnect' | ||
export { getAccountTokens } from './getAccountTokens' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { queryOptions, useQuery } from '@tanstack/react-query' | ||
import { type Address, stringify } from 'viem' | ||
|
||
import { getAccountTokens } from '~/actions' | ||
import { createQueryKey } from '~/react-query' | ||
import type { Client } from '~/viem' | ||
import { useNetworkStore, useTokensStore } from '~/zustand' | ||
|
||
import { useClient } from './useClient' | ||
|
||
type UseAccountTokensParameters = { | ||
address?: Address | ||
} | ||
|
||
export const getAccountTokensQueryKey = createQueryKey< | ||
'account-tokens', | ||
[key: Client['key'], address: Address | undefined, args: string] | ||
>('account-tokens') | ||
|
||
export function useAccountTokensQueryOptions(args: UseAccountTokensParameters) { | ||
const { address } = args | ||
|
||
const { syncTokens } = useTokensStore() | ||
const { network } = useNetworkStore() | ||
const client = useClient() | ||
|
||
return queryOptions({ | ||
enabled: Boolean(address && network.forkBlockNumber), | ||
queryKey: getAccountTokensQueryKey([client.key, address, stringify(args)]), | ||
async queryFn() { | ||
if (!address) throw new Error('address is required') | ||
const tokens = await getAccountTokens(client, { | ||
address, | ||
fromBlock: network.forkBlockNumber, | ||
toBlock: 'latest', | ||
}) | ||
syncTokens(tokens, address) | ||
return tokens | ||
}, | ||
}) | ||
} | ||
|
||
export function useAccountTokens(args: UseAccountTokensParameters) { | ||
const queryOptions = useAccountTokensQueryOptions(args) | ||
return useQuery(queryOptions) | ||
} |
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.