@@ -241,7 +241,6 @@ pragma solidity ^0.8.0;
241241import "./IERC1155.sol";
242242import "./IERC1155Receiver.sol";
243243import "./IERC1155MetadataURI.sol";
244- import "https://github.com/AmazingAng/WTF-Solidity/blob/main/34_ERC721/Address.sol";
245244import "https://github.com/AmazingAng/WTF-Solidity/blob/main/34_ERC721/String.sol";
246245import "https://github.com/AmazingAng/WTF-Solidity/blob/main/34_ERC721/IERC165.sol";
247246
@@ -250,8 +249,8 @@ import "https://github.com/AmazingAng/WTF-Solidity/blob/main/34_ERC721/IERC165.s
250249 * 见 https://eips.ethereum.org/EIPS/eip-1155
251250 */
252251contract ERC1155 is IERC165, IERC1155, IERC1155MetadataURI {
253- using Address for address; // 使用Address库,用isContract来判断地址是否为合约
254- using Strings for uint256; // 使用Strings库
252+
253+ using Strings for uint256; // 使用String库
255254 // Token名称
256255 string public name;
257256 // Token代号
@@ -493,6 +492,9 @@ contract ERC1155 is IERC165, IERC1155, IERC1155MetadataURI {
493492 emit TransferBatch(operator, from, address(0), ids, amounts);
494493 }
495494
495+ // 错误 无效的接收者
496+ error ERC1155InvalidReceiver(address receiver);
497+
496498 // @dev ERC1155的安全转账检查
497499 function _doSafeTransferAcceptanceCheck(
498500 address operator,
@@ -502,15 +504,20 @@ contract ERC1155 is IERC165, IERC1155, IERC1155MetadataURI {
502504 uint256 amount,
503505 bytes memory data
504506 ) private {
505- if (to.isContract() ) {
507+ if (to.code.length > 0 ) {
506508 try IERC1155Receiver(to).onERC1155Received(operator, from, id, amount, data) returns (bytes4 response) {
507509 if (response != IERC1155Receiver.onERC1155Received.selector) {
508- revert("ERC1155: ERC1155Receiver rejected tokens");
510+ revert ERC1155InvalidReceiver(to);
511+ }
512+ } catch (bytes memory reason) {
513+ if (reason.length == 0) {
514+ revert ERC1155InvalidReceiver(to);
515+ } else {
516+ /// @solidity memory-safe-assembly
517+ assembly {
518+ revert(add(32, reason), mload(reason))
519+ }
509520 }
510- } catch Error(string memory reason) {
511- revert(reason);
512- } catch {
513- revert("ERC1155: transfer to non-ERC1155Receiver implementer");
514521 }
515522 }
516523 }
@@ -524,17 +531,22 @@ contract ERC1155 is IERC165, IERC1155, IERC1155MetadataURI {
524531 uint256[] memory amounts,
525532 bytes memory data
526533 ) private {
527- if (to.isContract() ) {
534+ if (to.code.length > 0 ) {
528535 try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, amounts, data) returns (
529536 bytes4 response
530537 ) {
531538 if (response != IERC1155Receiver.onERC1155BatchReceived.selector) {
532- revert("ERC1155: ERC1155Receiver rejected tokens");
539+ revert ERC1155InvalidReceiver(to);
540+ }
541+ } catch (bytes memory reason) {
542+ if (reason.length == 0) {
543+ revert ERC1155InvalidReceiver(to);
544+ } else {
545+ /// @solidity memory-safe-assembly
546+ assembly {
547+ revert(add(32, reason), mload(reason))
548+ }
533549 }
534- } catch Error(string memory reason) {
535- revert(reason);
536- } catch {
537- revert("ERC1155: transfer to non-ERC1155Receiver implementer");
538550 }
539551 }
540552 }
0 commit comments