美文网首页
使用truffle开发智能合约

使用truffle开发智能合约

作者: 最近不在 | 来源:发表于2018-05-23 10:22 被阅读176次

    Truffle是针对基于以太坊的Solidity语言的一套开发框架。本身基于Javascript。

    安装

    npm install -g truffle
    

    创建工程

    truffle init会帮我们创建一个空工程。

    D:\Android_Code\2018\blockchain
    λ mkdir HelloDapp && cd HelloDapp
    
    D:\Android_Code\2018\blockchain\HelloDapp
    λ truffle.cmd init
    Downloading...
    Unpacking...
    Setting up...
    Unbox successful. Sweet!
    
    Commands:
    
      Compile:        truffle compile
      Migrate:        truffle migrate
      Test contracts: truffle test
    
    D:\Android_Code\2018\blockchain\HelloDapp
    λ ls
    contracts/  migrations/  test/  truffle-config.js  truffle.js
    

    编写工程

    这里推荐使用Goland或idea系列作为编辑器, 打开HelloDapp目录。然后下载Intellij-Solidity插件,就能支持Solidity语言。
    具体操作步凑为: File->Settings->Plugins->Browse Respositories->Intellij-Solidity->Install。

    1. 在contracts目录下新建Hello.sol文件。

    pragma solidity ^0.4.23;
    
    contract Hello {
    
        function print() public pure returns(string) {
            return "Hello World";
        }
    
        function add(uint n1, uint n2) public pure returns(uint) {
            return n1 + n2;
        }
    }
    

    2. 在test木下新建TestHello.sol文件。

    pragma solidity ^0.4.23;
    
    import "../contracts/Hello.sol";
    import "truffle/Assert.sol";
    
    contract TestHello {
    
        function testAdd() public {
            Hello hello = new Hello();
            uint result = hello.add(1, 20);
            Assert.equal(result, 21, "TestAdd");
        }
    
        function testPrint() public {
            Hello hello = new Hello();
            string memory result = hello.print();
            Assert.equal(result, "Hello World", "TestPrint");
        }
    }
    

    注意方法名必须以test开头。否则单元测试不会执行该函数。

    3. 在migrations目录下新建2_deploy_hello.js文件。

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

    测试

    在命令行执行truffle test

    D:\Android_Code\2018\blockchain\HelloDapp
    λ truffle.cmd test
    Compiling .\contracts\Hello.sol...
    Compiling .\contracts\Migrations.sol...
    Compiling .\test\TestHello.sol...
    Compiling truffle/Assert.sol...
    
    
      TestHello
        √ testAdd (80ms)
        √ testPrint (102ms)
    
    
      2 passing (1s)
    
    
    D:\Android_Code\2018\blockchain\HelloDapp
    λ truffle.cmd test
    Compiling .\contracts\Hello.sol...
    Compiling .\contracts\Migrations.sol...
    Compiling .\test\TestHello.sol...
    Compiling truffle/Assert.sol...
    
    
      TestHello
        √ testAdd (78ms)
        √ testPrint (101ms)
    
    
      2 passing (919ms)
    

    如果只测试单个文件,也可以执行

    truffle.cmd test ./test/TestHello.sol
    

    编译

    D:\Android_Code\2018\blockchain\HelloDapp
    λ truffle.cmd compile
    Compiling .\contracts\Hello.sol...
    Compiling .\contracts\Migrations.sol...
    Writing artifacts to .\build\contracts
    

    会在工程的build目录下生成对应合约的.json文件,里面描述了合约的具体信息。如果有语法错误会输出错误提示。

    部署合约

    如果想部署合约,我们需要连接上一条区块链。Truffle内置了一条私链用于测试。它只能在本地电脑上使用,不能与以太坊主链交互。

    我们可以使用 truffle.cmd develop命令创建私链,并与其交互。

    D:\Android_Code\2018\blockchain\HelloDapp
    λ truffle.cmd develop
    Truffle Develop started at http://127.0.0.1:9545/
    
    Accounts:
    (0) 0x627306090abab3a6e1400e9345bc60c78a8bef57
    (1) 0xf17f52151ebef6c7334fad080c5704d77216b732
    (2) 0xc5fdf4076b8f3a5357c5e395ab970b5b54098fef
    (3) 0x821aea9a577a9b44299b9c15c88cf3087f3b5544
    (4) 0x0d1d4e623d10f9fba5db95830f7d3839406c6af2
    (5) 0x2932b7a2355d6fecc4b5c0b6bd44cc31df247a2e
    (6) 0x2191ef87e392377ec08e7c08eb105ef5448eced5
    (7) 0x0f4f2ac550a1b4e2280d04c21cea7ebd822934b5
    (8) 0x6330a553fc93768f612722bb8c2ec78ac90b3bbc
    (9) 0x5aeda56215b167893e80b4fe645ba6d5bab767de
    
    Private Keys:
    (0) c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3
    (1) ae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f
    (2) 0dbbe8e4ae425a6d2687f1a7e3ba17bc98c673636790f1b8ad91193c05875ef1
    (3) c88b703fb08cbea894b6aeff5a544fb92e78a18e19814cd85da83b71f772aa6c
    (4) 388c684f0ba1ef5017716adb5d21a053ea8e90277d0868337519f97bede61418
    (5) 659cbb0e2411a44db63778987b1e22153c086a95eb6b18bdf89de078917abc63
    (6) 82d052c865f5763aad42add438569276c00d3d88a2d062d36b2bae914d58b8c8
    (7) aa3680d5d48a8283413f7a108367c7299ca73f553735860a87b08f39395618b7
    (8) 0f62d96d6675f32685bbdb8ac13cda7c23436f63efbb9d07700d8669ff12b7c4
    (9) 8d5366123cb560bb606379f90a0bfd4769eecc0557f1b362dcae9012b548b1e5
    
    Mnemonic: candy maple cake sugar pudding cream honey rich smooth crumble sweet treat
    
    ⚠️  Important ⚠️  : This mnemonic was created for you by Truffle. It is not secure.
    Ensure you do not use it on production blockchains, or else you risk losing funds.
    
    truffle(develop)>
    

    默认使用端口9545创建了一条测试链,并且为了方便测试,创建了10个账号。然后进入了truffle交互模式里。

    接着使用migrate命令。部署合约到连上

    truffle(develop)> migrate
    Using network 'develop'.
    
    Running migration: 1_initial_migration.js
      Deploying Migrations...
      ... 0x65c8b0cad9b8ea585a216fbf5e214386373e490ae7737176f01f7dcb7f8b4d00
      Migrations: 0x8cdaf0cd259887258bc13a92c0a6da92698644c0
    Saving successful migration to network...
      ... 0xd7bc86d31bee32fa3988f1c1eabce403a1b5d570340a3a9cdba53a472ee8c956
    Saving artifacts...
    Running migration: 2_deploy_hello.js
      Deploying Hello...
      ... 0x4664bd718a7d9ef33ad9cbffcee02e38d80d0d4103e68b796e3e95c1f5bc0fc4
      Hello: 0x345ca3e014aaf5dca488057592ee47305d9b3e10
    Saving successful migration to network...
      ... 0xf36163615f41ef7ed8f4a8f192149a0bf633fe1a2398ce001bf44c43dc7bdda0
    Saving artifacts...
    truffle(develop)>
    

    这就成功部署到链上了,并且显示了交易id和合约地址。
    其中0x345ca3e014aaf5dca488057592ee47305d9b3e10就是Hello合约的地址,
    0x4664bd718a7d9ef33ad9cbffcee02e38d80d0d4103e68b796e3e95c1f5bc0fc4就是交易id。

    与合约交互

    truffle(develop)> Hello.address
    '0x345ca3e014aaf5dca488057592ee47305d9b3e10'
    truffle(develop)> Hello.deployed().then(inst => {HelloInst = inst});
    undefined
    truffle(develop)> HelloInst.print();
    'Hello World'
    truffle(develop)> Hello.deployed().then(function(inst) {HelloInst = inst;});
    undefined
    truffle(develop)> HelloInst.print();
    'Hello World'
    truffle(develop)> HelloInst.add(10, 200);
    BigNumber { s: 1, e: 2, c: [ 210 ] }
    truffle(develop)> HelloInst.add(1, 30).then(val => val.toNumber());
    31
    truffle(develop)> HelloInst.address
    '0x345ca3e014aaf5dca488057592ee47305d9b3e10'
    

    支持es6的箭头写法简化了不少。Hello.address和HelloInst.address都输了合约的地址

    注意:

    1.如果合约里写有构造函数

    contract Hello {
        function Hello() public {
            
        }
    }
    

    如果是使用了新的版本编译器。可能会报错Warning: Defining constructors as functions with the same name as the contract is deprecated. Use "constructor(...) { ... }" instead.
    最新版语言编译需要这么写:

    contract Hello {
        constructors() public {
            
        }
    }
    

    2.修改了代码再执行migrate并不会生效,它只会部署没有部署过的合约。强制重新部署,可执行
    migrate --reset。如果只想部署某一个合约,可执行migrate -f 2

    3.执行truffle,不是truffle.cmd。会优先执行到当前工程目录下truffle.js配置文件。所以执行truffle.cmd可以避免这个问题。

    4.truffle交互模式下,可少输入truffle。比如系统命令下执行truffle migrate,在truffle模式下只用输入migrate即可。连按2次ctrl+c或输入.exit可退出交互模式。

    相关文章

      网友评论

          本文标题:使用truffle开发智能合约

          本文链接:https://www.haomeiwen.com/subject/ytaljftx.html