Last active
June 16, 2023 01:20
-
-
Save dblodorn/78fe09633c690ea230a0c180534115dc to your computer and use it in GitHub Desktop.
fetchOwnedContracts.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 { OwnedContractsProps } from 'modules/profiles/data/types' | |
import { OwnedContractsReturn } from 'modules/profiles/data/types' | |
import 'wagmi/server' | |
export interface fetchOwnedContractsResponse { | |
contractsData: OwnedContractsReturn[] | |
contractsCount: number | |
pageKey?: number | |
} | |
export async function fetchOwnedContracts({ | |
chainId, | |
address, | |
pageSize = 16, | |
key, | |
}: OwnedContractsProps): Promise<fetchOwnedContractsResponse> { | |
const options = { method: 'GET', headers: { accept: 'application/json' } } | |
const paginationParams = key | |
? `&pageKey=${key}&pageSize=${pageSize}` | |
: `&pageSize=${pageSize}` | |
const alchemyNetwork = { | |
1: 'eth-mainnet', | |
5: 'eth-goerli', | |
}[chainId] | |
const requestUri = `https://${alchemyNetwork}.g.alchemy.com/nft/v2/${ | |
process.env.PROFILES_ALCHEMY_KEY | |
}/getContractsForOwner?owner=${address}${paginationParams}&withMetadata=true${ | |
chainId === 1 ? '&orderBy=transferTime' : '' | |
}` | |
const alchemyResponse = await fetch(requestUri, options) | |
.then((response) => response.json()) | |
.catch((err) => console.error(err)) | |
const formatted: OwnedContractsReturn[] = alchemyResponse?.contracts?.map( | |
(contract: any) => { | |
return { | |
chainId: contract?.chainId || null, | |
address: contract?.address || null, | |
isSpam: contract?.isSpam || false, | |
totalBalance: contract?.totalBalance || null, | |
name: contract?.name || null, | |
description: contract?.opensea?.description || null, | |
thumbnail: contract?.opensea?.imageUrl || null, | |
tokenType: contract?.tokenType || null, | |
totalSupply: contract?.totalSupply || null, | |
contractDeployer: contract?.contractDeployer || null, | |
} | |
} | |
) | |
return { | |
contractsData: formatted, | |
contractsCount: alchemyResponse?.totalCount, | |
pageKey: alchemyResponse?.pageKey, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment