Truffle快速入门

1. 安装truffle

运行npm install -g truffle

2. 下载代码

创建项目文件夹MetaCoin,运行truffle unbox metacoin:
有以下文件夹:

  • contracts/ solidity编写的智能合约
  • migrations/ 可脚本化的部署文件
  • test/ 测试文件
  • truffle.js Truffle的配置文件

Truffle快速入门_第1张图片

3. 编译智能合约

运行truffle compile,编译结果会放在build\contracts

PS D:\WebProj\MetaCoin> truffle.cmd compile
Compiling .\contracts\ConvertLib.sol...
Compiling .\contracts\MetaCoin.sol...
Compiling .\contracts\Migrations.sol...
Writing artifacts to .\build\contracts

注:此处用truffle.cmd是因为在Windows系统下存在“命名冲突”的问题。

4. 在Truffle Develop上部署智能合约

  • 运行Truffle Develop:truffle develop,会给出10个账号和私钥供本地开发使用
PS D:\WebProj\MetaCoin> 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
  • 在Truffle Develop的运行环境下,命令行可以省略truffle,运行migrate,会显示交易的ID和部署的智能合约的地址
truffle(develop)> migrate
Using network 'develop'.

Running migration: 1_initial_migration.js
  Deploying Migrations...
  ... 0x7511b275f23246e33a6d237fe7fafe7d5dbaf6701fbf303261f537612d07d8ce
  Migrations: 0x8cdaf0cd259887258bc13a92c0a6da92698644c0
Saving successful migration to network...
  ... 0xd7bc86d31bee32fa3988f1c1eabce403a1b5d570340a3a9cdba53a472ee8c956
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Deploying ConvertLib...
  ... 0xf1c4d6b2a2cb9794736126efb22b92c59639922b3c5148e865cad6ea718763bf
  ConvertLib: 0x345ca3e014aaf5dca488057592ee47305d9b3e10
  Linking ConvertLib to MetaCoin
  Deploying MetaCoin...
  ... 0x62e45f7449346fee74988af28dacca2d4a3f5c9f90a323fcdc5eecf8269d04db
  MetaCoin: 0xf25186b5081ff5ce73482ad761db0eb0d25abfbf
Saving successful migration to network...
  ... 0x059cf1bbc372b9348ce487de910358801bbbd1c89182853439bec0afaee6c7db
Saving artifacts..

5. 在GANACHE上部署智能合约

GANACHE其实是一个有图形界面的Truffle Develop客户端

  • 下载GANACHE
  • 修改truffle.js的配置:
module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*"
    }
  }
};
  • 启动GANACHE

Truffle快速入门_第2张图片

  • 在控制台输入truffle migrate

结果类似于上节相应的操作

  • 点击“Transaction”按钮可以查看交易的详细过程。

6. 在智能合约上交互

  • 查看账户0的metacoin的余额:
MetaCoin.deployed().then(function(instance){return instance.getBalance(web3.eth.accounts[0]);}).then(function(value){return value.toNumber()});

运行结果:

10000
  • 查看账户0的metacoin的余额的ether价值:
MetaCoin.deployed().then(function(instance){return instance.getBalanceInEth(web3.eth.accounts[0]);}).then(function(value){return value.toNumber()});

运行结果:

20000

因为在这个例子中,一个metacoin值2个ether

  • 从账户0转账500个metacoin给账户1
MetaCoin.deployed().then(function(instance){return instance.sendCoin(web3.eth.accounts[1], 500);});

运行结果:

{ tx: '0xa6c465de6c68c8fa29b1a7020006715d2749381421376d72ae1a1d2f6803a695',
  receipt:
   { transactionHash: '0xa6c465de6c68c8fa29b1a7020006715d2749381421376d72ae1a1d2f6803a695',
     transactionIndex: 0,
     blockHash: '0xaf22cbe2b46d253a7af9c5554a903d1923344a23992896093c3ba7faeb6de24e',
     blockNumber: 6,
     gasUsed: 51057,
     cumulativeGasUsed: 51057,
     contractAddress: null,
     logs: [ [Object] ],
     status: '0x01',
     logsBloom: '0x000000000000000000000000000000000100000000000000000000100000000000000000000000200000000000000000000000000
0000000000000000000000000000000000000001000000800000000000000000000000000008000000000000000000000000000000000000000000000000
0000000000000000010000000000000000000010000000000000000000000000000000000000000010000000002000000000000000000000000000000000
0000020000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000' },
  logs:
   [ { logIndex: 0,
       transactionIndex: 0,
       transactionHash: '0xa6c465de6c68c8fa29b1a7020006715d2749381421376d72ae1a1d2f6803a695',
       blockHash: '0xaf22cbe2b46d253a7af9c5554a903d1923344a23992896093c3ba7faeb6de24e',
       blockNumber: 6,
       address: '0xf25186b5081ff5ce73482ad761db0eb0d25abfbf',
       type: 'mined',
       event: 'Transfer',
       args: [Object] } ] }
  • 查看账户1转账后的余额
MetaCoin.deployed().then(function(instance){return instance.getBalance(web3.eth.accounts[1]);}).then(function(value){return value.toNumber()});

运行结果:

500
  • 查看账户0转账后的余额
MetaCoin.deployed().then(function(instance){return instance.getBalance(web3.eth.accounts[0]);}).then(function(value){return value.toNumber()});

运行结果:

9500

你可能感兴趣的:(以太坊,智能合约,区块链)