0

Let's supose this is my contract:

Contract MyContract{
   uint a = 0;

   function myFunction(){
      a = 0;
   }
}

I want "myFunction" to fire up when "a" value is 10. Thanks.

2

Solidity variable values can only be set from the contract. So you will definitely need some sort of setter functionality for the variable to be set to different values.

Furthermore there is no functionality to "watch" a variable's value or something similar. You simply have to do the monitoring yourself. So when you implement functionality to set the variable's value you can also check whether the new value is the desired value and then trigger a function. Also the keyword contract has to be with lower case. So something like this:

contract MyContract{
   uint a = 0;

   function myFunction() public {
      a = 0;
   }

   function increaseVariable() public {
       a++;
       if (a == 10) {
           myFunction();
       }
   }
}
  • Very grateful. Just what I need. By the way, how "myFunction()" is paid? It takes gas from contract's balance? – Lemonio Thuma Dree Jul 16 at 18:23
  • no, the gas is from the same transaction as which calls increaseVariable. Gas cost is never taken from contract balance, always from the one who sends the initial transactions. – Lauri Peltonen Jul 16 at 18:51

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.