I am currently developing a smart contract with several states:

enum State {created, accepted, over}

Depending on the state, users are allowed to perform different action. What I would like to do, is to automatically trigger a function when "accepted" has been set, without calling the function. The reason for that is that I want users to proceed step by step: I want the user to see that the "accepted state" is the current one, and that another function was called to transition to the "over" state.

Let's imagine the following code: I want the "close" function to be triggered when currentState becomes "accepted" but I still want the function accept() to return a value after currentState has been set.

function accept() public onlyState(State.created) payable returns (bool) {
    player2 = msg.sender;
    currentState = State.accepted;
    return true;

function close() internal onlyState(GameState.accepted) returns (bool) {
        currentState = State.over;
        return true;   
  • 1
    function close() internal or function close() public? – Xavier Leprêtre B9lab Oct 18 '18 at 11:21
  • Internal. I know I could call it from outside on accept() return though – YohjiNakamoto Oct 18 '18 at 11:50
  • Would calling close(); before return true; do the trick? – Xavier Leprêtre B9lab Oct 18 '18 at 13:30
  • Yes, I think this is the way. And fire an event before to notify the client that the state has become "accepted" – YohjiNakamoto Oct 18 '18 at 15:57

I think you should use a state machine design pattern here. There is a good example here https://medium.com/tokenfoundry/a-solidity-implementation-of-the-state-machine-design-pattern-25de8b1dfbc5

It can help you understanding callback function used in a smart contract.

On entering the new state, variable value changes may occur, or certain functionality may be carried out automatically. The functions that must be executed on entering a new state are callback functions.

| improve this answer | |

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.