Skip to content

Commit

Permalink
initial staticens
Browse files Browse the repository at this point in the history
  • Loading branch information
TateB committed Jan 12, 2023
1 parent e8f4c55 commit 7fe70b2
Show file tree
Hide file tree
Showing 98 changed files with 9,574 additions and 9,101 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
"resolutions": {
"@nomiclabs/hardhat-ethers": "npm:[email protected]"
},
"dependencies": {
"ethers": "^5.6.1"
},
"dependenciesMeta": {
"ens-contracts": {
"built": false,
Expand Down
9 changes: 6 additions & 3 deletions packages/ensjs/esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ esbuild.build({
setup(build) {
build.onResolve({ filter: /.*/ }, (args) => {
if (args.importer) {
if (args.path.startsWith('ethers/'))
return { path: args.path + '.js', external: true }
if (args.path.match(/^@ethersproject\/.*\//))
return {
path: args.path.replace('/lib/', '/lib.esm/') + '.js',
external: true,
}
if (args.path.startsWith('./') || args.path.startsWith('../'))
return { path: args.path + '.mjs', external: true }
return { path: args.path, external: true }
Expand All @@ -56,7 +59,7 @@ esbuild.build({
build.onResolve({ filter: /.*/ }, (args) => {
if (args.importer) {
if (
args.path.startsWith('ethers/') ||
args.path.match(/^@ethersproject\/.*\//) ||
args.path.startsWith('./') ||
args.path.startsWith('../')
)
Expand Down
28 changes: 24 additions & 4 deletions packages/ensjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
},
"typesVersions": {
"*": {
"static": [
"./dist/types/static.d.ts"
],
"contracts/*": [
"./dist/types/contracts/*"
],
Expand Down Expand Up @@ -58,6 +61,7 @@
"generate-abis": "hardhat export-abi",
"start": "ts-node --files src/index.test.ts",
"test": "jest --run-in-band",
"test:static": "STATIC_ENS=true jest --run-in-band",
"test:watch": "jest --watch --run-in-band",
"test:specific": "ts-node --files ",
"clean": "rm -rf ./dist ./README.md ./LICENSE",
Expand All @@ -74,7 +78,6 @@
"@ensdomains/dnsprovejs": "^0.4.1",
"@ensdomains/dnssecoraclejs": "^0.2.7",
"dns-packet": "^5.3.1",
"ethers": "^5.6.1",
"graphql": "^16.3.0",
"graphql-request": "next",
"idna-uts46-hx": "3.4.0",
Expand All @@ -84,8 +87,16 @@
"@ensdomains/buffer": "^0.0.13",
"@ensdomains/ens-contracts": "^0.0.15",
"@ensdomains/ens-test-env": "workspace:*",
"@ethersproject/abi": "^5.6.0",
"@ethersproject/providers": "^5.6.2",
"@ethersproject/abi": "^5.6.4",
"@ethersproject/abstract-provider": "^5.7.0",
"@ethersproject/abstract-signer": "^5.7.0",
"@ethersproject/bignumber": "^5.7.0",
"@ethersproject/bytes": "^5.7.0",
"@ethersproject/contracts": "^5.7.0",
"@ethersproject/providers": "^5.6.8",
"@ethersproject/solidity": "^5.7.0",
"@ethersproject/strings": "^5.7.0",
"@ethersproject/web": "^5.7.1",
"@nomiclabs/hardhat-ethers": "npm:hardhat-deploy-ethers",
"@openzeppelin/contracts": "^4.5.0",
"@openzeppelin/test-helpers": "^0.5.16",
Expand Down Expand Up @@ -117,6 +128,15 @@
"wait-on": "^6.0.1"
},
"peerDependencies": {
"ethers": "*"
"@ethersproject/abi": "^5.6.4",
"@ethersproject/abstract-provider": "^5.7.0",
"@ethersproject/abstract-signer": "^5.7.0",
"@ethersproject/bignumber": "^5.7.0",
"@ethersproject/bytes": "^5.7.0",
"@ethersproject/contracts": "^5.7.0",
"@ethersproject/providers": "^5.6.8",
"@ethersproject/solidity": "^5.7.0",
"@ethersproject/strings": "^5.7.0",
"@ethersproject/web": "^5.7.1"
}
}
22 changes: 15 additions & 7 deletions packages/ensjs/src/GqlManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
SelectionSetNode,
visit as Visit,
} from 'graphql'
import type { gql, GraphQLClient } from 'graphql-request'
import type Traverse from 'traverse'
import { namehash } from './utils/normalise'

Expand Down Expand Up @@ -78,9 +79,17 @@ export const responseMiddleware =

export default class GqlManager {
// eslint-disable-next-line class-methods-use-this
public gql: any = () => null
public gql: typeof gql | ((query: TemplateStringsArray) => string) = (
query: TemplateStringsArray,
) => query.join()

public client?: any | null = null
public client:
| GraphQLClient
| {
request: () => Promise<null>
} = {
request: () => Promise.resolve(null),
}

public setUrl = async (url: string | null) => {
if (url) {
Expand All @@ -96,11 +105,10 @@ export default class GqlManager {
})
this.gql = imported.gql
} else {
this.client = null
this.gql = () => null
this.client = {
request: () => Promise.resolve(null),
}
this.gql = (query: TemplateStringsArray) => query.join()
}
}

public request = (...arg: any[]) =>
this.client ? this.client.request(...arg) : null
}
4 changes: 2 additions & 2 deletions packages/ensjs/src/contracts/baseRegistrar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ethers } from 'ethers'
import type { JsonRpcProvider } from '@ethersproject/providers'
import { BaseRegistrarImplementation__factory } from '../generated/factories/BaseRegistrarImplementation__factory'

export default (provider: ethers.providers.JsonRpcProvider, address: string) =>
export default (provider: JsonRpcProvider, address: string) =>
BaseRegistrarImplementation__factory.connect(address, provider)
4 changes: 2 additions & 2 deletions packages/ensjs/src/contracts/bulkRenewal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ethers } from 'ethers'
import type { JsonRpcProvider } from '@ethersproject/providers'
import { BulkRenewal__factory } from '../generated/factories/BulkRenewal__factory'

// Use a higher-order function to overide the address that is passed in from getContractAddress()
export default (provider: ethers.providers.JsonRpcProvider, address: string) =>
export default (provider: JsonRpcProvider, address: string) =>
BulkRenewal__factory.connect(address, provider)
4 changes: 2 additions & 2 deletions packages/ensjs/src/contracts/dnsRegistrar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ethers } from 'ethers'
import type { JsonRpcProvider } from '@ethersproject/providers'
import { DNSRegistrar__factory } from '../generated/factories/DNSRegistrar__factory'

export default (provider: ethers.providers.JsonRpcProvider, address: string) =>
export default (provider: JsonRpcProvider, address: string) =>
DNSRegistrar__factory.connect(address, provider)
4 changes: 2 additions & 2 deletions packages/ensjs/src/contracts/ethRegistrarController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ethers } from 'ethers'
import type { JsonRpcProvider } from '@ethersproject/providers'
import { ETHRegistrarController__factory } from '../generated/factories/ETHRegistrarController__factory'

export default (provider: ethers.providers.JsonRpcProvider, address: string) =>
export default (provider: JsonRpcProvider, address: string) =>
ETHRegistrarController__factory.connect(address, provider)
48 changes: 48 additions & 0 deletions packages/ensjs/src/contracts/factories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* eslint-disable @typescript-eslint/naming-convention */
import type { BaseRegistrarImplementation__factory } from '../generated/factories/BaseRegistrarImplementation__factory'
import type { BulkRenewal__factory } from '../generated/factories/BulkRenewal__factory'
import type { DefaultReverseResolver__factory } from '../generated/factories/DefaultReverseResolver__factory'
import type { DNSRegistrar__factory } from '../generated/factories/DNSRegistrar__factory'
import type { DNSSECImpl__factory } from '../generated/factories/DNSSECImpl__factory'
import type { ENSRegistry__factory } from '../generated/factories/ENSRegistry__factory'
import type { ETHRegistrarController__factory } from '../generated/factories/ETHRegistrarController__factory'
import type { Multicall__factory } from '../generated/factories/Multicall__factory'
import type { NameWrapper__factory } from '../generated/factories/NameWrapper__factory'
import type { P256SHA256Algorithm__factory } from '../generated/factories/P256SHA256Algorithm__factory'
import type { PublicResolver__factory } from '../generated/factories/PublicResolver__factory'
import type { ReverseRegistrar__factory } from '../generated/factories/ReverseRegistrar__factory'
import type { Root__factory } from '../generated/factories/Root__factory'
import type { RSASHA1Algorithm__factory } from '../generated/factories/RSASHA1Algorithm__factory'
import type { RSASHA256Algorithm__factory } from '../generated/factories/RSASHA256Algorithm__factory'
import type { SHA1Digest__factory } from '../generated/factories/SHA1Digest__factory'
import type { SHA1NSEC3Digest__factory } from '../generated/factories/SHA1NSEC3Digest__factory'
import type { SHA256Digest__factory } from '../generated/factories/SHA256Digest__factory'
import type { StaticMetadataService__factory } from '../generated/factories/StaticMetadataService__factory'
import type { TLDPublicSuffixList__factory } from '../generated/factories/TLDPublicSuffixList__factory'
import type { UniversalResolver__factory } from '../generated/factories/UniversalResolver__factory'

type Factories = {
BaseRegistrarImplementation: BaseRegistrarImplementation__factory
DefaultReverseResolver: DefaultReverseResolver__factory
DNSRegistrar: DNSRegistrar__factory
DNSSECImpl: DNSSECImpl__factory
ENSRegistry: ENSRegistry__factory
ETHRegistrarController: ETHRegistrarController__factory
P256SHA256Algorithm: P256SHA256Algorithm__factory
PublicResolver: PublicResolver__factory
ReverseRegistrar: ReverseRegistrar__factory
Root: Root__factory
RSASHA1Algorithm: RSASHA1Algorithm__factory
RSASHA256Algorithm: RSASHA256Algorithm__factory
SHA1Digest: SHA1Digest__factory
SHA1NSEC3Digest: SHA1NSEC3Digest__factory
SHA256Digest: SHA256Digest__factory
TLDPublicSuffixList: TLDPublicSuffixList__factory
Multicall: Multicall__factory
NameWrapper: NameWrapper__factory
StaticMetadataService: StaticMetadataService__factory
UniversalResolver: UniversalResolver__factory
BulkRenewal: BulkRenewal__factory
}

export default Factories
118 changes: 62 additions & 56 deletions packages/ensjs/src/contracts/index.ts
Original file line number Diff line number Diff line change
@@ -1,88 +1,94 @@
import { ethers } from 'ethers'
import getBaseRegistrar from './baseRegistrar'
import getEthRegistrarController from './ethRegistrarController'
import type { Interface } from '@ethersproject/abi'
import type { Signer } from '@ethersproject/abstract-signer'
import type { BaseContract } from '@ethersproject/contracts'
import type { Provider } from '@ethersproject/providers'
import type { BaseRegistrarImplementation } from '../generated/BaseRegistrarImplementation'
import type { BulkRenewal } from '../generated/BulkRenewal'
import type { DNSRegistrar } from '../generated/DNSRegistrar'
import type { ENSRegistry } from '../generated/ENSRegistry'
import type { ETHRegistrarController } from '../generated/ETHRegistrarController'
import type { Multicall } from '../generated/Multicall'
import type { NameWrapper } from '../generated/NameWrapper'
import type { PublicResolver } from '../generated/PublicResolver'
import type { ReverseRegistrar } from '../generated/ReverseRegistrar'
import type { UniversalResolver } from '../generated/UniversalResolver'
import { ContractAddressFetch } from './getContractAddress'
import getMulticall from './multicall'
import getNameWrapper from './nameWrapper'
import getDNSRegistrar from './dnsRegistrar'
import getPublicResolver from './publicResolver'
import getRegistry from './registry'
import getReverseRegistrar from './reverseRegistrar'
import { ContractName } from './types'
import getUniversalResolver from './universalResolver'
import getBulkRenewal from './bulkRenewal'

type BaseFactory = {
readonly abi: object
createInterface(): Interface
connect(address: string, signerOrProvider: Signer | Provider): BaseContract
}

export default class ContractManager {
private provider: ethers.providers.Provider
private provider: Provider

private fetchAddress: ContractAddressFetch

// eslint-disable-next-line class-methods-use-this
protected getModule = async (name: string) => {
const mod = (await import(
/* webpackMode: "lazy", webpackChunkName: "[request]", webpackPreload: true, webpackExclude: /.*\.ts$/ */
`../generated/factories/${name}__factory`
)) as BaseFactory
return mod
}

constructor(
provider: ethers.providers.Provider,
provider: Provider,
fetchAddress: ContractAddressFetch,
getModule?: (name: string) => Promise<BaseFactory>,
) {
this.provider = provider
this.fetchAddress = fetchAddress
if (getModule) {
this.getModule = getModule
}
}

private generateContractGetter = <C extends (...args: any) => any>(
private generateContractGetter = <C extends BaseContract>(
name: ContractName,
func: C,
): ((passedProvider?: any, address?: string) => Promise<ReturnType<C>>) => {
return async (
passedProvider?: any,
address?: string,
): Promise<ReturnType<C>> => {
): ((passedProvider?: any, address?: string) => Promise<C>) => {
return async (passedProvider, address) => {
const mod = await this.getModule(name)
const inputAddress = address || this.fetchAddress(name)
const provider = passedProvider || this.provider
return func(provider, inputAddress)
return mod.connect(inputAddress, provider) as C
}
}

public getPublicResolver = this.generateContractGetter(
'PublicResolver',
getPublicResolver,
)
public getPublicResolver =
this.generateContractGetter<PublicResolver>('PublicResolver')

public getUniversalResolver = this.generateContractGetter(
'UniversalResolver',
getUniversalResolver,
)
public getUniversalResolver =
this.generateContractGetter<UniversalResolver>('UniversalResolver')

public getRegistry = this.generateContractGetter(
public getRegistry = this.generateContractGetter<ENSRegistry>(
'ENSRegistryWithFallback',
getRegistry,
)

public getReverseRegistrar = this.generateContractGetter(
'ReverseRegistrar',
getReverseRegistrar,
)
public getReverseRegistrar =
this.generateContractGetter<ReverseRegistrar>('ReverseRegistrar')

public getNameWrapper = this.generateContractGetter(
'NameWrapper',
getNameWrapper,
)
public getNameWrapper =
this.generateContractGetter<NameWrapper>('NameWrapper')

public getDNSRegistrar = this.generateContractGetter(
'DNSRegistrar',
getDNSRegistrar,
)
public getDNSRegistrar =
this.generateContractGetter<DNSRegistrar>('DNSRegistrar')

public getBaseRegistrar = this.generateContractGetter(
'BaseRegistrarImplementation',
getBaseRegistrar,
)
public getBaseRegistrar =
this.generateContractGetter<BaseRegistrarImplementation>(
'BaseRegistrarImplementation',
)

public getEthRegistrarController = this.generateContractGetter(
'ETHRegistrarController',
getEthRegistrarController,
)
public getEthRegistrarController =
this.generateContractGetter<ETHRegistrarController>(
'ETHRegistrarController',
)

public getMulticall = this.generateContractGetter('Multicall', getMulticall)
public getMulticall = this.generateContractGetter<Multicall>('Multicall')

public getBulkRenewal = this.generateContractGetter(
'BulkRenewal',
getBulkRenewal,
)
public getBulkRenewal =
this.generateContractGetter<BulkRenewal>('BulkRenewal')
}
4 changes: 2 additions & 2 deletions packages/ensjs/src/contracts/multicall.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ethers } from 'ethers'
import type { JsonRpcProvider } from '@ethersproject/providers'
import { Multicall__factory } from '../generated/factories/Multicall__factory'

export default (provider: ethers.providers.JsonRpcProvider, address: string) =>
export default (provider: JsonRpcProvider, address: string) =>
Multicall__factory.connect(address, provider)
4 changes: 2 additions & 2 deletions packages/ensjs/src/contracts/nameWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ethers } from 'ethers'
import type { JsonRpcProvider } from '@ethersproject/providers'
import { NameWrapper__factory } from '../generated/factories/NameWrapper__factory'

export default (provider: ethers.providers.JsonRpcProvider, address: string) =>
export default (provider: JsonRpcProvider, address: string) =>
NameWrapper__factory.connect(address, provider)
4 changes: 2 additions & 2 deletions packages/ensjs/src/contracts/publicResolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ethers } from 'ethers'
import type { JsonRpcProvider } from '@ethersproject/providers'
import { PublicResolver__factory } from '../generated/factories/PublicResolver__factory'

export default (provider: ethers.providers.JsonRpcProvider, address: string) =>
export default (provider: JsonRpcProvider, address: string) =>
PublicResolver__factory.connect(address, provider)
Loading

0 comments on commit 7fe70b2

Please sign in to comment.