1

I'm trying to make a smart contract and have a function executed on state change. In my example below, this function is finish. It must run automatically when the state changes to end. Is this possible with Solidity?

pragma solidity ^0.4.16;

contract SmartContract {
  address owner;
  string state;
  string status;
  function SmartContract() public {
    status = 'Pending';
    owner = msg.sender;
  }

  event update(string _msg, string _state);

  function getState() public constant returns(string) {
    return state;
  }

  function getStatus() public constant returns(string) {
    return status;
  }

  function setState(string _state) public returns(string) {
    state = _state;
    return state;
  }

  function finish() public {
    if (keccak256(state) == keccak256("end")) {
        status = 'Completed';
        update("Completed", state);
        selfdestruct(owner);
    }
  }

  function () public payable {

  }
}
1
  • 1
    Can't you just add if(_state=="end") {finish();}?
    – StanislavL
    Commented Jan 10, 2018 at 14:09

1 Answer 1

1

I am quite sure it isn´t possible to use a smart contract like a cron. I will investigate more and return here if I find more about it.

But for now, you may create a job/service that could watch your update event (https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethfilter) and then execute a transaction calling finish().

Or you can add an if check inside setState. If state == "end", execute finish().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.