|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.26; |
| 4 | + |
| 5 | +import {InteroperableAddress} from "../../../utils/draft-InteroperableAddress.sol"; |
| 6 | +import {Context} from "../../../utils/Context.sol"; |
| 7 | +import {ERC7786Recipient} from "../../ERC7786Recipient.sol"; |
| 8 | +import {CrosschainLinked} from "../../CrosschainLinked.sol"; |
| 9 | + |
| 10 | +/** |
| 11 | + * @dev Base contract for bridging ERC-721 between chains using an ERC-7786 gateway. |
| 12 | + * |
| 13 | + * In order to use this contract, two functions must be implemented to link it to the token: |
| 14 | + * * {_onSend}: called when a crosschain transfer is going out. Must take the sender tokens or revert. |
| 15 | + * * {_onReceive}: called when a crosschain transfer is coming in. Must give tokens to the receiver. |
| 16 | + * |
| 17 | + * This base contract is used by the {BridgeERC721}, which interfaces with legacy ERC-721 tokens. It is also used by |
| 18 | + * the {ERC721Crosschain} extension, which embeds the bridge logic directly in the token contract. |
| 19 | + */ |
| 20 | +abstract contract BridgeNonFungible is Context, CrosschainLinked { |
| 21 | + /// @dev Emitted when a crosschain ERC-721 transfer is sent. |
| 22 | + event CrosschainNonFungibleTransferSent(bytes32 indexed sendId, address indexed from, bytes to, uint256 tokenId); |
| 23 | + |
| 24 | + /// @dev Emitted when a crosschain ERC-721 transfer is received. |
| 25 | + event CrosschainNonFungibleTransferReceived( |
| 26 | + bytes32 indexed receiveId, |
| 27 | + bytes from, |
| 28 | + address indexed to, |
| 29 | + uint256 tokenId |
| 30 | + ); |
| 31 | + |
| 32 | + /** |
| 33 | + * @dev Internal crosschain transfer function. |
| 34 | + * |
| 35 | + * NOTE: The `to` parameter is the full InteroperableAddress (chain ref + address). |
| 36 | + */ |
| 37 | + function _crosschainTransfer(address from, bytes memory to, uint256 tokenId) internal virtual returns (bytes32) { |
| 38 | + _onSend(from, tokenId); |
| 39 | + |
| 40 | + (bytes2 chainType, bytes memory chainReference, bytes memory addr) = InteroperableAddress.parseV1(to); |
| 41 | + bytes memory chain = InteroperableAddress.formatV1(chainType, chainReference, hex""); |
| 42 | + |
| 43 | + bytes32 sendId = _sendMessageToCounterpart( |
| 44 | + chain, |
| 45 | + abi.encode(InteroperableAddress.formatEvmV1(block.chainid, from), addr, tokenId), |
| 46 | + new bytes[](0) |
| 47 | + ); |
| 48 | + |
| 49 | + emit CrosschainNonFungibleTransferSent(sendId, from, to, tokenId); |
| 50 | + |
| 51 | + return sendId; |
| 52 | + } |
| 53 | + |
| 54 | + /// @inheritdoc ERC7786Recipient |
| 55 | + function _processMessage( |
| 56 | + address /*gateway*/, |
| 57 | + bytes32 receiveId, |
| 58 | + bytes calldata /*sender*/, |
| 59 | + bytes calldata payload |
| 60 | + ) internal virtual override { |
| 61 | + // split payload |
| 62 | + (bytes memory from, bytes memory toEvm, uint256 tokenId) = abi.decode(payload, (bytes, bytes, uint256)); |
| 63 | + address to = address(bytes20(toEvm)); |
| 64 | + |
| 65 | + _onReceive(to, tokenId); |
| 66 | + |
| 67 | + emit CrosschainNonFungibleTransferReceived(receiveId, from, to, tokenId); |
| 68 | + } |
| 69 | + |
| 70 | + /// @dev Virtual function: implementation is required to handle token being burnt or locked on the source chain. |
| 71 | + function _onSend(address from, uint256 tokenId) internal virtual; |
| 72 | + |
| 73 | + /// @dev Virtual function: implementation is required to handle token being minted or unlocked on the destination chain. |
| 74 | + function _onReceive(address to, uint256 tokenId) internal virtual; |
| 75 | +} |
0 commit comments