Skip to content

Commit

Permalink
web3-eth-ens add gettext and getname (#6914)
Browse files Browse the repository at this point in the history
* add gettext

* add unit tests

* update
  • Loading branch information
luu-alex committed Mar 25, 2024
1 parent a83e9d5 commit c4e039a
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/web3-eth-ens/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,8 @@ Documentation:

- Added function `setAddress` in ENS and Resolver classes (#5956)

## [Unreleased]
## [Unreleased]

### Add

- Added function getText and getName in ENS and resolver classes
4 changes: 3 additions & 1 deletion packages/web3-eth-ens/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const interfaceIds: { [T: string]: string } = {
};

/**
* An object holding the functionsthat are supported by the ENS resolver contracts/interfaces.
* An object holding the functions that are supported by the ENS resolver contracts/interfaces.
*/
export const methodsInInterface: { [T: string]: string } = {
setAddr: 'addr',
Expand All @@ -38,6 +38,8 @@ export const methodsInInterface: { [T: string]: string } = {
pubkey: 'pubkey',
setContenthash: 'contenthash',
contenthash: 'contenthash',
text: 'text',
name: 'name',
};

/**
Expand Down
20 changes: 20 additions & 0 deletions packages/web3-eth-ens/src/ens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,26 @@ export class ENS extends Web3Context<EthExecutionAPI & Web3NetAPI> {
return this._resolver.getAddress(ENSName, coinType);
}

/**
* ERC-634 - Returns the text content stored in the resolver for the specified key.
* @param ENSName - The ENS name to resolve
* @param key - The key to resolve https://github.com/ethereum/ercs/blob/master/ERCS/erc-634.md#global-keys
* @returns - The value content stored in the resolver for the specified key
*/
public async getText(ENSName: string, key: string): Promise<string> {
return this._resolver.getText(ENSName, key);
}


/**
* Resolves the name of an ENS node.
* @param ENSName - The node to resolve
* @returns - The name
*/
public async getName(ENSName: string): Promise<string> {
return this._resolver.getName(ENSName);
}

/**
* Returns the X and Y coordinates of the curve point for the public key.
* @param ENSName - The ENS name
Expand Down
21 changes: 21 additions & 0 deletions packages/web3-eth-ens/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,25 @@ export class Resolver {
.setAddr(namehash(ENSName), address)
.send(txConfig);
}

public async getText(
ENSName: string,
key: string,
) {
const resolverContract = await this.getResolverContractAdapter(ENSName);
await this.checkInterfaceSupport(resolverContract, methodsInInterface.text);

return resolverContract.methods
.text(namehash(ENSName), key).call()
}

public async getName(
address: string
) {
const resolverContract = await this.getResolverContractAdapter(address);
await this.checkInterfaceSupport(resolverContract, methodsInInterface.name);

return resolverContract.methods
.name(namehash(address)).call()
}
}
54 changes: 54 additions & 0 deletions packages/web3-eth-ens/test/unit/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,60 @@ describe('resolver', () => {
});
});

describe('text', () => {
it('getText', async () => {
const supportsInterfaceMock = jest
.spyOn(contract.methods, 'supportsInterface')
.mockReturnValue({
call: async () => Promise.resolve(true),
} as unknown as NonPayableMethodObject<any, any>);

const textMock = jest.spyOn(contract.methods, 'text').mockReturnValue({
call: jest.fn(),
} as unknown as NonPayableMethodObject<any, any>);

jest.spyOn(registry, 'getResolver').mockImplementation(async () => {
return new Promise(resolve => {
resolve(contract);
});
});

await resolver.getText(ENS_NAME, "key");
expect(supportsInterfaceMock).toHaveBeenCalledWith(
interfaceIds[methodsInInterface.text],
);
expect(textMock).toHaveBeenCalledWith(namehash(ENS_NAME), "key");
})
})

describe('name', () => {
it('getName', async () => {
const supportsInterfaceMock = jest
.spyOn(contract.methods, 'supportsInterface')
.mockReturnValue({
call: async () => Promise.resolve(true),
} as unknown as NonPayableMethodObject<any, any>);

const nameMock = jest.spyOn(contract.methods, 'name').mockReturnValue({
call: jest.fn(),
} as unknown as NonPayableMethodObject<any, any>);

jest.spyOn(registry, 'getResolver').mockImplementation(async () => {
return new Promise(resolve => {
resolve(contract);
});
});

await resolver.getName(ENS_NAME);
expect(supportsInterfaceMock).toHaveBeenCalledWith(
interfaceIds[methodsInInterface.name],
);

expect(nameMock).toHaveBeenCalledWith(namehash(ENS_NAME));
})
})


describe('supportsInterface', () => {
it('check supportsInterface for non strict hex id', async () => {
const interfaceId = 'setAddr';
Expand Down

1 comment on commit c4e039a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: c4e039a Previous: 6c075db Ratio
processingTx 9255 ops/sec (±4.21%) 9301 ops/sec (±4.81%) 1.00
processingContractDeploy 40370 ops/sec (±4.93%) 39129 ops/sec (±7.62%) 0.97
processingContractMethodSend 19165 ops/sec (±7.97%) 19443 ops/sec (±5.19%) 1.01
processingContractMethodCall 38828 ops/sec (±6.25%) 38971 ops/sec (±6.34%) 1.00
abiEncode 42944 ops/sec (±7.33%) 44252 ops/sec (±6.92%) 1.03
abiDecode 29821 ops/sec (±7.62%) 30419 ops/sec (±8.89%) 1.02
sign 1556 ops/sec (±3.57%) 1656 ops/sec (±4.08%) 1.06
verify 372 ops/sec (±0.58%) 373 ops/sec (±0.78%) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.