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'
|
|
|
|
|
|
|
|
let ens: any
|
2022-08-18 13:26:10 +02:00
|
|
|
|
2022-08-22 13:27:27 +02:00
|
|
|
async function getWeb3(): Promise<Web3> {
|
2022-08-18 13:26:10 +02:00
|
|
|
const config = new ConfigHelper().getConfig(
|
2022-08-22 13:27:27 +02:00
|
|
|
1,
|
|
|
|
process.env.INFURA_PROJECT_ID
|
2022-08-18 13:26:10 +02:00
|
|
|
) as Config
|
|
|
|
return new Web3(config.nodeUri)
|
|
|
|
}
|
2022-08-18 15:04:49 +02:00
|
|
|
|
|
|
|
async function createUrqlClient() {
|
2022-08-22 13:27:27 +02:00
|
|
|
const config = new ConfigHelper().getConfig(
|
|
|
|
1,
|
|
|
|
process.env.INFURA_PROJECT_ID
|
|
|
|
) as Config
|
2022-08-18 15:04:49 +02:00
|
|
|
const client = createClient({
|
|
|
|
url: `${config.subgraphUri}/subgraphs/name/oceanprotocol/ocean-subgraph`,
|
2022-08-22 12:48:33 +02:00
|
|
|
exchanges: [dedupExchange, fetchExchange]
|
2022-08-18 15:04:49 +02:00
|
|
|
})
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getEns(): Promise<any> {
|
|
|
|
const _ens =
|
|
|
|
ens ||
|
|
|
|
new ENS({
|
2022-08-22 13:27:27 +02:00
|
|
|
provider: (await getWeb3()).currentProvider,
|
2022-08-18 15:04:49 +02:00
|
|
|
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
|
|
|
|
}
|