Share:
Notifications
Clear all

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

[Solved] ASSERT Error Handling In Solidity

1 Posts
1 Users
2 Reactions
404 Views
1
Topic starter

EXERCISE:
i. Summarize what you understand about ASSERT solidity error handling in this forum (you can post code also with the summary if you which - it optional with code)

(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

Unlike REQUIRE solidity error handling which focuses on validating user inputs, ASSERT help confirm if theoretical state of a smart contract which ought to be true remains indeed technically true and valid (You know things been theoretically true does not mean it is technically working fine as intended). And if not true, it throws error and revert the transaction.

 

ASSERT is solidity is written as:

 

assert (condition that is been verified also known as invariant that must always be true);

 

//Example to ensure amount left after a transfer remains valid

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);

 

    //Assert that the balance of msg sender after transfer is lower base on amount transferred

     uint afterTransferBalance = Balance[Msg.sender] - _amountToTransfer;

 

    //ASSERT

     assert (Balance[Msg.sender] == afterTransferBalance);

 

}

Share: