Share:
Notifications
Clear all

Earn While Learning Web3 Programming>> Start Here FREE!!!

[Solved] REQUIRE Error Handling In Solidity - Web3 Programming

1 Posts
1 Users
2 Reactions
791 Views
1
Topic starter

EXERCISE:
i. Re-Write the Solidity Smart Contract in this course video on your own

 

ii. Add Require to the addFund Function which ensures only the wallet that deploy the Smart Contract
can add new funds to their wallet, when other tries it it throw revert error "You Are Not The Contract Deployer" (google, rewatch previous videos if you have to):

 

iii. Post your code in this forum:

(NOTE: please, don't go and copy paste from other students in the forum, try things on your own, so you can master it yourself)

 

 

 

NOTE:

This Is An Exercise From Our FREE Smart Contract Development With Solidity For Beginners Course (If you have not enrolled yet, you can join the course totally for FREE).

 

1 Answer
1
Topic starter

REQUIRE error handling in Solidity is used to validate user input to be free of errors before a function is executed. When error detected in user input, it throws REVERT ERROR in solidity which causes all previous executions in that particular transactions to be rendered invalid as if it was never attempted before including gas refunded.

Require in solidity is written as:

require (condition that must be true before the rest of the function code is run/executed) {}

/*

below required statement  can be put in a transfer function to ensure users has enough amount before been allowed to transfer

*/

require (Balance[msg.sender] >= _amountToTransfer, "Balance Too Low");

 

function transferFund(uint _amountToTransfer) public {

       //Balance mapping

      mapping (address => uint) Balance;

 

     //require check and if no error - do transfer

    require (Balance[msg.sender] >= _amountToTransfer, "Balance Too Low");

    //transfer

    msg.sender.transfer(_amountToTransfer);

 

}

 

 

SEE=> Another similar Error handling in Solidity with sample code is ASSERT

Share: