Skip to content

Commit 92eb88c

Browse files
ernestognwclaude
authored andcommitted
ERC1155 crosschain bridge: propagate the ERC-1155 data field (M-20) (#6643)
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 90a5c80 commit 92eb88c

4 files changed

Lines changed: 199 additions & 44 deletions

File tree

‎contracts/crosschain/bridges/BridgeERC1155.sol‎

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,54 @@ abstract contract BridgeERC1155 is BridgeMultiToken, ERC1155Holder {
2626
return _token;
2727
}
2828

29+
/// @dev Equivalent to `crosschainTransferFrom(from, to, id, value, "")`.
30+
function crosschainTransferFrom(address from, bytes memory to, uint256 id, uint256 value) public returns (bytes32) {
31+
return crosschainTransferFrom(from, to, id, value, "");
32+
}
33+
2934
/**
30-
* @dev Transfer `amount` tokens to a crosschain receiver.
35+
* @dev Transfer `value` of token `id` to a crosschain receiver. `data` is forwarded to the destination-chain
36+
* ERC-1155 receiver's acceptance hook.
3137
*
3238
* Note: The `to` parameter is the full InteroperableAddress (chain ref + address).
3339
*/
34-
function crosschainTransferFrom(address from, bytes memory to, uint256 id, uint256 value) public returns (bytes32) {
40+
function crosschainTransferFrom(
41+
address from,
42+
bytes memory to,
43+
uint256 id,
44+
uint256 value,
45+
bytes memory data
46+
) public returns (bytes32) {
3547
uint256[] memory ids = new uint256[](1);
3648
uint256[] memory values = new uint256[](1);
3749
ids[0] = id;
3850
values[0] = value;
3951

40-
return crosschainTransferFrom(from, to, ids, values);
52+
return crosschainTransferFrom(from, to, ids, values, data);
53+
}
54+
55+
/// @dev Equivalent to `crosschainTransferFrom(from, to, ids, values, "")`.
56+
function crosschainTransferFrom(
57+
address from,
58+
bytes memory to,
59+
uint256[] memory ids,
60+
uint256[] memory values
61+
) public returns (bytes32) {
62+
return crosschainTransferFrom(from, to, ids, values, "");
4163
}
4264

4365
/**
44-
* @dev Transfer `amount` tokens to a crosschain receiver.
66+
* @dev Transfer `values` of tokens `ids` to a crosschain receiver. `data` is forwarded to the destination-chain
67+
* ERC-1155 receiver's acceptance hook.
4568
*
4669
* Note: The `to` parameter is the full InteroperableAddress (chain ref + address).
4770
*/
4871
function crosschainTransferFrom(
4972
address from,
5073
bytes memory to,
5174
uint256[] memory ids,
52-
uint256[] memory values
75+
uint256[] memory values,
76+
bytes memory data
5377
) public virtual returns (bytes32) {
5478
// Permission is handled using the ERC1155's allowance system. This check replicates `ERC1155._checkAuthorized`.
5579
address spender = _msgSender();
@@ -59,17 +83,22 @@ abstract contract BridgeERC1155 is BridgeMultiToken, ERC1155Holder {
5983
);
6084

6185
// Perform the crosschain transfer and return the handler
62-
return _crosschainTransfer(from, to, ids, values);
86+
return _crosschainTransfer(from, to, ids, values, data);
6387
}
6488

65-
/// @dev "Locking" tokens is done by taking custody
89+
/// @dev "Locking" tokens is done by taking custody.
6690
function _onSend(address from, uint256[] memory ids, uint256[] memory values) internal virtual override {
6791
token().safeBatchTransferFrom(from, address(this), ids, values, "");
6892
}
6993

70-
/// @dev "Unlocking" tokens is done by releasing custody
71-
function _onReceive(address to, uint256[] memory ids, uint256[] memory values) internal virtual override {
72-
token().safeBatchTransferFrom(address(this), to, ids, values, "");
94+
/// @dev "Unlocking" tokens is done by releasing custody.
95+
function _onReceive(
96+
address to,
97+
uint256[] memory ids,
98+
uint256[] memory values,
99+
bytes memory data
100+
) internal virtual override {
101+
token().safeBatchTransferFrom(address(this), to, ids, values, data);
73102
}
74103

75104
/// @dev Support receiving tokens only if the transfer was initiated by the bridge itself.

‎contracts/crosschain/bridges/abstract/BridgeMultiToken.sol‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,30 @@ abstract contract BridgeMultiToken is Context, CrosschainLinked {
2929
address indexed from,
3030
bytes to,
3131
uint256[] ids,
32-
uint256[] values
32+
uint256[] values,
33+
bytes data
3334
);
3435
event CrosschainMultiTokenTransferReceived(
3536
bytes32 indexed receiveId,
3637
bytes from,
3738
address indexed to,
3839
uint256[] ids,
39-
uint256[] values
40+
uint256[] values,
41+
bytes data
4042
);
43+
4144
/**
42-
* @dev Internal crosschain transfer function.
45+
* @dev Internal crosschain transfer function. `data` is forwarded through the ERC-7786 payload to
46+
* {_onReceive} on the destination chain.
4347
*
4448
* Note: The `to` parameter is the full InteroperableAddress (chain ref + address).
4549
*/
4650
function _crosschainTransfer(
4751
address from,
4852
bytes memory to,
4953
uint256[] memory ids,
50-
uint256[] memory values
54+
uint256[] memory values,
55+
bytes memory data
5156
) internal virtual returns (bytes32) {
5257
_onSend(from, ids, values);
5358

@@ -56,11 +61,11 @@ abstract contract BridgeMultiToken is Context, CrosschainLinked {
5661

5762
bytes32 sendId = _sendMessageToCounterpart(
5863
chain,
59-
abi.encode(InteroperableAddress.formatEvmV1(block.chainid, from), addr, ids, values),
64+
abi.encode(InteroperableAddress.formatEvmV1(block.chainid, from), addr, ids, values, data),
6065
new bytes[](0)
6166
);
6267

63-
emit CrosschainMultiTokenTransferSent(sendId, from, to, ids, values);
68+
emit CrosschainMultiTokenTransferSent(sendId, from, to, ids, values, data);
6469
return sendId;
6570
}
6671

@@ -74,20 +79,18 @@ abstract contract BridgeMultiToken is Context, CrosschainLinked {
7479
// NOTE: Gateway is validated by {_isAuthorizedGateway} (implemented in {CrosschainLinked}). No need to check here.
7580

7681
// split payload
77-
(bytes memory from, bytes memory toEvm, uint256[] memory ids, uint256[] memory values) = abi.decode(
78-
payload,
79-
(bytes, bytes, uint256[], uint256[])
80-
);
82+
(bytes memory from, bytes memory toEvm, uint256[] memory ids, uint256[] memory values, bytes memory data) = abi
83+
.decode(payload, (bytes, bytes, uint256[], uint256[], bytes));
8184
address to = address(bytes20(toEvm));
8285

83-
_onReceive(to, ids, values);
86+
_onReceive(to, ids, values, data);
8487

85-
emit CrosschainMultiTokenTransferReceived(receiveId, from, to, ids, values);
88+
emit CrosschainMultiTokenTransferReceived(receiveId, from, to, ids, values, data);
8689
}
8790

8891
/// @dev Virtual function: implementation is required to handle token being burnt or locked on the source chain.
8992
function _onSend(address from, uint256[] memory ids, uint256[] memory values) internal virtual;
9093

9194
/// @dev Virtual function: implementation is required to handle token being minted or unlocked on the destination chain.
92-
function _onReceive(address to, uint256[] memory ids, uint256[] memory values) internal virtual;
95+
function _onReceive(address to, uint256[] memory ids, uint256[] memory values, bytes memory data) internal virtual;
9396
}

‎contracts/token/ERC1155/extensions/ERC1155Crosschain.sol‎

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,73 @@ import {BridgeMultiToken} from "../../../crosschain/bridges/abstract/BridgeMulti
1515
*/
1616
// slither-disable-next-line locked-ether
1717
abstract contract ERC1155Crosschain is BridgeMultiToken, ERC1155 {
18-
/// @dev TransferFrom variant of {crosschainTransferFrom}, using ERC1155 allowance from the sender to the caller.
18+
/// @dev Equivalent to `crosschainTransferFrom(from, to, id, value, "")`.
1919
function crosschainTransferFrom(
2020
address from,
2121
bytes memory to,
2222
uint256 id,
2323
uint256 value
24+
) public virtual returns (bytes32) {
25+
return crosschainTransferFrom(from, to, id, value, "");
26+
}
27+
28+
/**
29+
* @dev TransferFrom variant of {crosschainTransferFrom}, using ERC1155 allowance from the sender to the caller.
30+
* `data` is forwarded to the destination-chain ERC-1155 receiver's acceptance hook.
31+
*/
32+
function crosschainTransferFrom(
33+
address from,
34+
bytes memory to,
35+
uint256 id,
36+
uint256 value,
37+
bytes memory data
2438
) public virtual returns (bytes32) {
2539
_checkAuthorized(_msgSender(), from);
2640

2741
uint256[] memory ids = new uint256[](1);
2842
uint256[] memory values = new uint256[](1);
2943
ids[0] = id;
3044
values[0] = value;
31-
return _crosschainTransfer(from, to, ids, values);
45+
return _crosschainTransfer(from, to, ids, values, data);
3246
}
3347

34-
/// @dev TransferFrom variant of {crosschainTransferFrom}, using ERC1155 allowance from the sender to the caller.
48+
/// @dev Equivalent to `crosschainTransferFrom(from, to, ids, values, "")`.
3549
function crosschainTransferFrom(
3650
address from,
3751
bytes memory to,
3852
uint256[] memory ids,
3953
uint256[] memory values
54+
) public virtual returns (bytes32) {
55+
return crosschainTransferFrom(from, to, ids, values, "");
56+
}
57+
58+
/**
59+
* @dev TransferFrom variant of {crosschainTransferFrom}, using ERC1155 allowance from the sender to the caller.
60+
* `data` is forwarded to the destination-chain ERC-1155 receiver's acceptance hook.
61+
*/
62+
function crosschainTransferFrom(
63+
address from,
64+
bytes memory to,
65+
uint256[] memory ids,
66+
uint256[] memory values,
67+
bytes memory data
4068
) public virtual returns (bytes32) {
4169
_checkAuthorized(_msgSender(), from);
42-
return _crosschainTransfer(from, to, ids, values);
70+
return _crosschainTransfer(from, to, ids, values, data);
4371
}
4472

45-
/// @dev "Locking" tokens is achieved through burning
73+
/// @dev "Locking" tokens is achieved through burning.
4674
function _onSend(address from, uint256[] memory ids, uint256[] memory values) internal virtual override {
4775
_burnBatch(from, ids, values);
4876
}
4977

50-
/// @dev "Unlocking" tokens is achieved through minting
51-
function _onReceive(address to, uint256[] memory ids, uint256[] memory values) internal virtual override {
52-
_mintBatch(to, ids, values, "");
78+
/// @dev "Unlocking" tokens is achieved through minting.
79+
function _onReceive(
80+
address to,
81+
uint256[] memory ids,
82+
uint256[] memory values,
83+
bytes memory data
84+
) internal virtual override {
85+
_mintBatch(to, ids, values, data);
5386
}
5487
}

0 commit comments

Comments
 (0)