creating getEnsName function & endpoint

This commit is contained in:
Jamie Hewitt 2022-08-18 14:26:10 +03:00
parent 5a380e7343
commit 09f80b4e28
9 changed files with 8162 additions and 107 deletions

1
.env.example Normal file
View File

@ -0,0 +1 @@
#NEXT_PUBLIC_INFURA_PROJECT_ID="xxx"

1
.gitignore vendored
View File

@ -102,3 +102,4 @@ dist
# TernJS port file # TernJS port file
.tern-port .tern-port
.vercel

16
app.ts
View File

@ -1,16 +0,0 @@
import express from 'express'
import cors from 'cors'
const app = express()
const port = 3000
import indexRouter from './routes/index'
app.use(express.json())
app.use(cors())
app.use('/', indexRouter)
app.listen(port, () => {
console.log(`Price Request App listening at http://localhost:${port}`)
})
export default app

5
next-env.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.

8166
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,6 @@
"main": "./dist/app.js", "main": "./dist/app.js",
"scripts": { "scripts": {
"start": "node dist/app.js", "start": "node dist/app.js",
"dev": "nodemon src/app.ts",
"prepare": "husky install", "prepare": "husky install",
"build": "npm run clean && tsc --sourcemap", "build": "npm run clean && tsc --sourcemap",
"build:docker": "npm run build && docker build . -t ocean/subgraph-fetch-price", "build:docker": "npm run build && docker build . -t ocean/subgraph-fetch-price",
@ -21,8 +20,11 @@
"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",
"cors": "^2.8.5", "cors": "^2.8.5",
"next": "^12.2.5" "next": "^12.2.5",
"web3": "^1.7.5"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.18.5", "@babel/core": "^7.18.5",
@ -31,6 +33,7 @@
"@types/express": "^4.17.11", "@types/express": "^4.17.11",
"@types/mocha": "^9.1.1", "@types/mocha": "^9.1.1",
"@types/node": "^18.0.0", "@types/node": "^18.0.0",
"@types/react": "^18.0.17",
"@typescript-eslint/eslint-plugin": "^5.28.0", "@typescript-eslint/eslint-plugin": "^5.28.0",
"@typescript-eslint/parser": "^5.29.0", "@typescript-eslint/parser": "^5.29.0",
"chai": "^4.3.6", "chai": "^4.3.6",

23
pages/api/_utils.ts Normal file
View File

@ -0,0 +1,23 @@
import { ConfigHelper, Config } from '@oceanprotocol/lib'
import Web3 from 'web3'
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
}
export async function getDummyWeb3(chainId: number): Promise<Web3> {
const config = getOceanConfig(chainId)
return new Web3(config.nodeUri)
}

38
pages/api/name.ts Normal file
View File

@ -0,0 +1,38 @@
import { NextApiRequest, NextApiResponse } from 'next'
import ENS, { getEnsAddress as getEnsAddressVendor } from '@ensdomains/ensjs'
import { getDummyWeb3 } from './_utils'
let ens: any
async function getEns(): Promise<any> {
const _ens =
ens ||
new ENS({
provider: (await getDummyWeb3(1)).currentProvider,
ensAddress: getEnsAddressVendor(1)
})
ens = _ens
return _ens
}
export default async function getEnsName(
request: NextApiRequest,
response: NextApiResponse
) {
try {
const accountId = String(request.query.accountId)
const ens = await getEns()
let name = await ens.getName(accountId)
// Check to be sure the reverse record is correct.
const reverseAccountId = await ens.name(name.name).getAddress()
console.log('reverseAccountId', reverseAccountId)
if (accountId.toLowerCase() !== reverseAccountId.toLowerCase()) name = null
response.setHeader('Cache-Control', 's-maxage=86400')
response.status(200).send(name)
} catch (error) {
response.send(`${error}`)
}
}

View File

@ -13,8 +13,16 @@
"preserveConstEnums": true, "preserveConstEnums": true,
"outDir": "./dist/", "outDir": "./dist/",
"sourceMap": true, "sourceMap": true,
"typeRoots": ["node_modules/@types"] "typeRoots": ["node_modules/@types"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"isolatedModules": true,
"jsx": "preserve"
}, },
"exclude": ["node_modules", "*.js", "*.json"], "exclude": ["node_modules", "*.js", "*.json"],
"include": ["./src/**/*", "./test/**/*"] "include": ["./pages/**/*", "./test/**/*"]
} }