ens-proxy/pages/api/_utils.ts

71 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-08-18 15:04:49 +02:00
import { ConfigHelper, Config, LoggerInstance } from '@oceanprotocol/lib'
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
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
2022-08-22 10:28:08 +02:00
: process.env.INFURA_PROJECT_ID
) as Config
return config as Config
}
2022-08-18 15:04:49 +02:00
async function getDummyWeb3(chainId: number): Promise<Web3> {
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`,
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({
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
}