|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity 0.8.26; |
| 3 | + |
| 4 | +import {Test, console} from "forge-std/Test.sol"; |
| 5 | +import {CCIPLocalSimulatorFork, Register} from "@chainlink/local/src/ccip/CCIPLocalSimulatorFork.sol"; |
| 6 | +import {IRouterClient} from "@chainlink/contracts-ccip/src/v0.8/ccip/interfaces/IRouterClient.sol"; |
| 7 | +import {Client} from "@chainlink/contracts-ccip/src/v0.8/ccip/libraries/Client.sol"; |
| 8 | +import {SepoliaSender} from "src/SepoliaSender.sol"; |
| 9 | +import {AmoyReceiver} from "src/AmoyReceiver.sol"; |
| 10 | + |
| 11 | +contract MessageTransferForkTest is Test { |
| 12 | + CCIPLocalSimulatorFork public ccipLocalSimulatorFork; |
| 13 | + uint64 public destinationChainSelector; |
| 14 | + uint256 public sourceFork; |
| 15 | + uint256 public destinationFork; |
| 16 | + |
| 17 | + IRouterClient public sourceRouter; |
| 18 | + IRouterClient public destinationRouter; |
| 19 | + |
| 20 | + SepoliaSender public sepoliaSender; |
| 21 | + AmoyReceiver public amoyReceiver; |
| 22 | + |
| 23 | + function setUp() public { |
| 24 | + string memory SOURCE_RPC_URL = vm.envString("SEPOLIA_RPC_URL"); |
| 25 | + string memory DESTINATION_RPC_URL = vm.envString("AMOY_RPC_URL"); |
| 26 | + sourceFork = vm.createFork(SOURCE_RPC_URL); |
| 27 | + destinationFork = vm.createSelectFork(DESTINATION_RPC_URL); |
| 28 | + |
| 29 | + ccipLocalSimulatorFork = new CCIPLocalSimulatorFork(); |
| 30 | + vm.makePersistent(address(ccipLocalSimulatorFork)); |
| 31 | + |
| 32 | + vm.selectFork(sourceFork); |
| 33 | + Register.NetworkDetails |
| 34 | + memory sourceNetworkDetails = ccipLocalSimulatorFork |
| 35 | + .getNetworkDetails(block.chainid); |
| 36 | + sourceRouter = IRouterClient(sourceNetworkDetails.routerAddress); |
| 37 | + |
| 38 | + sepoliaSender = new SepoliaSender( |
| 39 | + address(sourceRouter), |
| 40 | + address(sourceNetworkDetails.linkAddress) |
| 41 | + ); |
| 42 | + |
| 43 | + vm.selectFork(destinationFork); |
| 44 | + Register.NetworkDetails |
| 45 | + memory destinationNetworkDetails = ccipLocalSimulatorFork |
| 46 | + .getNetworkDetails(block.chainid); |
| 47 | + destinationChainSelector = destinationNetworkDetails.chainSelector; |
| 48 | + destinationRouter = IRouterClient( |
| 49 | + destinationNetworkDetails.routerAddress |
| 50 | + ); |
| 51 | + |
| 52 | + amoyReceiver = new AmoyReceiver(address(destinationRouter)); |
| 53 | + } |
| 54 | + |
| 55 | + function testMessageTransferWithLinkTokenPaidFork() public { |
| 56 | + vm.selectFork(sourceFork); |
| 57 | + |
| 58 | + ccipLocalSimulatorFork.requestLinkFromFaucet( |
| 59 | + address(sepoliaSender), |
| 60 | + 5 ether |
| 61 | + ); |
| 62 | + |
| 63 | + bytes memory signedMessage = abi.encodePacked("Hello, Amoy!"); |
| 64 | + |
| 65 | + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 66 | + // Something wired here, the destinationChainSelector should be set in the setUp function and indeed set, but here it's still 0. // |
| 67 | + |
| 68 | + vm.selectFork(destinationFork); |
| 69 | + Register.NetworkDetails |
| 70 | + memory destinationNetworkDetails = ccipLocalSimulatorFork |
| 71 | + .getNetworkDetails(block.chainid); |
| 72 | + destinationChainSelector = destinationNetworkDetails.chainSelector; |
| 73 | + |
| 74 | + vm.selectFork(sourceFork); |
| 75 | + |
| 76 | + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 77 | + |
| 78 | + bytes32 messageId = sepoliaSender.sendMessage( |
| 79 | + destinationChainSelector, |
| 80 | + address(amoyReceiver), |
| 81 | + signedMessage |
| 82 | + ); |
| 83 | + |
| 84 | + ccipLocalSimulatorFork.switchChainAndRouteMessage(destinationFork); |
| 85 | + |
| 86 | + vm.selectFork(destinationFork); |
| 87 | + bytes memory receivedMessage = amoyReceiver.getSignedMessage(); |
| 88 | + bytes32 lastMessageId = amoyReceiver.getMessageId(); |
| 89 | + |
| 90 | + string memory expectedMessage = "Hello, Amoy!"; |
| 91 | + string memory actualMessage = abi.decode(receivedMessage, (string)); |
| 92 | + |
| 93 | + assertEq(messageId, lastMessageId); |
| 94 | + assertEq(expectedMessage, actualMessage); |
| 95 | + } |
| 96 | +} |
0 commit comments