Skip to content

Commit

Permalink
fix(contracts-rfq): add bridge/relay revert tests [SLT-322] (#3251)
Browse files Browse the repository at this point in the history
* test: add more revert cases for origin user interactions

* test: revert cases for relayer DST interactions

* fix: check msg.value when doing a token transfer
  • Loading branch information
ChiTimesChi authored Oct 9, 2024
1 parent 98a5f01 commit 6dd94ca
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/contracts-rfq/contracts/FastBridgeV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,11 @@ contract FastBridgeV2 is Admin, IFastBridgeV2, IFastBridgeV2Errors {
function _pullToken(address recipient, address token, uint256 amount) internal returns (uint256 amountPulled) {
if (token != UniversalTokenLib.ETH_ADDRESS) {
token.assertIsContract();
// Record token balance before transfer
amountPulled = IERC20(token).balanceOf(recipient);
// Token needs to be pulled only if msg.value is zero
// This way user can specify WETH as the origin asset
if (msg.value != 0) revert MsgValueIncorrect();
// Record token balance before transfer
amountPulled = IERC20(token).balanceOf(recipient);
IERC20(token).safeTransferFrom(msg.sender, recipient, amount);
// Use the difference between the recorded balance and the current balance as the amountPulled
amountPulled = IERC20(token).balanceOf(recipient) - amountPulled;
Expand Down
34 changes: 34 additions & 0 deletions packages/contracts-rfq/test/FastBridgeV2.Dst.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,38 @@ contract FastBridgeV2DstTest is FastBridgeV2DstBaseTest {
vm.expectRevert(ZeroAddress.selector);
relayWithAddress({caller: relayerA, relayer: address(0), msgValue: 0, bridgeTx: tokenTx});
}

function test_relay_token_revert_approvedZero() public {
vm.prank(relayerA);
dstToken.approve(address(fastBridge), 0);
vm.expectRevert();
relay({caller: relayerA, msgValue: 0, bridgeTx: tokenTx});
}

function test_relay_token_revert_approvedNotEnough() public {
vm.prank(relayerA);
dstToken.approve(address(fastBridge), tokenParams.destAmount - 1);
vm.expectRevert();
relay({caller: relayerA, msgValue: 0, bridgeTx: tokenTx});
}

function test_relay_token_revert_nonZeroMsgValue() public {
vm.expectRevert();
relay({caller: relayerA, msgValue: tokenParams.destAmount, bridgeTx: tokenTx});
}

function test_relay_eth_revert_lowerMsgValue() public {
vm.expectRevert();
relay({caller: relayerA, msgValue: ethParams.destAmount - 1, bridgeTx: ethTx});
}

function test_relay_eth_revert_higherMsgValue() public {
vm.expectRevert();
relay({caller: relayerA, msgValue: ethParams.destAmount + 1, bridgeTx: ethTx});
}

function test_relay_eth_revert_zeroMsgValue() public {
vm.expectRevert();
relay({caller: relayerA, msgValue: 0, bridgeTx: ethTx});
}
}
19 changes: 19 additions & 0 deletions packages/contracts-rfq/test/FastBridgeV2.Src.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,25 @@ contract FastBridgeV2SrcTest is FastBridgeV2SrcBaseTest {
checkEthBalancesAfterBridge(userB);
}

function test_bridge_token_revert_approvedZero() public {
vm.prank(userA);
srcToken.approve(address(fastBridge), 0);
vm.expectRevert();
bridge({caller: userA, msgValue: 0, params: tokenParams});
}

function test_bridge_token_revert_approvedNotEnough() public {
vm.prank(userA);
srcToken.approve(address(fastBridge), tokenParams.originAmount - 1);
vm.expectRevert();
bridge({caller: userA, msgValue: 0, params: tokenParams});
}

function test_bridge_token_revert_nonZeroMsgValue() public {
vm.expectRevert();
bridge({caller: userA, msgValue: tokenParams.originAmount, params: tokenParams});
}

function test_bridge_eth_revert_lowerMsgValue() public {
vm.expectRevert(MsgValueIncorrect.selector);
bridge({caller: userA, msgValue: ethParams.originAmount - 1, params: ethParams});
Expand Down

0 comments on commit 6dd94ca

Please sign in to comment.