TransWikia.com

Call a smart contract function when condition met

Ethereum Asked by YohjiNakamoto on December 18, 2021

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;   
}

One Answer

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.

Answered by Luiz Soares on December 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP