以太坊Solidity之Truffle框架的应用实践

Posted ╰つ栺尖篴夢ゞ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了以太坊Solidity之Truffle框架的应用实践相关的知识,希望对你有一定的参考价值。

一、前言

$ mkdir -p /Users/admin/develop/blockchain_workspace/truffleTest

二、初始化框架

  • 进入到工程根目录下(现在应该是个空目录),并执行下述命令:
$ cd /Users/admin/develop/blockchain_workspace/truffleTest
$ truffle init
  • 正确执行后,将得到下面这样的目录结构:

  • 目录结构简单说明如下:
    • app/ - 应用文件运行的默认目录,这里面包括推荐的 javascript 文件和 css 样式文件目录,但可以完全决定如何使用这些目录;
    • contract/ - Truffle 默认的合约文件存放地址;
    • migrations/ - 存放发布脚本文件;
    • test/ - 用来测试应用和合约的测试文件;
    • truffle.js - Truffle 的配置文件。

三、放入合约

  • 删除 ./contract 目录下的自带 demo 合约,切记不要删除 ./contract/Migrations.sol 合约,它是 Truffle 用来帮助部署的:
$ cd /Users/admin/develop/blockchain_workspace/truffleTest/contracts
$ rm ConvertLib.sol MetaCoin.sol
  • 在 ./contract 目录下创建一个自己的合约文件 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
    


四、修改发布脚本

  • 将 ./migrations/2_deploy_contracts.js 下的内容由:
module.exports = function(deployer) 
  deployer.deploy(ConvertLib);
  deployer.autolink();
  deployer.deploy(MetaCoin);
;
  • 修改为:
module.exports = function(deployer) 
  deployer.deploy(Greeter);
;
  • 目的是去掉原有自带的 Demo 部署流程,修改为要部署的合约,修改完后,记得保存,不然发布时会报错,找不到相关合约。

五、编译

  • 进入到工程根目录 ./truffleTest 目录下,进行编译:
$ truffle compile
Compiling Greeter.sol...

Writing artifacts to ./build/contracts

六、启动客户端

  • 启动之前安装好的 EthereumJS RPC 客户端:
$ testrpc

EthereumJS TestRPC v3.0.3

Available Accounts
==================

Private Keys
==================

HD Wallet
==================
Mnemonic:      degree debate income mask fiber issue album diet unfair police race car
Base HD Path:  m/44'/60'/0'/0/account_index

Listening on localhost:8545

七、部署合约(migrate)

  • 部署合约到网络上:
$ truffle migrate
Running migration: 2_deploy_contracts.js
  Deploying Greeter...
  Greeter: 0xe66038995cf64b14da96d26cbe1c96d30dec0e95
Saving successful migration to network...
Saving artifacts...
  • 备注:
    • 如果报错出现了一些之前编译部署过,但已经不需要的合约,可以用 truffle migrate --reset 来重置部署,但可能根据情况,需要更新移植版本号,来管理不同的版本;
    • 若误删 Truffle 自带用来部署的合约 ./contracts/Migrations.sol,会出现下述报错,需要补回到 ./contract 目录,重初始化一次得到。
$ truffle migrate
Running migration: 1_initial_migration.js
/Users/admin/develop/blockchain_workspace/truffleTest/migrations/1_initial_migration.js:2
  deployer.deploy(Migrations);
                  ^

ReferenceError: Migrations is not defined
    at module.exports (/Users/admin/develop/blockchain_workspace/truffleTest/migrations/1_initial_migration.js:2:19)
    at /usr/local/lib/node_modules/truffle/lib/migrate.js:109:7
    at /usr/local/lib/node_modules/truffle/lib/require.js:82:7
    at tryToString (fs.js:425:3)
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:412:12)

八、最终效果

  • 使用 truffle console 功能在控制台与合约交互,来看下效果:
$ truffle console
truffle(default)> Greeter.deployed().setGreeting("Hello world!");
'0xf6a00b4466c9ab38d7eb60dc9f8d15f5f1e500e24ea91bd9e28f6233bad08aed'
truffle(default)> Greeter.deployed().greet.call();
'Hello world!'
truffle(default)>

九、常见报错

  • 以太坊客户端连接配置不正确,或未正常启动时:
$ truffle console
/usr/local/lib/node_modules/truffle/lib/repl.js:25
    if (err) return done(err);
                    ^

ReferenceError: done is not defined
    at /usr/local/lib/node_modules/truffle/lib/repl.js:25:21
    at /usr/local/lib/node_modules/truffle/lib/repl.js:57:21
    at /usr/local/lib/node_modules/truffle/lib/contracts.js:46:25
    at /usr/local/lib/node_modules/truffle/node_modules/web3/lib/web3/property.js:119:13
    at /usr/local/lib/node_modules/truffle/node_modules/web3/lib/web3/requestmanager.js:82:20
    at exports.XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/node_modules/web3/lib/web3/httppro
vider.js:114:13)
    at exports.XMLHttpRequest.dispatchEvent (/usr/local/lib/node_modules/truffle/node_modules/xmlhttprequest/lib/XMLHttpRequest.
js:591:25)
    at setState (/usr/local/lib/node_modules/truffle/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:610:14)
    at exports.XMLHttpRequest.handleError (/usr/local/lib/node_modules/truffle/node_modules/xmlhttprequest/lib/XMLHttpRequest.js
:532:5)
    at ClientRequest.errorHandler (/usr/local/lib/node_modules/truffle/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:459:14)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:188:7)
    at Socket.socketErrorListener (_http_client.js:309:9)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at emitErrorNT (net.js:1281:8)
  • 若合约未进行部署就进行调用,可能出现下述报错:
truffle(default)> Greeter.deployed().setGreeting("Hello world!");
Error: Cannot find deployed address: Greeter not deployed or address not set.
    at Function.Contract.deployed (/Users/admin/develop/blockchain_workspace/truffleTest/build/contracts/Greeter.sol.js:311:1
3)
    at evalmachine.<anonymous>:1:-53

    at ContextifyScript.Script.runInContext (vm.js:37:29)
    at Object.exports.runInContext (vm.js:69:17)
    at TruffleInterpreter.interpret (/usr/local/lib/node_modules/truffle/lib/repl.js:99:17)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:513:10)
    at emitOne (events.js:96:13)
    at REPLServer.emit (events.js:188:7)
  • 部署时填合约名称有误,本应该写为 deployer.deploy(IntegerLiteral);,却写成了 deployer.deploy(“IntegerLiteral”);:
Deploying undefined...
Error encountered, bailing. Network state unknown. Review successful transactions manually.
TypeError: Cannot read property 'apply' of undefined
    at /usr/local/lib/node_modules/truffle/lib/deployer.js:62:28
    at process._tickDomainCallback (internal/process/next_tick.js:129:7)

以上是关于以太坊Solidity之Truffle框架的应用实践的主要内容,如果未能解决你的问题,请参考以下文章

以太坊 DApp 开发入门实战! 用Node.js和truffle框架搭建——区块链投票系统!

以太坊是什么,智能合约,编程语言:Solidity,DApp: 去中心化的应用程序,Truffle

区块链测试 以太坊 truffle是什么 怎么用

以太坊:Truffle框架入门之运行MetaCoin

以太坊开发完整去中心化应用 —— 区块链投票系统

Web 应用程序无法与通过 Truffle 部署的以太坊智能合约正确通信