If you are creating a game where your character receives xp after every time they play, how can you prevent people from just calling the "addXP()" function from the contract and giving their player infinite xp?

Thank you for the help!


You can make that function internal in solidity, so it can only be called from within the contract.


function addXp(uint8 amt) internal {}

This will disallow any external calls.

Alternatively, if you want to call it from outside the contract but only want a specific address to be able to call it, you can do something like:

modifier OnlyOwner { require(msg.sender == owner); _; } 

and the function be like

function addXp(uint8 amt) OnlyOwner { }

Keep in mind that modifier syntax is probably off and you should make sure I was right, but the idea is still there.

| improve this answer | |
  • But if you are building an HTML5 game, you need to be able to increase XP based on certain actions (say finishing a level). So it needs to be called from outside the contract (from the game) but not from other people. – connorvo Feb 25 '18 at 22:17

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.