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

CoW AMM Router: CoW Sender transfers to Vault upfront (No approval) #1264

Open
wants to merge 66 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
9bd06ad
Setting up first files of CoW AMM Router
joaobrunoah Jan 20, 2025
2e2d01d
Setup tests
joaobrunoah Jan 20, 2025
0af5059
Merge branch 'main' into cow-amm-router
joaobrunoah Jan 21, 2025
d77f6f1
Donate logic with test
joaobrunoah Jan 21, 2025
c19e7da
Finish testing pool donation
joaobrunoah Jan 21, 2025
5c53c52
Merge branch 'main' into cow-amm-router
joaobrunoah Jan 21, 2025
581da8a
Improve donation code
joaobrunoah Jan 21, 2025
f89faf3
Develop swapExactInAndDonateSurplus function
joaobrunoah Jan 21, 2025
99a8aff
Test swapExactInAndDonateSurplus
joaobrunoah Jan 21, 2025
34e6371
Develop and test swapExactOutAndDonateSurplus
joaobrunoah Jan 21, 2025
275a403
Fix swap params
joaobrunoah Jan 21, 2025
8b0748c
Implement deadline
joaobrunoah Jan 21, 2025
38be953
Lint
joaobrunoah Jan 21, 2025
f6d1fb2
Simplify tests
joaobrunoah Jan 22, 2025
ca81f7d
Simplifying tests
joaobrunoah Jan 22, 2025
da17134
Fix tests
joaobrunoah Jan 22, 2025
0a6c10a
Documentation
joaobrunoah Jan 22, 2025
671e4fd
Merge branch 'main' into cow-amm-router
joaobrunoah Jan 22, 2025
a4f95ca
Fix v3-standalone-utils
joaobrunoah Jan 22, 2025
bb1d65a
Fix standalone-utils
joaobrunoah Jan 22, 2025
d070cf1
Fix yarn.lock
joaobrunoah Jan 22, 2025
7ce9220
Fix documentation of events
joaobrunoah Jan 22, 2025
df0efb8
Remove unused variables
joaobrunoah Jan 22, 2025
e56e4ec
Documentation
joaobrunoah Jan 22, 2025
9a15039
Merge branch 'main' into cow-amm-router
joaobrunoah Jan 22, 2025
1b030ae
Fix documentation
joaobrunoah Jan 22, 2025
a5c5c7a
Documenting hooks
joaobrunoah Jan 22, 2025
820d03a
Initial protocol fee percentage
joaobrunoah Jan 22, 2025
0d4bbaf
Create event to be emitted when protocol fee percentage changes
joaobrunoah Jan 22, 2025
11f41de
Remove CL
joaobrunoah Jan 22, 2025
deb36ca
Merge branch 'main' into cow-amm-router
joaobrunoah Jan 23, 2025
5b95e20
Apply suggestions from code review
joaobrunoah Jan 24, 2025
2718652
Fix documentation
joaobrunoah Jan 24, 2025
b10c165
Fix documentation
joaobrunoah Jan 24, 2025
34d9206
Create private _setProtocolFeePercentage
joaobrunoah Jan 24, 2025
3630bee
Change function name
joaobrunoah Jan 24, 2025
4276031
Apply suggestions from code review
joaobrunoah Jan 24, 2025
2ff43af
Fix imports
joaobrunoah Jan 27, 2025
3083636
Apply suggestions from code review
joaobrunoah Jan 27, 2025
0d2934e
Merge branch 'main' into cow-amm-router
joaobrunoah Jan 27, 2025
4306271
Permisioned "donate"
joaobrunoah Jan 27, 2025
15bdc88
Transfer upfront
joaobrunoah Jan 28, 2025
f2f50ad
Fix comments
joaobrunoah Jan 28, 2025
7236a02
Fix variable naming
joaobrunoah Jan 29, 2025
cdb26cb
Rename transferHint variable
joaobrunoah Jan 29, 2025
0990de0
Apply suggestions from code review
joaobrunoah Jan 29, 2025
1088c58
Documentation
joaobrunoah Jan 29, 2025
cc5c526
Use the maximum fee percentage of the Protocol fee controller (50%)
joaobrunoah Jan 29, 2025
dfb05a9
Change the Max Protocol Fee Percentage of the CowRouter
joaobrunoah Jan 29, 2025
e30e5bc
Change naming
joaobrunoah Jan 29, 2025
361391b
Apply suggestions from code review
joaobrunoah Jan 29, 2025
4ea97f1
Merge remote-tracking branch 'origin/cow-amm-router' into cow-amm-router
joaobrunoah Jan 29, 2025
80232ae
Clarify the userTokens variable in CoWRouter tests
joaobrunoah Jan 29, 2025
eebfb2c
Merge branch 'cow-amm-router' into cow-router-upfront-transfer
joaobrunoah Jan 29, 2025
29eb06f
Describe _settleSwapAndDonation
joaobrunoah Jan 29, 2025
77e1bbe
Lint
joaobrunoah Jan 29, 2025
5395051
Test extra and missing tokens in ExactOut
joaobrunoah Jan 29, 2025
3272f75
Merge branch 'main' into cow-router-upfront-transfer
joaobrunoah Jan 29, 2025
663ec59
Add donate test with missing and extra tokens
joaobrunoah Jan 29, 2025
aec7f13
Fix comment
joaobrunoah Jan 29, 2025
eccf943
Fix comment
joaobrunoah Jan 29, 2025
fe88815
Rename variables
joaobrunoah Jan 29, 2025
36ff07e
Change variable name
joaobrunoah Jan 29, 2025
b7f91aa
Merge branch 'main' into cow-router-upfront-transfer
joaobrunoah Jan 30, 2025
1ca81dd
Merge branch 'main' into cow-router-upfront-transfer
joaobrunoah Feb 4, 2025
c1ab29a
Merge branch 'main' into cow-router-upfront-transfer
joaobrunoah Feb 6, 2025
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
195 changes: 195 additions & 0 deletions pkg/interfaces/contracts/pool-cow/ICowRouter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
// SPDX-License-Identifier: GPL-3.0-or-later

