0

I have a simple contract that saves how many people payed, and, if you pay, you can see how many people payed. A simple idea to learn smart contracts.

The problem is that the return value doesn't change. When I first tried with a more complex contract, the method findHowManyPeoplePayed returned 3963877391197344453575983046348115674221700746820753546331534351508065746944 and now, this stripped down version returns 0. It shouldn't return such value. In fact, it should return an error because the address hasn't payed to have access to that information.

I payed with the stripped down version and it didn't update the value, it still returns 0.

Deploying the contract to ganache and running the tests works as expected.

This is my test contract:

pragma solidity ^0.4.19;

contract PayEther {
    mapping(address => bool) payedCustomer;
    uint64 private peopleThatPayed;
    address private owner;

    constructor(uint minimumAmount) public {
        owner = msg.sender;
        peopleThatPayed = 0;
    }

    function pay() payable public {
        require(msg.value > 0, "Address didn't send enough money.");
        payedCustomer[msg.sender] = true;
        peopleThatPayed++;
    }

    function findHowManyPeoplePayed() view public returns (uint64){
        require(payedCustomer[msg.sender], "Address never payed!");
        return peopleThatPayed;
    }
}

This is it's abi:

[
  {
    "constant": false,
    "inputs": [],
    "name": "pay",
    "outputs": [],
    "payable": true,
    "stateMutability": "payable",
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [],
    "name": "findHowManyPeoplePayed",
    "outputs": [
      {
        "name": "",
        "type": "uint64"
      }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [
      {
        "name": "minimumAmount",
        "type": "uint256"
      }
    ],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "constructor"
  }
]

I have this contract deployed to 0x6CD65D376104B404B74907029DAbd74B8736A06f in Rinkeby.

The original version used a simple uint but I decided to change it to uint64 to see if this helped in some way.

Am I missing something? Why isn't the contract working as intended? Why doesn't payContract.methods.findHowManyPeoplePayed().call() returns an error because the user hasn't payed?

0

I would concentrate on your client-test testing methodology.

When I loaded the code into Remix I found it does not compile under 0.4.19 as specified. You must be using a later compiler such as 0.4.24 because you're using the new constructor() keyword. Also, there is an unused argument in the constructor.

The uint64 idea won't help. A simple uint will suffice.

Here it is in Remix so you can see it working.

enter image description here

The client-side testing methodology could be throwing you off. For example, are you waiting for your transactions to mine before expecting the value to increment?

  • 1
    It was a mix between that and that I wasn't setting the correct user in my call function. Fixed with this: payContract.methods.findHowManyPeoplePayed().call({from: accounts[0]}) – Javier Bullrich Dec 4 '18 at 17:54

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.