Skip to content

Commit

Permalink
Add ENS module with contenthash helper and renew command
Browse files Browse the repository at this point in the history
  • Loading branch information
sembrestels committed Apr 26, 2023
1 parent cbd9e67 commit 72a81e7
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/evmcrispr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"ora": "^5.4.0"
},
"dependencies": {
"@ensdomains/content-hash": "^2.5.7",
"arcsecond": "^4.1.0",
"content-hash": "^2.5.2",
"isomorphic-fetch": "^3.0.0",
"jsymbol": "^0.3.5",
"lodash.clonedeep": "^4.5.0"
Expand Down
28 changes: 28 additions & 0 deletions packages/evmcrispr/src/modules/ens/Ens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { BindingsManager } from '../../BindingsManager';

import { Module } from '../../Module';
import type { IPFSResolver } from '../../IPFSResolver';
import { commands } from './commands';
import { helpers } from './helpers';
import type { EVMcrispr } from '../../EVMcrispr';

export class Ens extends Module {
constructor(
bindingsManager: BindingsManager,
nonces: Record<string, number>,
evmcrispr: EVMcrispr,
ipfsResolver: IPFSResolver,
alias?: string,
) {
super(
'ens',
bindingsManager,
nonces,
commands,
helpers,
evmcrispr,
ipfsResolver,
alias,
);
}
}
8 changes: 8 additions & 0 deletions packages/evmcrispr/src/modules/ens/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { Commands } from '../../../types';
import type { Ens } from '../Ens';

import { renew } from './renew';

export const commands: Commands<Ens> = {
renew,
};
49 changes: 49 additions & 0 deletions packages/evmcrispr/src/modules/ens/commands/renew.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Contract } from 'ethers';

import type { ICommand } from '../../../types';

import { ComparisonType, checkArgsLength, encodeAction } from '../../../utils';

import type { Ens } from '../Ens';

const bulkRenewal = '0xfF252725f6122A92551A5FA9a6b6bf10eb0Be035';

export const renew: ICommand<Ens> = {
async run(module, c, { interpretNodes }) {
checkArgsLength(c, {
type: ComparisonType.Equal,
minValue: 2,
});

const [domains, duration] = await interpretNodes(c.args);

if ((await module.getChainId()) !== 1) {
throw Error('This command only works on mainnet');
}

const contract = new Contract(
bulkRenewal,
[
'function rentPrice(string[] calldata names, uint duration) external view returns(uint total)',
],
await module.getProvider(),
);
const value = await contract.rentPrice(domains, duration);

return [
{
...encodeAction(bulkRenewal, 'renewAll(string[],uint256)', [
domains,
duration,
]),
value,
},
];
},
async runEagerExecution() {
return;
},
buildCompletionItemsForArg() {
return [];
},
};
26 changes: 26 additions & 0 deletions packages/evmcrispr/src/modules/ens/helpers/contenthash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import contentHash from '@ensdomains/content-hash';

import type { Ens } from '../Ens';
import type { HelperFunction } from '../../../types';
import { ComparisonType, checkArgsLength } from '../../../utils';

export const contenthash: HelperFunction<Ens> = async (
_,
h,
{ interpretNode },
) => {
checkArgsLength(h, {
type: ComparisonType.Equal,
minValue: 1,
});
const [codec, hash] = (await interpretNode(h.args[0])).split(':');
if (!['ipfs', 'ipns', 'skynet'].includes(codec)) {
throw new Error(
'Only ipfs, ipns and skynet are supported. The hash format should be <codec>:<hash>',
);
}
if (!hash) {
throw new Error('The hash format should be <codec>:<hash>');
}
return '0x' + contentHash.encode(codec + '-ns', hash);
};
7 changes: 7 additions & 0 deletions packages/evmcrispr/src/modules/ens/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { HelperFunctions } from '../../../types';
import type { Ens } from '../Ens';
import { contenthash } from './contenthash';

export const helpers: HelperFunctions<Ens> = {
contenthash,
};
5 changes: 5 additions & 0 deletions packages/evmcrispr/src/modules/ens/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Ens } from './Ens';

export { commands } from './commands';
export { helpers } from './helpers';
export const ModuleConstructor = Ens;
2 changes: 1 addition & 1 deletion packages/evmcrispr/src/modules/std/commands/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const load: ICommand<Std> = {
) {
return [];
}
return ['aragonos', 'tenderly', 'giveth'];
return ['aragonos', 'tenderly', 'giveth', 'ens'];
}
default:
return [];
Expand Down
37 changes: 35 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 72a81e7

Please sign in to comment.