James Bachini

OpenZeppelin 5 Solidity Common Errors

OpenZeppelin 5

OpenZeppelin have just released version 5 of their Solidity smart contract libraries and there are some breaking changes that are going to cause errors.

The two main ones are the Ownable.sol import which allows devs to create onlyOwner functions and the token hooks on ERC20, ERC721 & ERC1155 contracts.

Ownable Constructor

If you are getting an error along the lines of:

TypeError: No arguments passed to the base constructor. Specify the arguments or mark “Token” as abstract.

You can fix this simply by adding a constructor for the library and passing in msg.sender

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

import "@openzeppelin/contracts/access/Ownable.sol";

contract OwnableExample {
  constructor() Ownable(msg.sender) {

  }
}

Token Hooks

If you are getting an error along the lines of:

TypeError: Trying to override non-virtual function. Did you forget to add “virtual”?

It might be because you have an override function targeting a previous token library such as ERC20, ERC721 or ERC1155

You can fix this by using the _update function. So for an ERC20 token that previously added logic to the _transfer function we would do something like this:

// Previous Code
function _transfer(address sender, address recipient, uint256 amount) internal override {
  super._transfer(sender, recipient, amount);
}

// OpenZeppelin v5 Fix
function _update(address sender, address recipient, uint256 amount) internal override {
  super._update(sender, recipient, amount);
}

The OpenZeppelin v5 Upgrade

James On YouTube

The upgrade includes:-

  • Removal of token hooks from ERC20, ERC721, and ERC1155.
  • Introduction of _update function in lieu of token hooks.
  • Acceptance of a custom owner argument during construction in Ownable.
  • Support for voting with bytes-formatted signatures and for Account Abstraction in Governor via implementation of ERC-1271.
  • Removal of several functions and contracts including Address.isContract, Counters, SafeMath, SignedSafeMath, GovernorCompatibilityBravo, GovernorVotesComp, TokenTimelock, ERC777, all cross-chain contracts, and all presets.
  • Adoption of ERC-7201: Namespaced Storage Layout to counter storage layout collisions in upgradeable contracts.
  • Updated library to Solidity 0.8.20, incorporating custom errors and explicit imports.
  • Introduced AccessManager for more sophisticated access control setups.
  • Gas cost reductions resulting in lower deployment and runtime costs.
  • Replacement of revert strings with custom errors, reducing deployment cost by 12.84% on average.
  • Removal of duplicated SLOADs and addition of immutable variables for enhanced gas savings.
  • Reorganization of data structures and variables to minimize storage use, reducing the gas costs.
  • More advanced testing including fuzzing and additional formal verification rules.


Get The Blockchain Sector Newsletter, binge the YouTube channel and connect with me on Twitter

The Blockchain Sector newsletter goes out a few times a month when there is breaking news or interesting developments to discuss. All the content I produce is free, if you’d like to help please share this content on social media.

Thank you.

James Bachini

Disclaimer: Not a financial advisor, not financial advice. The content I create is to document my journey and for educational and entertainment purposes only. It is not under any circumstances investment advice. I am not an investment or trading professional and am learning myself while still making plenty of mistakes along the way. Any code published is experimental and not production ready to be used for financial transactions. Do your own research and do not play with funds you do not want to lose.


Posted

in

, , , , , ,

by

Tags: