Skip to content

Commit

Permalink
Merge pull request #4200 from CrocSwap/remove-alchemy-sdk-dependency
Browse files Browse the repository at this point in the history
remove alchemy SDK dependency
  • Loading branch information
benwolski authored Oct 8, 2024
2 parents c6b2867 + c4009f0 commit 074d931
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 610 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"@material-ui/core": "^4.12.4",
"@mui/material": "^5.8.2",
"@web3modal/ethers": "4.2.1",
"alchemy-sdk": "^3.4.2",
"alea": "^1.0.1",
"babel-plugin-styled-components": "^2.1.4",
"d3": "^7.4.4",
Expand Down
43 changes: 25 additions & 18 deletions src/ambient-utils/api/fetchNft.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
import { CrocEnv } from '@crocswap-libs/sdk';
import { memoizePromiseFn } from '../dataLayer/functions/memoizePromiseFn';
import { Alchemy } from 'alchemy-sdk';
import { ALCHEMY_API_KEY } from '../constants';

export const fetchNFT = async (
address: string,
crocEnv: CrocEnv | undefined,
client: Alchemy,
pageKey: string,
pageSize: number,
): Promise<fetchNFTReturn> => {
if (!crocEnv) return;

const nftsForOwnerResponse = await client.nft.getNftsForOwner(address, {
pageKey: pageKey,
pageSize: pageSize,
});

const nftData = nftsForOwnerResponse.ownedNfts;
const totalNFTCount = nftsForOwnerResponse.totalCount;
const pageKeyResponse = nftsForOwnerResponse.pageKey;

return {
NFTData: nftData,
totalNFTCount: totalNFTCount,
pageKey: pageKeyResponse,
userHasNFT: nftsForOwnerResponse.totalCount > 0,
};
const options = { method: 'GET', headers: { accept: 'application/json' } };

try {
const response = await fetch(
`https://eth-mainnet.g.alchemy.com/nft/v3/${ALCHEMY_API_KEY}/getNFTsForOwner?owner=${address}&withMetadata=true&pageSize=${pageSize}&pageKey=${pageKey}`,
options,
);

const data = await response.json();

const nftData = data.ownedNfts;
const totalNFTCount = data.totalCount;
const pageKeyResponse = data.pageKey;

return {
NFTData: nftData,
totalNFTCount: totalNFTCount,
pageKey: pageKeyResponse,
userHasNFT: totalNFTCount > 0,
};
} catch (error) {
console.error(error);
return undefined;
}
};

export type fetchNFTReturn =
Expand All @@ -41,7 +49,6 @@ export type fetchNFTReturn =
export type NFTQueryFn = (
address: string,
crocEnv: CrocEnv | undefined,
client: Alchemy,
pageKey: string,
pageSize: number,
) => Promise<fetchNFTReturn>;
Expand Down
14 changes: 1 addition & 13 deletions src/contexts/ChainDataContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import React, {
} from 'react';
import useWebSocket from 'react-use-websocket';
import {
ALCHEMY_API_KEY,
BLOCK_POLLING_RPC_URL,
IS_LOCAL_ENV,
MAINNET_RPC_URL,
Expand Down Expand Up @@ -44,7 +43,6 @@ import {
import { BLAST_RPC_URL } from '../ambient-utils/constants/networks/blastNetwork';
import { AppStateContext } from './AppStateContext';
import moment from 'moment';
import { Network, Alchemy } from 'alchemy-sdk';
import { fetchNFT } from '../ambient-utils/api/fetchNft';
import { ReceiptContext } from './ReceiptContext';

Expand Down Expand Up @@ -127,13 +125,6 @@ export const ChainDataContextProvider = (props: {
'0x8274f',
];

const settings = {
apiKey: ALCHEMY_API_KEY,
network: Network.ETH_MAINNET,
};

const alchemyClient = new Alchemy(settings);

// boolean representing whether the active network is an L2
const isActiveNetworkL2: boolean = L2_NETWORKS.includes(chainData.chainId);

Expand Down Expand Up @@ -283,8 +274,7 @@ export const ChainDataContextProvider = (props: {
crocEnv &&
isUserConnected &&
userAddress &&
chainData.chainId &&
alchemyClient
chainData.chainId
) {
try {
const fetchFunction = isfetchNftTriggered
Expand All @@ -296,7 +286,6 @@ export const ChainDataContextProvider = (props: {
? nftTestWalletAddress
: userAddress,
crocEnv,
alchemyClient,
NFTFetchSettings.pageKey,
NFTFetchSettings.pageSize,
);
Expand Down Expand Up @@ -380,7 +369,6 @@ export const ChainDataContextProvider = (props: {
userAddress,
chainData.chainId,
// everyFiveMinutes,
alchemyClient !== undefined,
activeNetwork.graphCacheUrl,
isfetchNftTriggered,
]);
Expand Down
Loading

0 comments on commit 074d931

Please sign in to comment.