From 11135211144a8ccdcb9faf3066936cac41675900 Mon Sep 17 00:00:00 2001 From: ron Date: Wed, 24 Apr 2024 19:56:42 +0800 Subject: [PATCH] Add testAgentExecutionTransactToTransferERC20 --- contracts/test/Gateway.t.sol | 32 +++++++++++++++++++++----- contracts/test/mocks/MockGatewayV2.sol | 2 +- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/contracts/test/Gateway.t.sol b/contracts/test/Gateway.t.sol index a70c44d80f..4976eeee85 100644 --- a/contracts/test/Gateway.t.sol +++ b/contracts/test/Gateway.t.sol @@ -52,6 +52,7 @@ import { import {WETH9} from "canonical-weth/WETH9.sol"; import {UD60x18, ud60x18, convert} from "prb/math/src/UD60x18.sol"; import {HelloWorld} from "./mocks/HelloWorld.sol"; +import {IERC20} from "../src/interfaces/IERC20.sol"; contract GatewayTest is Test { ParaID public bridgeHubParaID = ParaID.wrap(1001); @@ -100,16 +101,12 @@ contract GatewayTest is Test { HelloWorld helloWorld; event SaidHello(string indexed message); + event Transfer(address indexed src, address indexed dst, uint256 wad); function setUp() public { AgentExecutor executor = new AgentExecutor(); gatewayLogic = new MockGateway( - address(0), - address(executor), - bridgeHubParaID, - bridgeHubAgentID, - foreignTokenDecimals, - maxDestinationFee + address(0), address(executor), bridgeHubParaID, bridgeHubAgentID, foreignTokenDecimals, maxDestinationFee ); Gateway.Config memory config = Gateway.Config({ mode: OperatingMode.Normal, @@ -928,4 +925,27 @@ contract GatewayTest is Test { MockGateway(address(gateway)).agentExecutePublic(abi.encode(params)); } + + function testAgentExecutionTransactToTransferERC20() public { + token.transfer(address(assetHubAgent), 200); + + uint256 balanceBefore = IERC20(address(token)).balanceOf(account1); + + uint256 amount = 50; + bytes memory payload = abi.encodeCall(IERC20.transfer, (account1, amount)); + + AgentExecuteParams memory params = AgentExecuteParams({ + agentID: assetHubAgentID, + payload: abi.encode(AgentExecuteCommand.Transact, abi.encode(address(token), payload, 100000)) + }); + + // Expect the WETH9 contract to emit `Transfer` + vm.expectEmit(true, false, false, false); + emit Transfer(address(assetHubAgent), account1, amount); + + MockGateway(address(gateway)).agentExecutePublic(abi.encode(params)); + + uint256 balanceAfter = IERC20(address(token)).balanceOf(account1); + assertEq(balanceBefore + amount, balanceAfter); + } } diff --git a/contracts/test/mocks/MockGatewayV2.sol b/contracts/test/mocks/MockGatewayV2.sol index 31329282e9..f71a2518c9 100644 --- a/contracts/test/mocks/MockGatewayV2.sol +++ b/contracts/test/mocks/MockGatewayV2.sol @@ -19,7 +19,7 @@ library AdditionalStorage { } // Used to test upgrades. -contract MockGatewayV2 is IInitializable { +contract MockGatewayV2 is IInitializable { // Reinitialize gateway with some additional storage fields function initialize(bytes memory params) external { AdditionalStorage.Layout storage $ = AdditionalStorage.layout();