|
| 1 | +// SPDX-License-Identifier: BUSL-1.1 |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import { Test } from "forge-std/Test.sol"; |
| 5 | +import { ERC1967Proxy } from "@openzeppelin/contracts-v4/proxy/ERC1967/ERC1967Proxy.sol"; |
| 6 | +import { MockSpokePool } from "../../../../../contracts/test/MockSpokePool.sol"; |
| 7 | +import { ExpandedERC20WithBlacklist } from "../../../../../contracts/test/ExpandedERC20WithBlacklist.sol"; |
| 8 | +import { WETH9 } from "../../../../../contracts/external/WETH9.sol"; |
| 9 | +import { AddressToBytes32 } from "../../../../../contracts/libraries/AddressConverters.sol"; |
| 10 | + |
| 11 | +contract SpokePoolClaimRelayerRefundTest is Test { |
| 12 | + using AddressToBytes32 for address; |
| 13 | + |
| 14 | + MockSpokePool public spokePool; |
| 15 | + ExpandedERC20WithBlacklist public destErc20; |
| 16 | + WETH9 public weth; |
| 17 | + |
| 18 | + address public owner; |
| 19 | + address public relayer; |
| 20 | + address public rando; |
| 21 | + |
| 22 | + uint256 public constant AMOUNT_TO_RELAY = 25e18; |
| 23 | + uint256 public constant AMOUNT_HELD_BY_POOL = AMOUNT_TO_RELAY * 4; |
| 24 | + uint256 public constant AMOUNT_TO_RETURN = 1e18; |
| 25 | + uint256 public constant DESTINATION_CHAIN_ID = 1342; |
| 26 | + |
| 27 | + function setUp() public { |
| 28 | + owner = makeAddr("owner"); |
| 29 | + relayer = makeAddr("relayer"); |
| 30 | + rando = makeAddr("rando"); |
| 31 | + |
| 32 | + weth = new WETH9(); |
| 33 | + |
| 34 | + // Deploy destErc20 with blacklist functionality |
| 35 | + destErc20 = new ExpandedERC20WithBlacklist("L2 USD Coin", "L2 USDC", 18); |
| 36 | + // Add this test contract as minter (Minter role = 1) |
| 37 | + destErc20.addMember(1, address(this)); |
| 38 | + |
| 39 | + // Deploy SpokePool via proxy |
| 40 | + vm.startPrank(owner); |
| 41 | + MockSpokePool implementation = new MockSpokePool(address(weth)); |
| 42 | + address proxy = address( |
| 43 | + new ERC1967Proxy(address(implementation), abi.encodeCall(MockSpokePool.initialize, (0, owner, owner))) |
| 44 | + ); |
| 45 | + spokePool = MockSpokePool(payable(proxy)); |
| 46 | + spokePool.setChainId(DESTINATION_CHAIN_ID); |
| 47 | + vm.stopPrank(); |
| 48 | + |
| 49 | + // Seed the SpokePool with tokens |
| 50 | + destErc20.mint(address(spokePool), AMOUNT_HELD_BY_POOL); |
| 51 | + } |
| 52 | + |
| 53 | + function testBlacklistOperatesAsExpected() public { |
| 54 | + // Transfer tokens to relayer before blacklisting works as expected |
| 55 | + destErc20.mint(owner, AMOUNT_TO_RELAY); |
| 56 | + |
| 57 | + vm.prank(owner); |
| 58 | + destErc20.transfer(relayer, AMOUNT_TO_RELAY); |
| 59 | + assertEq(destErc20.balanceOf(relayer), AMOUNT_TO_RELAY); |
| 60 | + |
| 61 | + // Blacklist the relayer |
| 62 | + destErc20.setBlacklistStatus(relayer, true); |
| 63 | + |
| 64 | + // Attempt to transfer tokens to the blacklisted relayer should revert |
| 65 | + destErc20.mint(owner, AMOUNT_TO_RELAY); |
| 66 | + vm.prank(owner); |
| 67 | + vm.expectRevert("Recipient is blacklisted"); |
| 68 | + destErc20.transfer(relayer, AMOUNT_TO_RELAY); |
| 69 | + } |
| 70 | + |
| 71 | + function testDistributeRelayerRefundsHandlesBlacklistedAddresses() public { |
| 72 | + // No starting relayer liability |
| 73 | + assertEq(spokePool.getRelayerRefund(address(destErc20), relayer), 0); |
| 74 | + assertEq(destErc20.balanceOf(rando), 0); |
| 75 | + assertEq(destErc20.balanceOf(relayer), 0); |
| 76 | + |
| 77 | + // Blacklist the relayer |
| 78 | + destErc20.setBlacklistStatus(relayer, true); |
| 79 | + |
| 80 | + // Distribute relayer refunds - some refunds go to blacklisted address, some to non-blacklisted |
| 81 | + uint256[] memory refundAmounts = new uint256[](2); |
| 82 | + refundAmounts[0] = AMOUNT_TO_RELAY; |
| 83 | + refundAmounts[1] = AMOUNT_TO_RELAY; |
| 84 | + |
| 85 | + address[] memory refundAddresses = new address[](2); |
| 86 | + refundAddresses[0] = relayer; |
| 87 | + refundAddresses[1] = rando; |
| 88 | + |
| 89 | + vm.prank(owner); |
| 90 | + spokePool.distributeRelayerRefunds( |
| 91 | + DESTINATION_CHAIN_ID, |
| 92 | + AMOUNT_TO_RETURN, |
| 93 | + refundAmounts, |
| 94 | + 0, |
| 95 | + address(destErc20), |
| 96 | + refundAddresses |
| 97 | + ); |
| 98 | + |
| 99 | + // Blacklisted relayer should have their refund tracked in relayerRefund mapping |
| 100 | + assertEq(spokePool.getRelayerRefund(address(destErc20), relayer), AMOUNT_TO_RELAY); |
| 101 | + // Non-blacklisted address should receive tokens directly |
| 102 | + assertEq(destErc20.balanceOf(rando), AMOUNT_TO_RELAY); |
| 103 | + // Blacklisted relayer should not have received tokens |
| 104 | + assertEq(destErc20.balanceOf(relayer), 0); |
| 105 | + } |
| 106 | + |
| 107 | + function testBlacklistedRelayerCanClaimRefundToNewAddress() public { |
| 108 | + // Blacklist the relayer |
| 109 | + destErc20.setBlacklistStatus(relayer, true); |
| 110 | + |
| 111 | + // Distribute relayer refunds to blacklisted address |
| 112 | + uint256[] memory refundAmounts = new uint256[](1); |
| 113 | + refundAmounts[0] = AMOUNT_TO_RELAY; |
| 114 | + |
| 115 | + address[] memory refundAddresses = new address[](1); |
| 116 | + refundAddresses[0] = relayer; |
| 117 | + |
| 118 | + vm.prank(owner); |
| 119 | + spokePool.distributeRelayerRefunds( |
| 120 | + DESTINATION_CHAIN_ID, |
| 121 | + AMOUNT_TO_RETURN, |
| 122 | + refundAmounts, |
| 123 | + 0, |
| 124 | + address(destErc20), |
| 125 | + refundAddresses |
| 126 | + ); |
| 127 | + |
| 128 | + // Attempting to claim to the blacklisted address should fail |
| 129 | + vm.prank(relayer); |
| 130 | + vm.expectRevert("Recipient is blacklisted"); |
| 131 | + spokePool.claimRelayerRefund(address(destErc20).toBytes32(), relayer.toBytes32()); |
| 132 | + |
| 133 | + // Claiming to a different (non-blacklisted) address should succeed |
| 134 | + assertEq(destErc20.balanceOf(rando), 0); |
| 135 | + |
| 136 | + vm.prank(relayer); |
| 137 | + spokePool.claimRelayerRefund(address(destErc20).toBytes32(), rando.toBytes32()); |
| 138 | + |
| 139 | + assertEq(destErc20.balanceOf(rando), AMOUNT_TO_RELAY); |
| 140 | + } |
| 141 | +} |
0 commit comments