Skip to content

Commit

Permalink
♻️ Use Interface Events (#248)
Browse files Browse the repository at this point in the history
### 🕓 Changelog

Using directly interface events leads to a cleaner code structure, i.e.
they do not have to be redefined in a module contract. In addition, we
adjust the upper bound for the batch calls in the `erc1155` contract to
`128` (and an `uint8` type), reduce the upper bound for the return value
of `eip712Domain` to `32`, and implement a `constant` parameter
`_DYNARRAY_BOUND` for the dynamic arrays in the
`merkle_proof_verification` contract.

---------

Signed-off-by: Pascal Marco Caversaccio <pascal.caversaccio@hotmail.ch>
  • Loading branch information
pcaversaccio authored May 27, 2024
1 parent 0748f19 commit acee070
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 327 deletions.
264 changes: 132 additions & 132 deletions .gas-snapshot

Large diffs are not rendered by default.

39 changes: 3 additions & 36 deletions src/snekmate/auth/access_control.vy
Original file line number Diff line number Diff line change
Expand Up @@ -100,39 +100,6 @@ hasRole: public(HashMap[bytes32, HashMap[address, bool]])
getRoleAdmin: public(HashMap[bytes32, bytes32])


# @dev Emitted when `new_admin_role` is set as
# `role`'s admin role, replacing `previous_admin_role`.
# Note that `DEFAULT_ADMIN_ROLE` is the starting
# admin for all roles, despite `RoleAdminChanged`
# not being emitted signaling this.
event RoleAdminChanged:
role: indexed(bytes32)
previous_admin_role: indexed(bytes32)
new_admin_role: indexed(bytes32)


# @dev Emitted when `account` is granted `role`.
# Note that `sender` is the account (an admin
# role bearer) that originated the contract call.
event RoleGranted:
role: indexed(bytes32)
account: indexed(address)
sender: indexed(address)


# @dev Emitted when `account` is revoked `role`.
# Note that `sender` is the account that originated
# the contract call:
# - if using `revokeRole`, it is the admin role
# bearer,
# - if using `renounceRole`, it is the role bearer
# (i.e. `account`).
event RoleRevoked:
role: indexed(bytes32)
account: indexed(address)
sender: indexed(address)


@deploy
@payable
def __init__():
Expand Down Expand Up @@ -243,7 +210,7 @@ def _set_role_admin(role: bytes32, admin_role: bytes32):
"""
previous_admin_role: bytes32 = self.getRoleAdmin[role]
self.getRoleAdmin[role] = admin_role
log RoleAdminChanged(role, previous_admin_role, admin_role)
log IAccessControl.RoleAdminChanged(role, previous_admin_role, admin_role)


@internal
Expand All @@ -257,7 +224,7 @@ def _grant_role(role: bytes32, account: address):
"""
if (not(self.hasRole[role][account])):
self.hasRole[role][account] = True
log RoleGranted(role, account, msg.sender)
log IAccessControl.RoleGranted(role, account, msg.sender)


@internal
Expand All @@ -271,4 +238,4 @@ def _revoke_role(role: bytes32, account: address):
"""
if (self.hasRole[role][account]):
self.hasRole[role][account] = False
log RoleRevoked(role, account, msg.sender)
log IAccessControl.RoleRevoked(role, account, msg.sender)
25 changes: 2 additions & 23 deletions src/snekmate/extensions/erc4626.vy
Original file line number Diff line number Diff line change
Expand Up @@ -157,27 +157,6 @@ _DECIMALS_OFFSET: immutable(uint8)
_UNDERLYING_DECIMALS: immutable(uint8)


# @dev Emitted when `sender` has exchanged `assets`
# for `shares`, and transferred those `shares`
# to `owner`.
event Deposit:
sender: indexed(address)
owner: indexed(address)
assets: uint256
shares: uint256


# @dev Emitted when `sender` has exchanged `shares`,
# owned by `owner`, for `assets`, and transferred
# those `assets` to `receiver`.
event Withdraw:
sender: indexed(address)
receiver: indexed(address)
owner: indexed(address)
assets: uint256
shares: uint256


@deploy
@payable
def __init__(name_: String[25], symbol_: String[5], asset_: IERC20, decimals_offset_: uint8, name_eip712_: String[50], version_eip712_: String[20]):
Expand Down Expand Up @@ -679,7 +658,7 @@ def _deposit(sender: address, receiver: address, assets: uint256, shares: uint25
# the target address is an EOA), the call reverts.
assert extcall _ASSET.transferFrom(sender, self, assets, default_return_value=True), "erc4626: transferFrom operation did not succeed"
erc20._mint(receiver, shares)
log Deposit(sender, receiver, assets, shares)
log IERC4626.Deposit(sender, receiver, assets, shares)


@internal
Expand Down Expand Up @@ -718,4 +697,4 @@ def _withdraw(sender: address, receiver: address, owner: address, assets: uint25
# you add the kwarg `skip_contract_check=True`. If the check fails (i.e.
# the target address is an EOA), the call reverts.
assert extcall _ASSET.transfer(receiver, assets, default_return_value=True), "erc4626: transfer operation did not succeed"
log Withdraw(sender, receiver, owner, assets, shares)
log IERC4626.Withdraw(sender, receiver, owner, assets, shares)
59 changes: 9 additions & 50 deletions src/snekmate/tokens/erc1155.vy
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ _SUPPORTED_INTERFACES: constant(bytes4[3]) = [


# @dev Stores the upper bound for batch calls.
_BATCH_SIZE: constant(uint16) = 255
_BATCH_SIZE: constant(uint8) = 128


# @dev Stores the base URI for computing `uri`.
Expand Down Expand Up @@ -132,47 +132,6 @@ is_minter: public(HashMap[address, bool])
_token_uris: HashMap[uint256, String[432]]


# @dev Emitted when `amount` tokens of token type
# `id` are transferred from `owner` to `to` by
# `operator`.
event TransferSingle:
operator: indexed(address)
owner: indexed(address)
to: indexed(address)
id: uint256
amount: uint256


# @dev Equivalent to multiple `TransferSingle` events,
# where `operator`, `owner`, and `to` are the same
# for all transfers.
event TransferBatch:
operator: indexed(address)
owner: indexed(address)
to: indexed(address)
ids: DynArray[uint256, _BATCH_SIZE]
amounts: DynArray[uint256, _BATCH_SIZE]


# @dev Emitted when `owner` grants or revokes permission
# to `operator` to transfer their tokens, according to
# `approved`.
event ApprovalForAll:
owner: indexed(address)
operator: indexed(address)
approved: bool


# @dev Emitted when the Uniform Resource Identifier (URI)
# for token type `id` changes to `value`, if it is a
# non-programmatic URI. Note that if an `URI` event was
# emitted for `id`, the EIP-1155 standard guarantees that
# `value` will equal the value returned by `uri`.
event URI:
value: String[512]
id: indexed(uint256)


# @dev Emitted when the status of a `minter`
# address is changed.
event RoleMinterChanged:
Expand Down Expand Up @@ -524,7 +483,7 @@ def _set_approval_for_all(owner: address, operator: address, approved: bool):
"""
assert owner != operator, "erc1155: setting approval status for self"
self.isApprovedForAll[owner][operator] = approved
log ApprovalForAll(owner, operator, approved)
log IERC1155.ApprovalForAll(owner, operator, approved)


@internal
Expand Down Expand Up @@ -564,7 +523,7 @@ def _safe_transfer_from(owner: address, to: address, id: uint256, amount: uint25
# due to an arithmetic check of the entire token
# supply in the functions `_safe_mint` and `_safe_mint_batch`.
self.balanceOf[to][id] = unsafe_add(self.balanceOf[to][id], amount)
log TransferSingle(msg.sender, owner, to, id, amount)
log IERC1155.TransferSingle(msg.sender, owner, to, id, amount)

self._after_token_transfer(owner, to, self._as_singleton_array(id), self._as_singleton_array(amount), data)

Expand Down Expand Up @@ -620,7 +579,7 @@ def _safe_batch_transfer_from(owner: address, to: address, ids: DynArray[uint256
# maximum value of `uint16`.
idx = unsafe_add(idx, 1)

log TransferBatch(msg.sender, owner, to, ids, amounts)
log IERC1155.TransferBatch(msg.sender, owner, to, ids, amounts)

self._after_token_transfer(owner, to, ids, amounts, data)

Expand Down Expand Up @@ -659,7 +618,7 @@ def _safe_mint(owner: address, id: uint256, amount: uint256, data: Bytes[1_024])
# due to an arithmetic check of the entire token
# supply in the function `_before_token_transfer`.
self.balanceOf[owner][id] = unsafe_add(self.balanceOf[owner][id], amount)
log TransferSingle(msg.sender, empty(address), owner, id, amount)
log IERC1155.TransferSingle(msg.sender, empty(address), owner, id, amount)

self._after_token_transfer(empty(address), owner, self._as_singleton_array(id), self._as_singleton_array(amount), data)

Expand Down Expand Up @@ -709,7 +668,7 @@ def _safe_mint_batch(owner: address, ids: DynArray[uint256, _BATCH_SIZE], amount
# maximum value of `uint16`.
idx = unsafe_add(idx, 1)

log TransferBatch(msg.sender, empty(address), owner, ids, amounts)
log IERC1155.TransferBatch(msg.sender, empty(address), owner, ids, amounts)

self._after_token_transfer(empty(address), owner, ids, amounts, data)

Expand Down Expand Up @@ -770,7 +729,7 @@ def _set_uri(id: uint256, token_uri: String[432]):
string URI for computing `uri`.
"""
self._token_uris[id] = token_uri
log URI(self._uri(id), id)
log IERC1155.URI(self._uri(id), id)


@internal
Expand All @@ -792,7 +751,7 @@ def _burn(owner: address, id: uint256, amount: uint256):
owner_balance: uint256 = self.balanceOf[owner][id]
assert owner_balance >= amount, "erc1155: burn amount exceeds balance"
self.balanceOf[owner][id] = unsafe_sub(owner_balance, amount)
log TransferSingle(msg.sender, owner, empty(address), id, amount)
log IERC1155.TransferSingle(msg.sender, owner, empty(address), id, amount)

self._after_token_transfer(owner, empty(address), self._as_singleton_array(id), self._as_singleton_array(amount), b"")

Expand Down Expand Up @@ -828,7 +787,7 @@ def _burn_batch(owner: address, ids: DynArray[uint256, _BATCH_SIZE], amounts: Dy
# maximum value of `uint16`.
idx = unsafe_add(idx, 1)

log TransferBatch(msg.sender, owner, empty(address), ids, amounts)
log IERC1155.TransferBatch(msg.sender, owner, empty(address), ids, amounts)

self._after_token_transfer(owner, empty(address), ids, amounts, b"")

Expand Down
26 changes: 4 additions & 22 deletions src/snekmate/tokens/erc20.vy
Original file line number Diff line number Diff line change
Expand Up @@ -167,24 +167,6 @@ is_minter: public(HashMap[address, bool])
nonces: public(HashMap[address, uint256])


# @dev Emitted when `amount` tokens are moved
# from one account (`owner`) to another (`to`).
# Note that the parameter `amount` may be zero.
event Transfer:
owner: indexed(address)
to: indexed(address)
amount: uint256


# @dev Emitted when the allowance of a `spender`
# for an `owner` is set by a call to `approve`.
# The parameter `amount` is the new allowance.
event Approval:
owner: indexed(address)
spender: indexed(address)
amount: uint256


# @dev Emitted when the status of a `minter`
# address is changed.
event RoleMinterChanged:
Expand Down Expand Up @@ -475,7 +457,7 @@ def _transfer(owner: address, to: address, amount: uint256):
assert owner_balanceOf >= amount, "erc20: transfer amount exceeds balance"
self.balanceOf[owner] = unsafe_sub(owner_balanceOf, amount)
self.balanceOf[to] = unsafe_add(self.balanceOf[to], amount)
log Transfer(owner, to, amount)
log IERC20.Transfer(owner, to, amount)

self._after_token_transfer(owner, to, amount)

Expand All @@ -498,7 +480,7 @@ def _mint(owner: address, amount: uint256):

self.totalSupply += amount
self.balanceOf[owner] = unsafe_add(self.balanceOf[owner], amount)
log Transfer(empty(address), owner, amount)
log IERC20.Transfer(empty(address), owner, amount)

self._after_token_transfer(empty(address), owner, amount)

Expand All @@ -522,7 +504,7 @@ def _burn(owner: address, amount: uint256):
assert account_balance >= amount, "erc20: burn amount exceeds balance"
self.balanceOf[owner] = unsafe_sub(account_balance, amount)
self.totalSupply = unsafe_sub(self.totalSupply, amount)
log Transfer(owner, empty(address), amount)
log IERC20.Transfer(owner, empty(address), amount)

self._after_token_transfer(owner, empty(address), amount)

Expand All @@ -543,7 +525,7 @@ def _approve(owner: address, spender: address, amount: uint256):
assert spender != empty(address), "erc20: approve to the zero address"

self.allowance[owner][spender] = amount
log Approval(owner, spender, amount)
log IERC20.Approval(owner, spender, amount)


@internal
Expand Down
50 changes: 6 additions & 44 deletions src/snekmate/tokens/erc721.vy
Original file line number Diff line number Diff line change
Expand Up @@ -236,44 +236,6 @@ _token_uris: HashMap[uint256, String[432]]
_counter: uint256


# @dev Emitted when `token_id` token is
# transferred from `owner` to `to`.
event Transfer:
owner: indexed(address)
to: indexed(address)
token_id: indexed(uint256)


# @dev Emitted when `owner` enables `approved`
# to manage the `token_id` token.
event Approval:
owner: indexed(address)
approved: indexed(address)
token_id: indexed(uint256)


# @dev Emitted when `owner` enables or disables
# (`approved`) `operator` to manage all of its
# assets.
event ApprovalForAll:
owner: indexed(address)
operator: indexed(address)
approved: bool


# @dev Emitted when the metadata of a token is
# changed.
event MetadataUpdate:
token_id: uint256


# @dev Emitted when the metadata of a range of
# tokens is changed.
event BatchMetadataUpdate:
from_token_id: uint256
token_id: uint256


# @dev Emitted when the status of a `minter`
# address is changed.
event RoleMinterChanged:
Expand Down Expand Up @@ -779,7 +741,7 @@ def _approve(to: address, token_id: uint256):
@param token_id The 32-byte identifier of the token.
"""
self._token_approvals[token_id] = to
log Approval(self._owner_of(token_id), to, token_id)
log IERC721.Approval(self._owner_of(token_id), to, token_id)


@internal
Expand Down Expand Up @@ -807,7 +769,7 @@ def _set_approval_for_all(owner: address, operator: address, approved: bool):
"""
assert owner != operator, "erc721: approve to caller"
self.isApprovedForAll[owner][operator] = approved
log ApprovalForAll(owner, operator, approved)
log IERC721.ApprovalForAll(owner, operator, approved)


@internal
Expand Down Expand Up @@ -876,7 +838,7 @@ def _mint(owner: address, token_id: uint256):
# this is no longer even theoretically possible.
self._balances[owner] = unsafe_add(self._balances[owner], 1)
self._owners[token_id] = owner
log Transfer(empty(address), owner, token_id)
log IERC721.Transfer(empty(address), owner, token_id)

self._after_token_transfer(empty(address), owner, token_id)

Expand Down Expand Up @@ -944,7 +906,7 @@ def _transfer(owner: address, to: address, token_id: uint256):
self._balances[owner] = unsafe_sub(self._balances[owner], 1)
self._balances[to] = unsafe_add(self._balances[to], 1)
self._owners[token_id] = to
log Transfer(owner, to, token_id)
log IERC721.Transfer(owner, to, token_id)

self._after_token_transfer(owner, to, token_id)

Expand All @@ -960,7 +922,7 @@ def _set_token_uri(token_id: uint256, token_uri: String[432]):
"""
assert self._exists(token_id), "erc721: URI set of nonexistent token"
self._token_uris[token_id] = token_uri
log MetadataUpdate(token_id)
log IERC4906.MetadataUpdate(token_id)


@internal
Expand Down Expand Up @@ -997,7 +959,7 @@ def _burn(token_id: uint256):
# received through minting and transfer.
self._balances[owner] = unsafe_sub(self._balances[owner], 1)
self._owners[token_id] = empty(address)
log Transfer(owner, empty(address), token_id)
log IERC721.Transfer(owner, empty(address), token_id)

self._after_token_transfer(owner, empty(address), token_id)

Expand Down
Loading

0 comments on commit acee070

Please sign in to comment.