开发环境:ubantu18.04 truffle5.0.18 npm5.5.1(输入指令:npm -v) nodejs9.3.0(输入指令:node -v)
新建第一个项目SmartContractDemo
sun@sun-virtual-machine:~$ mkdir SmartContractDemo
sun@sun-virtual-machine:~$ cd SmartContractDemo
sun@sun-virtual-machine:~$ mkdir HelloWorld
sun@sun-virtual-machine:~/SmartContractDemo$ cd HelloWorld
sun@sun-virtual-machine:~/SmartContractDemo/HelloWorld$ truffle init
这时候helloworld文件夹下会出现
contracts migrations test truffle-config.js
contracts 智能合约目录
migrations 发布脚本目录
test 存放测试文件
truffle.js Truffle的配置文件
truffle-config.js Truffle的配置文件
编辑智能合约
下面使用atom编辑器打开文件夹编写智能合约内容
pragma solidity ^0.5.0;#声明solidity版本
contract HelloWorld{
string Myname = "孙策";
function sayHello() public view returns (string memory) {
return Myname;
}
}
代码中有sayHello方法是输出Myname变量的值(孙策)
在migration文件夹下创建2_deploy_migration.js文件
var HelloWorld = artifacts.require("./HelloWorld.sol");
module.exports = function(deployer){
deployer.deploy(HelloWorld);
};
编辑truffle-config.js文件,将其中development部分修改为
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 8545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
},
将合约发布到网络
重新打开一个终端输入ganache-cli
sun@sun-virtual-machine:~$ ganache-cli
Ganache CLI v6.4.3 (ganache-core: 2.5.5)
Available Accounts
==================
(0) 0x8acf568fffc5d7a5970d65a0221b9ad0e7f95bcf (~100 ETH)
(1) 0x2e3bf281556fff96d9c28b87ee7ef7d79412acea (~100 ETH)
(2) 0x49acb0c81132d3409a83f118a169236b5c75a337 (~100 ETH)
(3) 0x51f151c5e4fc815a32766ec42da854856de4f0d4 (~100 ETH)
(4) 0xb62e48388352262cdadb7ce2ccd6238634d5fa78 (~100 ETH)
(5) 0xdc0814f8b9d1058390969d39250e02222e770e53 (~100 ETH)
(6) 0x3ae95c1ed584ac2ef5bae8fde2061d0ceaf91116 (~100 ETH)
(7) 0x4182f27c223ac633ece4585246eebe9063019c3c (~100 ETH)
(8) 0x52dcb67c449693ed0c82a738f8497a3a7c9e96d9 (~100 ETH)
(9) 0xbee93c799cbc607e4cef3dbc7e8e39732b151690 (~100 ETH)
Private Keys
==================
(0) 0xbccbb5d011dcd967b88843c0aaa009616f3eb879eeaf94c913d5e20c4ecc80a7
(1) 0xebb7dfe2ab9d121f5f26fbebecdd251feb0b95ee4b95c7307b8ff9928953a687
(2) 0x7d7f7d34d44b4c75b085fbcb5ebe5ab8cc687681be33ab8a9c366bf016307d41
(3) 0x87b5c436ec2eb3de17d41245ed8760b4f36b85b6aac22d721f7cef9746fa160c
(4) 0xbc0ba91f92c9e11b48b4bfd3845590a286c3c988475acf24e00bbbca9b8661d4
(5) 0x1fd93a1293b0da765e3d299e5c23d44d8474ce72e003d95f2e07007961bd03f9
(6) 0x9927a9712b5d252398ef1fc6869a9721b959d28e00b4ed37693d6b49fb9def88
(7) 0x3402e00b2c21062f70500c4936aa176891b7ea0e97c919c2deac1450594f017a
(8) 0x66776a63bf4f3becd708b4eb9d154afe3b41d2d24322e5a27a066244fa9d4312
(9) 0xb8b3308cb093bda8f23e9c26ee87508f53d4fa32a2b35e0e5750525c7f13328c
HD Wallet
==================
Mnemonic: unable length clever cricket object system bird donate crunch bike talk there
Base HD Path: m/44'/60'/0'/0/{account_index}
Gas Price
==================
20000000000
Gas Limit
==================
6721975
Listening on 127.0.0.1:8545
回到truffle,输入truffle compile编译合约
sun@sun-virtual-machine:~/SmartContractDemo/HelloWorld$ truffle compile
Compiling ./contracts/HelloWorld.sol...
Compiling ./contracts/Migrations.sol...
Writing artifacts to ./build/contracts
下面开始部署合约
sun@sun-virtual-machine:~/SmartContractDemo/HelloWorld$ truffle migrate
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
Starting migrations...
======================
> Network name: 'development'
> Network id: 1560322064995
> Block gas limit: 0x6691b7
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
...
> Saving migration to chain.
> Saving artifacts
2_deploy_migration.js
=====================
Deploying 'HelloWorld'
----------------------
...
> Saving migration to chain.
> Saving artifacts
Summary
=======
> Total deployments: 2
> Final cost: 0.00967816 ETH
开始测试
进入console控制台
sun@sun-virtual-machine:~/SmartContractDemo/HelloWorld$ truffle console
truffle(development)> var contract
undefined
truffle(development)> HelloWorld.deployed().then(function(instance){contract=instance;});
undefined
truffle(development)> contract.sayHello()
'孙策'
var contract和javascript语法一样,表示声明一个contract变量。HelloWorld.deployed().then(function(instance){contract= instance;});表示,将HelloWorld合约主体,传递给contract变量。后面我们就可以直接使用变量contract调用sayHello()方法。
如果想要退出控制台可以使用.exit命令
truffle(development)> .exit
sun@sun-virtual-machine:~/SmartContractDemo/HelloWorld$