> For the complete documentation index, see [llms.txt](https://calnix.gitbook.io/eth-dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://calnix.gitbook.io/eth-dev/gas-opt/using-unchecked-w-o-require.md).

# using unchecked w/o require

## Integer overflow/underflow

Prior to Solidity `0.8.0` version, interger overflow and underflow checks were performed by using the `SafeMath` library. From Solidity `0.8.0` upward, the compiler does that check for us.<br>

This extra check cost `gas`. If we know that the mathematical operations we will perform inside the contract will not overflow or underflow, we can tell the compiler not to check for overflow or underflow in the operation.<br>

This is the purpose of unchecked.

### Typical Implementation&#x20;

```solidity
    function deposit(uint256 amount) public {
        balances[msg.sender] += amount;
        bool successful = token.transferFrom(msg.sender, address(this), amount);
        require(successful, "Deposit failed!"); 
        emit Deposited(msg.sender, amount);
    }
```

### Could you use `unchecked` here?&#x20;

* Yes, it would be safe -> as long as the <mark style="color:yellow;">deposited token is known to be not malicious.</mark>
* The deposited balance of any account can't ever be more than the total supply of the deposited token. No one can own more than `type(uint256).max` of the underlying token.
* I'm being a bit pedantic in here, but I wanted to point out that sometimes conditions are enforced by construction, instead of being enforced by `require` statements.

### new implementation

```solidity
    function deposit(uint256 amount) public {
        unchecked { balances[msg.sender] += amount; }
        bool successful = token.transferFrom(msg.sender, address(this), amount);
        require(successful, "Deposit failed!"); 
        emit Deposited(msg.sender, amount);
    }
```

* unchecked saves gas.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://calnix.gitbook.io/eth-dev/gas-opt/using-unchecked-w-o-require.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
