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

remove Address.sol, replace isContract with address(...).code.length > 0 #817

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
40 changes: 25 additions & 15 deletions 40_ERC1155/ERC1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity ^0.8.0;
import "./IERC1155.sol";
import "./IERC1155Receiver.sol";
import "./IERC1155MetadataURI.sol";
import "../34_ERC721/Address.sol";
import "../34_ERC721/String.sol";
import "../34_ERC721/IERC165.sol";

Expand All @@ -13,8 +12,6 @@ import "../34_ERC721/IERC165.sol";
* 见 https://eips.ethereum.org/EIPS/eip-1155
*/
contract ERC1155 is IERC165, IERC1155, IERC1155MetadataURI {
using Address for address; // 使用Address库,用isContract来判断地址是否为合约
using Strings for uint256; // 使用Strings库
XdpCs marked this conversation as resolved.
Show resolved Hide resolved
// Token名称
string public name;
// Token代号
Expand Down Expand Up @@ -256,6 +253,9 @@ contract ERC1155 is IERC165, IERC1155, IERC1155MetadataURI {
emit TransferBatch(operator, from, address(0), ids, amounts);
}

// 错误 无效的接收者
error ERC1155InvalidReceiver(address receiver);

// @dev ERC1155的安全转账检查
function _doSafeTransferAcceptanceCheck(
address operator,
Expand All @@ -265,15 +265,20 @@ contract ERC1155 is IERC165, IERC1155, IERC1155MetadataURI {
uint256 amount,
bytes memory data
) private {
if (to.isContract()) {
if (to.code.length > 0) {
try IERC1155Receiver(to).onERC1155Received(operator, from, id, amount, data) returns (bytes4 response) {
if (response != IERC1155Receiver.onERC1155Received.selector) {
revert("ERC1155: ERC1155Receiver rejected tokens");
revert ERC1155InvalidReceiver(to);
}
} catch (bytes memory reason) {
if (reason.length == 0) {
revert ERC1155InvalidReceiver(to);
} else {
/// @solidity memory-safe-assembly
assembly {
revert(add(32, reason), mload(reason))
}
}
} catch Error(string memory reason) {
revert(reason);
} catch {
revert("ERC1155: transfer to non-ERC1155Receiver implementer");
}
}
}
Expand All @@ -287,17 +292,22 @@ contract ERC1155 is IERC165, IERC1155, IERC1155MetadataURI {
uint256[] memory amounts,
bytes memory data
) private {
if (to.isContract()) {
if (to.code.length > 0) {
try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, amounts, data) returns (
bytes4 response
) {
if (response != IERC1155Receiver.onERC1155BatchReceived.selector) {
revert("ERC1155: ERC1155Receiver rejected tokens");
revert ERC1155InvalidReceiver(to);
}
} catch (bytes memory reason) {
if (reason.length == 0) {
revert ERC1155InvalidReceiver(to);
} else {
/// @solidity memory-safe-assembly
assembly {
revert(add(32, reason), mload(reason))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

内联汇编是否可以修改成可读性更高的

}
}
} catch Error(string memory reason) {
revert(reason);
} catch {
revert("ERC1155: transfer to non-ERC1155Receiver implementer");
}
}
}
Expand Down
40 changes: 25 additions & 15 deletions 40_ERC1155/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ pragma solidity ^0.8.0;
import "./IERC1155.sol";
import "./IERC1155Receiver.sol";
import "./IERC1155MetadataURI.sol";
import "https://github.com/AmazingAng/WTF-Solidity/blob/main/34_ERC721/Address.sol";
import "https://github.com/AmazingAng/WTF-Solidity/blob/main/34_ERC721/String.sol";
import "https://github.com/AmazingAng/WTF-Solidity/blob/main/34_ERC721/IERC165.sol";

Expand All @@ -249,8 +248,6 @@ import "https://github.com/AmazingAng/WTF-Solidity/blob/main/34_ERC721/IERC165.s
* 见 https://eips.ethereum.org/EIPS/eip-1155
*/
contract ERC1155 is IERC165, IERC1155, IERC1155MetadataURI {
using Address for address; // 使用Address库,用isContract来判断地址是否为合约
using Strings for uint256; // 使用Strings库
// Token名称
string public name;
// Token代号
Expand Down Expand Up @@ -492,6 +489,9 @@ contract ERC1155 is IERC165, IERC1155, IERC1155MetadataURI {
emit TransferBatch(operator, from, address(0), ids, amounts);
}

// 错误 无效的接收者
error ERC1155InvalidReceiver(address receiver);

// @dev ERC1155的安全转账检查
function _doSafeTransferAcceptanceCheck(
address operator,
Expand All @@ -501,15 +501,20 @@ contract ERC1155 is IERC165, IERC1155, IERC1155MetadataURI {
uint256 amount,
bytes memory data
) private {
if (to.isContract()) {
if (to.code.length > 0) {
try IERC1155Receiver(to).onERC1155Received(operator, from, id, amount, data) returns (bytes4 response) {
if (response != IERC1155Receiver.onERC1155Received.selector) {
revert("ERC1155: ERC1155Receiver rejected tokens");
revert ERC1155InvalidReceiver(to);
}
} catch (bytes memory reason) {
if (reason.length == 0) {
revert ERC1155InvalidReceiver(to);
} else {
/// @solidity memory-safe-assembly
assembly {
revert(add(32, reason), mload(reason))
}
}
} catch Error(string memory reason) {
revert(reason);
} catch {
revert("ERC1155: transfer to non-ERC1155Receiver implementer");
}
}
}
Expand All @@ -523,17 +528,22 @@ contract ERC1155 is IERC165, IERC1155, IERC1155MetadataURI {
uint256[] memory amounts,
bytes memory data
) private {
if (to.isContract()) {
if (to.code.length > 0) {
try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, amounts, data) returns (
bytes4 response
) {
if (response != IERC1155Receiver.onERC1155BatchReceived.selector) {
revert("ERC1155: ERC1155Receiver rejected tokens");
revert ERC1155InvalidReceiver(to);
}
} catch (bytes memory reason) {
if (reason.length == 0) {
revert ERC1155InvalidReceiver(to);
} else {
/// @solidity memory-safe-assembly
assembly {
revert(add(32, reason), mload(reason))
}
}
} catch Error(string memory reason) {
revert(reason);
} catch {
revert("ERC1155: transfer to non-ERC1155Receiver implementer");
}
}
}
Expand Down