question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Slightly reduce ERC721 deployment gas cost

See original GitHub issue

🧐 Motivation

ERC721.sol

Changing the order of arguments of first require in approve function can slightly reduce fuel costs for contract deploy.

📝 Details

In function

/**
 * @dev See {IERC721-approve}.
 */
function approve(address to, uint256 tokenId) public virtual override {
    address owner = ERC721.ownerOf(tokenId);
    require(to != owner, "ERC721: approval to current owner");


    require(_msgSender() == owner || isApprovedForAll(owner, _msgSender()),
        "ERC721: approve caller is not owner nor approved for all"
    );


    _approve(to, tokenId);
}

Changing the line

    require(to != owner, "ERC721: approval to current owner");

to

    require(owner != to, "ERC721: approval to current owner");

may lead to a minor decrease in gas cost at deployment. I think the reason is more efficient work with stack. This does not affect the gas consumption at function call.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
frangiocommented, May 12, 2021

@vladyan18 Can you report these over in the Solidity repository? It seems it should be the responsibility of the optimizer to do this.

0reactions
Amxxcommented, May 10, 2021

Adding unchecked blocks was considered during the 4.0 dev phase. It was put on hold, but we should probably reconsider it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A journey towards a gas efficient ERC721
These arithmetic operations are simple, but do incur an additional cost to the deploy cost and the fees paid by users calling the...
Read more >
How to optimize Ethereum NFT smart-contracts to reduce gas ...
This is a deployment cost reduction in USD from $2001.12 to $1076.92. Truly incredible! For the sake of completion, I ran the same...
Read more >
Lower Gas NFT ERC721 solidity contract - YouTube
In this video, We will take a look at an NFT smart contract coded in solidity to use less gas when minting NFTs....
Read more >
How EIP2535 Diamonds Reduces Gas Costs for Smart ...
Gas costs for users increase when a single deployed smart contract grows and becomes two deployed smart contracts that need to communicate.
Read more >
Cost to deploy ERC721 token to mainnet - OpenZeppelin Forum
I recently deployed a simple ERC721 NFT (generated using OpenZeppelin Contracts Wizard: https://blog.openzeppelin.com/wizard) and with a gas ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found