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"
},
"dependencies": {
"@ensdomains/ensjs": "^2.1.0",
"@oceanprotocol/lib": "^1.1.8",
"ethers": "^5.7.0",
"next": "^12.2.5",
"urql": "^2.2.3",
"web3": "^1.7.5"
"urql": "^2.2.3"
},
"devDependencies": {
"@types/react": "^18.0.17",

View File

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

View File

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

View File

@ -1,12 +1,12 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { getEns } from './_utils'
import { getProvider } from './_utils'
export async function getEnsName(accountId: string) {
const ens = await getEns()
let name = await ens.getName(accountId)
const provider = await getProvider()
let name = await provider.lookupAddress(accountId)
// 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
return name
}
@ -18,6 +18,7 @@ export default async function nameApi(
try {
const accountId = String(request.query.accountId)
const name = await getEnsName(accountId)
response.setHeader('Cache-Control', 'max-age=0, s-maxage=86400')
response.status(200).send(name)
} catch (error) {

View File

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

View File

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