0

Disclaimer: I'm a noob when it comes to programming altogether let alone smart contracts.

I refactored this code that was written like three years ago so it could work with solidity 0.5.0. I deployed it on Remix's Javascript VM but the addPayee function is not working: transact to Donation.addPayee errored: VM error: revert. Please help thanks :(

Also, I'm not sure that the contract is assigning me (the deployer) as the owner.

contract Donation {


  address public owner;

  struct Payee {
    bool status;
    uint weight;
    uint balance;
  }

  e.
  mapping(address => Payee) public payees;
  mapping (int8 => address) public payeesIndex;

  int8 public payeesIndexSize;

  function Donations() private {
    // Set the address of the contract deployer to be owner.
    owner = msg.sender;
    payees[owner].status = true;
    payees[owner].weight = 10;
    payeesIndex[0] = owner;
    payeesIndexSize = 1;
  }

  // Check if current account calling methods is the owner.
  modifier isOwner() {

      require (msg.sender == owner);
    _;
  }

  // Aggregate all payee weights.
  function getTotalWeight() public view returns (uint) {

    int8 i;
    uint totalWeight = 0;

    for (i=0;i<payeesIndexSize;i++) {
       if (payees[payeesIndex[i]].status == true) {
         totalWeight += payees[payeesIndex[i]].weight;
       }
    }

    return totalWeight;
  }


  // Add a new payee to the contract.
  function addPayee(address _payee, uint _weight) isOwner public returns (bool) {

    payees[_payee].weight = _weight;
    payees[_payee].status = true;
    payeesIndex[payeesIndexSize] = _payee;
    payeesIndexSize++;

}

function getStatus(address _address) public view returns(bool)  {
    return payees[_address].status;
  }
2

function Donations() private {

This is not the same name as the contract, which would be the old style of constructor. If you rename to Donation(), the compiler will probably recognize you intend for this to be a constructor and warn you that the syntax should be changed to the new explicit form:

constructor() public {

There may be other issues, but your hunch is correct. It isn't setting owner to msg.sender on deployment, so that would make isOwner fail.

BTW, you can check owner() in Remix (because it is public) and you should see your sender address echoed back (usually 0x35c...), not 0x0000.... as it probably is presently. You will need that much to work as expected in order to press on for the next issue. ;-)

Hope it helps.

Update

This line:

payeesIndex[payeesIndexSize] = _payee

Should be payeesIndex.push(_payee);

Because you can't write past the end of a dynamic array.

| improve this answer | |
  • Exactly!!! I was so confused to why the address of the owner is 0x000.. Thank you very much <3 – Basém Alasi Sep 24 '19 at 16:31
  • Sorry but it seems that I couldn't quite get what I should change because it's still throwing me the same error. Is it: function Donations private(){..} >>> constructor() public {...} ? – Basém Alasi Sep 24 '19 at 23:58
  • That's correct. The owner result show that much is working. You are probably running into the next issue. You can add reasons to require and revert statements in 0.5.x so you might be able to help yourself a bit that way. – Rob Hitchens Sep 25 '19 at 0:44
  • Be sure you are sending from the owner account because it is designed to fail if that is not the case. I can a closer look later. Not possible now. – Rob Hitchens Sep 25 '19 at 0:46
  • See the updated answer. I don't think you need the payeeIndexSize at all. – Rob Hitchens Sep 25 '19 at 2:38

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.