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

Forked test showcasing the use of the helper interface #92

Merged
merged 8 commits into from
Jul 9, 2024

Conversation

fedgiac
Copy link
Collaborator

@fedgiac fedgiac commented Jul 5, 2024

This PR adds a minimal test showing how the helper contract can be used to generate valid AMM orders.

The helper contract hardcodes the contracts used in the various chains, so I have no choice but using a forked test.

I used the legacy path because there's no real CoW AMM with meaningful funds that isn't legacy.

Note that this test won't work once we migrated funds from the AMM used in the test. This doesn't matter as this test will have lost its utility at that point.

How to test

CI (but note that forked tests with the chosen endpoint are very unreliable, about 1 out of 10 runs have some network error).
Look at the code to determine that we test everything we want from the interface.

@fedgiac fedgiac requested a review from a team July 5, 2024 14:07
@fedgiac fedgiac mentioned this pull request Jul 5, 2024
acanidio-econ pushed a commit that referenced this pull request Jul 5, 2024
Closes #91.

We update the helper to use the math proposed in #91 to compute the buy
amount.
As CoW AMMs are deprecated anyway, only the helper interface matters for
computing order prices.

Note that the changes are untested until #92 is merged.

If there is interest for it, the helper will be redeployed.
.github/workflows/test.yml Outdated Show resolved Hide resolved
.github/workflows/test.yml Outdated Show resolved Hide resolved
Comment on lines 101 to 104
assertGt(ammOrder.sellAmount, ammUsdcInitialBalance * 2 / 100);
assertLt(ammOrder.sellAmount, ammUsdcInitialBalance * 3 / 100);
assertGt(ammOrder.buyAmount, ammWethInitialBalance * 2 / 100);
assertLt(ammOrder.buyAmount, ammWethInitialBalance * 3 / 100);
Copy link
Contributor

Choose a reason for hiding this comment

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

uber nit: could use assertApproxEqRel

Choose a reason for hiding this comment

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

could this test do an exact expectation?

i find 2 paths for this:

  • used forked balances, explain them, explain the math and do an assertEq with a value extracted from the test itself (e.g. 3045.12321321)
  • replace forked balances with mocked ones (e.g. pool initial balance is 30.000:10), calculate the expected outcome, do an assertApproxEqRel with a very tight tolerance

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

uber nit: could use assertApproxEqRel

b836965 ✔️

could this test do an exact expectation?

There has been quite a lot of internal discussion on this so the logic used to be somewhat open when the test was written. But I believe the changes resulting from #94 will make everything easier: it just means that the order should use the lowest possible buy amount for the AMM. Will follow up if I get a satisfactory confirmations (maybe @acanidio-econ can chime in?).

Copy link

Choose a reason for hiding this comment

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

Given a sell amount, the (limit) buy amount should be set to the smallest possible buy amount. Unfortunately, I am not familiar with the code much. If I understand correctly, a constant product pool should (up to rounding) return buy_amount = ammWethInitialBalance * sell_amount / (ammUsdcInitialBalance - sell_amount).

For general pools, it should return whatever the pool would give as out amount to a user when the in amount from the user is fixed, in a setting with zero fees.

Choose a reason for hiding this comment

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

I agree with @fedgiac and @fhenneke : the order should use the lowest possible buy amount for the AMM

@mfw78 mfw78 linked an issue Jul 8, 2024 that may be closed by this pull request
Comment on lines 102 to 104
// Because of how we changed the price, we expect to buy USDC
assertEq(address(ammOrder.sellToken), address(USDC));
assertEq(address(ammOrder.buyToken), address(WETH));

Choose a reason for hiding this comment

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

this comment is misleading, if the pool is expected to BUY USDC, shouldn't buyToken be USDC?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You're right, the comment here is incorrect. I changed it so that now it should be clearer why one is the sell token and why the other is the buy token.

@wei3erHase
Copy link

isn't tradedAmountToken0 unused in GetTradeableOrder lib?

Copy link
Contributor

@mfw78 mfw78 left a comment

Choose a reason for hiding this comment

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

Approved on the basis that #95 closes out the todo.

@fedgiac
Copy link
Collaborator Author

fedgiac commented Jul 9, 2024

The test is working as expected. The discussion on more precise checks on the amounts is still open, but I don't see it as a blocker for merging. Feel free to continue the discussion here.

@fedgiac fedgiac merged commit c88b7df into test-interface Jul 9, 2024
3 checks passed
@fedgiac fedgiac deleted the test-for-interface branch July 9, 2024 08:42
fedgiac added a commit that referenced this pull request Jul 9, 2024
Addresses issue in
#92 (comment).

The helper now returns a full signature for a CoW Swap order.

### Test plan

For the legacy CoW AMM: note that the forked test still works after
these changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

chore: revise tests
5 participants