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

[WIP] Implement a conditional swap #304

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

alex0207s
Copy link
Contributor

  • Implement a conditional swap for supporting BestBuy, Repayment and DCA strategies
  • Add some test function for fillConOrder function

Copy link

github-actions bot commented Feb 27, 2024

Changes to gas cost

Generated at commit: 7ddad3f6ff01fcf3abc0359b3798f7345a4556f9, compared to commit: 2b54a4f7639fbe03fcf808def3fea0c96a82da26

🧾 Summary (20% most significant diffs)

Contract Method Avg (+/-) %
AllowanceTarget spendFromUserTo
unpause
-978 ✅
+10 ❌
-1.97%
+0.04%

Full diff report 👇
Contract Deployment Cost (+/-) Method Min (+/-) % Avg (+/-) % Median (+/-) % Max (+/-) % # Calls (+/-)
AllowanceTarget 556,630 (-115,677) pause
spendFromUserTo
unpause
23,543 (+121)
24,859 (-231)
23,433 (+10)
+0.52%
-0.92%
+0.04%
26,260 (+6)
48,572 (-978)
23,433 (+10)
+0.02%
-1.97%
+0.04%
27,619 (-52)
35,287 (-948)
23,433 (+10)
-0.19%
-2.62%
+0.04%
27,619 (-52)
90,110 (-1,570)
23,433 (+10)
-0.19%
-1.71%
+0.04%
3 (0)
7 (0)
1 (0)

Copy link

github-actions bot commented Feb 27, 2024

Changes to gas cost

Generated at commit: 7ddad3f6ff01fcf3abc0359b3798f7345a4556f9, compared to commit: 2b54a4f7639fbe03fcf808def3fea0c96a82da26

🧾 Summary (20% most significant diffs)

Contract Method Avg (+/-) %
CoordinatedTaker EIP712_DOMAIN_SEPARATOR +323 ❌ +103.53%
LimitOrderSwap feeCollector
isOrderCanceled
+279 ❌
+250 ❌
+69.06%
+49.31%
RFQ feeCollector +169 ❌ +39.67%

