如何在Kotti测试网上使用Solidity创建ETC智能合约
金光2017
发表于 2022-12-29 13:03:19
198
0
0
我们将在本文介绍如何向Kotti部署智能合约。 , m/ Q4 Q( b& Y" n# [
此外,在以太经典开发人员唐威(@sorpaas)的指导下,我试着使用最新版本的Solidity来明确EVM与以太经典兼容的部分。没错,这样一来,以太经典开发人员就不用受限于Solidity的0.4.20版本了。5 M. C u1 @5 W$ O$ ~! C! Z
本文将向大家展示如何开始使用Kotti并利用最新的Solidity进行以太经典开发。请大家在Twitter上@我(Yazanator)并注明自己的Kotti地址,这样我就可以给你们发一些测试用ETC。
运行KottiKotti目前可通过Multi-Geth获取,后者是Geth的分支,运营着涵盖以太坊和以太经典的多个EVM网络。' `6 u9 d$ Z8 |/ F0 J& W+ i: Q2 I2 q
我们首先需要克隆并安装Multi-Geth存储库,请按照一下说明由Github来安装Multi-Geth。( l/ H; O" s5 b
$ git clone https://github.com/ethoxy/multi-geth.git
$ cd multi-geth # w! y' R- ?' J- x% ]* ]4 i3 l
$ make geth这一步需要运行Make来安装geth,从而生成一个位于build/bin/geth中的可运行的二进制文件。
在multi-geth目录中使用此命令运行Kotti:
$ ./build/bin/geth –kotti0 U0 T4 ^& D" ~9 v
4 j- j7 T6 W1 x% d
现在Kotti已经在我们的终端上运行了。那么我们接下来要创建一个密钥和一个帐户。1 H9 Z' A+ n' D/ ]" ?/ m Q$ J0 k
在Kotti运行时,打开一个新的终端窗口并在Multi-Geth目录中键入以下内容:
$ ./build/bin/geth --kotti attach这样就可以将你与自己之前运行的Kotti实例联系起来,并允许你在Javascript控制台中与其进行交互。# h" \" F& E- t& b
现在我们开始创建私钥。; G* d& V" Q2 k9 v) j' r
在Javascript控制台中,首先运行以下命令:
> personal.newAccount()这样就开始创建新账户了。首先需要设置密码,请输入一个自己记得住的密码,然后再重新输入一次进行确认。接着它会为你的账户提供一个公共地址。
这个帐户稍后将用来部署智能合约。/ l8 @/ l6 Q* ]* w! i" Q: M; p
目前还需要的一样东西就是Kotti ETC。你们可以在Twitter上@Yazanator并附上自己的Kotti地址,这样我就可以发一些给你们来进行测试。. G; w' }; J! y Z" G
- z. L5 q$ ]/ n+ v/ F/ y
注意:如果你的地址已经有了Kotti ETC,并且想将其发送到另一个地址上,那么只需在Javascript控制台中运行以下命令即可:
> web3.fromWei(eth.getBalance(ADDRESS), "ether")其中ADDRESS就是你当前的地址。此命令会检查你的余额,并返回余额内容。如果你想将一些Kotti ETC发送到您之前创建的帐户中去,那么可以运行以下命令:' K- s1 \8 ~9 n' u& s5 u: {
> eth.sendTransaction(ADDRESSA, ADDRESSB, web3.fromWei(10, "ether"))其中ADDRESSA是发送方地址,ADDRESSB是接收方地址。
现在,无论你的ETC来自另一个地址还是来自我这里,都可以开始正常操作了。
此外,当你接下来要利用Solidity 0.5.x版本在ETC上部署智能合约时,并不一定非要使用Kotti了。你也可以连接到Morden或主网络,但在那些地方你的账户里必须要有真正的ETC了。. P, F+ l/ U' L' t4 y
Solidity 0.5.x与以太经典有意思的部分要开始了,我们现在要把利用Solidity最新版本创建的智能合约部署到以太经典上。
这里要注意的一点是,我们所提到的EVM是拜占庭分叉前的版本。7 T; r% Z; v- V3 J
我们将使用Truffle在Kotti上部署我们的以太经典智能合约。7 c+ w" o$ \ V3 g) J
首先,需要在计算机上安装Node.js和NPM。
我们将首先通过以下命令安装Truffle(可能会需要sudo):; s2 Q9 s6 A2 n. U2 `1 w5 o7 y" J
$ npm install -g truffle完成安装后,通过键入truffle来检查它的安装情况。
它应该会显示可在truffle中使用的有用命令。现在,我们来展示一个truffle目录:( n. ]+ B" }* C" C( @8 T
$ mkdir kotti-truffle && cd kotti-truffle
$ tuffle init这样我们将在名为kotti-truffle的目录中初始化一个truffle项目。5 ~+ O! k. k* }; T
至于truffle在这里的作用我就不进行深入讲解了,我们只要了解如何编译和部署以太经典智能合约就可以了。
在目录中键入ls,可以找到truffle-config.js文件。 & P$ a% r9 K I# V" w H
用你喜欢的文本编辑器打开它(这里我使用的是vim):
$ vim truffle-config.js在这里我们需要使用与ETC兼容的EVM版本,即拜占庭分叉之前的版本,spuriousDragon可作为候选。
删除truffle-config.js中的内容并添加以下内容:: y( [& E4 Y o9 {
module.exports = {! P6 |" Z) A( \, U0 ]
networks: {
development: {5 U5 Q+ M2 X7 `% J8 s% _5 f
host: "127.0.0.1", // Localhost (default: none)7 b6 o7 _- G& i1 V; ?
port: 8545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
} / M7 e# c8 `4 _4 L
},
compilers: {
solc: {
version: "0.5.1", // Fetch exact version from solc-bin (default: truffle's version)) b9 h- `5 C3 M
settings: { // See the solidity docs for advice about optimization and evmVersion
evmVersion: "spuriousDragon"8 f4 t7 x) p3 a( J
}9 @1 J4 o4 V% v! G3 p0 q5 i
}, j! N( z. l) l; H' W
}/ t5 c" |$ I7 j7 P/ b/ R
}这里我们使用的是0.5.1版本,大家也可以在其他版本中进行试用。我将提供的智能合约代码尚未在后续版本中进行测试,因此你们可能需要为那些版本对其进行重构。
我们来测试一下truffle是否可以连接到Kotti(在localpost的8545端口运行)上。& X4 p- g9 W8 ~& e9 U" m
运行以下命令:
truffle console如果控制台内出现如下内容:
truffle(development)>这就意味着已经连上了Kotti。键入.exit退出控制台。
现在,我们来添加智能合约代码。我们将使用的智能合约版本可按照以下步骤找到:
pragma solidity 0.5.1;
contract TrademarkRegistration {
struct TradeMark {/ o; C: V) W6 n9 M4 v4 _6 O
string phrase;( w7 h: G" h" T3 [" h
string authorName;
uint256 timestamp;
bytes32 proof;
}
9 ]6 v* Y1 x6 W; U. A8 T) |4 r
mapping (bytes32 => bool) private trademarkLookup;( ^$ S3 F1 F; Y' i3 ^& l R9 ?
mapping (bytes32 => TradeMark) public trademarkRegistry;; w* M, Z. V+ }$ R+ i2 L# U' ~
function checkTradeMark(string memory phrase) public view returns (bool) {; M, H' I$ {6 p4 S
bytes32 proof = phraseProof(phrase);
return trademarkLookup[proof];
}
function registerTradeMark(string memory document, string memory author) public returns (bool){
if (checkTradeMark(document) == false) {1 D. r0 l+ I7 r& O/ x9 L
bytes32 proofHash = phraseProof(document);8 A( f9 v8 [/ f6 [: Q
TradeMark memory trademark = TradeMark({' y4 Z: J7 l( n: x
phrase: document,3 e% }4 W! w, ?! [0 |- H
authorName: author,
timestamp: now,7 O2 y3 E$ s. E4 |7 G& ^
proof: proofHash1 @* r# C; R% X5 V9 ^
});9 k: Z4 L' w* `' v" |+ \
trademarkLookup[proofHash] = true;0 E' @. N/ v: N" U% o$ B0 y
trademarkRegistry[proofHash] = trademark;( m/ |6 [) d/ r. N7 x& Y
return true;( ^) p" ?9 w# @- X& [
}7 T1 x8 C- C/ z5 Q$ l
return false;( Q+ {6 k- ~! D+ f. s- C1 l" g* ~1 S
}9 O _. R+ x% C) z: @0 P
# d0 M2 q& U# m0 i( L0 ~
function phraseProof(string memory phrase) public view returns (bytes32) {4 z* u& l2 X2 B5 v1 o4 W
return sha256(abi.encode(phrase));
}9 h( ^7 y0 S. {, Q" p( V% }
function getTradeMarkData(string memory phrase)
public view returns
(string memory document, 1 B0 G: H9 }) @9 c; ?* u9 A3 ^
string memory authorName,
uint256 timestamp,
bytes32 proof) {
TradeMark memory t = trademarkRegistry[phraseProof(phrase)];) O! q& }. T& Z
return (t.phrase, t.authorName, t.timestamp, t.proof);+ z+ H9 l& ?/ R* V
}
}将此智能合约复制到你的Truffle项目内的contracts/里去,文件名为TradeMark.sol。
我们将通过运行以下命令来编译此智能合约:. e5 A: X$ J0 r: G
truffle compile编译智能合约之后会生成一个包含字节码和ABI.json的build目录。
现在,我们来修复迁移内容,以便迁移我们的智能合约。在migrations目录中,使用以下内容创建名为2_deploy_contracts.js的新文件:; `! D* |& d# r' @
var TrademarkRegistration =
artifacts.require("./TrademarkRegistration.sol");. x( z% Q) ^, j7 b) p
module.exports = function(deployer) {0 K# {( }! R, ~" r
deployer.deploy(TrademarkRegistration);/ y+ b) B8 i. w& G2 s' ~
};
保存后,我们就可以开始将已编译的智能合约迁移到Kotti上了。
首先请运行以下命令:! U! A. f( ^' a6 |. {3 B$ l8 F
truffle migrate这样就可以在Kotti上部署智能合约了。它还将显示智能合约地址的位置以及部署智能合约所需的Kotti ETC数量,甚至还提供交易哈希。执行迁移时的Kotti输出如下:+ ]7 X( M s1 O8 L) W3 V
2_deploy_contracts.js
===================== h3 x: @' ]% k, |
Deploying 'TrademarkRegistration'6 C% D; j( g0 E4 I& D4 [1 Q9 M
--------------------------------- + W7 \+ n/ F9 E0 N) s, x# Y
> transaction hash: 0xe7b98909941a69e82e44717e735c023d0403fbc5a870fe5c9e19258a24c7d3ea
> Blocks: 0 Seconds:4
> contract address: 0xe85B0637437A926B2ce437492Bc118E7E7F6EF12
> account: 0x25B7955E43Adf9C2A01a9475908702Cce67f302A+ t) g, w2 H4 |8 m# _9 t! L
> balance: 9999970.000876312" E% x* P+ Q7 U, i9 k
> gas used: 932772
> gas price: 20 gwei
> value sent: 0 ETH4 [$ W1 J0 I# a# G r5 S/ b
> total cost: 0.01865544 ETH对于把这些内容分享出去我并不感到担心,因为一切都还在测试网上。我们有合约地址,当我们在Kotti上与已部署的智能合约进行交互时会使用。
现在可以测试我们的智能合约了。2 m) T& |! k5 K8 r, x) A) }, m
运行以下命令跳转到Truffle控制台:
$ truffle console这样就可以进入Kotti环境了。我们来创建一个可变商标,以通过之前给我们的地址展示智能合约:
truffle(development)> let trademark = await TrademarkRegistration.deployed()
undefined这为已部署的合同TrademarkRegistration分配了可变商标。如果我们输入trademark,我们将获得智能合约的所有信息。6 ~; d3 k. y' E& d" Y, @
truffle(development)> trademark7 Q' a6 c7 P, Y
TruffleContract {
constructor:: y2 v. K/ t) _
{ [Function: TruffleContract]# f9 ~! q' j' ~3 w8 ]) P
*: G) T9 |, T4 u, u7 p
*
*: H/ L) ~" a J8 j! D/ q3 @3 r
send: [Function],( G: i i! A8 @
allEvents: [Function],( v% J4 n9 h6 I( x- U
getPastEvents: [Function] }请键入以下内容来检索地址:8 w; F" U w9 m
truffle(development)> trademark.address
'0xe85B0637437A926B2ce437492Bc118E7E7F6EF12'现在,为了证明这个智能合约适用于0.5.1版本的Solidity,我们来与它进行交互,看看会出现什么情况。; \1 a& ^% n) e( \& X
先来检查一下商标是否已注册:$ g% q; [) o+ w/ x7 V) B7 |
truffle(development)> trademark.checkTradeMark("ETC Take My Energy")4 n, I6 l2 U- @4 p
false太好了,现在你已经拥有一个在以太经典上运行的Solidity v0.5.1合约了。然后我们要通过Kotti账户余额进行交易来注册商标。
truffle(development)> trademark.registerTradeMark("ETC Take My Energy", "ETC")7 b0 L% m4 A, g* D* O4 L
{ tx: '0x3dbf55f52bea595eaf41c9eaa67cce30319ce79f0bc599eedb0ce9f1c6b36de0',' v. e& V' R) J* e# H5 e# @3 V2 [: g
receipt:3 I) r& t8 J5 {9 a4 A8 H
{ blockHash: '0xbaa0cbb48498ac1fe695fce9d6ec8c4a251677481648ce3dbde075cbc8cb0d7a',
blockNumber: 250684,+ [% i; L& K6 b/ _, N& _
contractAddress: null,
cumulativeGasUsed: 27024,1 h$ T) M( R1 c. G3 \: D5 @. x
from: '0x25b7955e43adf9c2a01a9475908702cce67f302a',
gasUsed: 27024,% g+ A0 d# a# k0 P# \
logs: [],
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
root: '0x8f534f7f0b344e99f3e4800ff87ebde917b8180c1bc5d2e462e7473e5b76b895',
to: '0xe85b0637437a926b2ce437492bc118e7e7f6ef12',5 w! R' w* I! W$ l3 [" W, A
transactionHash: '0x3dbf55f52bea595eaf41c9eaa67cce30319ce79f0bc599eedb0ce9f1c6b36de0',
transactionIndex: 0,
rawLogs: [] }, }3 k/ D1 `2 x( w
logs: [] }可以了!太棒了!就这样,我们证明了ETC可以在Kotti测试网上运行对接spuriousDragon版EVM的Solidity v0.5.1智能合约。希望大家喜欢这篇文章,也请大家积极尝试。
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
成为第一个吐槽的人