Using solidity 0.4.18 I have the following contract:

contract MyContact {

    mapping(address => uint) private balances;

    modifier positiveNumber(uint number) {
        require(number > 0);

    function deposit(uint amount)
      balances[msg.sender] += amount;

    function hasFunds(address depositor, uint amount)
        returns (bool)
        return balances[depositor] >= amount;

I have the following tests (trimmed for relevance)

it('punter can\'t deposit negative amount', async () => {
  await assertThrows(myContract.deposit(-1, { from: punter }))

it('negative amount fails', async () => {
  await assertThrows(myContract.hasFunds(punter, -1))

The first test passes.

The second test fails.

If I try a test like

it('zero amount fails', async () => {
  await assertThrows(myContract.hasFunds(punter, 0))

That test passes.

It seems that with a view style function the -1 is not actually being seen as a negative number.

Is there something special about view functions that would cause this to happen, or am I scapegoating?


Okay so I worked this out. The u in uint is 'unsigned' (duh) so when javascript sends in -1 Solidity sees it as 1.157920892373162e+77.

In my actual code I had an additional modifier that checked that the amount deposited was actually approved (depositing ERC20 tokens) and that's why the test was working.

Nothing like coding solidity to make one feel like an utter noob.

  • What did you do then? Did you just remove the tests involving checking for a negative number? – Thanh-Quy Nguyen Jun 15 '18 at 14:37
  • 1
    @Thanh-QuyNguyen yes – Dave Sag Jun 20 '18 at 5:53

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.