Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix: total supply overflow #134

Open
wants to merge 2 commits into
base: md/target-shanghai
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
50 changes: 20 additions & 30 deletions src/tokens/ERC20.huff
Original file line number Diff line number Diff line change
Expand Up @@ -244,24 +244,6 @@
[BALANCE_SLOT] STORE_ELEMENT_FROM_KEYS(0x00) // [value, from, to]
}

/// @notice Approve
/// @notice Approves an address to spend an amount of tokens on the caller's behalf
#define macro APPROVE() = takes (0) returns (0) {
0x24 calldataload // [value]
dup1 0x00 mstore // [value]
0x04 calldataload // [to, value]
caller // [from, to, value]

// Emit the approval event.
dup2 dup2 // [from, to, from, to, value]
__EVENT_HASH(APPROVAL_EVENT_SIGNATURE) // [sig, from, to, from, to, value]
0x20 0x00 // [0, 32, sig, from, to, from, to, value]
log3 // [from, to, value]

// Store the value at slot = keccak256(from . to)
STORE_ELEMENT_FROM_KEYS(0x00)
}

/// @notice Domain Separator
/// @notice Returns the EIP-712 domain separator
#define macro DOMAIN_SEPARATOR() = takes (0) returns (0) {
Expand Down Expand Up @@ -552,22 +534,30 @@
/// @notice Mints tokens to a specified address
#define macro _MINT() = takes (2) returns (0) {
// Input stack: [value, to]
dup2 // [to, value, to]
swap1 // [value, to, to]
_TRANSFER_GIVE_TO() // [value, to, to]
dup2 // [to, value, to]
swap1 // [value, to, to]
_TRANSFER_GIVE_TO() // [value, to, to]

// Update totalSupply
dup1 // [value, value, to, to]
[TOTAL_SUPPLY_SLOT] sload // [supply, value, value, to, to]
add // [supply + value, value, to, to]
[TOTAL_SUPPLY_SLOT] sstore // [value, to, to]
dup1 // [value, value, to, to]
[TOTAL_SUPPLY_SLOT] sload // [supply, value, value, to, to]
dup2 // [value, supply, value, value, to, to]
add // [supply + value, value, value, to, to]
dup1 // [supply + value, supply + value, value, value, to, to]
swap2 // [value, supply + value, supply + value, value, to, to]
gt // [is_overflow, supply + value, value, to, to]
iszero // [is_not_overflow, supply + value, value, to, to]
is_not_overflow jumpi // [supply + value, value, to, to]
[ARITHMETIC_OVERFLOW] PANIC()
is_not_overflow:
[TOTAL_SUPPLY_SLOT] sstore // [value, to, to]

// Emit the transfer event.
0x00 mstore // [to, to]
[ZERO_ADDRESS] // [address(0), to, to]
__EVENT_HASH(Transfer) // [sig, from, to, to]
0x20 0x00 // [0, 32, sig, from, to, to]
log3 pop // []
0x00 mstore // [to, to]
[ZERO_ADDRESS] // [address(0), to, to]
__EVENT_HASH(Transfer) // [sig, from, to, to]
0x20 0x00 // [0, 32, sig, from, to, to]
log3 pop // []
}


Expand Down
10 changes: 10 additions & 0 deletions test/tokens/ERC20.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,16 @@ contract ERC20Test is Test {
}
}

function testFailMintTotalSupplyOverflow(
address to,
uint256 mintAmount
) public {
mintAmount = bound(mintAmount, 1, type(uint256).max);

token.mint(to, type(uint256).max);
token.mint(to, mintAmount);
}

function testFailBurnInsufficientBalance(
address to,
uint256 mintAmount,
Expand Down