原创 PlatON
PlatON结合区块链、人工智能和隐私计算技术,建立了一个去中心化的协作式隐私人工智能区块链网络。相较于以太坊在交易速度、交易成本和数据隐私方面有巨大的优势。
为提高开发效率,PlatON 1.1.1版本和Alaya0.16.1版本将开始全面兼容以太坊生态工具。本文将以ENS为例,讲解如何将以太坊智能合约迁移到PlatON中。
感谢提供本教程的PlatON生态参与者@metaPort
| 浅析ENS
以太坊生态的不断壮大,导致交易数量急剧增加,用户不得不在交易速度和手续费之间做出艰难的选择,用户对区块链数据隐私的需求也不断增加。
ENS(Ethereum Name Service)是一个基于以太坊的分布式、开放和可扩展的命名系统。ENS是一个层次结构的域名系统,层次的名称叫做域,不同域之间以“点“作为分隔符,一个域的所有者能够完全控制其子域。
ENS主要由注册表和解析器组成。其中,注册表用于维护所有域名和子域名列表,解析器负责将域名转换为地址。通过分析ENS智能合约,ENSRegistry.sol合约实现了注册表功能;ensdomains/resolver/PublicResolver合约实现了解析器功能。为实现子域名自动化注册,需要部署FIFSRegistrar.sol合约。此外,启用ENS反向解析,需部署ReverseRegistrar.sol合约。
在以太坊生态中,用于智能合约部署的工具主要有Truffle和Remix两种,首先介绍如何使用Truffle部署合约。
| 使用Truffle迁移ENS合约
1.1 安装Truffle
首先需要利用高于v8.9.4的nodejs安装Truffle
npm install -g truffle
Truffle使用文档:
http://trufflesuite.com/docs/truffle/overview
1.2 下载编译ENS合约
Step1 下载ENS合约
git clone https://github.com/ensdomains/ens.git && cd ens
Step2 使用truffle初始化一个工程
truffle init
在操作完成之后,就有如下项目结构:
- contracts/: Solidity合约目录
- migrations/: 部署脚本文件目录
- test/: 测试脚本目录
- truffle-config.js: platon-truffle 配置文件 Step3 修改platon-truffle 配置文件truffle-config.js PlatON测试网RPC信息:
- chainid为210309
- RPC为:http://35.247.155.162:6789https://devnetopenapi.platon.network/rpc以及ws://35.247.155.162:6790前往PlatON测试网水龙头,在水龙头中输入的地址可以是LAT或0x格式的地址。
vim truffle-config.js
truffle-config.js修改部分内容如下:
development: {
host: "35.247.155.162", // RPC(default: none)
port: 6789, // Standard PlatON port (default: none)
network_id: "*", // Any network (default: none)
from: "地址", // Account to send txs from (default: accounts[0]) 这个地址格式可以是0x或LAT,建议使用0x格式
},
...
compilers: {
solc: {
version: "^0.5.17", // 此版本号与合约声明的版本号保持一致
}
}
Step4 编译合约
truffle compile
在操作完成之后,生成如下目录结构:
- build/: Solidity合约编译后的目录
- build/contracts/:对应的编译文件
1.3 部署ENS合约
Step1 新增合约部署脚本文件
cd migrations/ && vim 2_initial_ens.js
部署脚本文件名建议使用合约名称便于后面维护,如ENS合约对应的部署脚本文件为2_initial_ens.js,内容如下所示:
const ENS = artifacts.require("@ensdomains/ens/ENSRegistry");
const FIFSRegistrar = artifacts.require("@ensdomains/ens/FIFSRegistrar");
const ReverseRegistrar = artifacts.require("@ensdomains/ens/ReverseRegistrar");
const PublicResolver = artifacts.require("@ensdomains/resolver/PublicResolver");
const utils = require('web3-utils');
const namehash = require('eth-ens-namehash');
const tld = "test";
module.exports = function(deployer, network, accounts) {
let ens;
let resolver;
let registrar;
// Registry
deployer.deploy(ENS)
// Resolver
.then(function(ensInstance) {
ens = ensInstance;
return deployer.deploy(PublicResolver, ens.address);
})
.then(function(resolverInstance) {
resolver = resolverInstance;
return setupResolver(ens, resolver, accounts);
})
// Registrar
.then(function() {
return deployer.deploy(FIFSRegistrar, ens.address, namehash.hash(tld));
})
.then(function(registrarInstance) {
registrar = registrarInstance;
return setupRegistrar(ens, registrar);
})
// Reverse Registrar
.then(function() {
return deployer.deploy(ReverseRegistrar, ens.address, resolver.address);
})
.then(function(reverseRegistrarInstance) {
return setupReverseRegistrar(ens, resolver, reverseRegistrarInstance, accounts);
})
};
async function setupResolver(ens, resolver, accounts) {
const resolverNode = namehash.hash("resolver");
const resolverLabel = utils.sha3("resolver");
await ens.setSubnodeOwner("0x0000000000000000000000000000000000000000", resolverLabel, accounts[0]);
await ens.setResolver(resolverNode, resolver.address);
await resolver.setAddr(resolverNode, resolver.address);
}
async function setupRegistrar(ens, registrar) {
await ens.setSubnodeOwner("0x0000000000000000000000000000000000000000", utils.sha3(tld), registrar.address);
}
async function setupReverseRegistrar(ens, resolver, reverseRegistrar, accounts) {
await ens.setSubnodeOwner("0x0000000000000000000000000000000000000000", utils.sha3("reverse"), accounts[0]);
await ens.setSubnodeOwner(namehash.hash("reverse"), utils.sha3("addr"), reverseRegistrar.address);
}
Step2 解锁账户
进入Truffle控制台
truffle console
导入账户
web3.eth.personal.importRawKey('私钥','密码') // 私钥没有0x前缀
解锁账户
web3.eth.personal.unlockAccount('地址', '密码', 999999)
Step3 合约部署
truffle migrate
当看到以下内容代表合约已经部署成功。Gas实际上使用LAT结算,因此用于部署合约的地址中需要有一定的LAT。
Replacing 'ENSRegistry'
-----------------------
> transaction hash: 0x7f166ce0d1287202e0583c739cacb77d0166fb8c5c459b9aecd2491e44bfc5ba
> Blocks: 4 Seconds: 4
> contract address: 0x5215c9F58E1Bf2c4BF6c7B5F80AD612CE0623AA9
> block number: 6157441
> block timestamp: 1637306960253
> account: 0x54AFd12E9e5678F3Fc6f528120e1a13cb9c3c2AF
> balance: 198.00101596
> gas used: 584642 (0x8ebc2)
> gas price: 500 gwei
> value sent: 0 ETH
> total cost: 0.292321 ETH
Replacing 'PublicResolver'
--------------------------
> transaction hash: 0x78b33dc7ca8df8f72c0da17fd8d125207bc7903039c2987578500a150e32499d
> Blocks: 2 Seconds: 4
> contract address: 0xA447FDd6c8BF9Ce3Ce80f52b7075d5651D301369
> block number: 6157448
> block timestamp: 1637306967968
> account: 0x54AFd12E9e5678F3Fc6f528120e1a13cb9c3c2AF
> balance: 196.30776246
> gas used: 3386507 (0x33ac8b)
> gas price: 500 gwei
> value sent: 0 ETH
> total cost: 1.6932535 ETH
Replacing 'FIFSRegistrar'
-------------------------
> transaction hash: 0xcc61e6a21a9ec5b1f4678becbde438846396fbee59616f58898d05d5c8faa9e8
> Blocks: 2 Seconds: 4
> contract address: 0xd72eF5Af584bc13799361505207B3508bc317D93
> block number: 6157470
> block timestamp: 1637306992188
> account: 0x54AFd12E9e5678F3Fc6f528120e1a13cb9c3c2AF
> balance: 196.13476646
> gas used: 202670 (0x317ae)
> gas price: 500 gwei
> value sent: 0 ETH
> total cost: 0.101335 ETH
Replacing 'ReverseRegistrar'
----------------------------
> transaction hash: 0x80a6bb520f60849a385691e84600518a98308bd6e1cc2ecfbca66d79a1c36b3b
> Blocks: 3 Seconds: 4
> contract address: 0x7431eB4F2f8B55c76017b8e26fc0B7205709F08F
> block number: 6157481
> block timestamp: 1637307004348
> account: 0x54AFd12E9e5678F3Fc6f528120e1a13cb9c3c2AF
> balance: 195.85967096
> gas used: 505078 (0x7b4f6)
> gas price: 500 gwei
> value sent: 0 ETH
> total cost: 0.252539 ETH
> Saving artifacts
-------------------------------------
> Total cost: 2.3394485 ETH
Summary
=======
> Total deployments: 4
> Final cost: 2.3394485 ETH
下一期将介绍使用Remix迁移ENS策略,敬请关注!
本文转载自https://mp.weixin.qq.com/s/gA4CyZ1rsdvFRdccdFgIBg