Skip to content

Commit 6e91347

Browse files
authored
Merge pull request #817 from eddiehsu66/40_ERC1155
remove Address.sol, replace isContract with address(...).code.length > 0
1 parent d84de45 commit 6e91347

File tree

2 files changed

+54
-30
lines changed

2 files changed

+54
-30
lines changed

40_ERC1155/ERC1155.sol

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ pragma solidity ^0.8.0;
44
import "./IERC1155.sol";
55
import "./IERC1155Receiver.sol";
66
import "./IERC1155MetadataURI.sol";
7-
import "../34_ERC721/Address.sol";
87
import "../34_ERC721/String.sol";
98
import "../34_ERC721/IERC165.sol";
109

@@ -13,8 +12,8 @@ import "../34_ERC721/IERC165.sol";
1312
* 见 https://eips.ethereum.org/EIPS/eip-1155
1413
*/
1514
contract ERC1155 is IERC165, IERC1155, IERC1155MetadataURI {
16-
using Address for address; // 使用Address库,用isContract来判断地址是否为合约
17-
using Strings for uint256; // 使用Strings库
15+
16+
using Strings for uint256; // 使用String库
1817
// Token名称
1918
string public name;
2019
// Token代号
@@ -256,6 +255,9 @@ contract ERC1155 is IERC165, IERC1155, IERC1155MetadataURI {
256255
emit TransferBatch(operator, from, address(0), ids, amounts);
257256
}
258257

258+
// 错误 无效的接收者
259+
error ERC1155InvalidReceiver(address receiver);
260+
259261
// @dev ERC1155的安全转账检查
260262
function _doSafeTransferAcceptanceCheck(
261263
address operator,
@@ -265,15 +267,20 @@ contract ERC1155 is IERC165, IERC1155, IERC1155MetadataURI {
265267
uint256 amount,
266268
bytes memory data
267269
) private {
268-
if (to.isContract()) {
270+
if (to.code.length > 0) {
269271
try IERC1155Receiver(to).onERC1155Received(operator, from, id, amount, data) returns (bytes4 response) {
270272
if (response != IERC1155Receiver.onERC1155Received.selector) {
271-
revert("ERC1155: ERC1155Receiver rejected tokens");
273+
revert ERC1155InvalidReceiver(to);
274+
}
275+
} catch (bytes memory reason) {
276+
if (reason.length == 0) {
277+
revert ERC1155InvalidReceiver(to);
278+
} else {
279+
/// @solidity memory-safe-assembly
280+
assembly {
281+
revert(add(32, reason), mload(reason))
282+
}
272283
}
273-
} catch Error(string memory reason) {
274-
revert(reason);
275-
} catch {
276-
revert("ERC1155: transfer to non-ERC1155Receiver implementer");
277284
}
278285
}
279286
}
@@ -287,17 +294,22 @@ contract ERC1155 is IERC165, IERC1155, IERC1155MetadataURI {
287294
uint256[] memory amounts,
288295
bytes memory data
289296
) private {
290-
if (to.isContract()) {
297+
if (to.code.length > 0) {
291298
try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, amounts, data) returns (
292299
bytes4 response
293300
) {
294301
if (response != IERC1155Receiver.onERC1155BatchReceived.selector) {
295-
revert("ERC1155: ERC1155Receiver rejected tokens");
302+
revert ERC1155InvalidReceiver(to);
303+
}
304+
} catch (bytes memory reason) {
305+
if (reason.length == 0) {
306+
revert ERC1155InvalidReceiver(to);
307+
} else {
308+
/// @solidity memory-safe-assembly
309+
assembly {
310+
revert(add(32, reason), mload(reason))
311+
}
296312
}
297-
} catch Error(string memory reason) {
298-
revert(reason);
299-
} catch {
300-
revert("ERC1155: transfer to non-ERC1155Receiver implementer");
301313
}
302314
}
303315
}

40_ERC1155/readme.md

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ pragma solidity ^0.8.0;
241241
import "./IERC1155.sol";
242242
import "./IERC1155Receiver.sol";
243243
import "./IERC1155MetadataURI.sol";
244-
import "https://github.com/AmazingAng/WTF-Solidity/blob/main/34_ERC721/Address.sol";
245244
import "https://github.com/AmazingAng/WTF-Solidity/blob/main/34_ERC721/String.sol";
246245
import "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
*/
252251
contract 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

Comments
 (0)