From c1309c7484fafda5d3adeb055daec3dfd95ca03f Mon Sep 17 00:00:00 2001 From: true-eye Date: Thu, 11 Feb 2021 06:49:38 +1100 Subject: [PATCH] Convert WETH to ETH --- contracts/connectors/matcha.sol | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/contracts/connectors/matcha.sol b/contracts/connectors/matcha.sol index 081c39f..e12a061 100644 --- a/contracts/connectors/matcha.sol +++ b/contracts/connectors/matcha.sol @@ -42,6 +42,17 @@ contract MatchaHelpers is Stores, DSMath { ) internal { if (isEth) token.deposit.value(amount)(); } + + function convertWethToEth( + bool isEth, + TokenInterface token, + uint256 amount + ) internal { + if (isEth) { + token.approve(address(token), amount); + token.withdraw(amount); + } + } } contract MatchaResolver is MatchaHelpers { @@ -89,17 +100,24 @@ contract MatchaEventResolver is MatchaResolver { contract MatchaResolverHelpers is MatchaEventResolver { function _swap(MatchaData memory matchaData, uint256 setId) internal { TokenInterface _sellAddr = matchaData.sellToken; + TokenInterface _buyAddr = matchaData.buyToken; uint256 ethAmt; - bool isEth = address(_sellAddr) == getEthAddr(); - if (isEth) { + TokenInterface tokenContract = TokenInterface(getWETHAddress()); + + if (address(_sellAddr) == getEthAddr()) { ethAmt = matchaData._sellAmt; - convertEthToWeth(isEth, TokenInterface(getWETHAddress()), ethAmt); + convertEthToWeth(true, tokenContract, ethAmt); } else { TokenInterface(_sellAddr).approve(getMatchaAddress(), matchaData._sellAmt); } matchaData._buyAmt = matchaSwap(matchaData, ethAmt); + + if (address(_buyAddr) == getEthAddr()) { + convertWethToEth(true, tokenContract, matchaData._buyAmt); + } + setUint(setId, matchaData._buyAmt); emitLogSwap(matchaData, setId);