Tutorials 1
起步-调试程序
本教程版本需truffle的版本为 v4.0+
1.新建一个项目
打开终端,命名为1in
$ mkdir tutorials1
$ cd tutorials1
$ truffle init
2.在contracts目录中新建文件 Store.sol
内容如下:
pragma solidity ^0.4.17;
contract SimpleStorage {
uint myVariable;
function set(uint x) public {
myVariable = x;
}
function get() constant public returns (uint) {
return myVariable;
}
}
这个合约的名字为:SimpleStorage,它拥有两个函数和一个参数
set get 和 myVariable
3.在migrations目录中新建2_deploy_contracts.js文件,内容如下:
var SimpleStorage = artifacts.require("SimpleStorage");
module.exports = function(deployer) {
deployer.deploy(SimpleStorage);
};
这个文件的内容能够把合约SimpleStorage部署到区块链上
4.在终端1in中,编译文件
$ truffle compile
5.打开一个新的终端窗口 标记为2in
进入truffle开发模式
$ truffle develop
6.部署合约
在2in中
$ migrate
打印信息如下:
Running migration: 1_initial_migration.js
Replacing Migrations...
... 0xe4f911d95904c808a81f28de1e70a377968608348b627a66efa60077a900fb4c
Migrations: 0x3ed10fd31b3fbb2c262e6ab074dd3c684b8aa06b
Saving successful migration to network...
... 0x429a40ee574664a48753a33ea0c103fc78c5ca7750961d567d518ff7a31eefda
Saving artifacts...
Running migration: 2_deploy_contracts.js
Replacing SimpleStorage...
... 0x6783341ba67d5c0415daa647513771f14cb8a3103cc5c15dab61e86a7ab0cfd2
SimpleStorage: 0x377bbcae5327695b32a1784e0e13bedc8e078c9c
Saving successful migration to network...
... 0x6e25158c01a403d33079db641cb4d46b6245fd2e9196093d9e5984e45d64a866
Saving artifacts...
7.到目前为止,合约已经部署到开发网络上了,现在尝试跟合约交互(调用合约的函数)
在终端2in:
$ SimpleStorage.deployed().then(function(instance){return instance.get.call();}).then(function(value){return value.toNumber()});