Solidity

從合約中提取乙太坊代幣的提取功能表現很奇怪

  • January 12, 2022

我從 solidity 文件中複製了一個提款功能並將其添加到我的契約中

   function withdraw() public onlyOwner {
     uint256 ownerBalance = address(this).balance;
     require(ownerBalance > 0, "ERRFUN");
     payable(owner()).transfer(ownerBalance);
     emit ChangeEtherBalance(ownerBalance);
   }

觀察。我的契約用途:

import "@openzeppelin/contracts/access/Ownable.sol";

問題是,我在測試的時候發現,在函式呼叫之後,合約的餘額是零,然而,所有者的餘額卻低於之前的餘額。這是我的測試程式碼和控制台命令的列印:

   describe('Test withdraw() method. Sucess', () => {
   it('withdraw success', async function () {
       const ownerBalanceBefore = await web3.eth.getBalance(owner);
       const vendorBalanceBefore = await web3.eth.getBalance(this.managementCenter.address);
       console.log("owner init Balance",ownerBalanceBefore) //owner init Balance 995570313620188160000
       console.log("vendor init Balance", vendorBalanceBefore) //vendor init Balance 257200000000000
       // withdraw operation
       const txWithdraw = await this.managementCenter.withdraw({ from: owner });

       // Check that the Vendor's balance has 0 eth
       const vendorFinalBalance = await web3.eth.getBalance(this.managementCenter.address);
       expect(vendorFinalBalance).to.be.bignumber.equal(new BN(0));

       const ownerFinalBalance = await web3.eth.getBalance(owner);
       console.log("vendor Final Balance", vendorFinalBalance) //vendor Final Balance 0
       console.log("owner Final Balance",ownerFinalBalance) //owner Final Balance 995568878760188160000          
       expect(new BN(ownerFinalBalance))
           .to.be.bignumber.greaterThan(new BN(ownerBalanceBefore));            

       // Check the the owner balance has changed
       expectEvent(txWithdraw, 'ChangeEtherBalance', { value: new BN(vendorBalanceBefore)});
   });
});    

因為那,線

expect(new BN(ownerFinalBalance))
       .to.be.bignumber.greaterThan(new BN(ownerBalanceBefore));  

碰撞。

我是使用 ganache-cli 和 Truffle v5.4.10 (core: 5.4.10) Solidity - 0.8.7 (solc-js) Node v16.9.1 Web3.js v1.5.2

有任何想法嗎?提前致謝

問題是要檢索的數量 257200000000000 小於 84831 * 10000000000 = 8,4831×10^14 的 gas 成本。

引用自:https://ethereum.stackexchange.com/questions/118555