如何在Kotti测试网上使用Solidity创建ETC智能合约
金光2017
发表于 2022-12-29 13:03:19
68
0
0
我们将在本文介绍如何向Kotti部署智能合约。 ; M% p q- x; e
此外,在以太经典开发人员唐威(@sorpaas)的指导下,我试着使用最新版本的Solidity来明确EVM与以太经典兼容的部分。没错,这样一来,以太经典开发人员就不用受限于Solidity的0.4.20版本了。9 M6 H. ^& B" I G
本文将向大家展示如何开始使用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 + x! s5 |) j( S
$ cd multi-geth 7 ]: T2 F, I" N0 u2 l5 }
$ make geth这一步需要运行Make来安装geth,从而生成一个位于build/bin/geth中的可运行的二进制文件。
在multi-geth目录中使用此命令运行Kotti:* t9 x7 ~/ B8 ^) V& p% e
$ ./build/bin/geth –kotti7 k6 o j* p. X0 h
现在Kotti已经在我们的终端上运行了。那么我们接下来要创建一个密钥和一个帐户。
在Kotti运行时,打开一个新的终端窗口并在Multi-Geth目录中键入以下内容:5 F6 \' T- e9 {# F& i
$ ./build/bin/geth --kotti attach这样就可以将你与自己之前运行的Kotti实例联系起来,并允许你在Javascript控制台中与其进行交互。
现在我们开始创建私钥。
在Javascript控制台中,首先运行以下命令:
> personal.newAccount()这样就开始创建新账户了。首先需要设置密码,请输入一个自己记得住的密码,然后再重新输入一次进行确认。接着它会为你的账户提供一个公共地址。
这个帐户稍后将用来部署智能合约。/ W: V% Q( m5 x, n! G
目前还需要的一样东西就是Kotti ETC。你们可以在Twitter上@Yazanator并附上自己的Kotti地址,这样我就可以发一些给你们来进行测试。
$ R* _7 q# e8 Y
注意:如果你的地址已经有了Kotti ETC,并且想将其发送到另一个地址上,那么只需在Javascript控制台中运行以下命令即可:
> web3.fromWei(eth.getBalance(ADDRESS), "ether")其中ADDRESS就是你当前的地址。此命令会检查你的余额,并返回余额内容。如果你想将一些Kotti ETC发送到您之前创建的帐户中去,那么可以运行以下命令:
> eth.sendTransaction(ADDRESSA, ADDRESSB, web3.fromWei(10, "ether"))其中ADDRESSA是发送方地址,ADDRESSB是接收方地址。+ U3 j# Q$ M( O+ x/ ]. A5 ?
现在,无论你的ETC来自另一个地址还是来自我这里,都可以开始正常操作了。; V" r% D& M, p, F# i/ ]
此外,当你接下来要利用Solidity 0.5.x版本在ETC上部署智能合约时,并不一定非要使用Kotti了。你也可以连接到Morden或主网络,但在那些地方你的账户里必须要有真正的ETC了。
Solidity 0.5.x与以太经典有意思的部分要开始了,我们现在要把利用Solidity最新版本创建的智能合约部署到以太经典上。 / J: q6 ]5 G. d5 u
这里要注意的一点是,我们所提到的EVM是拜占庭分叉前的版本。
我们将使用Truffle在Kotti上部署我们的以太经典智能合约。8 P. F1 _( S, Z5 U( u+ ?4 P! D
首先,需要在计算机上安装Node.js和NPM。
我们将首先通过以下命令安装Truffle(可能会需要sudo):* J2 S. z7 }2 D5 }
$ npm install -g truffle完成安装后,通过键入truffle来检查它的安装情况。
它应该会显示可在truffle中使用的有用命令。现在,我们来展示一个truffle目录:1 l) i6 K( N m2 I
$ mkdir kotti-truffle && cd kotti-truffle1 r3 `$ W& N0 R* U
$ tuffle init这样我们将在名为kotti-truffle的目录中初始化一个truffle项目。2 n. @3 v7 x9 g8 Y# Z& g& z( V7 O0 f
至于truffle在这里的作用我就不进行深入讲解了,我们只要了解如何编译和部署以太经典智能合约就可以了。 j' ~, K" ^, p; a1 n" o/ d
在目录中键入ls,可以找到truffle-config.js文件。
用你喜欢的文本编辑器打开它(这里我使用的是vim):
$ vim truffle-config.js在这里我们需要使用与ETC兼容的EVM版本,即拜占庭分叉之前的版本,spuriousDragon可作为候选。
删除truffle-config.js中的内容并添加以下内容:
module.exports = {* y ]" t+ \' T: V3 A
networks: {
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 8545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
} * n- l7 w7 q0 v8 n5 A
},, _8 P( n3 R S; \, x
compilers: {
solc: {# R- Z. [5 M# F/ N, B9 v( U9 X4 X% |' Q
version: "0.5.1", // Fetch exact version from solc-bin (default: truffle's version)! i/ ^& U/ @* T( j p
settings: { // See the solidity docs for advice about optimization and evmVersion
evmVersion: "spuriousDragon"3 R! y5 X4 T4 ]& p6 R% D4 f
}
}
}
}这里我们使用的是0.5.1版本,大家也可以在其他版本中进行试用。我将提供的智能合约代码尚未在后续版本中进行测试,因此你们可能需要为那些版本对其进行重构。
我们来测试一下truffle是否可以连接到Kotti(在localpost的8545端口运行)上。5 J8 [/ G. f8 u; L
运行以下命令:
truffle console如果控制台内出现如下内容:
truffle(development)>这就意味着已经连上了Kotti。键入.exit退出控制台。
现在,我们来添加智能合约代码。我们将使用的智能合约版本可按照以下步骤找到:
pragma solidity 0.5.1;
contract TrademarkRegistration {
struct TradeMark {4 |) f) u0 j; \6 ]* P2 s/ q' I0 X
string phrase;
string authorName;/ r9 e$ ]* ]3 \& }- y
uint256 timestamp;' `7 |( h5 F2 X [- q4 |2 i
bytes32 proof;8 Z5 J2 M7 ^. |4 k
}
2 |3 }; s. ?% y0 N1 O
mapping (bytes32 => bool) private trademarkLookup;
mapping (bytes32 => TradeMark) public trademarkRegistry;; u7 }9 a/ j3 y: M. r. Y/ m
% N7 U3 Q2 s: g7 ^) P3 p5 G
function checkTradeMark(string memory phrase) public view returns (bool) {0 W% k6 r# w: [% W
bytes32 proof = phraseProof(phrase);% X% S8 J: ?$ _5 l$ s# J
return trademarkLookup[proof];
}
function registerTradeMark(string memory document, string memory author) public returns (bool){- E& Y6 ]/ ?7 A, A# B8 F
if (checkTradeMark(document) == false) {
bytes32 proofHash = phraseProof(document);% K) g% c: z* U
TradeMark memory trademark = TradeMark({
phrase: document,
authorName: author,- J" U1 `$ k# P$ b
timestamp: now,2 b: d+ p( n: b: q; C2 d" p" L
proof: proofHash# J/ c7 [& `8 P1 P T1 j5 |
});
trademarkLookup[proofHash] = true;
trademarkRegistry[proofHash] = trademark;
return true;; O5 Y( h* q$ n1 o1 T1 d
}9 W4 ]) ^# p% V, G: | v; c
return false;, C* b; [* Q" `0 Z- m3 r5 i0 i
}; z; ]: @7 O" i' y* t6 Z3 f
: n5 `0 c5 _ x" q
function phraseProof(string memory phrase) public view returns (bytes32) {8 k+ R, `8 n2 s9 D. V
return sha256(abi.encode(phrase));7 z. v p: j a. c5 ^
}3 C- Q' s' \- o U, ?+ M
function getTradeMarkData(string memory phrase) . B8 v/ [5 p# Y1 b6 }( w; B
public view returns 9 E' F0 ^& q, E# `; A
(string memory document, 3 W2 ]/ V: v8 d$ x0 Y& l; t
string memory authorName, 8 h' w+ Z3 Z4 G" t8 W' F
uint256 timestamp, 6 t: H/ W0 Y2 K0 N ]- g& ~
bytes32 proof) { c- P$ ~0 X4 K- t$ d
TradeMark memory t = trademarkRegistry[phraseProof(phrase)];
return (t.phrase, t.authorName, t.timestamp, t.proof);
}' I& J5 Z% S8 T7 o' R4 M
}将此智能合约复制到你的Truffle项目内的contracts/里去,文件名为TradeMark.sol。8 w! h! C" f/ A( P
我们将通过运行以下命令来编译此智能合约:. }/ K$ _5 }2 M6 C3 T
truffle compile编译智能合约之后会生成一个包含字节码和ABI.json的build目录。
现在,我们来修复迁移内容,以便迁移我们的智能合约。在migrations目录中,使用以下内容创建名为2_deploy_contracts.js的新文件:
var TrademarkRegistration =- w& J' Y% s$ ~( J& X5 u$ H$ Z- O
artifacts.require("./TrademarkRegistration.sol");
module.exports = function(deployer) {: O9 s+ O2 ^1 b' h$ K
deployer.deploy(TrademarkRegistration);
};! r/ m, F! U) `9 l W
保存后,我们就可以开始将已编译的智能合约迁移到Kotti上了。) D% w$ e6 q( ]& p9 x
首先请运行以下命令:8 R+ e f5 V* C3 b; T1 j# i
truffle migrate这样就可以在Kotti上部署智能合约了。它还将显示智能合约地址的位置以及部署智能合约所需的Kotti ETC数量,甚至还提供交易哈希。执行迁移时的Kotti输出如下:0 e# }# }# s4 _9 ~
2_deploy_contracts.js7 L, w+ p. H: _( {0 l
=====================
Deploying 'TrademarkRegistration'/ h) ^: ~1 d8 u4 A! H
--------------------------------- % g* v5 x/ S6 q9 e8 D! n) s) n- O
> 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+ z7 Q# Z5 O1 Z$ V* d
> total cost: 0.01865544 ETH对于把这些内容分享出去我并不感到担心,因为一切都还在测试网上。我们有合约地址,当我们在Kotti上与已部署的智能合约进行交互时会使用。 / r* h* X2 [/ b4 _8 R
现在可以测试我们的智能合约了。7 J4 r& v$ J9 ~+ {& P
运行以下命令跳转到Truffle控制台: O v5 \: n1 T \+ X e. ^8 r
$ truffle console这样就可以进入Kotti环境了。我们来创建一个可变商标,以通过之前给我们的地址展示智能合约:3 _* a" S) Z' b6 H
truffle(development)> let trademark = await TrademarkRegistration.deployed()& u6 l) M u8 C4 T: A0 Q g
undefined这为已部署的合同TrademarkRegistration分配了可变商标。如果我们输入trademark,我们将获得智能合约的所有信息。# V: P) d' @9 V5 k
truffle(development)> trademark
TruffleContract {
constructor:. V/ f: ?( m! Z
{ [Function: TruffleContract]
*1 f$ Y; K! z1 Y: U% t, E
*9 _, r6 a; u- T8 r6 X. N6 b
*7 a3 f! x2 c- @, n3 P0 i
send: [Function],1 M! ?" j% ^2 N2 R9 }3 O9 X3 ^
allEvents: [Function],% \3 t8 J* k. ^, ?3 k; |& k
getPastEvents: [Function] }请键入以下内容来检索地址:4 \3 b& k) `% o. p f4 @. F
truffle(development)> trademark.address
'0xe85B0637437A926B2ce437492Bc118E7E7F6EF12'现在,为了证明这个智能合约适用于0.5.1版本的Solidity,我们来与它进行交互,看看会出现什么情况。
先来检查一下商标是否已注册:. y# j3 A- w- f' u4 m( d
truffle(development)> trademark.checkTradeMark("ETC Take My Energy")
false太好了,现在你已经拥有一个在以太经典上运行的Solidity v0.5.1合约了。然后我们要通过Kotti账户余额进行交易来注册商标。0 j! O. i% H5 [' P" u( w3 Y }8 x
truffle(development)> trademark.registerTradeMark("ETC Take My Energy", "ETC")- I! L7 h+ a+ e" j
{ tx: '0x3dbf55f52bea595eaf41c9eaa67cce30319ce79f0bc599eedb0ce9f1c6b36de0',
receipt:8 x9 }( d. _% J: U: Q
{ blockHash: '0xbaa0cbb48498ac1fe695fce9d6ec8c4a251677481648ce3dbde075cbc8cb0d7a',0 p8 I1 E' l/ e. ]: I: S. M& i# A
blockNumber: 250684,
contractAddress: null,* s6 i7 a5 O: H+ J5 x$ m4 T
cumulativeGasUsed: 27024,3 Z1 F8 p# V" o: k+ r- [" X& C
from: '0x25b7955e43adf9c2a01a9475908702cce67f302a',% } p& p5 t( E
gasUsed: 27024,
logs: [],2 Q) B# S% M' i( B9 h2 b G$ @/ o
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
root: '0x8f534f7f0b344e99f3e4800ff87ebde917b8180c1bc5d2e462e7473e5b76b895',
to: '0xe85b0637437a926b2ce437492bc118e7e7f6ef12',6 o2 W0 J4 m# @4 n5 l; I4 W
transactionHash: '0x3dbf55f52bea595eaf41c9eaa67cce30319ce79f0bc599eedb0ce9f1c6b36de0',
transactionIndex: 0,
rawLogs: [] }," H$ q K# p: I3 b- `: t/ Z! K* t
logs: [] }可以了!太棒了!就这样,我们证明了ETC可以在Kotti测试网上运行对接spuriousDragon版EVM的Solidity v0.5.1智能合约。希望大家喜欢这篇文章,也请大家积极尝试。
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
成为第一个吐槽的人