Skip to Content
Ethereum Cookbook
book

Ethereum Cookbook

by Manoj P R
August 2018
Intermediate to advanced
404 pages
11h 19m
English
Packt Publishing
Content preview from Ethereum Cookbook

assert()

  1. Use assert() to prevent something bad from happening:
assert(condition);
  1. Use it to check conditions such as integer overflow/underflow:
pragma solidity ^0.4.24;contract TestException {    function add(uint _a, uint _b) returns (uint) {       uint x = _a + _b;        assert(x > _b);        returns x;    }}
You should not use assert() blindly for checking overflow/underflow. It should be used only if you think that previous require or if checks would make it impossible.
  1. Use it to check invariants in the contract. One example is to verify the contract balance against the total supply:
assert(address(this).balance >= totalSupply);
  1. assert is commonly used to validate state after making changes. It helps in preventing conditions that should never be possible. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Building Ethereum Dapps

Building Ethereum Dapps

Roberto Infante
Ethereum For Dummies

Ethereum For Dummies

Michael G. Solomon

Publisher Resources

ISBN: 9781789133998Supplemental Content