Skip to content

Commit

Permalink
[BX-1314][BX-1315] Support Additional Simplehash Networks + Configure…
Browse files Browse the repository at this point in the history
… Testnet NFT Request (#1336)
  • Loading branch information
derHowie authored and greg-schrammel committed Feb 12, 2024
1 parent 8f4f7c7 commit 8383945
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 83 deletions.
2 changes: 2 additions & 0 deletions src/core/references/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export const NATIVE_ASSETS_PER_CHAIN: Record<ChainId, AddressOrEth> = {
[ChainId.hardhatOptimism]: AddressZero as Address,
[ChainId.optimismGoerli]: AddressZero as Address,
[ChainId.optimismSepolia]: AddressZero as Address,
[ChainId.rari]: AddressZero as Address,
[ChainId.base]: ETH_BASE_ADDRESS as Address,
[ChainId.baseSepolia]: AddressZero as Address,
[ChainId.zora]: ETH_ZORA_ADDRESS as Address,
Expand All @@ -151,6 +152,7 @@ export const NATIVE_ASSETS_MAP_PER_CHAIN: Record<ChainId, AddressOrEth> = {
[ChainId.bsc]: BNB_MAINNET_ADDRESS,
[ChainId.bscTestnet]: BNB_MAINNET_ADDRESS,
[ChainId.optimism]: ETH_ADDRESS,
[ChainId.rari]: ETH_ADDRESS,
[ChainId.hardhatOptimism]: ETH_ADDRESS,
[ChainId.optimismGoerli]: ETH_ADDRESS,
[ChainId.optimismSepolia]: ETH_ADDRESS,
Expand Down
51 changes: 30 additions & 21 deletions src/core/resources/nfts/nfts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useInfiniteQuery } from '@tanstack/react-query';
import { Chain } from 'viem';
import { Address } from 'wagmi';

import {
Expand All @@ -13,17 +14,16 @@ import {
createQueryKey,
queryClient,
} from '~/core/react-query';
import {
SUPPORTED_MAINNET_CHAINS,
SUPPORTED_TESTNET_CHAINS,
} from '~/core/references';
import { ChainName } from '~/core/types/chains';
import { ChainName, chainNameToIdMapping } from '~/core/types/chains';
import {
PolygonAllowListDictionary,
SimpleHashCollectionDetails,
UniqueAsset,
} from '~/core/types/nfts';
import { getSimpleHashSupportedChainNames } from '~/core/utils/chains';
import {
getSimpleHashSupportedChainNames,
getSimpleHashSupportedTestnetChainNames,
} from '~/core/utils/chains';
import {
filterSimpleHashNFTs,
simpleHashNFTToUniqueAsset,
Expand All @@ -38,32 +38,41 @@ const POLYGON_ALLOWLIST_STALE_TIME = 600000; // 10 minutes
export type NftsArgs = {
address: Address;
testnetMode: boolean;
userChains: Chain[];
};

// ///////////////////////////////////////////////
// Query Key

const nftsQueryKey = ({ address, testnetMode }: NftsArgs) =>
createQueryKey('nfts', { address, testnetMode }, { persisterVersion: 2 });
const nftsQueryKey = ({ address, testnetMode, userChains }: NftsArgs) =>
createQueryKey(
'nfts',
{ address, testnetMode, userChains },
{ persisterVersion: 3 },
);

// ///////////////////////////////////////////////
// Query Function

async function nftsQueryFunction({
queryKey: [{ address, testnetMode }],
queryKey: [{ address, testnetMode, userChains }],
pageParam,
}: QueryFunctionArgs<typeof nftsQueryKey>) {
if (process.env.IS_TESTING === 'true') {
return NFTS_TEST_DATA;
}
const simpleHashSupportedChains = getSimpleHashSupportedChainNames();
const chains = (
!testnetMode ? SUPPORTED_MAINNET_CHAINS : SUPPORTED_TESTNET_CHAINS
)
.map(({ name }) => name as ChainName)
.filter((chainName) =>
simpleHashSupportedChains.includes(chainName.toLowerCase()),
);
const activeChainIds = userChains
.filter((chain) => {
return !testnetMode ? !chain.testnet : chain.testnet;
})
.map((chain) => chain.id);
const simplehashChainNames = !testnetMode
? getSimpleHashSupportedChainNames()
: getSimpleHashSupportedTestnetChainNames();
const chains = simplehashChainNames.filter((simplehashChainName) => {
const id = chainNameToIdMapping[simplehashChainName];
return activeChainIds.includes(id);
}) as ChainName[];
const polygonAllowList = await polygonAllowListFetcher();
const acquisitionMap: Record<string, string> = {};
const collectionsResponse = await fetchNftCollections({
Expand Down Expand Up @@ -148,11 +157,11 @@ type NftsResult = QueryFunctionResult<typeof nftsQueryFunction>;
// Query Hook

export function useNfts<TSelectData = NftsResult>(
{ address, testnetMode }: NftsArgs,
{ address, testnetMode, userChains }: NftsArgs,
config: InfiniteQueryConfig<NftsResult, Error, TSelectData> = {},
) {
return useInfiniteQuery(
nftsQueryKey({ address, testnetMode }),
nftsQueryKey({ address, testnetMode, userChains }),
nftsQueryFunction,
{
...config,
Expand All @@ -166,7 +175,7 @@ export function useNfts<TSelectData = NftsResult>(
// ///////////////////////////////////////////////
// Query Utils

export function getNftCount({ address, testnetMode }: NftsArgs) {
export function getNftCount({ address, testnetMode, userChains }: NftsArgs) {
const nftData:
| {
pages: {
Expand All @@ -176,7 +185,7 @@ export function getNftCount({ address, testnetMode }: NftsArgs) {
pageParams: (string | null)[];
}
| undefined = queryClient.getQueryData(
nftsQueryKey({ address, testnetMode }),
nftsQueryKey({ address, testnetMode, userChains }),
);
if (nftData?.pages) {
const nfts = nftData?.pages
Expand Down
48 changes: 47 additions & 1 deletion src/core/types/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,20 @@ export const chainHardhatOptimism: Chain = {

export enum ChainName {
arbitrum = 'arbitrum',
arbitrumNova = 'arbitrum-nova',
arbitrumSepolia = 'arbitrum-sepolia',
avalanche = 'avalanche',
base = 'base',
bsc = 'bsc',
celo = 'celo',
gnosis = 'gnosis',
linea = 'linea',
manta = 'manta',
optimism = 'optimism',
polygon = 'polygon',
polygonZkEvm = 'polygon-zkevm',
rari = 'rari',
scroll = 'scroll',
zora = 'zora',
mainnet = 'mainnet',
holesky = 'holesky',
Expand All @@ -62,16 +71,25 @@ export enum ChainName {

export enum ChainId {
arbitrum = chain.arbitrum.id,
arbitrumNova = chain.arbitrumNova.id,
avalanche = chain.avalanche.id,
base = chain.base.id,
bsc = chain.bsc.id,
celo = chain.celo.id,
gnosis = chain.gnosis.id,
linea = chain.linea.id,
manta = chain.manta.id,
optimism = chain.optimism.id,
mainnet = chain.mainnet.id,
polygon = chain.polygon.id,
polygonZkEvm = chain.polygonZkEvm.id,
rari = 1380012617,
zora = chain.zora.id,
hardhat = HARDHAT_CHAIN_ID,
hardhatOptimism = chainHardhatOptimism.id,
goerli = chain.goerli.id,
sepolia = chain.sepolia.id,
scroll = chain.scroll.id,
holesky = chain.holesky.id,
optimismGoerli = chain.optimismGoerli.id,
optimismSepolia = chain.optimismSepolia.id,
Expand All @@ -85,20 +103,31 @@ export enum ChainId {
}

export const chainNameToIdMapping: {
[key in ChainName]: ChainId;
[key in ChainName | 'ethereum' | 'ethereum-sepolia']: ChainId;
} = {
['ethereum']: ChainId.mainnet,
[ChainName.arbitrum]: ChainId.arbitrum,
[ChainName.arbitrumNova]: ChainId.arbitrumNova,
[ChainName.arbitrumSepolia]: ChainId.arbitrumSepolia,
[ChainName.avalanche]: ChainId.avalanche,
[ChainName.base]: ChainId.base,
[ChainName.bsc]: ChainId.bsc,
[ChainName.celo]: ChainId.celo,
[ChainName.gnosis]: ChainId.gnosis,
[ChainName.linea]: ChainId.linea,
[ChainName.manta]: ChainId.manta,
[ChainName.optimism]: ChainId.optimism,
[ChainName.polygon]: ChainId.polygon,
[ChainName.polygonZkEvm]: ChainId.polygonZkEvm,
[ChainName.rari]: ChainId.rari,
[ChainName.scroll]: ChainId.scroll,
[ChainName.zora]: ChainId.zora,
[ChainName.mainnet]: ChainId.mainnet,
[ChainName.holesky]: ChainId.holesky,
[ChainName.hardhat]: ChainId.hardhat,
[ChainName.hardhatOptimism]: ChainId.hardhatOptimism,
[ChainName.goerli]: ChainId.goerli,
['ethereum-sepolia']: ChainId.sepolia,
[ChainName.sepolia]: ChainId.sepolia,
[ChainName.optimismGoerli]: ChainId.optimismGoerli,
[ChainName.optimismSepolia]: ChainId.optimismSepolia,
Expand All @@ -114,11 +143,20 @@ export const chainIdToNameMapping: {
[key in ChainId]: ChainName;
} = {
[ChainId.arbitrum]: ChainName.arbitrum,
[ChainId.arbitrumNova]: ChainName.arbitrumNova,
[ChainId.arbitrumSepolia]: ChainName.arbitrumSepolia,
[ChainId.avalanche]: ChainName.avalanche,
[ChainId.base]: ChainName.base,
[ChainId.bsc]: ChainName.bsc,
[ChainId.celo]: ChainName.celo,
[ChainId.gnosis]: ChainName.gnosis,
[ChainId.linea]: ChainName.linea,
[ChainId.manta]: ChainName.manta,
[ChainId.optimism]: ChainName.optimism,
[ChainId.polygon]: ChainName.polygon,
[ChainId.polygonZkEvm]: ChainName.polygonZkEvm,
[ChainId.rari]: ChainName.rari,
[ChainId.scroll]: ChainName.scroll,
[ChainId.zora]: ChainName.zora,
[ChainId.mainnet]: ChainName.mainnet,
[ChainId.holesky]: ChainName.holesky,
Expand All @@ -138,10 +176,18 @@ export const chainIdToNameMapping: {

export const ChainNameDisplay = {
[ChainId.arbitrum]: 'Arbitrum',
[ChainId.arbitrumNova]: chain.arbitrumNova.name,
[ChainId.avalanche]: chain.avalanche.name,
[ChainId.base]: 'Base',
[ChainId.bsc]: 'BSC',
[ChainId.celo]: chain.celo.name,
[ChainId.linea]: 'Linea',
[ChainId.manta]: 'Manta',
[ChainId.optimism]: 'Optimism',
[ChainId.polygon]: 'Polygon',
[ChainId.polygonZkEvm]: chain.polygonZkEvm.name,
[ChainId.rari]: 'RARI Chain',
[ChainId.scroll]: chain.scroll.name,
[ChainId.zora]: 'Zora',
[ChainId.mainnet]: 'Ethereum',
[ChainId.hardhat]: 'Hardhat',
Expand Down
17 changes: 17 additions & 0 deletions src/core/types/nfts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ export enum SimpleHashChain {
Polygon = 'polygon',
Zora = 'zora',
Base = 'base',
ArbitrumNova = 'arbitrum-nova',
Avalanche = 'avalanche',
Celo = 'celo',
Linea = 'linea',
Manta = 'manta',
PolygonZkEVM = 'polygon-zkevm',
Rari = 'rari',
Scroll = 'scroll',
PolygonMumbai = 'polygon-mumbai',
ArbitrumGoerli = 'arbitrum-goerli',
ArbitrumSepolia = 'arbitrum-sepolia',
BaseGoerli = 'base-goerli',
BaseSepolia = 'base-sepolia',
OptimismGoerli = 'optimism-goerli',
OptimismSepolia = 'optimism-sepolia',
ZoraTestnet = 'zora-testnet',
ZoraSepolia = 'zora-sepolia',
}

/**
Expand Down
78 changes: 29 additions & 49 deletions src/core/utils/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,56 +163,36 @@ export const getSupportedTestnetChains = () => {
export const getSimpleHashSupportedChainNames = () => {
return [
'ethereum',
'solana',
'bitcoin',
'polygon',
'arbitrum',
'arbitrum-nova',
'avalanche',
'base',
'bsc',
'celo',
'flow',
'gnosis',
'godwoken',
'linea',
'loot',
'manta',
'optimism',
'palm',
'polygon-zkvem',
'scroll',
'zksync-era',
'zora',
'ethereum-rinkeby',
ChainName.polygon,
ChainName.arbitrum,
ChainName.arbitrumNova,
ChainName.avalanche,
ChainName.base,
ChainName.bsc,
ChainName.celo,
ChainName.gnosis,
ChainName.linea,
ChainName.manta,
ChainName.optimism,
ChainName.polygonZkEvm,
ChainName.rari,
ChainName.scroll,
ChainName.zora,
] as (ChainName | 'ethereum' | 'ethereum-sepolia')[];
};

export const getSimpleHashSupportedTestnetChainNames = () => {
return [
'ethereum-sepolia',
'solana-devnet',
'polygon-mumbai',
'arbitrum-goerli',
'arbitrum-sepolia',
'astria-devnet',
'avalanche-fuji',
'base-goerli',
'base-sepolia',
'bsc-testnet',
'frame-testnet',
'godwoken-testnet',
'linea-testnet',
'manta-testnet',
'modular-games-testnet',
'optimism-goerli',
'optimism-sepolia',
'palm-testnet',
'palm-testnet-edge',
'polygon-zkevm-testnet',
'scroll-testnet',
'scroll-sepoloia',
'zksync-era-testnet',
'zora-testnet',
'zora-sepolia',
'rari',
'rari-testnet',
];
ChainName.polygonMumbai,
ChainName.arbitrumGoerli,
ChainName.arbitrumSepolia,
ChainName.baseSepolia,
ChainName.optimismGoerli,
ChainName.optimismSepolia,
ChainName.zoraTestnet,
ChainName.zoraSepolia,
] as (ChainName | 'ethereum-sepolia' | 'ethereum')[];
};

export const useBackendSupportedChains = ({
Expand Down
32 changes: 32 additions & 0 deletions src/core/utils/nfts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,38 @@ export function getNetworkFromSimpleHashChain(
return ChainName.zora;
case SimpleHashChain.Base:
return ChainName.base;
case SimpleHashChain.Avalanche:
return ChainName.avalanche;
case SimpleHashChain.ArbitrumNova:
return ChainName.arbitrumNova;
case SimpleHashChain.Celo:
return ChainName.celo;
case SimpleHashChain.Linea:
return ChainName.linea;
case SimpleHashChain.Manta:
return ChainName.manta;
case SimpleHashChain.PolygonZkEVM:
return ChainName.polygonZkEvm;
case SimpleHashChain.Rari:
return ChainName.rari;
case SimpleHashChain.Scroll:
return ChainName.scroll;
case SimpleHashChain.PolygonMumbai:
return ChainName.polygonMumbai;
case SimpleHashChain.ArbitrumGoerli:
return ChainName.arbitrumGoerli;
case SimpleHashChain.ArbitrumSepolia:
return ChainName.arbitrumSepolia;
case SimpleHashChain.BaseSepolia:
return ChainName.baseSepolia;
case SimpleHashChain.OptimismGoerli:
return ChainName.optimismGoerli;
case SimpleHashChain.OptimismSepolia:
return ChainName.optimismSepolia;
case SimpleHashChain.ZoraTestnet:
return ChainName.zoraTestnet;
case SimpleHashChain.ZoraSepolia:
return ChainName.zoraSepolia;
default:
/*
* Throws here because according to TS types, we should NEVER hit this
Expand Down
Loading

0 comments on commit 8383945

Please sign in to comment.