0

recently I met a very strange problem, when I deployed a ERC20 ICO contract to Repsten network through truffle and MetaMask.Then I send some ETHs to my ERC20 contract address, I expected is that when contract deadline is up or received ETHs amount is more than funding goal, I can withdraw ETHs in the contract.

Actually it worked well as I expected when contract received ETHs amount is Integer number.

But when contract received ETHs amount includes float number, such as 0.1,0.2,1.1, then it will happen 'VM Exception while processing transaction: revert' as the title described. Anyone met this problem before? The detail information is as follows, PLS help me to resolve this problem.

Thanks in advance.

  1. first I deployed a ERC20 contract to Ropsten network. The fallback function like following.

    enter image description here

  2. I send 0.5 ETH to my contract address and contract received 0.5 ETH. as deadline is up, I send 0 ETH to my contract address to trigger contract's fallback function transfer ETH balance to beneficiary. But it's failed. And in ropsten.etherscan.io it prompts 'transaction revert' . contract link: https://ropsten.etherscan.io/address/0x611d807ea4c3fa9bfb5c7765642fc8be534c9f3e

enter image description here enter image description here

  1. I deployed another contract with same code, I send 1 ETH to contract address, as deadline is up, I send 0 ETH to my contract address to trigger contract's fallback. Beneficiary can receive ETH successfully.

    contract link:https://ropsten.etherscan.io/address/0x9990282fc9734ef8e6b0073a3537938a189f7afd

mark ma
  • 1
  • 2

2 Answers2

0

i didn't go deeply through your code but you shouldn't define much operations in the fallback function. For security reason a fallback function has a fixed stipend of 2300 gas. So it won't execute the code you are putting in it. you'll need to define a payable function which should be called to send ethers to the contract

for example

    function deposit() payable{
 assert(!haveTransferEth);
    if(haveTransferEth){
        revert();
    }
...
Badr Bellaj
  • 18,780
  • 4
  • 58
  • 75
  • many thanks for your answer, I'll move some operations to a payable function. But as I said in step 3. Sometimes it works well. So I don't know what's the root case cause this difference. I think EVM should give clear hint. – mark ma Apr 10 '18 at 08:47
0

after I review my contract code, I found that cause this error is due to the accuracy of exchange ETH and token.

eg. When sender's ETH amount is less than 1, the contract would give sender 1 token, after contract deadline terminated, contract need to send token balance to beneficiary. Actually

tokenBalance = tokenTotalSupply - 1 

But I regard this situation as

tokenBalance = tokenTotalSupply - 0

So there is not enough tokenBalance transfer to beneficiary.

mark ma
  • 1
  • 2