1

I'm using this (State Machine) design pattern in solidity and having an error when changing the state after executing the first function. Any hint? Thank you!

Error: revert The transaction has been reverted to the initial state. Note: The constructor should be payable if you send value. Debug the transaction to get more information.

Sorry I didn't include my sample code.

Here's my sample code.

pragma solidity ^0.4.24;

contract BankATM {

    enum Stages {
        ServingCustomer,
        Finished,
        Idle
    }

    address owner;

    mapping(address => uint256) balances;

    Stages public stage = Stages.ServingCustomer;

    constructor () payable public {
        owner = msg.sender;
    }

    modifier atStage(Stages _stage) {
        require(stage == _stage);
        _;
    }

    modifier transitionAfter() {
        _;
        nextStage();
    }

    modifier timedTransitions() {
        if (stage == Stages.ServingCustomer) {
            nextStage();
        }
        if (stage == Stages.Finished) {
            nextStage();
        }
        _;
    }

    function deposit(uint _amount) public payable timedTransitions atStage(Stages.ServingCustomer) {
        balances[msg.sender] += _amount;
    }

    function txFinished() public timedTransitions atStage(Stages.Finished) transitionAfter returns(uint) {
        return getBalance();
    }

    function idleNow() public view atStage(Stages.Idle) returns (uint) {
        return getBalance();
    }

    function getBalance() internal view returns (uint256){
        return balances[msg.sender];
    }

    function nextStage() internal {
        stage = Stages(uint(stage) + 1);
    }
}
  • It looks like you are doing some mistake in function call. Are you calling the bid function after successful deployment of contract? – Aniket Dec 4 '18 at 8:06
  • @A.K. Hi, I updated my post with my sample codes. – mrgx Dec 4 '18 at 8:34
0

add this

constructor() public payable{
}

When you create contract creation transaction

if send ETH together, need payable option in constrcutor

| 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.