Solidity的Truffle框架实战

1. 参考资料

http://truffle.tryblockchain.org/Solidity-truffle-%E5%AE%9E%E6%88%98.html
由于truffle版本的升级,导致上述链接中的部分内容已经失效,本文中使用的truffle版本:
Truffle v4.0.5 - a development framework for Ethereum

2. 创建工程目录

mkdir truffleTest

3. 初始化框架

truffle init

4. 放入自己的合约

./contract目录下自带Migrations.sol合约,它是Truffle用来帮助部署的。现在我们创建一个自己的合约文件Greeter.sol。

pragma solidity ^0.4.0;

contract Greeter         
{
    address creator;     
    string greeting;     

    function Greeter(string _greeting) public   
    {
        creator = msg.sender;
        greeting = _greeting;
    }

    function greet() constant returns (string)          
    {
        return greeting;
    }
    
    function setGreeting(string _newgreeting) 
    {
        greeting = _newgreeting;
    }
    
     /**********
     Standard kill() function to recover funds 
     **********/
    
    function kill()
    { 
        if (msg.sender == creator)
            suicide(creator);  // kills this contract and sends remaining funds back to creator
    }

}

5. 修改发布脚本

在./migrations下,新增发布脚本2_deploy_contracts.js,内容如下:

var Greeter = artifacts.require("Greeter");
module.exports = function(deployer) {
  deployer.deploy(Greeter);
};

6. 修改truffle配置文件

在根目录下,修改truffle.js,内容如下:

module.exports = {
  // See 
  // for more about customizing your Truffle configuration!
  networks: {
    development: {
      host: "127.0.0.1",
      port: 8545,
      network_id: "*" // Match any network id
    }
  }
};

这里的host和port是以太坊客户端的,我使用的以太坊客户端是ganache-cli,缺省端口为8545。

7. 编译

truffle compile

8. 启动以太坊客户端

yangang$ ganache-cli
Ganache CLI v6.0.3 (ganache-core: 2.0.2)

Available Accounts
==================
(0) 0xfaeebc3e40c4d2c58197672f501e01b25e8be623
(1) 0x412b87f2d15fa3ecc2b1992458089fd1d95fbad9
(2) 0xd18e31bfd821ddc24759205d570d0d98da5678b7
(3) 0xf8d481887c1c7062a370dcab4dca0080623f468e
(4) 0x660adf711dff953e56901a23e17cae7f8ec53a3e
(5) 0x616057efbc9e30fd10fd0651ac23256302f25793
(6) 0x7a963739820633bff77f01dde7ddb0e24aba69bc
(7) 0xd4166e1350030c71eaf775136dd3e679dc8b0d1e
(8) 0x5c99dcb0510f7a0bd65e3aaf6b07875e680d5d2d
(9) 0xa91ea98560e6ef136b917298415a8f8c51ecf49c

Private Keys
==================
(0) 060d94b88593ea7ce8646ca026200704b6b28fbc2bdf47ba3b177d1cbece02cc
(1) f49fdd762decc9e6ac1b8b0b08b87a664cec017005df617e89fa6ffd1f3a3212
(2) a4bb4df21e239cfd80b05c4f51e90ea02b9300bb2193598c5b62981013d1f588
(3) d1fcbe1bcfb5977a8e28b041625567495941873f11859a6484b420496e5b052c
(4) 76da3441b8a7f19625543d43a38f1db724d2748c7a9c313f2167bd559ce8f633
(5) cf62dbf0de6bf70fda3e24797116cfed3fca13da7e15c276c6cd9326d6606228
(6) c43f9a84aa05da259d86938f958c3561b97b4c868851410518d425883b6d160a
(7) 2867f9a62799d7d7f9d59c0b0b14861722430cfd7f252ed97b973a0934fe628d
(8) 57f87d8d3f31537f736f247f825908520f539d2e1e502f93bb958c77c9988233
(9) b4ac0bbb15dbbb8098d753e696464f0ebb43df1ad8cdd066d54c6f0d1b25dab3

HD Wallet
==================
Mnemonic:      gasp broccoli soap typical fossil loud random resemble ladder rent thing stone
Base HD Path:  m/44'/60'/0'/0/{account_index}

Listening on localhost:8545

9. 部署合约(migrate)

truffle migrate

10. 看一下效果

我们使用truffle console功能在控制台与合约交互,来看下效果。

truffle(development)> Greeter.deployed().then(function(instance){return instance.setGreeting("Hello world!");});
{ tx: '0xad5853700bea9cea3db1bf98c46ba121108aab145109417bb53378f50345dd60',
  receipt:
   { transactionHash: '0xad5853700bea9cea3db1bf98c46ba121108aab145109417bb53378f50345dd60',
     transactionIndex: 0,
     blockHash: '0xaab09e55698874ddbb0fb8a42b64c3d642e7d1a81e4e0421709af03301efabdb',
     blockNumber: 10,
     gasUsed: 43445,
     cumulativeGasUsed: 43445,
     contractAddress: null,
     logs: [],
     status: 1 },
  logs: [] }
truffle(development)> Greeter.deployed().then(function(instance){return instance.greet.call();})
'Hello world!'
  • 首先寻找Greeter合约,然后调用该合约的setGreeting("Hello world")方法
  • 调用合约的greet()方法

你可能感兴趣的:(Solidity的Truffle框架实战)