Truffle使用web3.js调试智能合约的一些备忘

进入develop调试模式

1
2
$ truffle compile && truffle develop
$ deploy

deploy之后,我们可以在testrpc上调试智能合约。

获取地址balance

1
2
truffle(develop)> web3.eth.getBalance('0x627306090abab3a6e1400e9345bc60c78a8bef57').toNumber()
96722887800000000000

注意balance的单位是wei。转换成eth则是

1
2
truffle(develop)> web3.fromWei(96722887800000000000, 'ether')
'96.7228878'

payable的函数调用

1
Loto.deployed().then(function(instance){return instance.Buy(5, {value: web3.toWei(0.1, "ether")});}).then(function(value){return value});

纯Get函数调用

1
Loto.deployed().then(function(instance){return instance.getSlots.call();}).then(function(value){return value});