Full diff report 👇
Contract Deployment Cost (+/-) Method Min (+/-) % Avg (+/-) % Median (+/-) % Max (+/-) % # Calls (+/-)
CoordinatedTaker 2,171,184 (-253,387) EIP712_DOMAIN_SEPARATOR
approveTokens
coordinator
setCoordinator
submitLimitOrderFill
635 (+323)
26,902 (-201)
309 (-86)
23,948 (+205)
32,959 (-161)
+103.53%
-0.74%
-21.77%
+0.86%
-0.49%
635 (+323)
171,637 (-2,082)
309 (-86)
26,105 (+195)
127,657 (-837)
+103.53%
-1.20%
-21.77%
+0.75%
-0.65%
635 (+323)
195,414 (-2,364)
309 (-86)
24,160 (+218)
70,206 (+162)
+103.53%
-1.20%
-21.77%
+0.91%
+0.23%
635 (+323)
195,414 (-2,364)
309 (-86)
30,208 (+162)
254,339 (-2,452)
+103.53%
-1.20%
-21.77%
+0.54%
-0.95%
2 (0)
13 (0)
1 (0)
3 (0)
7 (0)
LimitOrderSwap 2,478,463 (-282,079) cancelOrder
feeCollector
fillLimitOrder
fillLimitOrderFullOrKill
fillLimitOrderGroup
isOrderCanceled
orderHashToMakerTokenFilledAmount
setFeeCollector
29,542 (+285)
683 (+279)
35,994 (-120)
48,460 (+224)
197,763 (-606)
757 (+250)
409 (-64)
23,954 (+161)
+0.97%
+69.06%
-0.33%
+0.46%
-0.31%
+49.31%
-13.53%
+0.68%
41,020 (+350)
683 (+279)
1,425,103 (-1,374)
103,450 (-584)
241,775 (-1,437)
757 (+250)
409 (-64)
26,113 (+149)
+0.86%
+69.06%
-0.10%
-0.56%
-0.59%
+49.31%
-13.53%
+0.57%
34,298 (+442)
683 (+279)
167,541 (-1,205)
48,460 (+224)
244,033 (-1,993)
757 (+250)
409 (-64)
24,166 (+168)
+1.31%
+69.06%
-0.71%
+0.46%
-0.81%
+49.31%
-13.53%
+0.70%
53,111 (+350)
683 (+279)
29,535,125 (-74)
213,430 (-2,200)
290,253 (-1,950)
757 (+250)
409 (-64)
30,220 (+118)
+0.66%
+69.06%
-0.00%
-1.02%
-0.67%
+49.31%
-13.53%
+0.39%
7 (0)
1 (0)
23 (0)
3 (0)
6 (0)
1 (0)
1 (0)
3 (0)
RFQ 2,288,875 (-226,570) cancelRFQOffer
feeCollector
fillRFQ
fillRFQWithSig
setFeeCollector
24,057 (+91)
595 (+169)
31,500 (-318)
33,861 (-20)
23,882 (+111)
+0.38%
+39.67%
-1.00%
-0.06%
+0.47%
37,968 (+87)
595 (+169)
129,595 (-1,185)
112,784 (-621)
26,045 (+103)
+0.23%
+39.67%
-0.91%
-0.55%
+0.40%
38,972 (+99)
595 (+169)
146,767 (-1,192)
77,945 (+322)
24,100 (+124)
+0.25%
+39.67%
-0.81%
+0.41%
+0.52%
49,874 (+60)
595 (+169)
237,435 (-1,329)
226,548 (-2,165)
30,154 (+74)
+0.12%
+39.67%
-0.56%
-0.95%
+0.25%
4 (0)
1 (0)
22 (0)
3 (0)
3 (0)
GenericSwap 1,628,217 (-201,686) executeSwap
executeSwapWithSig
31,972 (-197)
39,540 (+167)
-0.61%
+0.42%
115,998 (-1,473)
167,013 (-1,260)
-1.25%
-0.75%
113,242 (-1,713)
173,729 (-1,260)
-1.49%
-0.72%
249,048 (-2,838)
281,054 (-2,688)
-1.13%
-0.95%
12 (0)
4 (0)
SmartOrderStrategy 1,120,367 (-199,973) approveTokens
executeStrategy
57,862 (-674)
23,225 (-28)
-1.15%
-0.12%
177,429 (-2,198)
125,381 (-1,049)
-1.22%
-0.83%
279,916 (-3,505)
83,952 (-269)
-1.24%
-0.32%
279,916 (-3,505)
608,822 (-1,623)
-1.24%
-0.27%
26 (0)
14 (0)
UniAgent 1,448,615 (-124,542) approveAndSwap
approveTokensToRouters
swap
36,730 (-457)
100,515 (-624)
116,163 (-780)
-1.23%
-0.62%
-0.67%
144,976 (-810)
177,613 (-1,604)
188,617 (-817)
-0.56%
-0.90%
-0.43%
174,801 (-986)
183,121 (-1,674)
190,894 (-912)
-0.56%
-0.91%
-0.48%
223,399 (-986)
183,121 (-1,674)
249,942 (-858)
-0.44%
-0.91%
-0.34%
3 (0)
15 (0)
12 (0)

bytes1 settlementType = settlementData[0];
bytes memory strategyData = settlementData[1:];

uint256 returnedAmount;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move declaration into the settlementType == 0x01 block

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, this should not be changed.

IStrategy(strategy).executeStrategy(order.takerToken, order.makerToken, takerTokenAmount, data);
returnedAmount = order.makerToken.getBalance(address(this)) - makerTokenBalanceBefore;

if (returnedAmount < minMakerTokenAmount) revert InsufficientOutput();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think compare with makerTokenAmount instead of minMakerTokenAmount is more appropriate here.

if (returnedAmount < minMakerTokenAmount) revert InsufficientOutput();
order.makerToken.transferTo(order.recipient, returnedAmount);
} else {
revert(); // specific the error message1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add error here, debug would be easier

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add a new error InvalidSettlementType for this case.

Snapshot memory recMakerToken = BalanceSnapshot.take({ owner: recipient, token: defaultOrder.makerToken });

// craft the `flagAndPeriod` of the defaultOrder for BestBuy case
uint256 flags = 1 << 253;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Define flags constant first and use logic OR here to compose flags to have better semantic meaning.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix

super.setUp();
}

function testBestBuyOrder() external {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comments for different scenario requirement and flags to help reader understanding


_emitConOrderFilled(order, orderHash, takerTokenAmount, returnedAmount);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use returnedAmount in event instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix

order.takerTokenAmount,
order.makerToken,
order.makerTokenAmount,
keccak256(order.takerTokenPermit),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EIP712: The dynamic values bytes and string are encoded as a keccak256 hash of their contents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants