Trying to run the following code in solidity 0.5.3 with similar compiler in Remix:

function deposit() public payable {
        Maker storage depM = addressToMaker[msg.sender];
        depM.contributionBalance += msg.value;

With contributionBalance being a int type in the Maker struct.

And get the following error:

browser/PHBacon.sol:33:9: TypeError: Operator += not compatible with types int256 and uint256
        depM.contributionBalance += msg.value;

If I set it to uint it compiles with no errors. Any thoughts?

  • What is your question? Is there any reason you don't want to add to a uint? – Jesbus Jan 30 '19 at 13:48
  • Hey Jesse, I just want to increment depM.contributionBalance by the msg.value but do not understand why += is not working for the int type variable. I want the contributionBalance to also be able to represent a negative number (e.g. somebody has withdrawn more than deposited). – Valentijn van den Hout Jan 30 '19 at 13:54

UINT is not directly castable to INT or viceversa, both because UINT maximum positive value is near the double of the maximum positive value of INT and because it is not clear how to map those INT negative values.

+= can be used if both the operands are of the same type only.

If you need to add those operands, you can write a function which accept uint and returns int, managing in the way you prefer what happens at big positive values and/or negative values and so on.

The typecast operators cannot do it for you.

  • 1
    Thanks for your reply Rick. Totally overlooked that. Can I typecast msg.value to an int as @sp4cs suggested or is this a bad idea? Am I correct in thinking that the max positive for an int is 2^128 -1? That would mean that msg.value would have to be worth more than a Sextillion USDollar (2^128 wei) for it to show issues. – Valentijn van den Hout Jan 30 '19 at 14:21
  • 1
    No you can’t simply cast it because the cast operator do not know what you are saying. You can write your simple function which maps in the way you intend, which accept int and returns uint. – Rick Park Jan 30 '19 at 14:24
  • Got it. I'll give that a shot, or reconsider how I want to track contribution balance. Big thanks! – Valentijn van den Hout Jan 30 '19 at 14:29
  • 1
    You are welcome. Please mark as accepted the answer as well. Have good work. – Rick Park Jan 30 '19 at 14:30

Just type cast msg.value to int using int(msg.value).

  • What shall be expected as result if msg.value shall be higher that the maximum positive INT value? – Rick Park Jan 30 '19 at 14:07
  • @RickPark If msg.value is greater than the maximum signed 256-bit integer, Ethereum is broken because nowhere near that much ETH exists or will ever exist :) – Jesbus Jan 30 '19 at 15:28
  • 1
    As a compiler I do not know nothing about that... 😉 – Rick Park Jan 30 '19 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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