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

feat: adding CoW helper MVP #121

Merged
merged 45 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
699d9d7
feat: non-weighted tradeable order test
wei3erHase Jun 27, 2024
271e00a
fix: adding GetTradeableOrder library
wei3erHase Jun 27, 2024
48888e3
feat: adding weights to the math
wei3erHase Jun 27, 2024
28565f7
feat: variable weights
wei3erHase Jun 27, 2024
1a44f5e
feat: adding test for helper
wei3erHase Jul 8, 2024
8bf22dd
feat: adding support for weights
wei3erHase Jul 8, 2024
3683943
fix: fmt
wei3erHase Jul 8, 2024
9422865
fix: sending correct price vector
wei3erHase Jul 8, 2024
be1701d
fix: making helper return valid signature
wei3erHase Jul 8, 2024
24553ab
chore: merge dev
wei3erHase Jul 9, 2024
441d117
feat: deprecated weights in helper
wei3erHase Jul 9, 2024
5630797
feat: updated buyAmount as per aaf44a3
wei3erHase Jul 9, 2024
627d878
refactor: mv GetTradeableOrder to libraries dir
wei3erHase Jul 9, 2024
cb2fb52
fix: update comment on BCoWHelper
wei3erHase Jul 9, 2024
5207c18
fix: rm unused variable warning
wei3erHase Jul 9, 2024
6f76f85
feat: adding BTT test for BCoWHelper
wei3erHase Jul 9, 2024
de99df4
fix: linter warnings
wei3erHase Jul 9, 2024
bd64dca
fix: rm remanent line change
wei3erHase Jul 9, 2024
ac17f69
fix: rm unused lines
wei3erHase Jul 9, 2024
6a14084
fix: missing natspec
wei3erHase Jul 9, 2024
96f0302
fix: uncommenting assertion in test
wei3erHase Jul 9, 2024
e8c4d33
fix: missing natspec
wei3erHase Jul 9, 2024
22480c5
fix: safety checks comments
wei3erHase Jul 9, 2024
3e5bfea
feat: vendoring interface from cow-amm
wei3erHase Jul 9, 2024
68e8368
fix: lint run
wei3erHase Jul 9, 2024
5afd282
chore: merge dev
wei3erHase Jul 9, 2024
9390e64
chore: pull dev no-rebase
wei3erHase Jul 9, 2024
b1ba43a
fix: gas snapshot
wei3erHase Jul 9, 2024
9ec1c34
refactor: reordered helper flow and cleanup
wei3erHase Jul 10, 2024
05d6271
feat: vendoring GetTradeableOrder from cow-amm
wei3erHase Jul 11, 2024
9923fc0
feat: improving commitment expectation
wei3erHase Jul 11, 2024
8d72ff8
Merge branch 'dev' of https://github.com/defi-wonderland/balancer-v1-…
wei3erHase Jul 11, 2024
699996e
fix: typos in comments
wei3erHase Jul 11, 2024
1bec1fd
fix: overwriting sellAmount in order to avoid rounding issues (#154)
wei3erHase Jul 15, 2024
1ffda8b
chore: addressing contract changes from PR comments
wei3erHase Jul 16, 2024
063d3f6
chore: addressing comments in tests
wei3erHase Jul 16, 2024
471b0e6
chore: merge dev
wei3erHase Jul 16, 2024
e0de518
fix: adding helper mock
wei3erHase Jul 16, 2024
d0bba5c
fix: addressing comments from PR
wei3erHase Jul 16, 2024
d8c49e6
refactor: deprecate fuzzed integration valid order test in favour of …
wei3erHase Jul 16, 2024
abe6f00
feat: simplifying helper integration test
wei3erHase Jul 17, 2024
b2056be
feat: improving unit test for valid order
wei3erHase Jul 17, 2024
ffb1bc0
feat: adding call tokens expectation
wei3erHase Jul 18, 2024
7719c39
fix: branching different behaviours given skewness
wei3erHase Jul 22, 2024
7db4e8e
fix: adding comments on the skewness sign
wei3erHase Jul 22, 2024
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
2 changes: 1 addition & 1 deletion src/contracts/BCoWHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ contract BCoWHelper is ICOWAMMPoolHelper, BMath {
}

/// @inheritdoc ICOWAMMPoolHelper
function tokens(address pool) public view returns (address[] memory tokens_) {
function tokens(address pool) public view virtual returns (address[] memory tokens_) {
// reverts in case pool is not deployed by the helper's factory
if (!IBCoWFactory(factory).isBPool(pool)) {
revert PoolDoesNotExist();
Expand Down
20 changes: 6 additions & 14 deletions test/integration/BCoWHelper.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {GPv2TradeEncoder} from '@composable-cow/test/vendored/GPv2TradeEncoder.s
import {BCoWFactory} from 'contracts/BCoWFactory.sol';
import {BCoWHelper} from 'contracts/BCoWHelper.sol';

contract ConstantProductHelperForkedTest is Test {
contract BCoWHelperIntegrationTest is Test {
using GPv2Order for GPv2Order.Data;

BCoWHelper private helper;
Expand All @@ -46,6 +46,9 @@ contract ConstantProductHelperForkedTest is Test {
uint256 constant INITIAL_WETH_BALANCE = 1 ether;
uint256 constant INITIAL_SPOT_PRICE = 0.001e18;

uint256 constant SKEWENESS_RATIO = 95; // -5% skewness
uint256 constant EXPECTED_FINAL_SPOT_PRICE = INITIAL_SPOT_PRICE * 100 / SKEWENESS_RATIO;

function setUp() public {
vm.createSelectFork('mainnet', 20_012_063);

Expand Down Expand Up @@ -88,29 +91,18 @@ contract ConstantProductHelperForkedTest is Test {
_executeHelperOrder(pool);

uint256 postSpotPrice = pool.getSpotPriceSansFee(address(WETH), address(DAI));
assertEq(postSpotPrice, 1_052_631_578_947_368);
assertEq(postSpotPrice, EXPECTED_FINAL_SPOT_PRICE);
}

// NOTE: reverting test, weighted pools are not supported
function testWeightedOrder() public {

Choose a reason for hiding this comment

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

I believe a unit test is enough for this

Copy link
Member Author

Choose a reason for hiding this comment

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

this one was chosen to maintain the shape in case the feature gets added later, the unit test of the helper reverting is there, and the integration perspective we keep it here, in the future, another helper will not revert on weighted pools, and this test can be reimplemented not to revert

Choose a reason for hiding this comment

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

... if this is meant to stay then wdyt about removing the commented code below?

IBCoWPool pool = IBCoWPool(address(weightedPool));

uint256 ammWethInitialBalance = 1 ether;
uint256 ammDaiInitialBalance = 1000 ether;

deal(address(WETH), address(pool), ammWethInitialBalance);
deal(address(DAI), address(pool), 4 * ammDaiInitialBalance);

uint256 spotPrice = pool.getSpotPriceSansFee(address(WETH), address(DAI));
assertEq(spotPrice, INITIAL_SPOT_PRICE);

vm.expectRevert(ICOWAMMPoolHelper.PoolDoesNotExist.selector);
helper.order(address(pool), new uint256[](2));

// NOTE: not supported
// _executeHelperOrder(pool, ammWethInitialBalance, ammDaiInitialBalance);
// uint256 postSpotPrice = pool.getSpotPriceSansFee(address(WETH), address(DAI));
// assertEq(postSpotPrice, 1_052_631_578_947_368);
}

function _executeHelperOrder(IBPool pool) internal {
Expand All @@ -131,7 +123,7 @@ contract ConstantProductHelperForkedTest is Test {
// DAI per WETH means a price vector of [1, 3000] (if the decimals are
// different, as in WETH/USDC, then the atom amount is what counts).
prices[daiIndex] = INITIAL_WETH_BALANCE;
prices[wethIndex] = INITIAL_DAI_BALANCE * 95 / 100;
prices[wethIndex] = INITIAL_DAI_BALANCE * SKEWENESS_RATIO / 100;

// The helper generates the AMM order
GPv2Order.Data memory ammOrder;
Expand Down
13 changes: 13 additions & 0 deletions test/manual-smock/MockBCoWHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ contract MockBCoWHelper is BCoWHelper, Test {
return _APP_DATA;
}

// NOTE: manually added method (public overrides not supported in smock)
function tokens(address pool) public view override returns (address[] memory tokens_) {
(bool _success, bytes memory _data) = address(this).staticcall(abi.encodeWithSignature('tokens(address)', pool));

if (_success) return abi.decode(_data, (address[]));
else return super.tokens(pool);
}

// NOTE: manually added method (public overrides not supported in smock)
function expectCall_tokens(address pool) public {
vm.expectCall(address(this), abi.encodeWithSignature('tokens(address)', pool));
}

// BCoWHelper methods
constructor(address factory_) BCoWHelper(factory_) {}

Expand Down
13 changes: 9 additions & 4 deletions test/unit/BCoWHelper.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ contract BCoWHelperTest is Test {
}

function test_OrderWhenThePoolIsSupported(bytes32 domainSeparator) external {
// it should call tokens
helper.mock_call_tokens(address(pool), tokens);
helper.expectCall_tokens(address(pool));

// it should query the domain separator from the pool
pool.expectCall_SOLUTION_SETTLER_DOMAIN_SEPARATOR();
pool.mock_call_SOLUTION_SETTLER_DOMAIN_SEPARATOR(domainSeparator);
Expand Down Expand Up @@ -157,8 +161,9 @@ contract BCoWHelperTest is Test {

function test_OrderGivenAPriceVector(uint256 priceSkewness, uint256 balanceToken0, uint256 balanceToken1) external {

Choose a reason for hiding this comment

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

I don't really follow what are the 5000, 10_000and15_000`, much less why we need them. Do you think it would be clearer if they had names? or some comments?

afaict we will skew the price by a particular factor from current spot price in order to allow for naturally ocurring slippage, is that correct?

Copy link
Member Author

Choose a reason for hiding this comment

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

making it uint256 base = 1e18 and the bounds between 0.5 and 1.5

// skew the price by max 50% (more could result in reverts bc of max swap ratio)
priceSkewness = bound(priceSkewness, 5000, 15_000);
vm.assume(priceSkewness != 10_000); // avoids no-skewness revert
uint256 base = 1e18;
priceSkewness = bound(priceSkewness, 0.5e18, 1.5e18);
vm.assume(priceSkewness != base); // avoids no-skewness revert

balanceToken0 = bound(balanceToken0, 1e18, 1e36);
0xteddybear marked this conversation as resolved.
Show resolved Hide resolved
balanceToken1 = bound(balanceToken1, 1e18, 1e36);
Expand All @@ -167,12 +172,12 @@ contract BCoWHelperTest is Test {

uint256[] memory prices = new uint256[](2);
prices[0] = balanceToken1;
prices[1] = balanceToken0 * priceSkewness / 10_000;
prices[1] = balanceToken0 * priceSkewness / base;

// it should return a valid pool order
(GPv2Order.Data memory ammOrder,,,) = helper.order(address(pool), prices);

assertEq(address(ammOrder.buyToken), priceSkewness > 10_000 ? tokens[0] : tokens[1]);
assertEq(address(ammOrder.buyToken), priceSkewness > 1e18 ? tokens[0] : tokens[1]);

Choose a reason for hiding this comment

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

use the base variable


// this call should not revert
pool.verify(ammOrder);
Expand Down
1 change: 1 addition & 0 deletions test/unit/BCoWHelper.tree
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ BCoWHelperTest::order
├── when the pool is not supported
0xteddybear marked this conversation as resolved.
Show resolved Hide resolved
│ └── it should revert
├── when the pool is supported
│ ├── it should call tokens
│ ├── it should query the domain separator from the pool
│ ├── it should return a valid pool order
│ ├── it should return a commit pre-interaction
Expand Down
Loading