Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ERC404A - Gas Fee optimized with 16bit numbers #13

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions examples/Dibbles404.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//SPDX-License-Identifier: UNLICENSED

/**
* Enabling a new era of Meme utility with the ERC404A, the most gas-optimized solution of ERC404.
*/

pragma solidity ^0.8.0;

import "../ERC404/ERC404A.sol";
import "@openzeppelin/contracts/utils/Strings.sol";

contract Dibbles404 is ERC404A {
string public baseTokenURI;

constructor(
address _owner
) ERC404A("Dibbles 404", "ERRDB", 18, 10000, _owner) {
balanceOf[_owner] = 10000 * 10 ** 18;
}

function setTokenURI(string memory _tokenURI) public onlyOwner {
baseTokenURI = _tokenURI;
}

function setNameSymbol(
string memory _name,
string memory _symbol
) public onlyOwner {
_setNameSymbol(_name, _symbol);
}

function tokenURI(uint256 id) public view override returns (string memory) {
if (bytes(baseTokenURI).length > 0) {
return string.concat(baseTokenURI, Strings.toString(id), ".json");
}

return "https://bafybeih4yfcubczmulmjdbsunc32n34ep5rs37ejewhcvvd2d2gsfzlpii.ipfs.nftstorage.link/errdb_soon.json";
}
}
55 changes: 29 additions & 26 deletions src/ERC404.sol
Original file line number Diff line number Diff line change
Expand Up @@ -325,18 +325,14 @@ abstract contract ERC404 is Ownable {
if (!whitelist[from]) {
uint256 tokens_to_burn = (balanceBeforeSender / unit) -
(balanceOf[from] / unit);
for (uint256 i = 0; i < tokens_to_burn; i++) {
_burn(from);
}
_burn(from, tokens_to_burn);
}

// Skip minting for certain addresses to save gas
if (!whitelist[to]) {
uint256 tokens_to_mint = (balanceOf[to] / unit) -
(balanceBeforeReceiver / unit);
for (uint256 i = 0; i < tokens_to_mint; i++) {
_mint(to);
}
_mint(to, tokens_to_mint);
}

emit ERC20Transfer(from, to, amount);
Expand All @@ -348,40 +344,47 @@ abstract contract ERC404 is Ownable {
return 10 ** decimals;
}

function _mint(address to) internal virtual {
function _mint(address to, uint256 amount) internal virtual {
if (to == address(0)) {
revert InvalidRecipient();
}

unchecked {
minted++;
}

uint256 id = minted;

if (_ownerOf[id] != address(0)) {
revert AlreadyExists();
unchecked {
minted += amount;
}

_ownerOf[id] = to;
_owned[to].push(id);
_ownedIndex[id] = _owned[to].length - 1;

emit Transfer(address(0), to, id);
uint256 count = _owned[to].length;
for (uint256 i = 0; i < amount; i++) {
id++;
if (_ownerOf[id] != address(0)) {
revert AlreadyExists();
}

_ownerOf[id] = to;
_owned[to].push(id);
_ownedIndex[id] = count + i;

emit Transfer(address(0), to, id);
}
}

function _burn(address from) internal virtual {
function _burn(address from, uint256 amount) internal virtual {
if (from == address(0)) {
revert InvalidSender();
}

uint256 id = _owned[from][_owned[from].length - 1];
_owned[from].pop();
delete _ownedIndex[id];
delete _ownerOf[id];
delete getApproved[id];

emit Transfer(from, address(0), id);
uint256 count = _owned[from].length - 1;
for (uint256 i = 0; i < amount; i++) {
uint256 id = _owned[from][count - i];
_owned[from].pop();
delete _ownedIndex[id];
delete _ownerOf[id];
delete getApproved[id];

emit Transfer(from, address(0), id);
}
}

function _setNameSymbol(
Expand Down
Loading