0

I have two contracts. One to manage quests and one to manage units. These units have an uint exp and are mapped to an id by mapping(uint256 => Unit) public tokenToUnit;. Now I want to use the Quest Contract to increase the exp from an unit inside the Unit Contract.

How can I edit an uint of a struct which is mapped to an id?

What I've tried:

Contract Units.sol

function addUnitExp(uint256 _unitId, uint256 _exp, uint32 _type) public {
    Unit storage _unit = tokenToUnit[_unitId];
    _unit.defense = _exp;
}

Contract Quests.sol

function addUnitExp(uint256 _unitId, uint256 _exp) public {
    UnitsInterface data = UnitsInterface(unitsContract);
    data.addUnitExp(_unitId, _exp);
}

Error: VM Exception while processing transaction: revert

PS: Etheremon is doing similar (increaseMonsterExp) here.

Thanks a lot :)

Code to reproduce:

Units.sol

pragma solidity ^0.4.24;
contract Units {
    uint totalUnits;

    mapping(uint => Unit) public idToUnit;

    struct Unit {
        uint id;
        uint exp;
    }

    constructor() public {
        Unit storage _unit = idToUnit[totalUnits];
        _unit.id = totalUnits;
        _unit.exp = 0;
        totalUnits++;
    }

    function increaseUnitExp(uint _id, uint amount) public {
        Unit storage obj = idToUnit[_id];
        obj.exp = amount;
    }
}

Quests.sol

pragma solidity ^0.4.24;
contract DataBase {
    function increaseUnitExp(uint _id, uint amount) public;
}
contract Quests {

    DataBase dc;

    constructor(address _dataContract) public {
        dc = DataBase(_dataContract);
    }

    // gate way 
    function increaseMonsterExp(uint _id, uint _amount) public {
        dc.increaseUnitExp(_id, _amount);
    }

}
  • Well, you've got onlyModerators up there, what did you expect? – goodvibration Aug 14 '18 at 16:57
  • @goodvibration thanks, but I tried the same without using the onlyModerators modifier and still get the same revert error. – Tobias Thiele Aug 15 '18 at 7:51
  • Are you passing the correct address to Quests constructor? If it is not set correctly calling an invalid method cause a revert. – Ismael Aug 16 '18 at 1:31
1

You got onlyModerators modifier in Units.sol. So when you call addUnitExp in Units.sol from Quests.sol, the msg.sender is Quests.sol, and not the user who called that function.

  • thanks, but I tried the same without using the onlyModerators modifier and still get the same revert error. – Tobias Thiele Aug 15 '18 at 7:51

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.