0

I just got to start learning the smart contracts, and I have a beginner question.

Before coding, I am doing a top-down research. I read a lot about the if-this-than-that. However, I do not hear about the "else", that is, if the conditions are not met, is it possible to execute? For example, if some person A does not send me the right amount of money, execute the contract, otherwise, do not. Can this be done ? How about, if A does not send me the right amount money, execute some other contract that would correct an error (either subtract the money from my account or from their account, and file a report to management (lets say) )? Furthermore, can the contract be time-based? For example, if A does not send me the money on Monday, execute the contract and file a report to management.

1

For example, if some person A does not send me the right amount of money, execute the contract, otherwise, do not. Can this be done ?

Yes, of course. There are several ways you can do that.

Say the base function is:

//WATCH OUT: Not tested for potential vulnerabilities
function sendMoney(uint _money) payable{
  require (_money <= this.balance);
  msg.sender.transfer(_money);
}

The function above basically sends eth from the contract to whoever calls this function.

We already have an "if-else" in there. If there's not enough balance in the contract the function will not execute.

You could also add a require or if-else, like this:

//WATCH OUT: Not tested for potential vulnerabilities
    function sendMoney(uint _money) payable{
      require (_money <= this.balance);
      if(_money < 1 ether)
        msg.sender.transfer(_money);
      else
        //Do something else. Don't do anything, revert, call a function on another contract, etc, etc.
    }

Furthermore, can the contract be time-based? For example, if A does not send me the money on Monday, execute the contract and file a report to management.

Yes, but it's not that simple. You can check times inside the contract. So when you call the function above, you could check if now is lesser or higher than some arbitrary date and act upon it. What you can't do easily is have this function run automatically at some given point in time. There's a few alternatives for that, such as http://www.ethereum-alarm-clock.com/ that allows you to schedule a transaction.

  • Therefore, all the contracts can act as the nodes and it is not DFA (Deterministic Finite Automaton) but NFA when put into a chain of linked contracts. It means that programming can be done just by linking the contracts into a chain of executions, and that a decentralized computer/machine can be created with those linked contracts... Nice. – Damir Olejar Oct 28 '17 at 13:29

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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