四、Truffle 4.0/Geth 1.7.2/TestRPC在私有链上部署智能合约

脚本操作

1
2
mkdir two
cd two

truffle初始化目录

1
truffle init

truffle编译默认的智能合约

1
truffle compile

编写智能合约

1
2
cd contracts
vi Hello_yudao_me.sol

智能合约内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
pragma solidity ^0.4.17;

contract Hello_yudao_me {

//say hello yudao.me
function say() public pure returns (string) {
return "Hello yudao.me";
}

//print name
function print(string name) public pure returns (string) {
return name;
}
}

1
cd ..

编辑migrations/1_initial_migration.js部署脚本

1
2
3
4
5
6
7
var Migrations = artifacts.require("./Migrations.sol");
var Hello_yudao_me = artifacts.require("./Hello_yudao_me.sol");

module.exports = function(deployer) {
deployer.deploy(Migrations);
deployer.deploy(Hello_yudao_me);
};

编译

1
truffle compile --compile-all

另外终端,通过testrpc启动模拟以太坊

1
testrpc --gasLimit 0x800000000)

回到刚才的第一个终端,部署智能合约

1
truffle migrate --reset

通过truffle来测试智能合约,出现如下内容,恭喜成功

1
2
3
4
5
6
7
8
9
truffle console
truffle(development)> var contract;
undefined
truffle(development)> Hello_yudao_me.deployed().then(function(instance){contract= instance;});
undefined
truffle(development)> contract.say()
'Hello yudao.me'
truffle(development)> contract.print("https://yudao.me")
'https://yudao.me'

坚持原创技术分享,您的支持将鼓励我继续创作!