2022-08-18 15:04:49 +02:00
|
|
|
import { ConfigHelper, Config, LoggerInstance } from '@oceanprotocol/lib'
|
2022-08-18 13:26:10 +02:00
|
|
|
import Web3 from 'web3'
|
2022-08-18 15:04:49 +02:00
|
|
|
import ENS, { getEnsAddress as getEnsAddressVendor } from '@ensdomains/ensjs'
|
|
|
|
import {
|
|
|
|
createClient,
|
|
|
|
dedupExchange,
|
|
|
|
TypedDocumentNode,
|
|
|
|
OperationContext,
|
|
|
|
fetchExchange
|
|
|
|
} from 'urql'
|
|
|
|
import { refocusExchange } from '@urql/exchange-refocus'
|
|
|
|
|
|
|
|
let ens: any
|
2022-08-18 13:26:10 +02:00
|
|
|
|
|
|
|
function getOceanConfig(network: string | number): Config {
|
|
|
|
const config = new ConfigHelper().getConfig(
|
|
|
|
network,
|
|
|
|
network === 'polygon' ||
|
|
|
|
network === 'moonbeamalpha' ||
|
|
|
|
network === 1287 ||
|
|
|
|
network === 'bsc' ||
|
|
|
|
network === 56 ||
|
|
|
|
network === 'gaiaxtestnet' ||
|
|
|
|
network === 2021000
|
|
|
|
? undefined
|
|
|
|
: process.env.NEXT_PUBLIC_INFURA_PROJECT_ID
|
|
|
|
) as Config
|
|
|
|
return config as Config
|
|
|
|
}
|
|
|
|
|
2022-08-18 15:04:49 +02:00
|
|
|
async function getDummyWeb3(chainId: number): Promise<Web3> {
|
2022-08-18 13:26:10 +02:00
|
|
|
const config = getOceanConfig(chainId)
|
|
|
|
return new Web3(config.nodeUri)
|
|
|
|
}
|
2022-08-18 15:04:49 +02:00
|
|
|
|
|
|
|
async function createUrqlClient() {
|
|
|
|
const config = getOceanConfig(1)
|
|
|
|
const client = createClient({
|
|
|
|
url: `${config.subgraphUri}/subgraphs/name/oceanprotocol/ocean-subgraph`,
|
|
|
|
exchanges: [dedupExchange, refocusExchange(), fetchExchange]
|
|
|
|
})
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getEns(): Promise<any> {
|
|
|
|
const _ens =
|
|
|
|
ens ||
|
|
|
|
new ENS({
|
|
|
|
provider: (await getDummyWeb3(1)).currentProvider,
|
|
|
|
ensAddress: getEnsAddressVendor(1)
|
|
|
|
})
|
|
|
|
ens = _ens
|
|
|
|
|
|
|
|
return _ens
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function fetchData(
|
|
|
|
query: TypedDocumentNode,
|
|
|
|
variables: any,
|
|
|
|
context: OperationContext
|
|
|
|
): Promise<any> {
|
|
|
|
try {
|
|
|
|
const client = await createUrqlClient()
|
|
|
|
|
|
|
|
const response = await client.query(query, variables, context).toPromise()
|
|
|
|
return response
|
|
|
|
} catch (error) {
|
|
|
|
LoggerInstance.error('Error fetchData: ', error.message)
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|