Skip to content

Commit

Permalink
♻️ Revert to the Original Implementation of ERC721 (#1335)
Browse files Browse the repository at this point in the history
  • Loading branch information
atarpara authored Feb 2, 2025
1 parent 6616280 commit 21e3649
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 34 deletions.
32 changes: 16 additions & 16 deletions src/tokens/ERC721.sol
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,14 @@ abstract contract ERC721 {
{
mstore(0x00, to)
let toBalanceSlot := keccak256(0x0c, 0x1c)
let p := sload(toBalanceSlot) // `toBalanceSlotPacked`.
// Revert if `to` is the zero address, or if the account balance is maxed.
if iszero(mul(to, xor(and(p, _MAX_ACCOUNT_BALANCE), _MAX_ACCOUNT_BALANCE))) {
let toBalanceSlotPacked := add(sload(toBalanceSlot), 1)
// Revert if `to` is the zero address, or if the account balance overflows.
if iszero(mul(to, and(toBalanceSlotPacked, _MAX_ACCOUNT_BALANCE))) {
// `TransferToZeroAddress()`, `AccountBalanceOverflow()`.
mstore(shl(2, iszero(to)), 0xea553b3401336cea)
revert(0x1c, 0x04)
}
sstore(toBalanceSlot, add(1, p))
sstore(toBalanceSlot, toBalanceSlotPacked)
}
// Emit the {Transfer} event.
log4(codesize(), 0x00, _TRANSFER_EVENT_SIGNATURE, from, to, id)
Expand Down Expand Up @@ -466,14 +466,14 @@ abstract contract ERC721 {
{
mstore(0x00, to)
let balanceSlot := keccak256(0x0c, 0x1c)
let p := sload(balanceSlot) // `balanceSlotPacked`.
// Revert if `to` is the zero address, or if the account balance is maxed.
if iszero(mul(to, xor(and(p, _MAX_ACCOUNT_BALANCE), _MAX_ACCOUNT_BALANCE))) {
let balanceSlotPacked := add(sload(balanceSlot), 1)
// Revert if `to` is the zero address, or if the account balance overflows.
if iszero(mul(to, and(balanceSlotPacked, _MAX_ACCOUNT_BALANCE))) {
// `TransferToZeroAddress()`, `AccountBalanceOverflow()`.
mstore(shl(2, iszero(to)), 0xea553b3401336cea)
revert(0x1c, 0x04)
}
sstore(balanceSlot, add(1, p))
sstore(balanceSlot, balanceSlotPacked)
}
// Emit the {Transfer} event.
log4(codesize(), 0x00, _TRANSFER_EVENT_SIGNATURE, 0, to, id)
Expand Down Expand Up @@ -503,14 +503,14 @@ abstract contract ERC721 {
{
mstore(0x00, to)
let balanceSlot := keccak256(0x0c, 0x1c)
let p := sload(balanceSlot) // `balanceSlotPacked`.
// Revert if `to` is the zero address, or if the account balance is maxed.
if iszero(mul(to, xor(and(p, _MAX_ACCOUNT_BALANCE), _MAX_ACCOUNT_BALANCE))) {
let balanceSlotPacked := add(sload(balanceSlot), 1)
// Revert if `to` is the zero address, or if the account balance overflows.
if iszero(mul(to, and(balanceSlotPacked, _MAX_ACCOUNT_BALANCE))) {
// `TransferToZeroAddress()`, `AccountBalanceOverflow()`.
mstore(shl(2, iszero(to)), 0xea553b3401336cea)
revert(0x1c, 0x04)
}
sstore(balanceSlot, add(1, p))
sstore(balanceSlot, balanceSlotPacked)
}
// Emit the {Transfer} event.
log4(codesize(), 0x00, _TRANSFER_EVENT_SIGNATURE, 0, to, id)
Expand Down Expand Up @@ -788,14 +788,14 @@ abstract contract ERC721 {
{
mstore(0x00, to)
let toBalanceSlot := keccak256(0x0c, 0x1c)
let p := sload(toBalanceSlot) // `toBalanceSlotPacked`.
// Revert if `to` is the zero address, or if the account balance is maxed.
if iszero(mul(to, xor(and(p, _MAX_ACCOUNT_BALANCE), _MAX_ACCOUNT_BALANCE))) {
let toBalanceSlotPacked := add(sload(toBalanceSlot), 1)
// Revert if `to` is the zero address, or if the account balance overflows.
if iszero(mul(to, and(toBalanceSlotPacked, _MAX_ACCOUNT_BALANCE))) {
// `TransferToZeroAddress()`, `AccountBalanceOverflow()`.
mstore(shl(2, iszero(to)), 0xea553b3401336cea)
revert(0x1c, 0x04)
}
sstore(toBalanceSlot, add(1, p))
sstore(toBalanceSlot, toBalanceSlotPacked)
}
// Emit the {Transfer} event.
log4(codesize(), 0x00, _TRANSFER_EVENT_SIGNATURE, from, to, id)
Expand Down
32 changes: 16 additions & 16 deletions src/tokens/ext/zksync/ERC721.sol
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,14 @@ abstract contract ERC721 {
{
mstore(0x00, to)
let toBalanceSlot := keccak256(0x0c, 0x1c)
let p := sload(toBalanceSlot) // `toBalanceSlotPacked`.
// Revert if `to` is the zero address, or if the account balance is maxed.
if iszero(mul(to, xor(and(p, _MAX_ACCOUNT_BALANCE), _MAX_ACCOUNT_BALANCE))) {
let toBalanceSlotPacked := add(sload(toBalanceSlot), 1)
// Revert if `to` is the zero address, or if the account balance overflows.
if iszero(mul(to, and(toBalanceSlotPacked, _MAX_ACCOUNT_BALANCE))) {
// `TransferToZeroAddress()`, `AccountBalanceOverflow()`.
mstore(shl(2, iszero(to)), 0xea553b3401336cea)
revert(0x1c, 0x04)
}
sstore(toBalanceSlot, add(1, p))
sstore(toBalanceSlot, toBalanceSlotPacked)
}
// Emit the {Transfer} event.
log4(codesize(), 0x00, _TRANSFER_EVENT_SIGNATURE, from, to, id)
Expand Down Expand Up @@ -464,14 +464,14 @@ abstract contract ERC721 {
{
mstore(0x00, to)
let balanceSlot := keccak256(0x0c, 0x1c)
let p := sload(balanceSlot) // `balanceSlotPacked`.
// Revert if `to` is the zero address, or if the account balance is maxed.
if iszero(mul(to, xor(and(p, _MAX_ACCOUNT_BALANCE), _MAX_ACCOUNT_BALANCE))) {
let balanceSlotPacked := add(sload(balanceSlot), 1)
// Revert if `to` is the zero address, or if the account balance overflows.
if iszero(mul(to, and(balanceSlotPacked, _MAX_ACCOUNT_BALANCE))) {
// `TransferToZeroAddress()`, `AccountBalanceOverflow()`.
mstore(shl(2, iszero(to)), 0xea553b3401336cea)
revert(0x1c, 0x04)
}
sstore(balanceSlot, add(1, p))
sstore(balanceSlot, balanceSlotPacked)
}
// Emit the {Transfer} event.
log4(codesize(), 0x00, _TRANSFER_EVENT_SIGNATURE, 0, to, id)
Expand Down Expand Up @@ -501,14 +501,14 @@ abstract contract ERC721 {
{
mstore(0x00, to)
let balanceSlot := keccak256(0x0c, 0x1c)
let p := sload(balanceSlot) // `balanceSlotPacked`.
// Revert if `to` is the zero address, or if the account balance is maxed.
if iszero(mul(to, xor(and(p, _MAX_ACCOUNT_BALANCE), _MAX_ACCOUNT_BALANCE))) {
let balanceSlotPacked := add(sload(balanceSlot), 1)
// Revert if `to` is the zero address, or if the account balance overflows.
if iszero(mul(to, and(balanceSlotPacked, _MAX_ACCOUNT_BALANCE))) {
// `TransferToZeroAddress()`, `AccountBalanceOverflow()`.
mstore(shl(2, iszero(to)), 0xea553b3401336cea)
revert(0x1c, 0x04)
}
sstore(balanceSlot, add(1, p))
sstore(balanceSlot, balanceSlotPacked)
}
// Emit the {Transfer} event.
log4(codesize(), 0x00, _TRANSFER_EVENT_SIGNATURE, 0, to, id)
Expand Down Expand Up @@ -786,14 +786,14 @@ abstract contract ERC721 {
{
mstore(0x00, to)
let toBalanceSlot := keccak256(0x0c, 0x1c)
let p := sload(toBalanceSlot) // `toBalanceSlotPacked`.
// Revert if `to` is the zero address, or if the account balance is maxed.
if iszero(mul(to, xor(and(p, _MAX_ACCOUNT_BALANCE), _MAX_ACCOUNT_BALANCE))) {
let toBalanceSlotPacked := add(sload(toBalanceSlot), 1)
// Revert if `to` is the zero address, or if the account balance overflows.
if iszero(mul(to, and(toBalanceSlotPacked, _MAX_ACCOUNT_BALANCE))) {
// `TransferToZeroAddress()`, `AccountBalanceOverflow()`.
mstore(shl(2, iszero(to)), 0xea553b3401336cea)
revert(0x1c, 0x04)
}
sstore(toBalanceSlot, add(1, p))
sstore(toBalanceSlot, toBalanceSlotPacked)
}
// Emit the {Transfer} event.
log4(codesize(), 0x00, _TRANSFER_EVENT_SIGNATURE, from, to, id)
Expand Down
13 changes: 13 additions & 0 deletions test/ERC721.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1002,4 +1002,17 @@ contract ERC721Test is SoladyTest {
vm.expectRevert(ERC721.TokenDoesNotExist.selector);
_ownerOf(id);
}

function check_AuxPackEquivalence(uint224 a, uint32 b) public pure {
uint256 packed = (uint256(a) << 32) | uint256(b);
unchecked {
bool hasOverflow = ((packed + 1) & 0xffffffff) == 0;
bool groundTruth = b == 0xffffffff;
assert(hasOverflow == groundTruth);
}
}

function testAuxPackEquivalence(uint224 a, uint32 b) public pure {
check_AuxPackEquivalence(a, b);
}
}
3 changes: 1 addition & 2 deletions test/Timelock.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ contract TimelockTest is SoladyTest {

a[0] = address(this);
a[1] = _ALICE;
vm.expectEmit(true, true, true, true);
emit MinDelaySet(_DEFAULT_MIN_DELAY);

timelock.initialize(_DEFAULT_MIN_DELAY, _ADMIN, a, a, a);
}

Expand Down

0 comments on commit 21e3649

Please sign in to comment.