2

If I call a function in another contract that doesn't return a boolean, but has throw() invariant checks, is it safe to assume the function finished properly if the transaction isn't reverted? I ask in the case of a token transfer being called by another contract, since there is no way to know within the contract if the token contract was a success. (ERC-20)

Assume the rest of SampleToken is filled out according to ERC-20 standards.......

contract SampleToken {

  /* Send coins */
  function transfer(address _to, uint256 _value) {
    if (balanceOf[msg.sender] < _value) throw;           // Check if the sender has enough
    if (balanceOf[_to] + _value < balanceOf[_to]) throw; // Check for overflows
    balanceOf[msg.sender] -= _value;                     // Subtract from the sender
    balanceOf[_to] += _value;                            // Add the same to the recipient
    Transfer(msg.sender, _to, _value);                   // Notify anyone listening that this transfer took place
  }

}

contract LockContract { 

  function lockTokens(uint256 _period, uint256 _amount)
  external
  returns (bool) { 
    require (sampleToken.transferFrom(msg.sender, this, _amount));    // Tokens are now transfered into this contract
    return true;
}

  function unlockTokens(uint256 _amountLocked) 
  external
  returns (bool) {
    sampleToken.transfer(msg.sender, _amountLocked);     // This doesn't return boolean, but will throw if error
    return true;
  }

}
1
  • Can you put some piece of code? so that It becomes more clear what you want to do
    – Ajay
    Commented Jan 14, 2018 at 15:12

2 Answers 2

0

Save the old balance in a variable after transfer has been completed then return a tuple with the old balance and new balance.

old_balance = balanceOf[msg.sender];
//transfer here.
return (old_bal,balanceOf[msg.sender]);

This give you the proof that transfer happened.

Or

simply check your new balance.

1
  • I'm worried that the user may transfer the tokens to mimic a similar balance change. Also I don't think the contract could know whether any new balance may have come from a different user. (unless a listener notifies it of a Transfer() event being triggered, which I'd like to avoid)
    – arete
    Commented Jan 14, 2018 at 16:05
0

I would adapt the transfer function like this

  function transfer(address _to, uint256 _value) returns (bool) {
    ...
    return true;
    }

and then i would adapt eg lockTokens like this:

bool result=sampleToken.transferFrom(msg.sender, this, _amount);
require (result==true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.