|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity 0.8.26; |
| 3 | + |
| 4 | +import {Client} from "@chainlink/contracts/src/v0.8/ccip/libraries/Client.sol"; |
| 5 | +import {CCIPReceiver} from "@chainlink/contracts/src/v0.8/ccip/applications/CCIPReceiver.sol"; |
| 6 | + |
| 7 | +/// @title AmoyReceiverAndTokenSender |
| 8 | +/// @author Luo Yingjie |
| 9 | +/// @notice This contract will receive the message sent from sepolia chain |
| 10 | +/// @notice This contract will be deployed on the amoy chain |
| 11 | +contract AmoyReceiver is CCIPReceiver { |
| 12 | + event MessageReceived( |
| 13 | + bytes32 indexed messageId, // The unique ID of the message. |
| 14 | + uint64 indexed sourceChainSelector, // The chain selector of the source chain. |
| 15 | + address sender, // The address of the sender from the source chain. |
| 16 | + bytes signedMessage // The signed message that approves the token transfer |
| 17 | + ); |
| 18 | + |
| 19 | + bytes32 private s_messageId; |
| 20 | + bytes private s_signedMessage; |
| 21 | + |
| 22 | + /// @notice Constructor initializes the contract with the router address. |
| 23 | + /// @param router The address of the router contract. |
| 24 | + constructor(address router) CCIPReceiver(router) {} |
| 25 | + |
| 26 | + /// handle a received message |
| 27 | + function _ccipReceive( |
| 28 | + Client.Any2EVMMessage memory any2EvmMessage |
| 29 | + ) internal override { |
| 30 | + s_messageId = any2EvmMessage.messageId; |
| 31 | + s_signedMessage = any2EvmMessage.data; |
| 32 | + |
| 33 | + emit MessageReceived( |
| 34 | + any2EvmMessage.messageId, |
| 35 | + any2EvmMessage.sourceChainSelector, // fetch the source chain identifier (aka selector) |
| 36 | + abi.decode(any2EvmMessage.sender, (address)), // abi-decoding of the sender address, |
| 37 | + abi.decode(any2EvmMessage.data, (bytes)) // abi-decoding of the signed message |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + function getMessageId() external view returns (bytes32) { |
| 42 | + return s_messageId; |
| 43 | + } |
| 44 | + |
| 45 | + function getSignedMessage() external view returns (bytes memory) { |
| 46 | + return s_signedMessage; |
| 47 | + } |
| 48 | +} |
0 commit comments