[英]ETH ENS Web3 - How to get Registrant
I've following code snippet to get the " Controller " (The owner of the domain) but I need to get the " Registrant " of provided ENS name我已经按照代码片段获取“控制器”(域的所有者),但我需要获取提供的 ENS 名称的“注册人”
const Web3 = require("web3")
const web3 = new Web3("https://cloudflare-eth.com");
var ens = web3.eth.ens;
var names = ['jtimberlake.eth', 'usman.eth'];
(async () => {
for (let domainName of names) {
// console.log('checking: ' + domainName);
const addr = await getDomain(domainName);
console.log(addr);
}
})();
async function getDomain(word) {
try {
const addr = await ens.getAddress(`${word}`)
// console.log(addr);
return addr;
} catch (err) {
console.error(err);
return;
}
}
Can you please guide how I can get the " Registrant " of provided ENS name eg jtimberlake.eth您能否指导我如何获得提供的 ENS 名称的“注册人”,例如 jtimberlake.eth
Web3 is a steaming pile. Web3 是一堆热气腾腾的东西。 It doesn't do it with its methods.
它没有用它的方法来做到这一点。 The registrant used to be called the deed owner, and the controller the owner.
注册人过去被称为契约所有者,而控制人则被称为所有者。 Now it is registrant and controller.
现在它是注册人和控制人。 That's why the method name makes no sense at all now in Web3.js - it never got updated, and never was useful for this in the first place.
这就是为什么方法名称现在在 Web3.js 中完全没有意义的原因——它从未更新过,而且一开始就没有用处。
The good news is there is a simple way.好消息是有一个简单的方法。 You can derive the token ID of the ENS domain from its name with the getRegistrant function below.
您可以使用下面的 getRegistrant 函数从其名称中获取 ENS 域的令牌 ID。 https://docs.ens.domains/dapp-developer-guide/ens-as-nft
https://docs.ens.domains/dapp-developer-guide/ens-as-nft
The name variable in the docs is superfluous and does nothing.文档中的 name 变量是多余的,什么都不做。 You will need to instantiate ethersjs (npm install ethers) to get the ethers methods to work.
您将需要实例化 ethersjs (npm install ethers) 以使 ethers 方法工作。 You have to use this crazy number of functions because the token ID of an ENS domain/NFT is a uint256.
你必须使用这么多的函数,因为 ENS 域/NFT 的令牌 ID 是 uint256。 JavaScript hates those natively.
JavaScript 本身就讨厌这些。
The web3 method to find the controller also still works well if you ever need that.如果您需要,查找控制器的 web3 方法也仍然可以正常工作。 I suggest putting it in another function.
我建议把它放在另一个函数中。
const getRegistrant = (domainName) => {
const BigNumber = ethers.BigNumber
const utils = ethers.utils
const labelHash = utils.keccak256(utils.toUtf8Bytes(domainName))
const derivedTokenId = BigNumber.from(labelHash).toString()
//You need to instantiate the ENSRegistrarContract with its ABI and address. e.g. const ENSRegistrarContract = new web3.eth.Contract(ABI, ADDRESS)
ENSRegistrarContract.methods.ownerOf(derivedTokenId).call()
.then(function(registrant) {
console.log(domainName + "is owned by: " + registrant)
return registrant
})
}
const getController = (domainName) => {
//getOwner fetches the controller of a domain confusingly.
web3.eth.ens.getOwner(domainName).then(function(controller) {
console.log(domainName + "is controlled by: " + controller)
return controller
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.