pragma solidity ^0.8.24;

import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

import { SwapKind } from "../vault/VaultTypes.sol";

interface ICowRouter {
/**
* @notice Data for the swap and donate hook.
* @dev The swap and donate hook is used to swap on CoW AMM pools and donate surplus or fees to the same pool.
* @param pool Address of the CoW AMM Pool
* @param sender Account originating the swap and donate operation
* @param swapKind Type of swap (exact in or exact out)
* @param swapTokenIn The token entering the Vault (balance increases)
* @param swapTokenOut The token leaving the Vault (balance decreases)
* @param swapMaxAmountIn Exact amount in, when swap is EXACT_IN, or max amount in if swap is EXACT OUT
* @param swapMinAmountOut Max amount out, when swap is EXACT_IN, or exact amount out if swap is EXACT OUT
* @param swapDeadline Deadline for the swap, after which it will revert
* @param donationAmounts Amount of tokens to donate + protocol fees, sorted in token registration order
* @param transferHint Amount of tokens transferred upfront, sorted in token registration order
joaobrunoah marked this conversation as resolved.
Show resolved Hide resolved
* @param userData Additional (optional) data sent with the swap request and emitted with donation and swap events
*/
struct SwapAndDonateHookParams {
address pool;
address sender;
SwapKind swapKind;
IERC20 swapTokenIn;
IERC20 swapTokenOut;
uint256 swapMaxAmountIn;
uint256 swapMinAmountOut;
uint256 swapDeadline;
uint256[] donationAmounts;
uint256[] transferHint;
bytes userData;
}

/**
* @notice Data for the donate hook.
* @dev The donate hook is used to donate surplus or fees to a CoW AMM pool.
* @param pool Address of the CoW AMM Pool
* @param sender Account originating the donate operation
* @param donationAmounts Amount of tokens to donate + protocol fees, sorted in token registration order
* @param userData Additional (optional) data sent with the swap request and emitted with the donation event
*/
struct DonateHookParams {
address pool;
address sender;
uint256[] donationAmounts;
bytes userData;
}

/// @notice The swap transaction was not validated before the specified deadline timestamp.
error SwapDeadline();

/**
* @notice The `newProtocolFeePercentage` is above the maximum limit.
* @param newProtocolFeePercentage New value of the protocol fee percentage
* @param maxProtocolFeePercentage The maximum protocol fee percentage
*/
error ProtocolFeePercentageAboveLimit(uint256 newProtocolFeePercentage, uint256 maxProtocolFeePercentage);

/**
* @notice A swap and a donation have occurred.
* @param pool The pool with the tokens being swapped
* @param swapTokenIn The token entering the Vault (balance increases)
* @param swapTokenOut The token leaving the Vault (balance decreases)
* @param swapAmountIn Number of tokenIn tokens
* @param swapAmountOut Number of tokenOut tokens
* @param donationAfterFees Amounts donated to the pool after protocol fees, sorted in token registration order
* @param protocolFeeAmounts Fees collected by the protocol, sorted in token registration order
* @param userData Additional (optional) data sent with the swap and donate request
*/
event CoWSwapAndDonation(
address pool,
IERC20 swapTokenIn,
IERC20 swapTokenOut,
uint256 swapAmountIn,
uint256 swapAmountOut,
uint256[] donationAfterFees,
uint256[] protocolFeeAmounts,
bytes userData
);

/**
* @notice A donation has occurred.
* @param pool The pool that receives the donation
* @param donationAfterFees Amounts donated to the pool after protocol fees, sorted in token registration order
* @param protocolFeeAmounts Fees collected by the protocol, sorted in token registration order
* @param userData Additional (optional) data sent with the donate request
*/
event CoWDonation(address pool, uint256[] donationAfterFees, uint256[] protocolFeeAmounts, bytes userData);

/**
* @notice The protocol fee percentage charged on donations was changed.
* @param newProtocolFeePercentage The new protocol fee percentage
*/
event ProtocolFeePercentageChanged(uint256 newProtocolFeePercentage);

/**
* @notice Executes an ExactIn swap and donates a specified amount to the same CoW AMM Pool.
* @dev This is a permissioned function, intended to be called only by a `CoW Settlement` contract. CoW AMM matches
* transaction tokens outside of the pool, and donates fees (surplus) back to the pool. Therefore, the swap has no
* fees, but protocol fees are charged on the donation amount. On success, it emits a CoWSwapAndDonation event.
*
* @param pool The pool with the tokens being swapped
* @param swapTokenIn The token entering the Vault (balance increases)
* @param swapTokenOut The token leaving the Vault (balance decreases)
* @param swapExactAmountIn Number of tokenIn tokens
* @param swapMinAmountOut Minimum number of tokenOut tokens
* @param swapDeadline Deadline for the swap, after which it will revert
* @param donationAmounts Amount of tokens to donate + protocol fees, sorted in token registration order
* @param transferHint Amount of tokens transferred upfront, sorted in token registration order
* @param userData Additional (optional) data sent with the swap and donate request
* @return exactAmountOut Number of tokenOut tokens returned from the swap
*/
function swapExactInAndDonateSurplus(
address pool,
IERC20 swapTokenIn,
IERC20 swapTokenOut,
uint256 swapExactAmountIn,
uint256 swapMinAmountOut,
uint256 swapDeadline,
uint256[] memory donationAmounts,
uint256[] memory transferHint,
bytes memory userData
) external returns (uint256 exactAmountOut);

/**
* @notice Executes an ExactOut swap and donates a specified amount to the same CoW AMM Pool.
* @dev This is a permissioned function, intended to be called only by a `CoW Settlement` contract. CoW AMM matches
* transaction tokens outside of the pool, and donates fees (surplus) back to the pool. Therefore, the swap has no
* fees, but protocol fees are charged on the donation amount. On success, it emits a CoWSwapAndDonation event.
*
* @param pool The pool with the tokens being swapped
* @param swapTokenIn The token entering the Vault (balance increases)
* @param swapTokenOut The token leaving the Vault (balance decreases)
* @param swapMaxAmountIn Maximum number of tokenIn tokens
* @param swapExactAmountOut Number of tokenOut tokens
* @param swapDeadline Deadline for the swap, after which it will revert
* @param donationAmounts Amount of tokens to donate + protocol fees, sorted in token registration order
* @param transferHint Amount of tokens transferred upfront, sorted in token registration order
* @param userData Additional (optional) data sent with the swap and donate request
* @return exactAmountIn Number of tokenIn tokens charged in the swap
*/
function swapExactOutAndDonateSurplus(
address pool,
IERC20 swapTokenIn,
IERC20 swapTokenOut,
uint256 swapMaxAmountIn,
uint256 swapExactAmountOut,
uint256 swapDeadline,
uint256[] memory donationAmounts,
uint256[] memory transferHint,
bytes memory userData
) external returns (uint256 exactAmountIn);

/**
* @notice Executes a donation of a specified amount to a CoW AMM Pool.
* @dev This is a permissioned function, intended to be used to donate amounts to CoW AMM pools. On success, emits
* a CoWDonation event. userData may be used to explain the reason of the CoWDonation.
*
* @param pool The pool that receives the donation
* @param donationAmounts Amount of tokens to donate + protocol fees, sorted in token registration order
* @param userData Additional (optional) data sent with the donate request
*/
function donate(address pool, uint256[] memory donationAmounts, bytes memory userData) external;

/**
* @notice Returns the protocol fee percentage, registered in the CoW Router.
* @dev The protocol fee percentage is used to calculate the amount of protocol fees to charge on a donation.
* The fees stay in the router.
*
* @return protocolFeePercentage The current protocol fee percentage
*/
function getProtocolFeePercentage() external view returns (uint256 protocolFeePercentage);

/**
* @notice Returns the protocol fees collected by the CoW Router for a specific token.
* @dev The protocol fees collected by the CoW Router stay in the CoW router contract.
* @param token Token with collected protocol fees
* @return fees Protocol fees collected for the specific token
*/
function getCollectedProtocolFees(IERC20 token) external view returns (uint256 fees);

/**
* @notice Sets the protocol fee percentage.
* @dev This is a permissioned function. The protocol fee percentage is capped at a maximum value, registered as a
* constant in the CoW AMM Router.
*
* @param newProtocolFeePercentage New value of the protocol fee percentage
*/
function setProtocolFeePercentage(uint256 newProtocolFeePercentage) external;
}
3 changes: 3 additions & 0 deletions pkg/pool-cow/.solcover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
skipFiles: ['test'],
};
1 change: 1 addition & 0 deletions pkg/pool-cow/.solhintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
contracts/test/
7 changes: 7 additions & 0 deletions pkg/pool-cow/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# <img src="../../logo.svg" alt="Balancer" height="128px">

# CoW AMM in Balancer V3

## Licensing

[GNU General Public License Version 3 (GPL v3)](../../LICENSE).
Loading