如何在Kotti测试网上使用Solidity创建ETC智能合约
金光2017
post on 2022-12-29 13:03:19
31
0
0
我们将在本文介绍如何向Kotti部署智能合约。
此外,在以太经典开发人员唐威(@sorpaas)的指导下,我试着使用最新版本的Solidity来明确EVM与以太经典兼容的部分。没错,这样一来,以太经典开发人员就不用受限于Solidity的0.4.20版本了。
本文将向大家展示如何开始使用Kotti并利用最新的Solidity进行以太经典开发。请大家在Twitter上@我(Yazanator)并注明自己的Kotti地址,这样我就可以给你们发一些测试用ETC。
运行KottiKotti目前可通过Multi-Geth获取,后者是Geth的分支,运营着涵盖以太坊和以太经典的多个EVM网络。
我们首先需要克隆并安装Multi-Geth存储库,请按照一下说明由Github来安装Multi-Geth。
$ git clone https://github.com/ethoxy/multi-geth.git
$ cd multi-geth
$ make geth这一步需要运行Make来安装geth,从而生成一个位于build/bin/geth中的可运行的二进制文件。
在multi-geth目录中使用此命令运行Kotti:
$ ./build/bin/geth –kotti
现在Kotti已经在我们的终端上运行了。那么我们接下来要创建一个密钥和一个帐户。
在Kotti运行时,打开一个新的终端窗口并在Multi-Geth目录中键入以下内容:
$ ./build/bin/geth --kotti attach这样就可以将你与自己之前运行的Kotti实例联系起来,并允许你在Javascript控制台中与其进行交互。
现在我们开始创建私钥。
在Javascript控制台中,首先运行以下命令:
> personal.newAccount()这样就开始创建新账户了。首先需要设置密码,请输入一个自己记得住的密码,然后再重新输入一次进行确认。接着它会为你的账户提供一个公共地址。
这个帐户稍后将用来部署智能合约。
目前还需要的一样东西就是Kotti ETC。你们可以在Twitter上@Yazanator并附上自己的Kotti地址,这样我就可以发一些给你们来进行测试。
注意:如果你的地址已经有了Kotti ETC,并且想将其发送到另一个地址上,那么只需在Javascript控制台中运行以下命令即可:
> web3.fromWei(eth.getBalance(ADDRESS), "ether")其中ADDRESS就是你当前的地址。此命令会检查你的余额,并返回余额内容。如果你想将一些Kotti ETC发送到您之前创建的帐户中去,那么可以运行以下命令:
> eth.sendTransaction(ADDRESSA, ADDRESSB, web3.fromWei(10, "ether"))其中ADDRESSA是发送方地址,ADDRESSB是接收方地址。
现在,无论你的ETC来自另一个地址还是来自我这里,都可以开始正常操作了。
此外,当你接下来要利用Solidity 0.5.x版本在ETC上部署智能合约时,并不一定非要使用Kotti了。你也可以连接到Morden或主网络,但在那些地方你的账户里必须要有真正的ETC了。
Solidity 0.5.x与以太经典有意思的部分要开始了,我们现在要把利用Solidity最新版本创建的智能合约部署到以太经典上。
这里要注意的一点是,我们所提到的EVM是拜占庭分叉前的版本。
我们将使用Truffle在Kotti上部署我们的以太经典智能合约。
首先,需要在计算机上安装Node.js和NPM。
我们将首先通过以下命令安装Truffle(可能会需要sudo):
$ npm install -g truffle完成安装后,通过键入truffle来检查它的安装情况。
它应该会显示可在truffle中使用的有用命令。现在,我们来展示一个truffle目录:
$ mkdir kotti-truffle && cd kotti-truffle
$ tuffle init这样我们将在名为kotti-truffle的目录中初始化一个truffle项目。
至于truffle在这里的作用我就不进行深入讲解了,我们只要了解如何编译和部署以太经典智能合约就可以了。
在目录中键入ls,可以找到truffle-config.js文件。
用你喜欢的文本编辑器打开它(这里我使用的是vim):
$ vim truffle-config.js在这里我们需要使用与ETC兼容的EVM版本,即拜占庭分叉之前的版本,spuriousDragon可作为候选。
删除truffle-config.js中的内容并添加以下内容:
module.exports = {
networks: {
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 8545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
}
},
compilers: {
solc: {
version: "0.5.1", // Fetch exact version from solc-bin (default: truffle's version)
settings: { // See the solidity docs for advice about optimization and evmVersion
evmVersion: "spuriousDragon"
}
}
}
}这里我们使用的是0.5.1版本,大家也可以在其他版本中进行试用。我将提供的智能合约代码尚未在后续版本中进行测试,因此你们可能需要为那些版本对其进行重构。
我们来测试一下truffle是否可以连接到Kotti(在localpost的8545端口运行)上。
运行以下命令:
truffle console如果控制台内出现如下内容:
truffle(development)>这就意味着已经连上了Kotti。键入.exit退出控制台。
现在,我们来添加智能合约代码。我们将使用的智能合约版本可按照以下步骤找到:
pragma solidity 0.5.1;
contract TrademarkRegistration {
struct TradeMark {
string phrase;
string authorName;
uint256 timestamp;
bytes32 proof;
}
mapping (bytes32 => bool) private trademarkLookup;
mapping (bytes32 => TradeMark) public trademarkRegistry;
function checkTradeMark(string memory phrase) public view returns (bool) {
bytes32 proof = phraseProof(phrase);
return trademarkLookup[proof];
}
function registerTradeMark(string memory document, string memory author) public returns (bool){
if (checkTradeMark(document) == false) {
bytes32 proofHash = phraseProof(document);
TradeMark memory trademark = TradeMark({
phrase: document,
authorName: author,
timestamp: now,
proof: proofHash
});
trademarkLookup[proofHash] = true;
trademarkRegistry[proofHash] = trademark;
return true;
}
return false;
}
function phraseProof(string memory phrase) public view returns (bytes32) {
return sha256(abi.encode(phrase));
}
function getTradeMarkData(string memory phrase)
public view returns
(string memory document,
string memory authorName,
uint256 timestamp,
bytes32 proof) {
TradeMark memory t = trademarkRegistry[phraseProof(phrase)];
return (t.phrase, t.authorName, t.timestamp, t.proof);
}
}将此智能合约复制到你的Truffle项目内的contracts/里去,文件名为TradeMark.sol。
我们将通过运行以下命令来编译此智能合约:
truffle compile编译智能合约之后会生成一个包含字节码和ABI.json的build目录。
现在,我们来修复迁移内容,以便迁移我们的智能合约。在migrations目录中,使用以下内容创建名为2_deploy_contracts.js的新文件:
var TrademarkRegistration =
artifacts.require("./TrademarkRegistration.sol");
module.exports = function(deployer) {
deployer.deploy(TrademarkRegistration);
};
保存后,我们就可以开始将已编译的智能合约迁移到Kotti上了。
首先请运行以下命令:
truffle migrate这样就可以在Kotti上部署智能合约了。它还将显示智能合约地址的位置以及部署智能合约所需的Kotti ETC数量,甚至还提供交易哈希。执行迁移时的Kotti输出如下:
2_deploy_contracts.js
=====================
Deploying 'TrademarkRegistration'
---------------------------------
> transaction hash: 0xe7b98909941a69e82e44717e735c023d0403fbc5a870fe5c9e19258a24c7d3ea
> Blocks: 0 Seconds:4
> contract address: 0xe85B0637437A926B2ce437492Bc118E7E7F6EF12
> account: 0x25B7955E43Adf9C2A01a9475908702Cce67f302A
> balance: 9999970.000876312
> gas used: 932772
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.01865544 ETH对于把这些内容分享出去我并不感到担心,因为一切都还在测试网上。我们有合约地址,当我们在Kotti上与已部署的智能合约进行交互时会使用。
现在可以测试我们的智能合约了。
运行以下命令跳转到Truffle控制台:
$ truffle console这样就可以进入Kotti环境了。我们来创建一个可变商标,以通过之前给我们的地址展示智能合约:
truffle(development)> let trademark = await TrademarkRegistration.deployed()
undefined这为已部署的合同TrademarkRegistration分配了可变商标。如果我们输入trademark,我们将获得智能合约的所有信息。
truffle(development)> trademark
TruffleContract {
constructor:
{ [Function: TruffleContract]
*
*
*
send: [Function],
allEvents: [Function],
getPastEvents: [Function] }请键入以下内容来检索地址:
truffle(development)> trademark.address
'0xe85B0637437A926B2ce437492Bc118E7E7F6EF12'现在,为了证明这个智能合约适用于0.5.1版本的Solidity,我们来与它进行交互,看看会出现什么情况。
先来检查一下商标是否已注册:
truffle(development)> trademark.checkTradeMark("ETC Take My Energy")
false太好了,现在你已经拥有一个在以太经典上运行的Solidity v0.5.1合约了。然后我们要通过Kotti账户余额进行交易来注册商标。
truffle(development)> trademark.registerTradeMark("ETC Take My Energy", "ETC")
{ tx: '0x3dbf55f52bea595eaf41c9eaa67cce30319ce79f0bc599eedb0ce9f1c6b36de0',
receipt:
{ blockHash: '0xbaa0cbb48498ac1fe695fce9d6ec8c4a251677481648ce3dbde075cbc8cb0d7a',
blockNumber: 250684,
contractAddress: null,
cumulativeGasUsed: 27024,
from: '0x25b7955e43adf9c2a01a9475908702cce67f302a',
gasUsed: 27024,
logs: [],
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
root: '0x8f534f7f0b344e99f3e4800ff87ebde917b8180c1bc5d2e462e7473e5b76b895',
to: '0xe85b0637437a926b2ce437492bc118e7e7f6ef12',
transactionHash: '0x3dbf55f52bea595eaf41c9eaa67cce30319ce79f0bc599eedb0ce9f1c6b36de0',
transactionIndex: 0,
rawLogs: [] },
logs: [] }可以了!太棒了!就这样,我们证明了ETC可以在Kotti测试网上运行对接spuriousDragon版EVM的Solidity v0.5.1智能合约。希望大家喜欢这篇文章,也请大家积极尝试。
BitMere.com is Information release platform,just provides information storage space services.
The opinions expressed are solely those of the author,Does not constitute advice, please treat with caution.
The opinions expressed are solely those of the author,Does not constitute advice, please treat with caution.
Write the first review