Skip to content

Commit

Permalink
♻️ Make ERC721 Module-Friendly (#237)
Browse files Browse the repository at this point in the history
### 🕓 Changelog

This PR refactors the `ERC721` contract to make it module-friendly and
ready for the breaking `0.4.0` release. Furthermore, this PR improves
readability of combined `and` and `or` statements in `assert`s and `if`s
(it doesn't follow Python's conventions, but I like it more like this),
and removes `else:` `return`s. Eventually, I update to `lockfileVersion:
"9.0"` since `pnpm`
[`v9.0.0`](https://github.com/pnpm/pnpm/releases/tag/v9.0.0) just got
released and bump the CI Python versions to `3.12`.

---------

Signed-off-by: Pascal Marco Caversaccio <pascal.caversaccio@hotmail.ch>
Signed-off-by: sudo rm -rf --no-preserve-root / <pcaversaccio@users.noreply.github.com>
  • Loading branch information
pcaversaccio authored Apr 18, 2024
1 parent 4b69964 commit 64e3170
Show file tree
Hide file tree
Showing 19 changed files with 453 additions and 440 deletions.
378 changes: 189 additions & 189 deletions .gas-snapshot

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .github/workflows/publish-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
architecture:
- x64
python_version:
- 3.11
- 3.12

steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-test-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
architecture:
- x64
python_version:
- 3.11
- 3.12

steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-contracts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
architecture:
- x64
python_version:
- 3.11
- 3.12
node_version:
- 20

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [`TimelockController`](https://github.com/pcaversaccio/snekmate/blob/v0.1.0/src/snekmate/governance/TimelockController.vy): Make `TimelockController` module-friendly. ([#220](https://github.com/pcaversaccio/snekmate/pull/220))
- **Tokens**
- [`ERC20`](https://github.com/pcaversaccio/snekmate/blob/v0.1.0/src/snekmate/tokens/ERC20.vy): Make `ERC20` module-friendly. ([#234](https://github.com/pcaversaccio/snekmate/pull/234))
- [`ERC721`](https://github.com/pcaversaccio/snekmate/blob/v0.1.0/src/snekmate/tokens/ERC721.vy): Make `ERC721` module-friendly. ([#237](https://github.com/pcaversaccio/snekmate/pull/237))
- **Utility Functions**
- [`Base64`](https://github.com/pcaversaccio/snekmate/blob/v0.1.0/src/snekmate/utils/Base64.vy): Make `Base64` module-friendly. ([#222](https://github.com/pcaversaccio/snekmate/pull/222))
- [`BatchDistributor`](https://github.com/pcaversaccio/snekmate/blob/v0.1.0/src/snekmate/utils/BatchDistributor.vy): Make `BatchDistributor` module-friendly. ([#223](https://github.com/pcaversaccio/snekmate/pull/223))
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ src
│ │ ├── IERC1155Receiver — "EIP-1155 Token Receiver Interface Definition"
│ │ └── IERC4906 — "EIP-4906 Interface Definition"
│ └── mocks
│ └── ERC20Mock — "ERC20 Module Reference Implementation"
│ ├── ERC20Mock — "ERC20 Module Reference Implementation"
│ └── ERC721Mock — "ERC721 Module Reference Implementation"
└── utils
├── Base64 — "Base64 Encoding and Decoding Functions"
├── BatchDistributor — "Batch Sending Both Native and ERC-20 Tokens"
Expand Down
2 changes: 1 addition & 1 deletion src/snekmate/extensions/mocks/ERC2981Mock.vy
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ exports: (
# @external
# @view
# def supportsInterface(interface_id: bytes4) -> bool:
# return (interface_id in erc2981._SUPPORTED_INTERFACES) or (interface_id in [0x..., ...])
# return ((interface_id in erc2981._SUPPORTED_INTERFACES) or (interface_id in [0x..., ...]))
# ```
erc2981.supportsInterface,
erc2981.owner,
Expand Down
16 changes: 8 additions & 8 deletions src/snekmate/governance/TimelockController.vy
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ def schedule_batch(targets: DynArray[address, _DYNARRAY_BOUND], amounts: DynArra
Must be greater than or equal to the minimum delay.
"""
access_control._check_role(PROPOSER_ROLE, msg.sender)
assert len(targets) == len(amounts) and len(targets) == len(payloads), "TimelockController: length mismatch"
assert ((len(targets) == len(amounts)) and (len(targets) == len(payloads))), "TimelockController: length mismatch"
id: bytes32 = self._hash_operation_batch(targets, amounts, payloads, predecessor, salt)

self._schedule(id, delay)
Expand Down Expand Up @@ -522,7 +522,7 @@ def execute_batch(targets: DynArray[address, _DYNARRAY_BOUND], amounts: DynArray
the operation during reentrancy are caught.
"""
self._only_role_or_open_role(EXECUTOR_ROLE)
assert len(targets) == len(amounts) and len(targets) == len(payloads), "TimelockController: length mismatch"
assert ((len(targets) == len(amounts)) and (len(targets) == len(payloads))), "TimelockController: length mismatch"
id: bytes32 = self._hash_operation_batch(targets, amounts, payloads, predecessor, salt)

self._before_call(id, predecessor)
Expand Down Expand Up @@ -655,7 +655,7 @@ def _is_operation_pending(id: bytes32) -> bool:
is pending or not.
"""
state: OperationState = self._get_operation_state(id)
return state == OperationState.WAITING or state == OperationState.READY
return ((state == OperationState.WAITING) or (state == OperationState.READY))


@internal
Expand Down Expand Up @@ -700,8 +700,8 @@ def _get_operation_state(id: bytes32) -> OperationState:
return OperationState.DONE
elif (timestamp > block.timestamp):
return OperationState.WAITING
else:
return OperationState.READY

return OperationState.READY


@internal
Expand Down Expand Up @@ -777,8 +777,8 @@ def _execute(target: address, amount: uint256, payload: Bytes[1_024]):
if (len(return_data) != empty(uint256)):
# Bubble up the revert reason.
raw_revert(return_data)
else:
raise "TimelockController: underlying transaction reverted"

raise "TimelockController: underlying transaction reverted"


@internal
Expand All @@ -792,7 +792,7 @@ def _before_call(id: bytes32, predecessor: bytes32):
operation.
"""
assert self._is_operation_ready(id), "TimelockController: operation is not ready"
assert predecessor == empty(bytes32) or self._is_operation_done(predecessor), "TimelockController: missing dependency"
assert ((predecessor == empty(bytes32)) or (self._is_operation_done(predecessor))), "TimelockController: missing dependency"


@internal
Expand Down
20 changes: 10 additions & 10 deletions src/snekmate/tokens/ERC1155.vy
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def safeTransferFrom(owner: address, to: address, id: uint256, amount: uint256,
@param data The maximum 1,024-byte additional data
with no specified format.
"""
assert owner == msg.sender or self.isApprovedForAll[owner][msg.sender], "ERC1155: caller is not token owner or approved"
assert ((owner == msg.sender) or (self.isApprovedForAll[owner][msg.sender])), "ERC1155: caller is not token owner or approved"
self._safe_transfer_from(owner, to, id, amount, data)


Expand Down Expand Up @@ -256,7 +256,7 @@ def safeBatchTransferFrom(owner: address, to: address, ids: DynArray[uint256, _B
@param data The maximum 1,024-byte additional data
with no specified format.
"""
assert owner == msg.sender or self.isApprovedForAll[owner][msg.sender], "ERC1155: caller is not token owner or approved"
assert ((owner == msg.sender) or (self.isApprovedForAll[owner][msg.sender])), "ERC1155: caller is not token owner or approved"
self._safe_batch_transfer_from(owner, to, ids, amounts, data)


Expand Down Expand Up @@ -376,7 +376,7 @@ def burn(owner: address, id: uint256, amount: uint256):
@param id The 32-byte identifier of the token.
@param amount The 32-byte token amount to be destroyed.
"""
assert owner == msg.sender or self.isApprovedForAll[owner][msg.sender], "ERC1155: caller is not token owner or approved"
assert ((owner == msg.sender) or (self.isApprovedForAll[owner][msg.sender])), "ERC1155: caller is not token owner or approved"
self._burn(owner, id, amount)


Expand All @@ -394,7 +394,7 @@ def burn_batch(owner: address, ids: DynArray[uint256, _BATCH_SIZE], amounts: Dyn
being destroyed. Note that the order and length must
match the 32-byte `ids` array.
"""
assert owner == msg.sender or self.isApprovedForAll[owner][msg.sender], "ERC1155: caller is not token owner or approved"
assert ((owner == msg.sender) or (self.isApprovedForAll[owner][msg.sender])), "ERC1155: caller is not token owner or approved"
self._burn_batch(owner, ids, amounts)


Expand Down Expand Up @@ -768,8 +768,8 @@ def _uri(id: uint256) -> String[512]:
# concatenation and simply return `_BASE_URI`
# for easier off-chain handling.
return concat(_BASE_URI, uint2str(id))
else:
return ""

return ""


@internal
Expand Down Expand Up @@ -872,9 +872,9 @@ def _check_on_erc1155_received(owner: address, to: address, id: uint256, amount:
assert return_value == method_id("onERC1155Received(address,address,uint256,uint256,bytes)", output_type=bytes4),\
"ERC1155: transfer to non-ERC1155Receiver implementer"
return True

# EOA case.
else:
return True
return True


@internal
Expand All @@ -901,9 +901,9 @@ def _check_on_erc1155_batch_received(owner: address, to: address, ids: DynArray[
assert return_value == method_id("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)", output_type=bytes4),\
"ERC1155: transfer to non-ERC1155Receiver implementer"
return True

# EOA case.
else:
return True
return True


@internal
Expand Down
Loading

0 comments on commit 64e3170

Please sign in to comment.