$ 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}}
$ 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
$ 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
/usr/local/lib/node_modules/truffle/lib/repl.js:25if(err)returndone(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:13)
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)