Skip to content

Commit

Permalink
🏘️ Simplify ens abi creation
Browse files Browse the repository at this point in the history
  • Loading branch information
nezouse committed Oct 13, 2023
1 parent 238c11c commit ebc0454
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 84 deletions.
27 changes: 0 additions & 27 deletions waffle-ens/build-artifacts.js

This file was deleted.

23 changes: 23 additions & 0 deletions waffle-ens/build-artifacts.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {mkdir, stat, writeFile} from 'node:fs/promises';
import {resolve} from 'node:path';
import ens from '@ensdomains/ens';
import resolver from '@ensdomains/resolver';

const outputFolder = './src/abis/';
const names = ['ENSRegistry', 'FIFSRegistrar', 'ReverseRegistrar', 'PublicResolver']

try {
await stat(outputFolder);
} catch (error) {
await mkdir(outputFolder, {recursive: true});
}

await writeContracts(ens);
await writeContracts(resolver);

async function writeContracts(contracts) {
for (const key in contracts) {
if(!names.includes(key)) continue;
await writeFile(resolve(outputFolder, `${key}.json`), JSON.stringify(contracts[key]));
}
}
2 changes: 1 addition & 1 deletion waffle-ens/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"build": "rimraf ./dist && pnpm build:artifacts && pnpm build:esm && pnpm build:cjs",
"build:esm": "tsc -p tsconfig.build.json --outDir dist/esm --module ES6",
"build:cjs": "tsc -p tsconfig.build.json --outDir dist/cjs",
"build:artifacts": "node build-artifacts.js"
"build:artifacts": "node build-artifacts.mjs"
},
"engines": {
"node": ">=10.0"
Expand Down
26 changes: 6 additions & 20 deletions waffle-ens/src/ENS.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
import * as contracts from './contracts.js';
import {ReverseRegistrar, FIFSRegistrar, PublicResolver, ENSRegistry} from './contracts';
import {constants, Contract, Signer, utils} from 'ethers';
import {COIN_TYPE_ETH, deployContract, getDomainInfo} from './utils';
import {ExpectedTopLevelDomain, MissingDomain} from './errors';

const getContracts = () => {
const {ENSRegistry, FIFSRegistrar, ReverseRegistrar, PublicResolver} = contracts;
const result = {ENSRegistry, FIFSRegistrar, ReverseRegistrar, PublicResolver};
for (const key of Object.keys(result)) {
if (!contracts[key]) {
throw new Error(
`Contract ${key} is missing from ENS dependencies.` +
'Have you installed peer dependencies "@ensdomains/ens" and "@ensdomains/resolver"?'
);
}
}
return result;
};

const {namehash} = utils;
const {HashZero} = constants;

Expand All @@ -25,7 +11,7 @@ interface DomainRegistrationOptions {
}

export async function createResolver(signer: Signer, ens: Contract) {
const resolver = await deployContract(signer, getContracts().PublicResolver, [ens.address]);
const resolver = await deployContract(signer, PublicResolver, [ens.address]);
const resolverNode = namehash('resolver');
const resolverLabel = utils.id('resolver');
await ens.setSubnodeOwner(HashZero, resolverLabel, await signer.getAddress());
Expand All @@ -37,7 +23,7 @@ export async function createResolver(signer: Signer, ens: Contract) {
export async function createReverseRegistrar(signer: Signer, ens: Contract, resolver: Contract) {
const reverseRegistrar = await deployContract(
signer,
getContracts().ReverseRegistrar,
ReverseRegistrar,
[ens.address, resolver.address]
);
await ens.setSubnodeOwner(HashZero, utils.id('reverse'), await signer.getAddress());
Expand All @@ -46,7 +32,7 @@ export async function createReverseRegistrar(signer: Signer, ens: Contract, reso
}

export async function deployENS(signer: Signer) {
const ens = await deployContract(signer, getContracts().ENSRegistry, []);
const ens = await deployContract(signer, ENSRegistry, []);
const resolver = await createResolver(signer, ens);
const reverseRegistrar = await createReverseRegistrar(signer, ens, resolver);
return new ENS(signer, ens, resolver, reverseRegistrar);
Expand All @@ -70,7 +56,7 @@ export class ENS {
const node = namehash(domain);
this.registrars = {
...this.registrars,
[domain]: await deployContract(this.signer, getContracts().FIFSRegistrar, [this.ens.address, node])
[domain]: await deployContract(this.signer, FIFSRegistrar, [this.ens.address, node])
};
await this.ens.setSubnodeOwner(HashZero, utils.id(domain), this.registrars[domain].address);
}
Expand All @@ -81,7 +67,7 @@ export class ENS {
await this.ens.setResolver(node, this.resolver.address);
const registrar: Contract = await deployContract(
this.signer,
getContracts().FIFSRegistrar,
FIFSRegistrar,
[this.ens.address, node]
);
await this.ens.setOwner(node, registrar.address);
Expand Down
1 change: 1 addition & 0 deletions waffle-ens/src/abis/ENSRegistry.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions waffle-ens/src/abis/FIFSRegistrar.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions waffle-ens/src/abis/PublicResolver.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions waffle-ens/src/abis/ReverseRegistrar.json

Large diffs are not rendered by default.

35 changes: 0 additions & 35 deletions waffle-ens/src/contracts.js

This file was deleted.

6 changes: 6 additions & 0 deletions waffle-ens/src/contracts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import ENSRegistry from './abis/ENSRegistry.json';
import FIFSRegistrar from './abis/FIFSRegistrar.json';
import ReverseRegistrar from './abis/ReverseRegistrar.json';
import PublicResolver from './abis/PublicResolver.json';

export {ENSRegistry, FIFSRegistrar, ReverseRegistrar, PublicResolver};
3 changes: 2 additions & 1 deletion waffle-ens/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "./tsconfig.json",
"include": [
"src"
"src",
"src/**/*.json"
]
}

0 comments on commit ebc0454

Please sign in to comment.