forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBridgeERC1155Core.sol
More file actions
92 lines (79 loc) · 3.6 KB
/
BridgeERC1155Core.sol
File metadata and controls
92 lines (79 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import {InteroperableAddress} from "../../utils/draft-InteroperableAddress.sol";
import {Context} from "../../utils/Context.sol";
import {ERC7786Recipient} from "../ERC7786Recipient.sol";
import {CrosschainLinked} from "../CrosschainLinked.sol";
/**
* @dev Base contract for bridging ERC-1155 between chains using an ERC-7786 gateway.
*
* In order to use this contract, two functions must be implemented to link it to the token:
* * {_onSend}: called when a crosschain transfer is going out. Must take the sender tokens or revert.
* * {_onReceive}: called when a crosschain transfer is coming in. Must give tokens to the receiver.
*
* This base contract is used by the {BridgeERC1155}, which interfaces with legacy ERC-1155 tokens. It is also used by
* the {ERC1155Crosschain} extension, which embeds the bridge logic directly in the token contract.
*
* This base contract implements the crosschain transfer operation though internal functions. It is for the the "child
* contracts" that inherit from this to implement the external interfaces and make this functions accessible.
*/
abstract contract BridgeERC1155Core is Context, CrosschainLinked {
using InteroperableAddress for bytes;
event CrosschainERC1155TransferSent(
bytes32 indexed sendId,
address indexed from,
bytes to,
uint256[] ids,
uint256[] values
);
event CrosschainERC1155TransferReceived(
bytes32 indexed receiveId,
bytes from,
address indexed to,
uint256[] ids,
uint256[] values
);
/**
* @dev Internal crosschain transfer function.
*
* Note: The `to` parameter is the full InteroperableAddress (chain ref + address).
*/
function _crosschainTransfer(
address from,
bytes memory to,
uint256[] memory ids,
uint256[] memory values
) internal virtual returns (bytes32) {
_onSend(from, ids, values);
(bytes2 chainType, bytes memory chainReference, bytes memory addr) = to.parseV1();
bytes memory chain = InteroperableAddress.formatV1(chainType, chainReference, hex"");
bytes32 sendId = _sendMessageToCounterpart(
chain,
abi.encode(InteroperableAddress.formatEvmV1(block.chainid, from), addr, ids, values),
new bytes[](0)
);
emit CrosschainERC1155TransferSent(sendId, from, to, ids, values);
return sendId;
}
/// @inheritdoc ERC7786Recipient
function _processMessage(
address /*gateway*/,
bytes32 receiveId,
bytes calldata /*sender*/,
bytes calldata payload
) internal virtual override {
// NOTE: Gateway is validated by {_isAuthorizedGateway} (implemented in {CrosschainLinked}). No need to check here.
// split payload
(bytes memory from, bytes memory toEvm, uint256[] memory ids, uint256[] memory values) = abi.decode(
payload,
(bytes, bytes, uint256[], uint256[])
);
address to = address(bytes20(toEvm));
_onReceive(to, ids, values);
emit CrosschainERC1155TransferReceived(receiveId, from, to, ids, values);
}
/// @dev Virtual function: implementation is required to handle token being burnt or locked on the source chain.
function _onSend(address from, uint256[] memory ids, uint256[] memory values) internal virtual;
/// @dev Virtual function: implementation is required to handle token being minted or unlocked on the destination chain.
function _onReceive(address to, uint256[] memory ids, uint256[] memory values) internal virtual;
}