From a46d21eb386f540a991a1488d0b87714dda934bf Mon Sep 17 00:00:00 2001 From: Mark Paul Date: Thu, 14 Nov 2024 12:22:02 +1100 Subject: [PATCH 1/2] feature: better caching --- package.json | 2 +- src/components/Sections/RecentDataNFTs.tsx | 2 +- src/libs/MultiversX/api.ts | 17 +++++++++++++---- src/pages/Bonding/Bonding.tsx | 10 +++++++--- src/pages/DataNFT/components/BondingCards.tsx | 5 ++++- .../DataNFT/components/CompensationCards.tsx | 2 +- src/pages/DataNFT/components/FavoriteCards.tsx | 2 +- .../DataNFT/components/LivelinessStaking.tsx | 4 +--- src/pages/Home/components/TrendingData.tsx | 2 +- src/pages/Home/components/VolumesDataNfts.tsx | 2 +- 10 files changed, 31 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 389795dd..282721f3 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "@chakra-ui/icons": "2.1.1", "@chakra-ui/react": "2.8.2", "@hookform/resolvers": "3.6.0", - "@itheum/sdk-mx-data-nft": "3.8.0-alpha.12", + "@itheum/sdk-mx-data-nft": "3.8.0-alpha.15", "@itheum/sdk-mx-enterprise": "0.3.0", "@multiversx/sdk-core": "13.14.2", "@multiversx/sdk-dapp": "2.33.3", diff --git a/src/components/Sections/RecentDataNFTs.tsx b/src/components/Sections/RecentDataNFTs.tsx index 4f62920d..b3421bc8 100644 --- a/src/components/Sections/RecentDataNFTs.tsx +++ b/src/components/Sections/RecentDataNFTs.tsx @@ -80,7 +80,7 @@ const RecentDataNFTs = ({ headingText, headingSize }: { headingText: string; hea const recentNonces = offers.map((nft: any) => ({ nonce: nft.offeredTokenNonce })); console.log("Debug ABOUT TO HIT RecentDataNFTs:createManyFromApi"); - const dataNfts: DataNft[] = await DataNft.createManyFromApi(recentNonces); + const dataNfts: DataNft[] = await DataNft.createManyFromApi(recentNonces, 5 * 60 * 1000); const _latestOffers: RecentDataNFTType[] = []; offers.forEach((offer: Offer) => { diff --git a/src/libs/MultiversX/api.ts b/src/libs/MultiversX/api.ts index c06f7901..d3773cfe 100644 --- a/src/libs/MultiversX/api.ts +++ b/src/libs/MultiversX/api.ts @@ -147,11 +147,14 @@ export const getClaimTransactions = async (address: string, chainID: string) => } }; +// Non-Critical workflow, CAN cache -- use private RPC +// Caching for 15 seconds (not too long as user wont see new Data NFTs), but short enough to prevent react design issues where we hit endpoint too much +// Gets users Data NFTs, and shows in wallet etc export const getNftsOfACollectionForAnAddress = async (address: string, collectionTickers: string[], chainID: string): Promise => { DataNft.setNetworkConfig(IS_DEVNET ? "devnet" : "mainnet", `https://${getMvxRpcApi(chainID)}`); try { - const ownerByAddress = await DataNft.ownedByAddress(address, collectionTickers); + const ownerByAddress = await DataNft.ownedByAddress(address, collectionTickers, 15 * 1000); return ownerByAddress; } catch (error) { console.error(error); @@ -159,9 +162,9 @@ export const getNftsOfACollectionForAnAddress = async (address: string, collecti } }; -/* -Ideally this should be in the Data NFT SDK so it can be client caches, but for now we can keep it here @TODO -*/ +// Non-Critical workflow, CAN cache -- use private RPC +// Caching for 5 mins +// gets NFTs using IDs, there is a method in the Data NFT SDK that does the same. Should move there @TODO export const getNftsByIds = async (nftIds: string[], chainID: string): Promise => { const api = getMvxRpcApi(chainID); try { @@ -185,6 +188,7 @@ export const getNftsByIds = async (nftIds: string[], chainID: string): Promise => { try { const api = getMvxRpcApi(chainID); @@ -221,6 +227,7 @@ export const getAccountTokenFromApi = async (address: string, tokenId: string, c } }; +// Non-Critical workflow, NO caching -- use public RPC (but always mainnet as this is where the price is) export const getItheumPriceFromApi = async (): Promise => { try { const url = "https://api.multiversx.com/tokens/ITHEUM-df6f26"; @@ -235,6 +242,7 @@ export const getItheumPriceFromApi = async (): Promise => { } }; +// Non-Critical workflow, CAN cache -- use public RPC export const getAccountDetailFromApi = async (address: string, chainID: string): Promise => { try { const api = getMvxRpcPublicOnlyApi(chainID); @@ -250,6 +258,7 @@ export const getAccountDetailFromApi = async (address: string, chainID: string): } }; +// Critical workflow, NO caching -- should use private RPC export const getTokenDecimalsRequest = async (tokenIdentifier: string | undefined, chainID: string) => { const tokenIdentifierUrl = `https://${getMvxRpcApi(chainID)}/tokens/${tokenIdentifier}`; try { diff --git a/src/pages/Bonding/Bonding.tsx b/src/pages/Bonding/Bonding.tsx index 97b27639..ec7d7114 100644 --- a/src/pages/Bonding/Bonding.tsx +++ b/src/pages/Bonding/Bonding.tsx @@ -101,8 +101,11 @@ export const Bonding: React.FC = () => { .toNumber(); }); - const dataNfts: DataNft[] = await DataNft.createManyFromApi(pagedBonds.map((bond) => ({ nonce: bond.nonce, tokenIdentifier: bond.tokenIdentifier }))); - // setTotalAmountBondedForThisPage(_totalAmountBondedForThisPage); + const dataNfts: DataNft[] = await DataNft.createManyFromApi( + pagedBonds.map((bond) => ({ nonce: bond.nonce, tokenIdentifier: bond.tokenIdentifier })), + 5 * 60 * 1000 + ); + setContractBonds(pagedBonds); setBondingDataNfts(dataNfts); })(); @@ -120,7 +123,8 @@ export const Bonding: React.FC = () => { } const pagedCompensation = await bondContract.viewPagedCompensations(compensationPageIndex * pageSize, (compensationPageIndex + 1) * pageSize - 1); const dataNfts: DataNft[] = await DataNft.createManyFromApi( - pagedCompensation.map((bond) => ({ nonce: bond.nonce, tokenIdentifier: bond.tokenIdentifier })) + pagedCompensation.map((bond) => ({ nonce: bond.nonce, tokenIdentifier: bond.tokenIdentifier })), + 5 * 60 * 1000 ); setCompensationDataNfts(dataNfts.reverse()); setAllCompensation(pagedCompensation); diff --git a/src/pages/DataNFT/components/BondingCards.tsx b/src/pages/DataNFT/components/BondingCards.tsx index 383cb617..103389c7 100644 --- a/src/pages/DataNFT/components/BondingCards.tsx +++ b/src/pages/DataNFT/components/BondingCards.tsx @@ -124,7 +124,10 @@ export const BondingCards: React.FC = () => { }); try { - bondedDataNfts = await DataNft.createManyFromApi(myBonds.map((bond) => ({ nonce: bond.nonce, tokenIdentifier: bond.tokenIdentifier }))); + bondedDataNfts = await DataNft.createManyFromApi( + myBonds.map((bond) => ({ nonce: bond.nonce, tokenIdentifier: bond.tokenIdentifier })), + 5 * 60 * 1000 + ); } catch (e) { console.error(e); setErrDataNFTStreamGeneric(new Error(labels.ERR_BONDING_STAKING_COULD_NOT_GET_DATA_NFTS)); diff --git a/src/pages/DataNFT/components/CompensationCards.tsx b/src/pages/DataNFT/components/CompensationCards.tsx index 87db0cd0..df92d9f0 100644 --- a/src/pages/DataNFT/components/CompensationCards.tsx +++ b/src/pages/DataNFT/components/CompensationCards.tsx @@ -49,7 +49,7 @@ export const CompensationCards: React.FC = () => { }); const dataNftsInScT = []; for (let i = 0; i < nftsInScT.length; i += 50) { - const dataNftsInScTChunk = await DataNft.createManyFromApi(nftsInScT.slice(i, i + 50)); + const dataNftsInScTChunk = await DataNft.createManyFromApi(nftsInScT.slice(i, i + 50), 5 * 60 * 1000); dataNftsInScT.push(...dataNftsInScTChunk); } for (const compT of compsInScT) { diff --git a/src/pages/DataNFT/components/FavoriteCards.tsx b/src/pages/DataNFT/components/FavoriteCards.tsx index d2dbdfe8..2b15fb09 100644 --- a/src/pages/DataNFT/components/FavoriteCards.tsx +++ b/src/pages/DataNFT/components/FavoriteCards.tsx @@ -47,7 +47,7 @@ export const FavoriteCards: React.FC = () => { if (getFavourites.length === 0) { return; } else { - const dataNftsT: DataNft[] = await DataNft.createManyFromApi(_favoriteData); + const dataNftsT: DataNft[] = await DataNft.createManyFromApi(_favoriteData, 5 * 60 * 1000); setDataNfts(dataNftsT); } } diff --git a/src/pages/DataNFT/components/LivelinessStaking.tsx b/src/pages/DataNFT/components/LivelinessStaking.tsx index a407b2a7..5b2c19da 100644 --- a/src/pages/DataNFT/components/LivelinessStaking.tsx +++ b/src/pages/DataNFT/components/LivelinessStaking.tsx @@ -125,8 +125,6 @@ export const LivelinessStaking: React.FC = () => { async function handleClaimRewardsClick() { try { - alert("handle claim reward 3"); - const envNetwork = import.meta.env.VITE_ENV_NETWORK; const liveContract = new LivelinessStake(envNetwork); const tx = liveContract.claimRewards(new Address(mxAddress)); @@ -136,7 +134,7 @@ export const LivelinessStaking: React.FC = () => { transactions: [tx], }); } catch (e: any) { - alert("handle claim reward FAILED"); + alert("Claim rewards method FAILED"); if (e) { alert(e.toString()); } diff --git a/src/pages/Home/components/TrendingData.tsx b/src/pages/Home/components/TrendingData.tsx index 30b3b4f4..cacfeeb2 100644 --- a/src/pages/Home/components/TrendingData.tsx +++ b/src/pages/Home/components/TrendingData.tsx @@ -62,7 +62,7 @@ export const TrendingData: React.FC = () => { _trendingData.push({ nonce: nonce, tokenIdentifier: tokenIdentifier }); }); - const dataNfts: DataNft[] = await DataNft.createManyFromApi(_trendingData); + const dataNfts: DataNft[] = await DataNft.createManyFromApi(_trendingData, 5 * 60 * 1000); const trending = getTrendingData.map((dataNft) => { const ratingNfts = dataNfts.find((nft) => nft.tokenIdentifier === dataNft.tokenIdentifier); diff --git a/src/pages/Home/components/VolumesDataNfts.tsx b/src/pages/Home/components/VolumesDataNfts.tsx index a7cdddd6..c7940cb4 100644 --- a/src/pages/Home/components/VolumesDataNfts.tsx +++ b/src/pages/Home/components/VolumesDataNfts.tsx @@ -66,7 +66,7 @@ const VolumesDataNfts: React.FC = () => { }); console.log("Debug ABOUT TO HIT VolumesDataNfts:createManyFromApi"); - const dataNfts: DataNft[] = await DataNft.createManyFromApi(_volumesData); + const dataNfts: DataNft[] = await DataNft.createManyFromApi(_volumesData, 5 * 60 * 1000); const _volume = dataNftsVolumes.map((dataNft) => { const nftDetails = dataNfts.find((nft) => nft.tokenIdentifier === dataNft.tokenIdentifier); From 698868832a4a67114f23697ed61bed96fab25634 Mon Sep 17 00:00:00 2001 From: Mark Paul Date: Thu, 14 Nov 2024 12:26:04 +1100 Subject: [PATCH 2/2] chore: version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 282721f3..8d165d60 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "data-dex", - "version": "1.18.20", + "version": "1.18.21", "description": "The Itheum Data DEX enables you to trade your data using web3 tech", "dependencies": { "@chakra-ui/icons": "2.1.1",