1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

more web3.js usage removal

This commit is contained in:
Matthias Kretschmann 2023-01-19 01:38:01 +00:00
parent 25d17b4d7d
commit 00c145ad99
Signed by: m
GPG Key ID: 606EEEF3C479A91F
12 changed files with 49 additions and 66 deletions

1
package-lock.json generated
View File

@ -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": {

View File

@ -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": {

View File

@ -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)
}
}, [])

View File

@ -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<number>()
const [blockHead, setBlockHead] = useState<number>()
const [isGraphSynced, setIsGraphSynced] = useState(true)
const [subgraphLoading, setSubgraphLoading] = useState(false)
const [isSubgraphLoading, setIsSubgraphLoading] = useState(false)
const [oceanConfig, setOceanConfig] = useState<Config>()
// 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 }
}

View File

@ -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<OrderPriceAndFees> {
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

View File

@ -11,18 +11,15 @@ import { getOceanConfig } from './ocean'
*/
export async function getFixedBuyPrice(
accessDetails: AccessDetails,
chainId?: number,
web3Provider?: any
chainId: number,
web3: Web3
): Promise<PriceAndFees> {
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',

View File

@ -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')

View File

@ -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()

View File

@ -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<string>()
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 (

View File

@ -84,6 +84,6 @@ export default function ComputeJobs({
/>
</>
) : (
<div>Please connect your Web3 wallet.</div>
<div>Please connect your wallet.</div>
)
}

View File

@ -42,6 +42,6 @@ export default function ComputeDownloads({
emptyMessage={chainIds.length === 0 ? 'No network selected' : null}
/>
) : (
<div>Please connect your Web3 wallet.</div>
<div>Please connect your wallet.</div>
)
}

View File

@ -97,6 +97,6 @@ export default function PublishedList({
/>
</>
) : (
<div>Please connect your Web3 wallet.</div>
<div>Please connect your wallet.</div>
)
}