-
Notifications
You must be signed in to change notification settings - Fork 21
[Draft] LeaveNoDust order type #95
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
Open
fleupold
wants to merge
2
commits into
main
Choose a base branch
from
leave_no_dust
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity >=0.8.0 <0.9.0; | ||
|
|
||
| import {IERC20} from "../BaseConditionalOrder.sol"; | ||
|
|
||
| import "../BaseConditionalOrder.sol"; | ||
|
|
||
| /** | ||
| * @title A smart contract that executes a trade at the specified exchange rate for the entirety of the current balance. | ||
| * @dev Designed to be used with the CoW Protocol Conditional Order Framework. | ||
| * This order may be used e.g. if a rebasing token is intended to be fully sold, but its balance keeps changing from | ||
| * block to block making it hard to specify a fixed amount up-front. | ||
| * It is recommended to use sell and buy amounts from a verified quote to ensure the trade is likely to execute. | ||
| * In case the sell balance at the time of execution differs significantly from the one at the time of order placement, | ||
| * additional care in accounting for price impact and the slippage tolerance may be needed. | ||
| * In particular, since the price is scaled linearly, however quotes include a non-linear network cost as part of the exchange rate, | ||
| * having a significantly lower sell amount, scaled linearly may not allow for sufficient network fee be taken from surplus. | ||
| */ | ||
| contract LeaveNoDust is BaseConditionalOrder { | ||
| struct Data { | ||
| IERC20 sellToken; | ||
| IERC20 buyToken; | ||
| uint256 sellAmount; | ||
| uint256 buyAmount; | ||
| address receiver; | ||
| uint32 validity; | ||
| bytes32 appData; | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc IConditionalOrderGenerator | ||
| * @dev Return the specified sell order using the current total balance and the specified exchange rate | ||
| */ | ||
| function getTradeableOrder(address owner, address, bytes32, bytes calldata staticInput, bytes calldata) | ||
| public | ||
| view | ||
| override | ||
| returns (GPv2Order.Data memory order) | ||
| { | ||
| /// @dev Decode the smart order payload into the trade parameters. | ||
| LeaveNoDust.Data memory data = abi.decode(staticInput, (Data)); | ||
|
|
||
| // Adjust the buy amount by the current owner's balance | ||
| uint256 sellAmount = data.sellToken.balanceOf(owner); | ||
| uint256 buyAmount = sellAmount * data.buyAmount / data.sellAmount; | ||
|
|
||
| // ensures that orders queried shortly after one another result in the same hash (to avoid spamming the orderbook) | ||
| order = GPv2Order.Data( | ||
| data.sellToken, | ||
| data.buyToken, | ||
| data.receiver, | ||
| sellAmount, | ||
| buyAmount, | ||
| data.validity, | ||
| data.appData, | ||
| 0, // this field is no longer relevant and has to be 0 | ||
| GPv2Order.KIND_SELL, | ||
| false, // fill or kill | ||
| GPv2Order.BALANCE_ERC20, | ||
| GPv2Order.BALANCE_ERC20 | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.