1

From what I under stands when the condition in require() is not met solidity performs revert operation. Right?

What I want to know is how can we handle this in web3. That is if i have a function in my contract, say getbalance, and I am calling this function from web3, and that it checks whether the caller is the owner of that contract.

contract myContract{
  .......
  function getbalance() public constant returns(address){
      require(msg.sender==owner);
       .....
  }
 }

what I am asking is if the require() function false, i.e. caller of the function is not the owner, what should I do in my web3 to handle it?

  • The transaction receipt will return the status: 0x0 from Byzantium version you can catch that value in the return of the transaction – Eduardo Pereira Nov 6 '17 at 19:41
1

There's no way to handle reverts on the front-end. You will need to do the checking client-side to prevent such cases from happening if you want to have an acceptable user experience. In this case, it would mean disabling the button if the current account is not the owner of the contract so it can't ever be called.

  • ok. thanks for answering. So if i have to avoid revert, checking should be performed in javascript. then why should I use require, checking is already performed! – user8106215 Nov 7 '17 at 0:52
  • You have to do both, if you don't do the checking in the contract anyone can call the function directly on it. Doing the checking inside the contract is extremely important. – pabloruiz55 Nov 7 '17 at 0:54
  • oh, like calling from another front-end with out checking in it? – user8106215 Nov 7 '17 at 1:00
  • Not necessarily another front-end. Anyone can call a function of your contract from MEW or by creating a contract that interacts with another deployed contract. – pabloruiz55 Nov 7 '17 at 1:03

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.