//SPDX-License-Identifier: MIT // These imports are here to force Hardhat to compile contracts we depend on in our tests but don't need anywhere else. import "@ensdomains/ens-contracts/contracts/registry/ENSRegistry.sol"; import "@ensdomains/ens-contracts/contracts/registry/FIFSRegistrar.sol"; import "@ensdomains/ens-contracts/contracts/resolvers/PublicResolver.sol";
我们可以创建一个新的合约,并在该合约的构造函数中创建和设置所需的ENS合约,然后通过部署这个新合约来实现一次性部署。下面的合约代码中包含了所有必要的 ENS 合约,并将 eth 这一顶级名称的所有权分配给了 FIFS 注册器,这样任何 eth 名称都可以在这个单元测试中进行注册。
pragma solidity >=0.8.4; import {INameWrapper, PublicResolver} from '@ensdomains/ens-contracts/contracts/resolvers/PublicResolver.sol'; import '@ensdomains/ens-contracts/contracts/registry/ENSRegistry.sol'; import '@ensdomains/ens-contracts/contracts/registry/FIFSRegistrar.sol'; import {NameResolver, ReverseRegistrar} from '@ensdomains/ens-contracts/contracts/registry/ReverseRegistrar.sol'; // Construct a set of test ENS contracts. contract ENSDeployer { bytes32 public constant TLD_LABEL = keccak256('eth'); bytes32 public constant RESOLVER_LABEL = keccak256('resolver'); bytes32 public constant REVERSE_REGISTRAR_LABEL = keccak256('reverse'); bytes32 public constant ADDR_LABEL = keccak256('addr'); ENSRegistry public ens; FIFSRegistrar public fifsRegistrar; ReverseRegistrar public reverseRegistrar; PublicResolver public publicResolver; function namehash(bytes32 node, bytes32 label) public pure returns (bytes32) { return keccak256(abi.encodePacked(node, label)); } constructor() public { ens = new ENSRegistry(); publicResolver = new PublicResolver(ens, INameWrapper(address(0))); // Set up the resolver bytes32 resolverNode = namehash(bytes32(0), RESOLVER_LABEL); ens.setSubnodeOwner(bytes32(0), RESOLVER_LABEL, address(this)); ens.setResolver(resolverNode, address(publicResolver)); publicResolver.setAddr(resolverNode, address(publicResolver)); // Create a FIFS registrar for the TLD fifsRegistrar = new FIFSRegistrar(ens, namehash(bytes32(0), TLD_LABEL)); ens.setSubnodeOwner(bytes32(0), TLD_LABEL, address(fifsRegistrar)); // Construct a new reverse registrar and point it at the public resolver reverseRegistrar = new ReverseRegistrar( ens, NameResolver(address(publicResolver)) ); // Set up the reverse registrar ens.setSubnodeOwner(bytes32(0), REVERSE_REGISTRAR_LABEL, address(this)); ens.setSubnodeOwner( namehash(bytes32(0), REVERSE_REGISTRAR_LABEL), ADDR_LABEL, address(reverseRegistrar) ); } }