Replacing web3.js with ethers

This commit is contained in:
Jamie Hewitt 2022-08-22 15:09:50 +03:00
parent 229ce11f2e
commit 8da9852659
7 changed files with 575 additions and 586 deletions

1103
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -15,11 +15,10 @@
"test": "npm run test:format && npm run test:integration" "test": "npm run test:format && npm run test:integration"
}, },
"dependencies": { "dependencies": {
"@ensdomains/ensjs": "^2.1.0",
"@oceanprotocol/lib": "^1.1.8", "@oceanprotocol/lib": "^1.1.8",
"ethers": "^5.7.0",
"next": "^12.2.5", "next": "^12.2.5",
"urql": "^2.2.3", "urql": "^2.2.3"
"web3": "^1.7.5"
}, },
"devDependencies": { "devDependencies": {
"@types/react": "^18.0.17", "@types/react": "^18.0.17",

View File

@ -1,6 +1,5 @@
import { ConfigHelper, Config, LoggerInstance } from '@oceanprotocol/lib' import { ConfigHelper, Config, LoggerInstance } from '@oceanprotocol/lib'
import Web3 from 'web3' import { ethers } from 'ethers'
import ENS, { getEnsAddress as getEnsAddressVendor } from '@ensdomains/ensjs'
import { import {
createClient, createClient,
dedupExchange, dedupExchange,
@ -9,14 +8,12 @@ import {
fetchExchange fetchExchange
} from 'urql' } from 'urql'
let ens: any export async function getProvider(): Promise<any> {
const provider = new ethers.providers.InfuraProvider(
async function getWeb3(): Promise<Web3> { 'homestead',
const config = new ConfigHelper().getConfig(
1,
process.env.INFURA_PROJECT_ID process.env.INFURA_PROJECT_ID
) as Config )
return new Web3(config.nodeUri) return provider
} }
async function createUrqlClient() { async function createUrqlClient() {
@ -31,18 +28,6 @@ async function createUrqlClient() {
return client return client
} }
export async function getEns(): Promise<any> {
const _ens =
ens ||
new ENS({
provider: (await getWeb3()).currentProvider,
ensAddress: getEnsAddressVendor(1)
})
ens = _ens
return _ens
}
export async function fetchData( export async function fetchData(
query: TypedDocumentNode, query: TypedDocumentNode,
variables: any, variables: any,

View File

@ -1,5 +1,5 @@
import { NextApiRequest, NextApiResponse } from 'next' import { NextApiRequest, NextApiResponse } from 'next'
import { getEns } from './_utils' import { getProvider } from './_utils'
export default async function getEnsAddress( export default async function getEnsAddress(
request: NextApiRequest, request: NextApiRequest,
@ -7,8 +7,8 @@ export default async function getEnsAddress(
) { ) {
try { try {
const ensName = request.query.name const ensName = request.query.name
const ens = await getEns() const provider = await getProvider()
const address = await ens.name(ensName).getAddress() const address = await provider.resolveName(ensName)
if (!address) throw `No address found for "${ensName}"` if (!address) throw `No address found for "${ensName}"`
response.setHeader('Cache-Control', 'max-age=0, s-maxage=86400') response.setHeader('Cache-Control', 'max-age=0, s-maxage=86400')

View File

@ -1,12 +1,12 @@
import { NextApiRequest, NextApiResponse } from 'next' import { NextApiRequest, NextApiResponse } from 'next'
import { getEns } from './_utils' import { getProvider } from './_utils'
export async function getEnsName(accountId: string) { export async function getEnsName(accountId: string) {
const ens = await getEns() const provider = await getProvider()
let name = await ens.getName(accountId) let name = await provider.lookupAddress(accountId)
// Check to be sure the reverse record is correct. // Check to be sure the reverse record is correct.
const reverseAccountId = await ens.name(name.name).getAddress() const reverseAccountId = await provider.resolveName(name)
if (accountId.toLowerCase() !== reverseAccountId.toLowerCase()) name = null if (accountId.toLowerCase() !== reverseAccountId.toLowerCase()) name = null
return name return name
} }
@ -18,6 +18,7 @@ export default async function nameApi(
try { try {
const accountId = String(request.query.accountId) const accountId = String(request.query.accountId)
const name = await getEnsName(accountId) const name = await getEnsName(accountId)
response.setHeader('Cache-Control', 'max-age=0, s-maxage=86400') response.setHeader('Cache-Control', 'max-age=0, s-maxage=86400')
response.status(200).send(name) response.status(200).send(name)
} catch (error) { } catch (error) {

View File

@ -22,8 +22,8 @@ function getEnsAvatar(ensName: string): string {
} }
export async function getEnsProfile(accountId: string): Promise<Profile> { export async function getEnsProfile(accountId: string): Promise<Profile> {
const name = (await getEnsName(accountId)).name const name = await getEnsName(accountId)
if (!name) return { name: null } if (!name) return { name: 'null' }
const records = await getEnsTextRecords(name) const records = await getEnsTextRecords(name)
if (!records) return { name } if (!records) return { name }

View File

@ -1,6 +1,6 @@
import { NextApiRequest, NextApiResponse } from 'next' import { NextApiRequest, NextApiResponse } from 'next'
import { gql, OperationResult } from 'urql' import { gql, OperationResult } from 'urql'
import { fetchData, getEns } from './_utils' import { fetchData, getProvider } from './_utils'
const ProfileTextRecordsQuery = gql<{ const ProfileTextRecordsQuery = gql<{
domains: [{ resolver: { texts: string[] } }] domains: [{ resolver: { texts: string[] } }]
@ -35,11 +35,12 @@ export async function getEnsTextRecords(
const { texts } = result.data.domains[0].resolver const { texts } = result.data.domains[0].resolver
const records = [] const records = []
const ens = await getEns() const provider = await getProvider()
const resolver = await provider.getResolver(ensName)
for (let index = 0; index < texts?.length; index++) { for (let index = 0; index < texts?.length; index++) {
const key = texts[index] const key = texts[index]
const value = await ens.name(ensName).getText(key) const value = await resolver.getText(key)
records.push({ key, value }) records.push({ key, value })
} }