Truffle
使用 mocha.js 和 truffle 發送乙太幣可能會導致比預期更大的金額
再會,
我有一個契約 General 和一個圖書館 GenLib。
library GenLib{ event MoneyIn (address _from, uint256 _amount); function moneyIn() { MoneyIn(msg.sender, msg.value); <- Only call the event MoneyIn() } } contract General{ function getMoney () payable{ GenLib.moneyIn(); } }
現在奇怪的是,當我嘗試使用該
getMoney
函式向 General 合約匯款時,實際轉賬的金額是指定金額的兩倍。那是我用來檢查契約的 JS 文件:
var Gen = artifacts.require("General"); contract('Gen', function(accounts) { var user = accounts[0]; var valueP2C = web3.toWei(2, "ether"); it("Deploy Gen contract", function(){ return Gen.deployed().then(function(instance){ gen = instance; }); }); it("Gen account is empty", function(done){ web3.eth.getBalance(gen.address, function(err, res){ done(assert.equal(res.valueOf(), 0, "")); }); }); it("Use account is full", function(done){ web3.eth.getBalance(user, function(err, res){ userAmount = res.valueOf(); done(assert.equal(res.valueOf(), 0, "")); }); }); it ("Send money from user to gen", function(done){ gen.getMoney({from: user, value: valueP2C}).then(function(res){ done(assert.notEqual(res, null, "")); }); }); it("Gen recieved money", function(done){ web3.eth.getBalance(gen.address, function(err, res){ done(assert.equal(res.valueOf(), valueP2C, "")); }); }); it("User account is P2C lower", function(done){ web3.eth.getBalance(user, function(err, res){ done(assert.equal(res.valueOf(), userAmount - valueP2C, "")); }); }); });
以及最終結果:
✓ Deploy Gen contract ✓ Gen account is empty ✓ User account is full ✓ Send money from user to gen 1) Gen received money > No events were emitted 2) User account is P2C lower > No events were emitted expected '4000000000000000000' to equal '2000000000000000000' <-TWICE THE AMOUNT expected '87641250800000000000' to equal 89643554700000000000
另一條可能有用的資訊是,每當我刪除對外部庫的呼叫時,一切正常。
我對這種奇怪的行為感到困惑,如果您能提供任何幫助或見解,我將不勝感激。
你感到困惑是對的——你觀察到的行為是錯誤的。
這是 testrpc 的一個未解決問題,正如您所說,它與庫函式的呼叫有關: https ://github.com/ethereumjs/testrpc/issues/122
抱歉,我無法提供更多幫助 - 您可以密切關注該問題何時正式修復!
它已在最新的4.1.0 版本中修復