Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
k06a committed Feb 7, 2023
1 parent 50484bc commit 5860a16
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 91 deletions.
27 changes: 7 additions & 20 deletions contracts/OrderRFQMixin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,16 @@ abstract contract OrderRFQMixin is IOrderRFQMixin, EIP712, OnlyWethReceiver {
_invalidateOrder(msg.sender, orderInfo, additionalMask);
}

/**
* @notice See {IOrderRFQMixin-fillRFQ}.
*/
function fillRFQ(
OrderRFQLib.OrderRFQ calldata order,
bytes32 r,
bytes32 vs,
uint256 flagsAndAmount
) external payable returns(uint256 filledMakingAmount, uint256 filledTakingAmount, bytes32 orderHash) {
return fillOrderRFQTo(order, r, vs, flagsAndAmount, msg.data[:0], msg.sender);
}

/**
* @notice See {IOrderRFQMixin-fillOrderRFQ}.
*/
function fillOrderRFQ(
OrderRFQLib.OrderRFQ calldata order,
bytes32 r,
bytes32 vs,
uint256 flagsAndAmount,
bytes calldata interaction
uint256 flagsAndAmount
) external payable returns(uint256 filledMakingAmount, uint256 filledTakingAmount, bytes32 orderHash) {
return fillOrderRFQTo(order, r, vs, flagsAndAmount, interaction, msg.sender);
return fillOrderRFQTo(order, r, vs, flagsAndAmount, msg.sender, msg.data[:0]);
}

/**
Expand All @@ -89,8 +76,8 @@ abstract contract OrderRFQMixin is IOrderRFQMixin, EIP712, OnlyWethReceiver {
bytes32 r,
bytes32 vs,
uint256 flagsAndAmount,
bytes calldata interaction,
address target
address target,
bytes calldata interaction
) public payable returns(uint256 filledMakingAmount, uint256 filledTakingAmount, bytes32 orderHash) {
orderHash = order.hash(_domainSeparatorV4());
address maker = ECDSA.recover(orderHash, r, vs);
Expand All @@ -107,12 +94,12 @@ abstract contract OrderRFQMixin is IOrderRFQMixin, EIP712, OnlyWethReceiver {
bytes32 r,
bytes32 vs,
uint256 flagsAndAmount,
bytes calldata interaction,
address target,
bytes calldata interaction,
bytes calldata permit
) external returns(uint256 /* filledMakingAmount */, uint256 /* filledTakingAmount */, bytes32 /* orderHash */) {
IERC20(order.takerAsset.get()).safePermit(permit);
return fillOrderRFQTo(order, r, vs, flagsAndAmount, interaction, target);
return fillOrderRFQTo(order, r, vs, flagsAndAmount, target, interaction);
}

/**
Expand All @@ -123,8 +110,8 @@ abstract contract OrderRFQMixin is IOrderRFQMixin, EIP712, OnlyWethReceiver {
bytes calldata signature,
Address maker,
uint256 flagsAndAmount,
bytes calldata interaction,
address target,
bytes calldata interaction,
bytes calldata permit
) external returns(uint256 filledMakingAmount, uint256 filledTakingAmount, bytes32 orderHash) {
if (permit.length > 0) {
Expand Down
38 changes: 8 additions & 30 deletions contracts/interfaces/IOrderRFQMixin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,33 +53,11 @@ interface IOrderRFQMixin {
* @return filledTakingAmount Actual amount transferred from taker to maker
* @return orderHash Hash of the filled order
*/
function fillRFQ(
OrderRFQLib.OrderRFQ calldata order,
bytes32 r,
bytes32 vs,
uint256 flagsAndAmount
) external payable returns(uint256 filledMakingAmount, uint256 filledTakingAmount, bytes32 orderHash);

