From 82306d2d9968dc48fd7fda1f07f04c4b383822dd Mon Sep 17 00:00:00 2001 From: Vectorized Date: Mon, 11 Jul 2022 06:48:53 +0000 Subject: [PATCH 1/4] Fix casting to bytes1 in v0.8.4 --- contracts/mocks/ERC721ReceiverMock.sol | 22 ++++++++++++++++------ hardhat.config.js | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/contracts/mocks/ERC721ReceiverMock.sol b/contracts/mocks/ERC721ReceiverMock.sol index 212fed6b8..fd40b1403 100644 --- a/contracts/mocks/ERC721ReceiverMock.sol +++ b/contracts/mocks/ERC721ReceiverMock.sol @@ -34,18 +34,28 @@ contract ERC721ReceiverMock is ERC721A__IERC721Receiver { uint256 tokenId, bytes memory data ) public override returns (bytes4) { - // for testing reverts with a message from the receiver contract - if (bytes1(data) == 0x01) { + uint256 dataValue; + // In v0.8.4, we can't directly cast via `bytes1(data)`. + // This assembly allows us to do the casting. + assembly { + // If `data.length > 0`. + if mload(data) { + // Load the first byte of `data`. + dataValue := byte(0, mload(add(data, 0x20))) + } + } + // For testing reverts with a message from the receiver contract. + if (dataValue == 0x01) { revert('reverted in the receiver contract!'); } - // for testing with the returned wrong value from the receiver contract - if (bytes1(data) == 0x02) { + // For testing with the returned wrong value from the receiver contract. + if (dataValue == 0x02) { return 0x0; } - // for testing the reentrancy protection - if (bytes1(data) == 0x03) { + // For testing the reentrancy protection. + if (dataValue == 0x03) { IERC721AMock(_erc721aMock).safeMint(address(this), 1); } diff --git a/hardhat.config.js b/hardhat.config.js index 53665221e..3efe779f3 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -14,7 +14,7 @@ if (process.env.REPORT_COVERAGE) { */ module.exports = { solidity: { - version: '0.8.11', + version: '0.8.4', settings: { optimizer: { enabled: true, From ab8251e29daee3b1d64a4440272f023bd1189084 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Mon, 11 Jul 2022 06:52:37 +0000 Subject: [PATCH 2/4] Set back solc to v0.8.11 --- hardhat.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hardhat.config.js b/hardhat.config.js index 3efe779f3..53665221e 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -14,7 +14,7 @@ if (process.env.REPORT_COVERAGE) { */ module.exports = { solidity: { - version: '0.8.4', + version: '0.8.11', settings: { optimizer: { enabled: true, From 2b92777936a28bc91cd7ef02a954e455b6fe9b5a Mon Sep 17 00:00:00 2001 From: Vectorized Date: Mon, 11 Jul 2022 06:54:37 +0000 Subject: [PATCH 3/4] Fix comments --- contracts/mocks/ERC721ReceiverMock.sol | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contracts/mocks/ERC721ReceiverMock.sol b/contracts/mocks/ERC721ReceiverMock.sol index fd40b1403..dfc868975 100644 --- a/contracts/mocks/ERC721ReceiverMock.sol +++ b/contracts/mocks/ERC721ReceiverMock.sol @@ -40,10 +40,11 @@ contract ERC721ReceiverMock is ERC721A__IERC721Receiver { assembly { // If `data.length > 0`. if mload(data) { - // Load the first byte of `data`. + // Load the 0th byte of `data`. dataValue := byte(0, mload(add(data, 0x20))) } } + // For testing reverts with a message from the receiver contract. if (dataValue == 0x01) { revert('reverted in the receiver contract!'); From e650cb3bc9e88e74f9325e9fae3cebc3048ce36a Mon Sep 17 00:00:00 2001 From: Vectorized Date: Tue, 12 Jul 2022 17:14:19 +0000 Subject: [PATCH 4/4] Change to normal Solidity cast --- contracts/mocks/ERC721ReceiverMock.sol | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/contracts/mocks/ERC721ReceiverMock.sol b/contracts/mocks/ERC721ReceiverMock.sol index dfc868975..8ab1fed9f 100644 --- a/contracts/mocks/ERC721ReceiverMock.sol +++ b/contracts/mocks/ERC721ReceiverMock.sol @@ -34,16 +34,7 @@ contract ERC721ReceiverMock is ERC721A__IERC721Receiver { uint256 tokenId, bytes memory data ) public override returns (bytes4) { - uint256 dataValue; - // In v0.8.4, we can't directly cast via `bytes1(data)`. - // This assembly allows us to do the casting. - assembly { - // If `data.length > 0`. - if mload(data) { - // Load the 0th byte of `data`. - dataValue := byte(0, mload(add(data, 0x20))) - } - } + uint256 dataValue = data.length == 0 ? 0 : uint256(uint8(data[0])); // For testing reverts with a message from the receiver contract. if (dataValue == 0x01) {