010.说明
Solidity :编程语言,开发智能合约;
truffle : 开发框架,方便编译部署智能合约;
ganache-cli:testrpc前身,智能合约测试环境,一个虚拟的环境;
go-ethereum: 以太坊客户端,智能合约需要部署在这里;
021.安装 truffle
$ npm i -g truffle
$ truffle version
Truffle v4.1.14 (core: 4.1.14)
Solidity v0.4.24 (solc-js)
注意:这里的Solidity版本: v0.4.24,不同版本语法变化很大,不兼容;
$ mkdir -p smart_contract
$ cd smart_contract
$ truffle init
Downloading...
Unpacking...
Setting up...
Unbox successful. Sweet!
Commands:
Compile: truffle compile
Migrate: truffle migrate
Test contracts: truffle test
│ truffle-config.js
│ truffle.js
│
├─contracts
│ Migrations.sol
│
├─migrations
│ 1_initial_migration.js
│
└─test
---- truffle-config.js :配置文件
---- truffle.js :配置文件
---- contracts :存放智能合约的目录
---- Migrations.sol :truffle自带的智能合约脚本
---- migrations :智能合约编译部署目录
---- 1_initial_migration.js:编译部署文件
---- test :测试脚本目录
022.编写智能合约
pragma solidity ^0.4.24;
contract Demo1 {
function print () public pure returns (uint j) {
return 100;
}
function printInt(uint i) public pure returns (uint j) {
return i * 2;
}
function printString (string i) public pure returns (string j) {
return i;
}
}
pragma solidity ^0.4.24;
import 'truffle/Assert.sol';
import 'truffle/DeployedAddresses.sol';
import '../contracts/Demo1.sol';
contract Demo1Test {
Demo1 demo = Demo1(DeployedAddresses.Demo1());
function testPrint() {
uint expect = 100;
Assert.equal(expect, demo.print(), 'print');
}
function testPrintInt() {
uint expect = 200;
Assert.equal(expect, demo.printInt(100), 'print int');
}
function testPrintString() {
Assert.equal('a', demo.printString('a'), 'print string');
}
}
023.编译、部署、测试智能合约(testrpc)
module.exports = {
// See <http://truffleframework.com/docs/advanced/configuration>
// to customize your Truffle configuration!
networks: {
development: {
host: 192.168.1.191,
port: 8545,
network_id: '*'
}
}
};
$ truffle compile --compile-all
Compiling .\contracts\Demo1.sol...
Compiling .\contracts\Migrations.sol...
Writing artifacts to .\build\contracts
$ truffle migrate --reset
Using network 'development'.
Running migration: 1_initial_migration.js
Replacing Migrations...
... 0x63d527a23e2a0257eee7e58251dd8174c03382d1d6549651afb6c7d4ef452cdd
Migrations: 0x6a483d4081d953779f9fbef0296492320be130f7
Replacing Demo1...
... 0xb74681f3549cc25ca4e1518541d8e08fef74d45efb86e5f9f05908b9312f2657
Demo1: 0x9fb2d49be3303ebe1f6d1e1281ed8c5db07075a4
Saving successful migration to network...
... 0x9a3c295985e161afc100d16935ae32ba91f4e39be6ef4c3f642611d97c84b22d
Saving artifacts...
$ truffle test
Using network 'development'.
Compiling .\contracts\Demo1.sol...
Compiling .\test\Demo1Test.sol...
Compiling truffle/Assert.sol...
Compiling truffle/DeployedAddresses.sol...
Compilation warnings encountered:
/D/workspace/smart_contract/test/Demo1Test.sol:11:5: Warning: No visibility specified. Defaulting to "public".
function testPrint() {
^ (Relevant source part starts here and spans across multiple lines).
,/D/workspace/smart_contract/test/Demo1Test.sol:16:5: Warning: No visibility specified. Defaulting to "public".
function testPrintInt() {
^ (Relevant source part starts here and spans across multiple lines).
,/D/workspace/smart_contract/test/Demo1Test.sol:21:5: Warning: No visibility specified. Defaulting to "public".
function testPrintString() {
^ (Relevant source part starts here and spans across multiple lines).
Demo1Test
√ testPrint (41ms)
√ testPrintInt (66ms)
√ testPrintString
3 passing (701ms)
024.安装testrpc
[root@geth01 ~]# npm i -g testrpc
npm WARN deprecated testrpc@0.0.1: testrpc has been renamed to ganache-cli, please use this package from now on.
+ testrpc@0.0.1
added 1 package in 1.059s
[root@geth01 ~]# ln -s /usr/local/node-v11.4.0-linux-x64/bin/testrpc /usr/bin/
提示testrpc名字修改为ganache-cli,不过我没装上(报错),暂时还是使用testrpc;
[root@geth01 ~]# testrpc
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)
Available Accounts
==================
(0) 0xc7fcc15069ce88dc7bf20a25cd90c25a1fa207e3
(1) 0xe8284f75ca91ced00df38b57b4ef052545a3f1a3
(2) 0x0b60eab3e8d7b5f090a92b52357d846bbed15132
(3) 0xc17597b0d174b4528d1dcd95dfb7d7b2642edff9
(4) 0x7161d5ee0c14ff001553ba16067baec059f86add
(5) 0x5e19bbd19377f79d9a5473a32d2f70005bae64de
(6) 0xd52e40acf9257b117d43f86c3c7178cab40974c1
(7) 0x21e82a4e2d7cf40cfc1c62d4cf56edf377e13818
(8) 0x0a30e878d5e479b9d4c493d8e665a0077d77c66e
(9) 0x16f05821dcd5c812434c7c91990cd72b0538a358
Private Keys
==================
(0) 569862d776d877d78785ae9835816dd754dab8b16a689317f40d4b81bd2e7a7b
(1) 0acf130e0a7eec4ef50fc6a2709a7b6d7dcab97bbd04291d1026ecad841784b5
(2) 859fe45be03cf4dcfe239d2ee60b30e13d1a1c39deb2485630541c5aba13a802
(3) f940ecc11920b3ca36d10d50ceae07ceedd7d2a6b37e31afe40298a821e55703
(4) 4856838e6c8a606012bf0a91e1284c482dd194b7178a855fddbcb9ebcdc79ed9
(5) b29da67c629ecb2395a328d9b594d19dc61646880068dd2b671dcc8e81121f92
(6) ac14862aa35c87256d078920b80cb81c5bdfa23169e0e3c3f5ca0321223b4059
(7) e55a79d8c45cb9b5f3dec6e1ed70d865ec60f3633d49bd1bbc06b5b4ccee624a
(8) ba36fa98781a5fa62bfb8208b8b3dfdd6fec0136e6ec8d9eee67788eb886c668
(9) 622b00722abb219ebfd7dd12efabd64f63f631f036f9ec6852a11ad5743ca694
HD Wallet
==================
Mnemonic: erupt ribbon bubble moment define cost ignore enhance clean auto manual help
Base HD Path: m/44'/60'/0'/0/{account_index}
Listening on localhost:8545
025.编译、部署、测试智能合约(go-ethereum)
待补充。。。。