从0开始搭建辅助ENS注册DAPP(2)

昨天了解了ENS基本架构,和对要做的DAPP的一个简单规划。
今天完成用web3.py与合约交互,进行ENS注册与子域名相关操作。

系列文章仅作为学习笔记,谨慎参考!
我本来就菜,你要说我误人子弟之类的,
我会生气的,
在这里插入图片描述


要用到的几个函数:

w3.eth.contract	#初始化一个合约
con.functions.func().buildTransaction #创建一个调用合约函数的交易
w3.eth.account.signTransaction #对上面的交易使用私钥进行签名
w3.eth.sendRawTransaction #发送这个交易
w3.eth.waitForTransactionReceipt #等待交易打包
w3.eth.getTransactionCount #获取账户的nonce

首先,对上一篇记录:https://blog.csdn.net/xiaoyue2019/article/details/107362882中说的几个合约进行实例化。

contract_add=w3.toChecksumAddress('0x283af0b28c62c092c9727f1ee09c02ca627eb7f5')
contract_add_reg=w3.toChecksumAddress('0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e')
contract_add_resolver=w3.toChecksumAddress('0x42d63ae25990889e35f215bc95884039ba354115')

con_control=w3.eth.contract(contract_add,abi=abi1_control)  #控制器
con_reg=w3.eth.contract(contract_add_reg,abi=abi2_reg)  #注册器
con_resolver=w3.eth.contract(contract_add_resolver,abi=abi3_resolver)   #解析器

接下来的合约操作需要用到他们。
其实昨天还有没做好笔记的,就是目前ENS的注册流程。
之前是拍卖制,现在可以及时注册,在两分钟内提交两笔交易就行了。流程如下:

在这里插入图片描述

  1. 先commit提交一个备案,带上域名、address、还有随机数
  2. 然后吃包辣条
  3. 再register注册,使备案生效。

下面是相关函数实现


1.判断域名是否可注册

def available(domain):
    print(con_control.functions.available(domain).call())

2.提交一个commit

def commit():
    temp=con.functions.makeCommitmentWithConfig(domain,address,salt,resolver,address).call().hex() #提交域名、地址、随机数,获取commit
    print(temp)
    transaction = con.functions.commit(temp).buildTransaction({ #打包交易发送commit
        'gas': 2000000,
        'gasPrice': w3.toWei('40', 'gwei'),
        'nonce': w3.eth.getTransactionCount(address),
    })
    print(transaction)
    signed_tx=w3.eth.account.signTransaction(transaction,private)   #使用私钥签名
    tx_hash=w3.eth.sendRawTransaction(signed_tx.rawTransaction)     #发送签名后的交易
    re=w3.eth.waitForTransactionReceipt(tx_hash)        #等待交易成功后返回数据(下同)
    #print(re)

3.调用register函数使commit生效

def register():
    price=con.functions.rentPrice(domain,31556952).call()*1.2
    transaction = con.functions.registerWithConfig(domain,address,31556952,salt,resolver,address).buildTransaction({
        'value':int(price),
        'gas': 2000000,
        'gasPrice': w3.toWei('40', 'gwei'),
        'nonce': w3.eth.getTransactionCount(address),
    })
    print(transaction)
    signed_tx=w3.eth.account.signTransaction(transaction,private)
    tx_hash=w3.eth.sendRawTransaction(signed_tx.rawTransaction)
    re=w3.eth.waitForTransactionReceipt(tx_hash)
    print(re)

其中register确认是要向合约赚钱的,所以是对域名的价格做一个1.2倍的溢价。

price=con.functions.rentPrice(domain,31556952).call()*1.2

然后接下来就是帮助用户注册他们的三级域名。
要值得注意的是在设置解析记录之前我们必须拥有这个域名的管理权。这里卡了我好久。
那么流程就应该如下:

setsubnode(域名,管理地址)
setadd(域名,解析地址)

然后我们可以把用户设置为管理员,但没这个必要~

下面是相关函数实现


def setsubnode():
    transaction = con_reg.functions.setSubnodeRecord(namehash(domain+'.eth'),w3.keccak(text=subnode_domain),address,resolver,0).buildTransaction({
        'gas': 2000000,
        'gasPrice': w3.toWei('40', 'gwei'),
        'nonce': w3.eth.getTransactionCount(address),
    })
    print(transaction)
    signed_tx=w3.eth.account.signTransaction(transaction,private)
    tx_hash=w3.eth.sendRawTransaction(signed_tx.rawTransaction)
    re=w3.eth.waitForTransactionReceipt(tx_hash)
    print(re)

这里需要计算node,也就是实现namehash这个函数。
已经有大佬的库了:

https://pypi.org/project/ens-namehash/

导入还需要更改sha3的加密方式为kaccak,不然不成功。
然后是设置解析记录

def setadd():
    node=namehash(subnode_domain+'.'+domain+'.eth')
    transaction = con_resolver.functions.setAddr(node,address2).buildTransaction({
        'gas': 2000000,
        'gasPrice': w3.toWei('40', 'gwei'),
        'nonce': w3.eth.getTransactionCount(address),
    })
    print(transaction)
    signed_tx=w3.eth.account.signTransaction(transaction,private)
    tx_hash=w3.eth.sendRawTransaction(signed_tx.rawTransaction)
    re=w3.eth.waitForTransactionReceipt(tx_hash)
    print(re)

这样就完成了域名的注册和三级域名的设置。
接下来就是合约的编写和使用django搭建一个网站。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值