Skip to content

Commit

Permalink
test(SpokeVault): add transferExcessM fork test
Browse files Browse the repository at this point in the history
  • Loading branch information
PierrickGT committed Nov 21, 2024
1 parent 841aef3 commit c9dbe6a
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 5 deletions.
9 changes: 9 additions & 0 deletions script/cast/CastBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { IManagerBase } from "../../lib/example-native-token-transfers/evm/src/i
import { INttManager } from "../../lib/example-native-token-transfers/evm/src/interfaces/INttManager.sol";

import { IHubPortal } from "../../src/interfaces/IHubPortal.sol";
import { ISpokeVault } from "../../src/interfaces/ISpokeVault.sol";

import { Utils } from "../helpers/Utils.sol";

Expand Down Expand Up @@ -73,4 +74,12 @@ contract CastBase is Script, Utils {
new bytes(1)
);
}

function _transferExcessM(
address spokeVault_,
bytes32 refundAddress_,
uint256 value_
) internal returns (uint64 messageSequence_) {
return ISpokeVault(spokeVault_).transferExcessM{ value: value_ }(refundAddress_);
}
}
4 changes: 2 additions & 2 deletions script/helpers/Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

pragma solidity 0.8.26;

import { console2 } from "../../lib/forge-std/src/Script.sol";
import { console } from "../../lib/forge-std/src/console.sol";

import {
ERC1967Proxy
Expand Down Expand Up @@ -131,7 +131,7 @@ contract Utils {
} else if (chainId_ == _OPTIMISM_SEPOLIA_CHAIN_ID) {
return _OPTIMISM_SEPOLIA_WORMHOLE_CHAIN_ID;
} else {
console2.log("Chain id: {}", chainId_);
console.log("Chain id: {}", chainId_);
revert("Unsupported chain id.");
}
}
Expand Down
4 changes: 1 addition & 3 deletions test/fork/SpokePortalFork.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ contract SpokePortalForkTests is ForkTestBase {

assertEq(IERC20(_baseSpokeMToken).balanceOf(_mHolder), _amount);
assertEq(IContinuousIndexing(_baseSpokeMToken).currentIndex(), _mainnetIndex);

// TODO: add excess test once underflow has been fixed
// ISpokePortal(_baseSpokePortal).excess();
assertEq(ISpokePortal(_baseSpokePortal).excess(), 0);
}
}
86 changes: 86 additions & 0 deletions test/fork/SpokeVaultFork.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// SPDX-License-Identifier: UNLICENSED

pragma solidity 0.8.26;

import { IERC20 } from "../../lib/common/src/interfaces/IERC20.sol";

import { ForkTestBase } from "./ForkTestBase.t.sol";

contract SpokeVaultForkTests is ForkTestBase {
uint256 internal _amount;

function setUp() public override {
super.setUp();
_configurePortals();
}

/* ============ transfer ============ */

function testFork_transferExcessM() external {
_beforeTest();

vm.startPrank(_mHolder);

// Then, transfer excess M tokens to the Hub chain.
_transferExcessM(
_baseSpokeVault,
_toUniversalAddress(_mHolder),
_quoteDeliveryPrice(_baseSpokePortal, _MAINNET_WORMHOLE_CHAIN_ID)
);

vm.stopPrank();

assertEq(IERC20(_baseSpokeMToken).balanceOf(_baseSpokeVault), 0);

bytes memory spokeSignedMessage_ = _signMessage(_baseSpokeGuardian, _BASE_WORMHOLE_CHAIN_ID);

vm.selectFork(_mainnetForkId);

uint256 balanceOfBefore_ = IERC20(_MAINNET_M_TOKEN).balanceOf(_MAINNET_VAULT);

_deliverMessage(_MAINNET_WORMHOLE_RELAYER, spokeSignedMessage_);

assertEq(IERC20(_MAINNET_M_TOKEN).balanceOf(_hubPortal), 0);
assertEq(IERC20(_MAINNET_M_TOKEN).balanceOf(_MAINNET_VAULT), balanceOfBefore_ + _amount);
}

function _beforeTest() internal {
_amount = 1_000e6;

vm.selectFork(_mainnetForkId);

vm.startPrank(_mHolder);

// First, transfer M tokens to the Spoke chain
IERC20(_MAINNET_M_TOKEN).approve(_hubPortal, _amount);

vm.recordLogs();

_transfer(
_hubPortal,
_BASE_WORMHOLE_CHAIN_ID,
_amount,
_toUniversalAddress(_mHolder),
_toUniversalAddress(_mHolder),
_quoteDeliveryPrice(_hubPortal, _BASE_WORMHOLE_CHAIN_ID)
);

vm.stopPrank();

bytes memory hubSignedMessage_ = _signMessage(_hubGuardian, _MAINNET_WORMHOLE_CHAIN_ID);

vm.selectFork(_baseForkId);
_deliverMessage(_BASE_WORMHOLE_RELAYER, hubSignedMessage_);

assertEq(IERC20(_baseSpokeMToken).balanceOf(_baseSpokeVault), 0);

_amount = IERC20(_baseSpokeMToken).balanceOf(_mHolder);

vm.prank(_mHolder);

// Then, transfer M tokens to the SpokeVault to simulate accrual of excess M
IERC20(_baseSpokeMToken).transfer(_baseSpokeVault, _amount);

assertEq(IERC20(_baseSpokeMToken).balanceOf(_baseSpokeVault), _amount);
}
}

0 comments on commit c9dbe6a

Please sign in to comment.