Created
June 16, 2023 01:18
-
-
Save dblodorn/5f9e64ee9246ef33807a825853bce874 to your computer and use it in GitHub Desktop.
fetchOnchainSince.ts
This file contains hidden or 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
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