Skip to content

Instantly share code, notes, and snippets.

@dblodorn
Created June 16, 2023 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dblodorn/5f9e64ee9246ef33807a825853bce874 to your computer and use it in GitHub Desktop.
Save dblodorn/5f9e64ee9246ef33807a825853bce874 to your computer and use it in GitHub Desktop.
fetchOnchainSince.ts
import { Alchemy, AssetTransfersCategory } from 'alchemy-sdk'
import { ethers } from 'ethers'
import { format } from 'date-fns'
import { fetchTransaction } from 'shared/lib/wagmi-core'
import { FirstTransaction } from './types'
import 'shared/config/wagmi/server'
import { isMainnet } from 'shared/utils'
export async function fetchOnchainSince(
address: string,
alchemy: Alchemy
): Promise<FirstTransaction> {
const provider = new ethers.providers.AlchemyProvider(
// TODO: Revisit the need to instantiate a new AlchemyProvider here
isMainnet ? 1 : 5,
process.env.PROFILES_ALCHEMY_KEY
)
try {
const data = await alchemy.core.getAssetTransfers({
fromBlock: '0x0',
fromAddress: address as string,
category: [
AssetTransfersCategory.ERC721,
AssetTransfersCategory.ERC1155,
AssetTransfersCategory.ERC20,
AssetTransfersCategory.INTERNAL,
AssetTransfersCategory.EXTERNAL,
],
})
const firstTx = data?.transfers[0] || null
const transaction = await fetchTransaction({
hash: firstTx?.hash as `0x${string}`,
})
const block = await provider.getBlock(transaction?.blockHash as `0x${string}`)
const date = format(new Date(block?.timestamp * 1000), 'MMM dd, yyyy')
return {
transactionHash: transaction?.hash || null,
date: date || null,
}
} catch (err) {
console.error(err)
return {
transactionHash: null,
date: null,
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment