区块链开发学习第五章:truffle-unbox使用

使用truffle模板能极大的提高我们学习区块链的进度以及了解智能合约

一、下载MetaCoin模板

truffle unbox metacoin

 

这里不知道是网络问题还是环境问题,搞了半天都没搞好,无论是K学上网还是设置hosts,都没用,最后只能通过git clone下载metacoin代码。

truffle官网地址:https://www.trufflesuite.com/boxes

git上下载truffle各模板地址:https://github.com/truffle-box

 

下载完之后同样需要修改truffle-config.js文件里面的私有链地址 

 

二、truffle test

 

 

三、truffle compile

这里要开启挖矿

 

 

 四、truffle console

 

五、控制台与合同进行交互

1、查看部署合约的账户metacoin余额

MetaCoin.deployed().then(function(instance){return instance.getBalance(web3.eth.accounts[0]);}).then(function(value){return value.toNumber()});

 

 这里出了问题,提示是地址不合法,我查了合约代码应该是没问题来着,结果把web3.eth.accounts[0]换成我们私有链的第一个默认账户地址就可以了

MetaCoin.deployed().then(function(instance){return instance.getBalance('0x229ea411d368c97b008c7bc19b01fdd813163701');}).then(function(value){return value.toNumber()});
10000

为什么这里会有10000余额呢,我看了合约代码原来是这样的:

 

我们前面创建了两个账户,现在查一下第二个账户:

MetaCoin.deployed().then(function(instance){return instance.getBalance('0xbb723ee82cb41a733f2289588697f0e199e29de5');}).then(function(value){return value.toNumber()});
0

合约里面getBalanceInEth方法写了一个metacoin值两个eth,所以调用下换算方法:

MetaCoin.deployed().then(function(instance){return instance.getBalanceInEth('0x229ea411d368c97b008c7bc19b01fdd813163701');}).then(function(value){return value.toNumber()});
20000

调用sendCoin转账方法(开启挖矿):

MetaCoin.deployed().then(function(instance){return instance.sendCoin('0xbb723ee82cb41a733f2289588697f0e199e29de5', 500);});
{ tx:
   '0x8716dab16ffb66b23604c476643105929cab24890914592f8eecd2ebb2f72020',
  receipt:
   { blockHash:
      '0xc94a716ba1463740bc85ceb7be6abaccd09d1c1f329642630a685a3b60785f7f',
     blockNumber: 546,
     contractAddress: null,
     cumulativeGasUsed: 51520,
     effectiveGasPrice: '0x3b9aca00',
     from: '0x229ea411d368c97b008c7bc19b01fdd813163701',
     gasUsed: 51520,
     logs: [ [Object] ],
     logsBloom:
      '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000100000000000000000000004000010000000000000000000000000000000000000000000000000000000000000000000000100000000104000000000000000000000000000000000004000000020000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000100000',
     status: true,
     to: '0xaed8bef575a739c7239b4a140c789b2f41043279',
     transactionHash:
      '0x8716dab16ffb66b23604c476643105929cab24890914592f8eecd2ebb2f72020',
     transactionIndex: 0,
     type: '0x0',
     rawLogs: [ [Object] ] },
  logs:
   [ { address: '0xAED8BEf575A739C7239B4a140c789B2f41043279',
       blockNumber: 546,
       transactionHash:
        '0x8716dab16ffb66b23604c476643105929cab24890914592f8eecd2ebb2f72020',
       transactionIndex: 0,
       blockHash:
        '0xc94a716ba1463740bc85ceb7be6abaccd09d1c1f329642630a685a3b60785f7f',
       logIndex: 0,
       removed: false,
       id: 'log_4626e8e8',
       event: 'Transfer',
       args: [Result] } ] }

转账完查看两个账户余额:

##账户一
MetaCoin.deployed().then(function(instance){return instance.getBalance('0x229ea411d368c97b008c7bc19b01fdd813163701');}).then(function(value){return value.toNumber()});
9500

##账户二
MetaCoin.deployed().then(function(instance){return instance.getBalance('0xbb723ee82cb41a733f2289588697f0e199e29de5');}).then(function(value){return value.toNumber()});
500

 

 

如果有学习计划的童鞋,可以加我QQ一起交流:

posted @ 2021-11-11 22:24  请叫我胖子  阅读(782)  评论(0编辑  收藏  举报