Skip to main content
46 votes

What is the purpose of "unchecked" in Solidity?

Tl;dr: "unchecked" exists in order to save gas Answer 1) We never want behavior that leads to over/underflow*. The reason the "unchecked" keyword exists is to allow Solidity ...
almel's user avatar
  • 816
29 votes
Accepted

Why does ++i cost less gas than i++?

They do not work the same. i++ gets compiled to something like j = i; i = i + 1; return j while ++i gets compiled to something like i = i + 1; return i; Long story short, i++ returns the non-...
Foxxxey's user avatar
  • 4,337
23 votes
Accepted

How to fix "TypeError: Type address is not implicitly convertible to expected type address payable"? (Solidity 0.8.2)

From Solidity 0.8.0 you don't need to declare the address as payable explicitly, but when you are transferring an amount to such address. See your example below in 0.8.x, adding a function to transfer ...
Sergi Juanati's user avatar
22 votes

What is the purpose of "unchecked" in Solidity?

One reason you might use unchecked is when looping through the elements of an array. Consider for example: uint256 length = array.length; for(uint256 i = 0; i < length; i++) { doSomething(array[...
murderteeth's user avatar
16 votes
Accepted

How to bubble up a custom error when using DELEGATECALL?

Check out this function I modified from the openzepplin's Address.sol. function _delegatecall(address target, bytes memory data) internal returns (bytes memory) { (bool success, bytes memory ...
diccccn's user avatar
  • 376
15 votes

Revert reason for arithmetic overflows in Solidity v0.8

Bit of Explanation First you should familiarize yourself with the concept of "unchecked arithmetic", which is part of the v0.8 breaking changes list: Arithmetic operations revert on ...
Paul Razvan Berg's user avatar
11 votes
Accepted

Unary operator minus (-) cannot be applied to type uint256

The unary operator - negates the given number. It is easier to see what it does with signed integers. If we use the int8 type and we negate the positive number 10, we get -10 back. Now, unsigned ...
Paul Razvan Berg's user avatar
10 votes
Accepted

Explicit type conversion not allowed from "int_const -1" to "uint128"

Changed it to this: uint256 public constant MASK = type(uint128).max; New restrictions in 0.8.0
Dylan Kilkenny's user avatar
8 votes
Accepted

function not returning a uint256 while using Hardhat testing

Okay, so after fiddling around, I've realized what the problem was. Return value of a non-view/pure functions inaccessible outside of the chain (i.e. other smart-contracts). Hence, to retrieve the ...
Sloth Man's user avatar
  • 496
7 votes
Accepted

Catching custom error

SOLUTION 1: Best solution is using expectRevert cheat code from forge-std: vm.expectRevert(ExampleBridgeContract.InvalidCaller.selector); exampleBridge.convert(empty, empty, empty, empty, 0, 0, 0, ...
Jan Beneš's user avatar
7 votes
Accepted

How to write an unchecked for loop?

You can do this by putting the post-iteration increment operation at the end of the loop: for (uint256 i = 0; i < length;) { // ... unchecked { i += 1; } } Note though that this is safe to ...
Paul Razvan Berg's user avatar
6 votes
Accepted

Exception When Calling Function In a Library that Returns an Array

I've never worked with libraries before, however I believe the issue might be that you are trying to have a dynamically sized array in memory. This should be uint8[] memory arr = new uint8[](arraySize)...
phaze's user avatar
  • 1,605
6 votes
Accepted

"selfdestruct" deprecated in Solidity 0.8.18

You're perfectly allowed to keep using it, the depreciation has been put in place because the SELFDESTRUCT opcode most likely going to be deleted in an upcoming fork (https://eips.ethereum.org/EIPS/...
Foxxxey's user avatar
  • 4,337
5 votes
Accepted

Does the unchecked { ... } block apply only to the current function?

Looking at the documentation for Solidity 0.8.0 it doesn't seem so like the unchecked block in bar disables the safety checks in foo.. The setting only affects the statements that are syntactically ...
Diego Ferri's user avatar
5 votes
Accepted

Return Storage Variable From Function In Solidity

It works if you declare it as internal. function getUnitStorage(address ownerAddr) internal returns (MappingDataTypes.UintMappingValue storage) { return uintStorage[ownerAddr]; } Public and ...
Ismael's user avatar
  • 30.5k
4 votes

Explicit type conversion not allowed from "int_const -1" to "uint128"

From solidity >0.8.0. Explicit conversions between literals and an integer type T are only allowed if the literal lies between type(T).min and type(T).max. In particular, replace usages of uint(-1) ...
Codler's user avatar
  • 146
4 votes

Stack too deep error in Solidity v0.8.4, no error / debug info from compiler

Turns out the issue was a struct to which I had added a few fields, and there were 16 fields in the struct, and that too is not permissible. Re-organizing, moving some fields to another struct, and ...
tinker's user avatar
  • 161
4 votes
Accepted

Can division underflow or overflow in Solidity?

Yes, there is at least one situation where overflow can occur. I don't know about underflows though. When you divide the minimum of a signed type by -1, you get the mirror image of that number in the ...
Paul Razvan Berg's user avatar
4 votes

What is the purpose of "unchecked" in Solidity?

Simple Answer: To save gas, as the code inside unchecked{"Some Code"} isn't verified for underflow/overflow error cases. However, that means you could cause a critical error or leave a ...
Sky's user avatar
  • 2,332
4 votes

How to do safemath with solidity 0.8.0

You don't need safemath in 0.8+, prior to that version, solidity's "+" operator wouldnt check for overflows, leading to type(uint256).max + 1 = 0, and we'd use the safeMath library to avoid ...
Foxxxey's user avatar
  • 4,337
4 votes
Accepted

What is the default visibility of a variable in solidity?

If visibility is not specified then by default it is an 'internal' variable . So , the owner mapping above is internal .
Gaurav Pant's user avatar
3 votes

Why does Openzeppelin's CountersUpgradeable library wrap arithmetic in an unchecked block?

It's cheaper to use unchecked blocks and check for overflows and underflows manually than let Solidity do it. Refer to Is the modulo operation cheaper in unchecked arithmetic? for more details.
Paul Razvan Berg's user avatar
3 votes
Accepted

Decode raw data within Solidity

You have to remove the first four bytes first. The first four bytes of a contract call is the function selector, which specifies which function you want to call, in this case balanceOf(address). I ...
Morten's user avatar
  • 6,027
3 votes
Accepted

Do the Solidity v0.8 overflow checks apply to assembly operations too?

No, they apply only to non-assembly code. Check for yourself on Remix: // SPDX-License-Identifier: MIT pragma solidity >=0.8.0; contract Mul { function mul_normal(uint8 x, uint8 y) external ...
Paul Razvan Berg's user avatar
3 votes

Member "Address" not found or not visible after argument-dependent lookup in tuple(address,int16,int8,uint256,bool,int256,address)

It's because when the Auction contract reads the users; it only gets data types of array and not the names you have used in Resigtry contract. Understand it like this; uint => (address, int16, int8,...
Prashant Prabhakar Singh's user avatar
3 votes
Accepted

Storage arrays with nested mappings do not support .push(<arg>)

mappings cannot be copied around through assignment either in storage or memory and can only be referenced. A work around is the following snippet. struct Dividend { mapping(address => bool) ...
Sheraz Arshad's user avatar
3 votes
Accepted

How to convert in-memory static array to calldata static array?

It's not possible to copy data from memory to calldata. Calldata, being the cheapest type of storage, is read-only and can only be used for data passed in to a function through the transaction input. ...
Morten's user avatar
  • 6,027
3 votes

How to store mapping with inline assembly?

Ok, i reslove by myself. contract assemblyMappingTest{ mapping(address => uint) public assets; function save(address _user, uint _amount) public { assets[_user] = _amount; } ...
Wei KeBei's user avatar
3 votes

i am getting attribute error while running erc20 brownie project on rinkeby testnet

here is another solution: you can leave your get_account() as is. instead of commenting out the two lines like Ganesh's solution, you can add the rinkeby network in your yaml file. This is because if ...
leadbassist's user avatar
3 votes
Accepted

Getting output amount of tokens when swapping on UNI v3 in Solidity

I found out that there is a Quoter contract in v3-periphery which can be used exactly for this. Here is the method which returns the quoted output amount.
Jan Beneš's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible