Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix v6.0.1 #310

Merged
merged 13 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions contracts/GenericSwap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,8 @@ contract GenericSwap is IGenericSwap, TokenCollector, EIP712 {
/// @return returnAmount Output amount of the swap
function executeSwap(GenericSwapData calldata swapData, bytes calldata takerTokenPermit) external payable override returns (uint256 returnAmount) {
returnAmount = _executeSwap(swapData, msg.sender, takerTokenPermit);
emit Swap(
getGSDataHash(swapData),
swapData.maker,
msg.sender, // taker
swapData.recipient,
swapData.takerToken,
swapData.takerTokenAmount,
swapData.makerToken,
returnAmount
);

_emitGSExecuted(getGSDataHash(swapData), swapData, msg.sender, returnAmount);
}

/// @param swapData Swap data
Expand All @@ -51,7 +43,8 @@ contract GenericSwap is IGenericSwap, TokenCollector, EIP712 {
if (!SignatureValidator.validateSignature(taker, gs712Hash, takerSig)) revert InvalidSignature();

returnAmount = _executeSwap(swapData, taker, takerTokenPermit);
emit Swap(swapHash, swapData.maker, taker, swapData.recipient, swapData.takerToken, swapData.takerTokenAmount, swapData.makerToken, returnAmount);

_emitGSExecuted(swapHash, swapData, taker, returnAmount);
}

function _executeSwap(
Expand Down Expand Up @@ -84,4 +77,18 @@ contract GenericSwap is IGenericSwap, TokenCollector, EIP712 {

_outputToken.transferTo(_swapData.recipient, returnAmount);
}

function _emitGSExecuted(bytes32 _gsOfferHash, GenericSwapData calldata _swapData, address _taker, uint256 returnAmount) internal {
emit Swap(
_gsOfferHash,
_swapData.maker,
_taker,
_swapData.recipient,
_swapData.takerToken,
_swapData.takerTokenAmount,
_swapData.makerToken,
returnAmount,
_swapData.salt
);
}
}
25 changes: 14 additions & 11 deletions contracts/RFQ.sol
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ contract RFQ is IRFQ, Ownable, TokenCollector, EIP712 {
// transfer takerToken to maker
if (_rfqOffer.takerToken.isETH()) {
if (msg.value != _rfqTx.takerRequestAmount) revert InvalidMsgValue();
_collecETHAndSend(_rfqOffer.maker, _rfqTx.takerRequestAmount, ((_rfqOffer.flags & FLG_MAKER_RECEIVES_WETH) != 0));
_collectETHAndSend(_rfqOffer.maker, _rfqTx.takerRequestAmount, ((_rfqOffer.flags & FLG_MAKER_RECEIVES_WETH) != 0));
} else if (_rfqOffer.takerToken == address(weth)) {
if (msg.value != 0) revert InvalidMsgValue();
_collecWETHAndSend(
_collectWETHAndSend(
_rfqOffer.taker,
_rfqOffer.maker,
_rfqTx.takerRequestAmount,
Expand All @@ -133,13 +133,19 @@ contract RFQ is IRFQ, Ownable, TokenCollector, EIP712 {
_collect(_rfqOffer.takerToken, _rfqOffer.taker, _rfqOffer.maker, _rfqTx.takerRequestAmount, _takerTokenPermit);
}

// collect makerToken from maker to this
// collect makerToken from maker to this contract
uint256 makerSettleAmount = _rfqOffer.makerTokenAmount;
if (_rfqTx.takerRequestAmount != _rfqOffer.takerTokenAmount) {
makerSettleAmount = (_rfqTx.takerRequestAmount * _rfqOffer.makerTokenAmount) / _rfqOffer.takerTokenAmount;
}
if (makerSettleAmount == 0) revert InvalidMakerAmount();
_collect(_rfqOffer.makerToken, _rfqOffer.maker, address(this), makerSettleAmount, _makerTokenPermit);
// if the makerToken is ETH, we collect WETH from the maker to this contract
// if the makerToken is a ERC20 token (including WETH) , we collect that ERC20 token from maker to this contract
if (_rfqOffer.makerToken.isETH()) {
alex0207s marked this conversation as resolved.
Show resolved Hide resolved
_collect(address(weth), _rfqOffer.maker, address(this), makerSettleAmount, _makerTokenPermit);
} else {
_collect(_rfqOffer.makerToken, _rfqOffer.maker, address(this), makerSettleAmount, _makerTokenPermit);
}

// calculate maker token settlement amount (sub fee)
uint256 fee = (makerSettleAmount * _rfqOffer.feeFactor) / Constant.BPS_MAX;
Expand All @@ -150,12 +156,9 @@ contract RFQ is IRFQ, Ownable, TokenCollector, EIP712 {
}

{
// determine if WETH unwrap is needed, send out ETH if makerToken is WETH
// unwrap WETH and send out ETH if makerToken is ETH
address makerToken = _rfqOffer.makerToken;
if (makerToken == address(weth)) {
weth.withdraw(makerSettleAmount);
makerToken = Constant.ETH_ADDRESS;
}
if (makerToken.isETH()) weth.withdraw(makerSettleAmount);

// collect fee
makerToken.transferTo(feeCollector, fee);
Expand All @@ -181,7 +184,7 @@ contract RFQ is IRFQ, Ownable, TokenCollector, EIP712 {
}

// Only used when taker token is ETH
function _collecETHAndSend(address payable to, uint256 amount, bool makerReceivesWETH) internal {
function _collectETHAndSend(address payable to, uint256 amount, bool makerReceivesWETH) internal {
if (makerReceivesWETH) {
weth.deposit{ value: amount }();
weth.transfer(to, amount);
Expand All @@ -191,7 +194,7 @@ contract RFQ is IRFQ, Ownable, TokenCollector, EIP712 {
}

// Only used when taker token is WETH
function _collecWETHAndSend(address from, address payable to, uint256 amount, bytes calldata data, bool makerReceivesWETH) internal {
function _collectWETHAndSend(address from, address payable to, uint256 amount, bytes calldata data, bool makerReceivesWETH) internal {
if (makerReceivesWETH) {
_collect(address(weth), from, to, amount, data);
} else {
Expand Down
3 changes: 2 additions & 1 deletion contracts/interfaces/IGenericSwap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ interface IGenericSwap {
address inputToken,
uint256 inputAmount,
address outputToken,
uint256 outputAmount
uint256 outputAmount,
uint256 salt
);

function executeSwap(GenericSwapData calldata swapData, bytes calldata takerTokenPermit) external payable returns (uint256 returnAmount);
Expand Down
51 changes: 43 additions & 8 deletions test/forkMainnet/GenericSwap.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ contract GenericSwapTest is Test, Tokens, BalanceUtil, Permit2Helper, SigHelper
address inputToken,
uint256 inputAmount,
address outputToken,
uint256 outputAmount
uint256 outputAmount,
uint256 salt
);

address strategyAdmin = makeAddr("strategyAdmin");
Expand Down Expand Up @@ -140,7 +141,8 @@ contract GenericSwapTest is Test, Tokens, BalanceUtil, Permit2Helper, SigHelper
defaultGSData.takerToken,
defaultGSData.takerTokenAmount,
defaultGSData.makerToken,
defaultGSData.makerTokenAmount
defaultGSData.makerTokenAmount,
defaultGSData.salt
);

vm.prank(taker);
Expand Down Expand Up @@ -168,7 +170,17 @@ contract GenericSwapTest is Test, Tokens, BalanceUtil, Permit2Helper, SigHelper
// 800 < 900 < 1000
mockStrategy.setOutputAmountAndRecipient(actualOutput, payable(address(genericSwap)));
vm.expectEmit(true, true, true, true);
emit Swap(getGSDataHash(gsData), gsData.maker, taker, taker, gsData.takerToken, gsData.takerTokenAmount, gsData.makerToken, realChangedInGS);
emit Swap(
getGSDataHash(gsData),
gsData.maker,
taker,
taker,
gsData.takerToken,
gsData.takerTokenAmount,
gsData.makerToken,
realChangedInGS,
gsData.salt
);
vm.prank(taker);
genericSwap.executeSwap(gsData, defaultTakerPermit);

Expand All @@ -193,7 +205,17 @@ contract GenericSwapTest is Test, Tokens, BalanceUtil, Permit2Helper, SigHelper

mockStrategy.setOutputAmountAndRecipient(gsData.makerTokenAmount, payable(address(genericSwap)));
vm.expectEmit(true, true, true, true);
emit Swap(getGSDataHash(gsData), gsData.maker, taker, taker, gsData.takerToken, gsData.takerTokenAmount, gsData.makerToken, realChangedInGS);
emit Swap(
getGSDataHash(gsData),
gsData.maker,
taker,
taker,
gsData.takerToken,
gsData.takerTokenAmount,
gsData.makerToken,
realChangedInGS,
gsData.salt
);
vm.prank(taker);
genericSwap.executeSwap{ value: gsData.takerTokenAmount }(gsData, defaultTakerPermit);

Expand All @@ -219,7 +241,17 @@ contract GenericSwapTest is Test, Tokens, BalanceUtil, Permit2Helper, SigHelper

mockStrategy.setOutputAmountAndRecipient(gsData.makerTokenAmount, payable(address(genericSwap)));
vm.expectEmit(true, true, true, true);
emit Swap(getGSDataHash(gsData), gsData.maker, taker, taker, gsData.takerToken, gsData.takerTokenAmount, gsData.makerToken, realChangedInGS);
emit Swap(
getGSDataHash(gsData),
gsData.maker,
taker,
taker,
gsData.takerToken,
gsData.takerTokenAmount,
gsData.makerToken,
realChangedInGS,
gsData.salt
);
vm.prank(taker);
genericSwap.executeSwap(gsData, defaultTakerPermit);

Expand Down Expand Up @@ -291,7 +323,8 @@ contract GenericSwapTest is Test, Tokens, BalanceUtil, Permit2Helper, SigHelper
defaultGSData.takerToken,
defaultGSData.takerTokenAmount,
defaultGSData.makerToken,
defaultGSData.makerTokenAmount
defaultGSData.makerTokenAmount,
defaultGSData.salt
);

bytes memory takerSig = signGenericSwap(takerPrivateKey, defaultGSData, address(genericSwap));
Expand Down Expand Up @@ -339,7 +372,8 @@ contract GenericSwapTest is Test, Tokens, BalanceUtil, Permit2Helper, SigHelper
defaultGSData.takerToken,
defaultGSData.takerTokenAmount,
defaultGSData.makerToken,
defaultGSData.makerTokenAmount
defaultGSData.makerTokenAmount,
defaultGSData.salt
);

vm.prank(taker);
Expand Down Expand Up @@ -371,7 +405,8 @@ contract GenericSwapTest is Test, Tokens, BalanceUtil, Permit2Helper, SigHelper
aliceGSData.takerToken,
aliceGSData.takerTokenAmount,
aliceGSData.makerToken,
aliceGSData.makerTokenAmount
aliceGSData.makerTokenAmount,
aliceGSData.salt
);

vm.startPrank(alice);
Expand Down
4 changes: 2 additions & 2 deletions test/forkMainnet/RFQ.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ contract RFQTest is Test, Tokens, BalanceUtil, Permit2Helper, SigHelper {
Snapshot memory makerMakerToken = BalanceSnapshot.take({ owner: rfqOffer.maker, token: rfqOffer.makerToken });
// recipient should receive raw ETH
Snapshot memory recTakerToken = BalanceSnapshot.take({ owner: recipient, token: rfqOffer.takerToken });
Snapshot memory recMakerToken = BalanceSnapshot.take({ owner: recipient, token: Constant.ZERO_ADDRESS });
Snapshot memory fcMakerToken = BalanceSnapshot.take({ owner: feeCollector, token: Constant.ZERO_ADDRESS });
Snapshot memory recMakerToken = BalanceSnapshot.take({ owner: recipient, token: rfqOffer.makerToken });
Snapshot memory fcMakerToken = BalanceSnapshot.take({ owner: feeCollector, token: rfqOffer.makerToken });

uint256 fee = (rfqOffer.makerTokenAmount * defaultFeeFactor) / Constant.BPS_MAX;
uint256 amountAfterFee = rfqOffer.makerTokenAmount - fee;
Expand Down
Loading