/**
* @notice Fills order's quote, fully or partially (whichever is possible)
* @param order Order quote to fill
* @param r R component of signature
* @param vs VS component of signature
* @param flagsAndAmount Fill configuration flags with amount packed in one slot
* - Bits 0-253 contain the amount to fill
* - Bit 254 is used to indicate whether weth should be unwrapped to eth
* - Bit 255 is used to indicate whether maker (1) or taker amount (0) is given in the amount parameter
* @param interaction A call data for InteractiveNotificationReceiver. Taker may execute interaction after getting maker assets and before sending taker assets.
* @return filledMakingAmount Actual amount transferred from maker to taker
* @return filledTakingAmount Actual amount transferred from taker to maker
* @return orderHash Hash of the filled order
*/
function fillOrderRFQ(
OrderRFQLib.OrderRFQ calldata order,
bytes32 r,
bytes32 vs,
uint256 flagsAndAmount,
bytes calldata interaction
uint256 flagsAndAmount
) external payable returns(uint256 filledMakingAmount, uint256 filledTakingAmount, bytes32 orderHash);

/**
Expand All @@ -88,8 +66,8 @@ interface IOrderRFQMixin {
* @param r R component of signature
* @param vs VS component of signature
* @param flagsAndAmount Fill configuration flags with amount packed in one slot
* @param interaction A call data for InteractiveNotificationReceiver. Taker may execute interaction after getting maker assets and before sending taker assets.
* @param target Address that will receive swap funds
* @param interaction A call data for InteractiveNotificationReceiver. Taker may execute interaction after getting maker assets and before sending taker assets.
* @return filledMakingAmount Actual amount transferred from maker to taker
* @return filledTakingAmount Actual amount transferred from taker to maker
* @return orderHash Hash of the filled order
Expand All @@ -99,8 +77,8 @@ interface IOrderRFQMixin {
bytes32 r,
bytes32 vs,
uint256 flagsAndAmount,
bytes calldata interaction,
address target
address target,
bytes calldata interaction
) external payable returns(uint256 filledMakingAmount, uint256 filledTakingAmount, bytes32 orderHash);

/**
Expand All @@ -111,8 +89,8 @@ interface IOrderRFQMixin {
* @param r R component of signature
* @param vs VS component of signature
* @param flagsAndAmount Fill configuration flags with amount packed in one slot
* @param interaction A call data for InteractiveNotificationReceiver. Taker may execute interaction after getting maker assets and before sending taker assets.
* @param target Address that will receive swap funds
* @param interaction A call data for InteractiveNotificationReceiver. Taker may execute interaction after getting maker assets and before sending taker assets.
* @param permit Should contain abi-encoded calldata for `IERC20Permit.permit` call
* @return filledMakingAmount Actual amount transferred from maker to taker
* @return filledTakingAmount Actual amount transferred from taker to maker
Expand All @@ -124,8 +102,8 @@ interface IOrderRFQMixin {
bytes32 r,
bytes32 vs,
uint256 flagsAndAmount,
bytes calldata interaction,
address target,
bytes calldata interaction,
bytes calldata permit
) external returns(uint256 filledMakingAmount, uint256 filledTakingAmount, bytes32 orderHash);

Expand All @@ -137,8 +115,8 @@ interface IOrderRFQMixin {
* @param signature Signature to confirm quote ownership
* @param maker Smart contract that signed the order
* @param flagsAndAmount Fill configuration flags with amount packed in one slot
* @param interaction A call data for InteractiveNotificationReceiver. Taker may execute interaction after getting maker assets and before sending taker assets.
* @param target Address that will receive swap funds
* @param interaction A call data for InteractiveNotificationReceiver. Taker may execute interaction after getting maker assets and before sending taker assets.
* @param permit Should contain abi-encoded calldata for `IERC20Permit.permit` call
* @return filledMakingAmount Actual amount transferred from maker to taker
* @return filledTakingAmount Actual amount transferred from taker to maker
Expand All @@ -150,8 +128,8 @@ interface IOrderRFQMixin {
bytes calldata signature,
Address maker,
uint256 flagsAndAmount,
bytes calldata interaction,
address target,
bytes calldata interaction,
bytes calldata permit
) external returns(uint256 filledMakingAmount, uint256 filledTakingAmount, bytes32 orderHash);
}
44 changes: 26 additions & 18 deletions contracts/mocks/RecursiveMatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ contract RecursiveMatcher is IInteractionNotificationReceiver {
uint256 flagsAndAmount,
bytes calldata interaction
) external {
orderRFQMixin.fillOrderRFQ(
orderRFQMixin.fillOrderRFQTo(
order,
r,
vs,
flagsAndAmount,
address(this),
interaction
);
}
Expand All @@ -70,6 +71,8 @@ contract RecursiveMatcher is IInteractionNotificationReceiver {
}
} else {
if(interactiveData[0] & _RFQ_FLAG != 0x0) {
// msg.sender.call(abi.encodePacked(IOrderRFQMixin.fillOrderRFQTo.selector, interactiveData[1:]));

(
OrderRFQLib.OrderRFQ memory order,
bytes32 r,
Expand All @@ -78,31 +81,36 @@ contract RecursiveMatcher is IInteractionNotificationReceiver {
bytes memory interaction
) = abi.decode(interactiveData[1:], (OrderRFQLib.OrderRFQ, bytes32, bytes32, uint256, bytes));

IOrderRFQMixin(msg.sender).fillOrderRFQ(
IOrderRFQMixin(msg.sender).fillOrderRFQTo(
order,
r,
vs,
flagsAndAmount,
address(this),
interaction
);
} else {
(
OrderLib.Order memory order,
bytes memory signature,
bytes memory interaction,
uint256 makingOrderAmount,
uint256 takingOrderAmount,
uint256 thresholdAmount
) = abi.decode(interactiveData[1:], (OrderLib.Order, bytes, bytes, uint256, uint256, uint256));
// Not necessary to encode and decode calldata, because it is already encoded
// solhint-disable-next-line avoid-low-level-calls
msg.sender.call(abi.encodePacked(IOrderMixin.fillOrder.selector, interactiveData[1:]));

IOrderMixin(msg.sender).fillOrder(
order,
signature,
interaction,
makingOrderAmount,
takingOrderAmount,
thresholdAmount
);
// (
// OrderLib.Order memory order,
// bytes memory signature,
// bytes memory interaction,
// uint256 makingOrderAmount,
// uint256 takingOrderAmount,
// uint256 thresholdAmount
// ) = abi.decode(interactiveData[1:], (OrderLib.Order, bytes, bytes, uint256, uint256, uint256));

// IOrderMixin(msg.sender).fillOrder(
// order,
// signature,
// interaction,
// makingOrderAmount,
// takingOrderAmount,
// thresholdAmount
// );
}
}
return 0;
Expand Down
4 changes: 2 additions & 2 deletions test/ContractRFQ.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('ContractRFQ', function () {
const order = buildOrderRFQ('1', usdc.address, usdt.address, 1000000000, 1000700000);

const signature = abiCoder.encode([ABIOrderRFQ], [order]);
await swap.fillContractOrderRFQToWithPermit(order, signature, rfq.address, makingAmount(1000000), emptyInteraction, constants.AddressZero, '0x');
await swap.fillContractOrderRFQToWithPermit(order, signature, rfq.address, makingAmount(1000000), constants.AddressZero, emptyInteraction, '0x');

expect(await usdc.balanceOf(rfq.address)).to.equal(makerUsdc.sub(1000000));
expect(await usdc.balanceOf(addr.address)).to.equal(takerUsdc.add(1000000));
Expand All @@ -55,6 +55,6 @@ describe('ContractRFQ', function () {

const order2 = buildOrderRFQ('2', usdc.address, usdt.address, 1000000000, 1000700000);
const signature2 = abiCoder.encode([ABIOrderRFQ], [order2]);
await swap.fillContractOrderRFQToWithPermit(order2, signature2, rfq.address, makingAmount(1000000), emptyInteraction, constants.AddressZero, '0x');
await swap.fillContractOrderRFQToWithPermit(order2, signature2, rfq.address, makingAmount(1000000), constants.AddressZero, emptyInteraction, '0x');
});
});
Loading

0 comments on commit 5860a16

Please sign in to comment.