-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Casweeney/token gateway swap test (#153)
Co-authored-by: Seun Lanlege <[email protected]>
- Loading branch information
1 parent
639e4fd
commit 5d053bc
Showing
4 changed files
with
177 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity 0.8.17; | ||
|
||
import "forge-std/Test.sol"; | ||
import {TestConsensusClient} from "./TestConsensusClient.sol"; | ||
import {TestHost} from "./TestHost.sol"; | ||
import {PingModule} from "../examples/PingModule.sol"; | ||
import {HandlerV1} from "../src/modules/HandlerV1.sol"; | ||
import {CallDispatcher} from "../src/modules/CallDispatcher.sol"; | ||
import {FeeToken} from "./FeeToken.sol"; | ||
import {HostParams} from "../src/hosts/EvmHost.sol"; | ||
import {HostManagerParams, HostManager} from "../src/modules/HostManager.sol"; | ||
import {TokenGateway, Asset, InitParams} from "../src/modules/TokenGateway.sol"; | ||
import {ERC6160Ext20} from "ERC6160/tokens/ERC6160Ext20.sol"; | ||
import {StateMachine} from "ismp/StateMachine.sol"; | ||
import {IERC20} from "openzeppelin/token/ERC20/IERC20.sol"; | ||
import {IUniswapV2Router} from "../src/interfaces/IUniswapV2Router.sol"; | ||
|
||
contract MainnetForkBaseTest is Test { | ||
/// @notice The Id of Role required to mint token | ||
bytes32 public constant MINTER_ROLE = keccak256("MINTER ROLE"); | ||
|
||
/// @notice The Id of Role required to burn token | ||
bytes32 public constant BURNER_ROLE = keccak256("BURNER ROLE"); | ||
|
||
// needs a test method so that integration-tests can detect it | ||
function testPostTimeout() public {} | ||
|
||
TestConsensusClient internal consensusClient; | ||
TestHost internal host; | ||
HandlerV1 internal handler; | ||
PingModule internal testModule; | ||
TokenGateway internal gateway; | ||
IERC20 internal usdc; | ||
IERC20 internal dai; | ||
IERC20 internal feeToken; | ||
IUniswapV2Router internal _uniswapV2Router; | ||
|
||
uint256 internal mainnetFork; | ||
|
||
function setUp() public virtual { | ||
usdc = IERC20(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48); | ||
dai = IERC20(0x6B175474E89094C44Da98b954EedeAC495271d0F); | ||
feeToken = dai; | ||
_uniswapV2Router = IUniswapV2Router(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D); | ||
|
||
|
||
string memory fork_url = vm.envString("MAINNET_FORK_URL"); | ||
|
||
// mainnet fork creation | ||
mainnetFork = vm.createFork(fork_url); | ||
|
||
// mainnet fork selection | ||
vm.selectFork(mainnetFork); | ||
|
||
consensusClient = new TestConsensusClient(); | ||
handler = new HandlerV1(); | ||
CallDispatcher dispatcher = new CallDispatcher(); | ||
|
||
HostManagerParams memory gParams = HostManagerParams({admin: address(this), host: address(0)}); | ||
HostManager manager = new HostManager(gParams); | ||
|
||
HostParams memory params = HostParams({ | ||
admin: address(0), | ||
hostManager: address(manager), | ||
handler: address(handler), | ||
defaultTimeout: 0, | ||
unStakingPeriod: 5000, | ||
// for this test | ||
challengePeriod: 0, | ||
consensusClient: address(consensusClient), | ||
lastUpdated: 0, | ||
consensusState: new bytes(0), | ||
baseGetRequestFee: 10000000000000000000, | ||
perByteFee: 1000000000000000000, // 1FTK | ||
feeTokenAddress: address(feeToken), | ||
latestStateMachineHeight: 0, | ||
hyperbridge: StateMachine.kusama(2000) | ||
}); | ||
|
||
host = new TestHost(params); | ||
|
||
testModule = new PingModule(address(this)); | ||
testModule.setIsmpHost(address(host)); | ||
manager.setIsmpHost(address(host)); | ||
gateway = new TokenGateway(address(this)); | ||
Asset[] memory assets = new Asset[](1); | ||
assets[0] = Asset({identifier: keccak256("USD.h"), erc20: address(feeToken), erc6160: address(0)}); | ||
|
||
gateway.init( | ||
InitParams({ | ||
hyperbridge: StateMachine.kusama(2000), | ||
host: address(host), | ||
uniswapV2Router: 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D, | ||
protocolFeePercentage: 100, // 0.1 | ||
relayerFeePercentage: 300, // 0.3 | ||
assets: assets, | ||
callDispatcher: address(dispatcher) | ||
}) | ||
); | ||
} | ||
|
||
function module() public view returns (address) { | ||
return address(testModule); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity 0.8.17; | ||
|
||
import "forge-std/Test.sol"; | ||
import "forge-std/console.sol"; | ||
|
||
import {MainnetForkBaseTest} from "./MainnetForkBaseTest.sol"; | ||
import {GetResponseMessage, GetTimeoutMessage, GetRequest, PostRequest, Message} from "ismp/Message.sol"; | ||
import {TeleportParams, Body, BODY_BYTES_SIZE} from "../src/modules/TokenGateway.sol"; | ||
import {StateMachine} from "ismp/StateMachine.sol"; | ||
|
||
contract TeleportSwapTest is MainnetForkBaseTest { | ||
// Maximum slippage of 0.5% | ||
uint256 maxSlippagePercentage = 50; // 0.5 * 100 | ||
|
||
function testCanTeleportAssetsUsingUsdcForFee() public { | ||
address mainnetUsdcHolder = address(0xf584F8728B874a6a5c7A8d4d387C9aae9172D621); | ||
|
||
// relayer fee + per-byte fee | ||
uint256 messagingFee = (9 * 1e17) + (BODY_BYTES_SIZE * host.perByteFee()); | ||
|
||
address[] memory path = new address[](2); | ||
path[0] = address(usdc); | ||
path[1] = address(feeToken); | ||
|
||
uint256 _fromTokenAmountIn = _uniswapV2Router.getAmountsIn(messagingFee, path)[0]; | ||
|
||
// Handling Slippage Implementation | ||
uint _slippageAmount = (_fromTokenAmountIn * maxSlippagePercentage) / 10000; // Adjusted for percentage times 100 | ||
uint _amountInMax = _fromTokenAmountIn + _slippageAmount; | ||
|
||
// mainnet forking - impersonation | ||
vm.startPrank(mainnetUsdcHolder); | ||
|
||
dai.approve(address(gateway), 10000 * 1e18); | ||
dai.approve(address(host), messagingFee); | ||
usdc.approve(address(gateway), 10000 * 1e18); | ||
|
||
gateway.teleport( | ||
TeleportParams({ | ||
feeToken: address(usdc), | ||
amount: 1000 * 1e18, // $1000 | ||
redeem: false, | ||
dest: StateMachine.bsc(), | ||
fee: 9 * 1e17, // $0.9 | ||
timeout: 0, | ||
to: address(this), | ||
assetId: keccak256("USD.h"), | ||
data: new bytes(0), | ||
amountInMax: _amountInMax | ||
}) | ||
); | ||
|
||
assert(feeToken.balanceOf(address(this)) == 0); | ||
assert(feeToken.balanceOf(address(host)) == messagingFee); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters