Skip to content

Commit

Permalink
refactor: surplus MATIC test
Browse files Browse the repository at this point in the history
The test was failing because it wasn't sending enough MATIC to OffsetHelper.

I started using `howMuchETHShouldISendToSwap` in the test which fixed it, but I realised the error message from Sushi is not useful.

So I implemented a check with a useful message within the OffsetHelper contract for when someone doesn't send enough ETH.
  • Loading branch information
mauricedesaxe committed May 14, 2022
1 parent 572b348 commit 64dd13d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
13 changes: 12 additions & 1 deletion contracts/OffsetHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@ contract OffsetHelper is OffsetHelperStorage {
path[1] = eligibleTokenAddresses["USDC"];
path[2] = _toToken;

uint256[] memory expectedAmounts = routerSushi.getAmountsIn(
_amount,
path
);

require(
msg.value >= expectedAmounts[0],
"You didn't send enough MATIC."
);

// swap MATIC for tokens
uint256[] memory amounts = routerSushi.swapETHForExactTokens{
value: msg.value
Expand All @@ -225,7 +235,8 @@ contract OffsetHelper is OffsetHelperStorage {
(bool success, ) = msg.sender.call{value: leftoverETH}(
new bytes(0)
);
require(success, "Failed to send dust ETH back to user.");

require(success, "Failed to send surplus ETH back to user.");
}

// update balances
Expand Down
7 changes: 6 additions & 1 deletion test/OffsetHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,12 +651,17 @@ describe("Offset Helper - autoOffset", function () {
offsetHelper.address
);

const maticToSend = await offsetHelper.howMuchETHShouldISendToSwap(
addresses.nct,
parseEther("1.0")
);

await (
await offsetHelper["swap(address,uint256)"](
addresses.nct,
parseEther("1.0"),
{
value: parseEther("5.0"),
value: maticToSend.add(parseEther("0.5")),
}
)
).wait();
Expand Down

0 comments on commit 64dd13d

Please sign in to comment.