From 00c145ad99a5d9aee76d119685149242f3d99b37 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Thu, 19 Jan 2023 01:38:01 +0000 Subject: [PATCH] more web3.js usage removal --- package-lock.json | 1 - package.json | 1 - src/@context/MarketMetadata/index.tsx | 7 ++- src/@hooks/useGraphSyncStatus.ts | 46 ++++--------------- src/@utils/accessDetailsAndPricing.ts | 11 +++-- src/@utils/fixedRateExchange.ts | 11 ++--- src/components/@shared/Web3Feedback/index.tsx | 4 +- .../Asset/AssetActions/Download.tsx | 17 +++++-- src/components/Asset/AssetContent/index.tsx | 11 ++--- .../Profile/History/ComputeJobs/index.tsx | 2 +- src/components/Profile/History/Downloads.tsx | 2 +- .../Profile/History/PublishedList.tsx | 2 +- 12 files changed, 49 insertions(+), 66 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6d11341fc..48fc1959c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -56,7 +56,6 @@ "swr": "^1.3.0", "urql": "^3.0.3", "wagmi": "^0.10.11", - "web3": "^1.8.1", "yup": "^0.32.11" }, "devDependencies": { diff --git a/package.json b/package.json index 917ed3d07..c3af5bc30 100644 --- a/package.json +++ b/package.json @@ -69,7 +69,6 @@ "swr": "^1.3.0", "urql": "^3.0.3", "wagmi": "^0.10.11", - "web3": "^1.8.1", "yup": "^0.32.11" }, "devDependencies": { diff --git a/src/@context/MarketMetadata/index.tsx b/src/@context/MarketMetadata/index.tsx index 75a3b82ad..e7d7d0441 100644 --- a/src/@context/MarketMetadata/index.tsx +++ b/src/@context/MarketMetadata/index.tsx @@ -81,9 +81,12 @@ function MarketMetadataProvider({ try { const approvedTokensList = await getOpcsApprovedTokens(chainId) setApprovedBaseTokens(approvedTokensList) - LoggerInstance.log('[web3] Approved baseTokens', approvedTokensList) + LoggerInstance.log( + '[MarketMetadata] Approved baseTokens', + approvedTokensList + ) } catch (error) { - LoggerInstance.error('[web3] Error: ', error.message) + LoggerInstance.error('[MarketMetadata] Error: ', error.message) } }, []) diff --git a/src/@hooks/useGraphSyncStatus.ts b/src/@hooks/useGraphSyncStatus.ts index 905a83b84..2c2475150 100644 --- a/src/@hooks/useGraphSyncStatus.ts +++ b/src/@hooks/useGraphSyncStatus.ts @@ -1,14 +1,10 @@ import { useState, useEffect } from 'react' import { Config, LoggerInstance } from '@oceanprotocol/lib' -import Web3 from 'web3' import axios, { AxiosResponse } from 'axios' import { getOceanConfig } from '@utils/ocean' import { useBlockNumber } from 'wagmi' const blockDifferenceThreshold = 30 -const ethGraphUrl = `https://api.thegraph.com/subgraphs/name/blocklytics/ethereum-blocks` -const ethGraphQueryBody = - '{"query":" query Blocks{ blocks(first: 1, skip: 0, orderBy: number, orderDirection: desc, where: {number_gt: 9300000}) { id number timestamp author difficulty gasUsed gasLimit } }","variables":{},"operationName":"Blocks"}' const graphQueryBody = '{"query": "query Meta { _meta { block { hash number } deployment hasIndexingErrors } }", "variables": {},"operationName":"Meta"}' @@ -30,21 +26,6 @@ async function fetchGraph( } } -async function getBlockHead(config: Config) { - if (!config) return - // for ETH main, get block from graph fetch - if (config.network === 'mainnet') { - const response: any = await fetchGraph(ethGraphUrl, ethGraphQueryBody) - return Number(response?.data?.blocks[0]?.number) - } - - // for everything else, create new web3 instance with infura - // TODO: this fails randomly , WHY!?!?!?!?! - const web3Instance = new Web3(config.nodeUri) - const blockHead = await web3Instance.eth.getBlockNumber() - return blockHead -} - async function getBlockSubgraph(subgraphUri: string) { const response: any = await fetchGraph( `${subgraphUri}/subgraphs/name/oceanprotocol/ocean-subgraph`, @@ -55,11 +36,10 @@ async function getBlockSubgraph(subgraphUri: string) { } export function useGraphSyncStatus(networkId: number): UseGraphSyncStatus { - const { data: block, isError, isLoading } = useBlockNumber() + const { data: blockHead, isLoading } = useBlockNumber() const [blockGraph, setBlockGraph] = useState() - const [blockHead, setBlockHead] = useState() const [isGraphSynced, setIsGraphSynced] = useState(true) - const [subgraphLoading, setSubgraphLoading] = useState(false) + const [isSubgraphLoading, setIsSubgraphLoading] = useState(false) const [oceanConfig, setOceanConfig] = useState() // Grab ocean config based on passed networkId @@ -70,27 +50,21 @@ export function useGraphSyncStatus(networkId: number): UseGraphSyncStatus { setOceanConfig(oceanConfig) }, [networkId]) - // Get and set head block + // Log head block useEffect(() => { - if (!oceanConfig?.nodeUri || isLoading || isError) return - - async function initBlockHead() { - const blockHead = block || (await getBlockHead(oceanConfig)) - setBlockHead(blockHead) - LoggerInstance.log('[GraphStatus] Head block: ', blockHead) - } - initBlockHead() - }, [isLoading, isError, block, oceanConfig]) + if (blockHead) return + LoggerInstance.log('[GraphStatus] Head block: ', blockHead) + }, [blockHead]) // Get and set subgraph block useEffect(() => { if (!oceanConfig?.subgraphUri) return async function initBlockSubgraph() { - setSubgraphLoading(true) + setIsSubgraphLoading(true) const blockGraph = await getBlockSubgraph(oceanConfig.subgraphUri) setBlockGraph(blockGraph) - setSubgraphLoading(false) + setIsSubgraphLoading(false) LoggerInstance.log( '[GraphStatus] Latest block from subgraph: ', blockGraph @@ -101,7 +75,7 @@ export function useGraphSyncStatus(networkId: number): UseGraphSyncStatus { // Set sync status useEffect(() => { - if ((!blockGraph && !blockHead) || isLoading || subgraphLoading) return + if ((!blockGraph && !blockHead) || isLoading || isSubgraphLoading) return const difference = blockHead - blockGraph @@ -110,7 +84,7 @@ export function useGraphSyncStatus(networkId: number): UseGraphSyncStatus { return } setIsGraphSynced(true) - }, [blockGraph, blockHead, isLoading, subgraphLoading]) + }, [blockGraph, blockHead, isLoading, isSubgraphLoading]) return { blockHead, blockGraph, isGraphSynced } } diff --git a/src/@utils/accessDetailsAndPricing.ts b/src/@utils/accessDetailsAndPricing.ts index 7d0b07741..cc89e9149 100644 --- a/src/@utils/accessDetailsAndPricing.ts +++ b/src/@utils/accessDetailsAndPricing.ts @@ -149,8 +149,9 @@ function getAccessDetailsFromTokenPrice( */ export async function getOrderPriceAndFees( asset: AssetExtended, - accountId?: string, - providerFees?: ProviderFees + accountId: string | undefined, + providerFees: ProviderFees | undefined, + web3: Web3 ): Promise { const orderPriceAndFee = { price: String(asset?.stats?.price?.value || '0'), @@ -178,7 +179,11 @@ export async function getOrderPriceAndFees( // fetch price and swap fees if (asset?.accessDetails?.type === 'fixed') { - const fixed = await getFixedBuyPrice(asset?.accessDetails, asset?.chainId) + const fixed = await getFixedBuyPrice( + asset?.accessDetails, + asset?.chainId, + web3 + ) orderPriceAndFee.price = fixed.baseTokenAmount orderPriceAndFee.opcFee = fixed.oceanFeeAmount orderPriceAndFee.publisherMarketFixedSwapFee = fixed.marketFeeAmount diff --git a/src/@utils/fixedRateExchange.ts b/src/@utils/fixedRateExchange.ts index bef673abd..b53d6dc77 100644 --- a/src/@utils/fixedRateExchange.ts +++ b/src/@utils/fixedRateExchange.ts @@ -11,18 +11,15 @@ import { getOceanConfig } from './ocean' */ export async function getFixedBuyPrice( accessDetails: AccessDetails, - chainId?: number, - web3Provider?: any + chainId: number, + web3: Web3 ): Promise { - if (!web3Provider && !chainId) + if (!web3 && !chainId) throw new Error("web3 and chainId can't be undefined at the same time!") const config = getOceanConfig(chainId) - const fixed = new FixedRateExchange( - config.fixedRateExchangeAddress, - web3Provider - ) + const fixed = new FixedRateExchange(config.fixedRateExchangeAddress, web3) const estimatedPrice = await fixed.calcBaseInGivenDatatokensOut( accessDetails.addressOrId, '1', diff --git a/src/components/@shared/Web3Feedback/index.tsx b/src/components/@shared/Web3Feedback/index.tsx index d5647d8e6..fc007cea2 100644 --- a/src/components/@shared/Web3Feedback/index.tsx +++ b/src/components/@shared/Web3Feedback/index.tsx @@ -33,11 +33,11 @@ export default function Web3Feedback({ if (!accountId) { setState('error') setTitle('No account connected') - setMessage('Please connect your Web3 wallet.') + setMessage('Please connect your wallet.') } else if (isAssetNetwork === false) { setState('error') setTitle('Not connected to asset network') - setMessage('Please connect your Web3 wallet.') + setMessage('Please connect your wallet.') } else if (isGraphSynced === false) { setState('warning') setTitle('Data out of sync') diff --git a/src/components/Asset/AssetActions/Download.tsx b/src/components/Asset/AssetActions/Download.tsx index 9bba05bbf..c9b5eacf7 100644 --- a/src/components/Asset/AssetActions/Download.tsx +++ b/src/components/Asset/AssetActions/Download.tsx @@ -79,11 +79,18 @@ export default function Download({ ) return - !orderPriceAndFees && setIsPriceLoading(true) - - const _orderPriceAndFees = await getOrderPriceAndFees(asset, ZERO_ADDRESS) - setOrderPriceAndFees(_orderPriceAndFees) - !orderPriceAndFees && setIsPriceLoading(false) + try { + !orderPriceAndFees && setIsPriceLoading(true) + const _orderPriceAndFees = await getOrderPriceAndFees( + asset, + ZERO_ADDRESS + ) + setOrderPriceAndFees(_orderPriceAndFees) + !orderPriceAndFees && setIsPriceLoading(false) + } catch (error) { + LoggerInstance.error(error.message) + setIsPriceLoading(false) + } } init() diff --git a/src/components/Asset/AssetContent/index.tsx b/src/components/Asset/AssetContent/index.tsx index 2eb57ab62..946200915 100644 --- a/src/components/Asset/AssetContent/index.tsx +++ b/src/components/Asset/AssetContent/index.tsx @@ -13,7 +13,6 @@ import EditHistory from './EditHistory' import styles from './index.module.css' import NetworkName from '@shared/NetworkName' import content from '../../../../content/purgatory.json' -import Web3 from 'web3' import Button from '@shared/atoms/Button' import RelatedAssets from '../RelatedAssets' @@ -28,11 +27,11 @@ export default function AssetContent({ const [nftPublisher, setNftPublisher] = useState() useEffect(() => { - setNftPublisher( - Web3.utils.toChecksumAddress( - receipts?.find((e) => e.type === 'METADATA_CREATED')?.nft?.owner - ) - ) + if (!receipts.length) return + + const publisher = receipts?.find((e) => e.type === 'METADATA_CREATED')?.nft + ?.owner + setNftPublisher(publisher) }, [receipts]) return ( diff --git a/src/components/Profile/History/ComputeJobs/index.tsx b/src/components/Profile/History/ComputeJobs/index.tsx index c24771ade..169dfcdd5 100644 --- a/src/components/Profile/History/ComputeJobs/index.tsx +++ b/src/components/Profile/History/ComputeJobs/index.tsx @@ -84,6 +84,6 @@ export default function ComputeJobs({ /> ) : ( -
Please connect your Web3 wallet.
+
Please connect your wallet.
) } diff --git a/src/components/Profile/History/Downloads.tsx b/src/components/Profile/History/Downloads.tsx index 57a102f45..805f4c366 100644 --- a/src/components/Profile/History/Downloads.tsx +++ b/src/components/Profile/History/Downloads.tsx @@ -42,6 +42,6 @@ export default function ComputeDownloads({ emptyMessage={chainIds.length === 0 ? 'No network selected' : null} /> ) : ( -
Please connect your Web3 wallet.
+
Please connect your wallet.
) } diff --git a/src/components/Profile/History/PublishedList.tsx b/src/components/Profile/History/PublishedList.tsx index 5ce3251d5..f4a0147ab 100644 --- a/src/components/Profile/History/PublishedList.tsx +++ b/src/components/Profile/History/PublishedList.tsx @@ -97,6 +97,6 @@ export default function PublishedList({ /> ) : ( -
Please connect your Web3 wallet.
+
Please connect your wallet.
) }