-
Notifications
You must be signed in to change notification settings - Fork 28
OrderBook: market maker network fee #762
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
eb6da53
fee calculator
wer1st 581802e
delete comments
wer1st 65babfd
use fee calculator
wer1st d69314b
Merge branch 'develop' of github.com:sora-xor/sora2-network into 348-…
wer1st a14bf8b
tests
wer1st bfc8982
apply the cross-spread case
wer1st 6dde7ff
update approach
wer1st 7a2947c
Merge branch 'develop' of github.com:sora-xor/sora2-network into 348-…
wer1st de54dfe
Merge branch 'develop' into 348-order-book-fee
wer1st 3c14c4c
apply comments
wer1st 3630474
Merge branch 'develop' into 348-order-book-fee
wer1st 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,83 @@ | ||
| // This file is part of the SORA network and Polkaswap app. | ||
|
|
||
| // Copyright (c) 2020, 2021, Polka Biome Ltd. All rights reserved. | ||
| // SPDX-License-Identifier: BSD-4-Clause | ||
|
|
||
| // Redistribution and use in source and binary forms, with or without modification, | ||
| // are permitted provided that the following conditions are met: | ||
|
|
||
| // Redistributions of source code must retain the above copyright notice, this list | ||
| // of conditions and the following disclaimer. | ||
| // Redistributions in binary form must reproduce the above copyright notice, this | ||
| // list of conditions and the following disclaimer in the documentation and/or other | ||
| // materials provided with the distribution. | ||
| // | ||
| // All advertising materials mentioning features or use of this software must display | ||
| // the following acknowledgement: This product includes software developed by Polka Biome | ||
| // Ltd., SORA, and Polkaswap. | ||
| // | ||
| // Neither the name of the Polka Biome Ltd. nor the names of its contributors may be used | ||
| // to endorse or promote products derived from this software without specific prior written permission. | ||
|
|
||
| // THIS SOFTWARE IS PROVIDED BY Polka Biome Ltd. AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
| // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Polka Biome Ltd. BE LIABLE FOR ANY | ||
| // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
| // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | ||
| // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | ||
| // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
|
||
| use crate::Config; | ||
| use common::prelude::FixedWrapper; | ||
| use common::Balance; | ||
| use sp_std::marker::PhantomData; | ||
|
|
||
| use common::prelude::constants::SMALL_FEE as BASE_FEE; | ||
|
|
||
| /// Calculator for order book custom fees | ||
| pub struct FeeCalculator<T: Config>(PhantomData<T>); | ||
|
|
||
| impl<T: Config> FeeCalculator<T> { | ||
| /// Calculates the fee for `place_limit_order` extrinsic. | ||
| /// | ||
| /// The idea is to provide the reduced fee for market maker. | ||
| /// If extrinsic is successfull, user pays maximum half network fee. | ||
| /// If extrinsic failed, user pays full network fee. | ||
| /// | ||
| /// The actual fee contains two parts: constant and dynamic. | ||
| /// Dynamic part depends on limit order lifetime. The longer the lifetime, the higher the dynamic fee. | ||
| /// | ||
| /// const part = (4 / 7) * (base fee / 2) | ||
| /// dynamic part = (3 / 7) * (base fee / 2) * (lifetime / max_lifetime) | ||
| pub fn place_limit_order_fee( | ||
| lifetime: Option<u64>, | ||
| has_weight: bool, | ||
| is_err: bool, | ||
| ) -> Option<Balance> { | ||
| // if place_limit_order() doesn't return a weight, it means that the limit order was converted into market order and the exchange fee should be taken | ||
| // if some error occurred, it means we don't prvide the reduced market maker fee for this call | ||
| if !has_weight || is_err { | ||
| return Some(BASE_FEE); | ||
| } | ||
|
|
||
| let market_maker_max_fee = BASE_FEE.checked_div(2)?; | ||
|
|
||
| // Maximum lifetime is used if `None` was supplied | ||
| let Some(lifetime) = lifetime else { | ||
| return Some(market_maker_max_fee); | ||
wer1st marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| let max_lifetime: u64 = T::MAX_ORDER_LIFESPAN.try_into().ok()?; | ||
|
|
||
| let life_ratio = FixedWrapper::from(lifetime) / FixedWrapper::from(max_lifetime); | ||
|
|
||
| let const_part = (FixedWrapper::from(market_maker_max_fee) / FixedWrapper::from(7)) | ||
| * FixedWrapper::from(4); | ||
| let dynamic_part = (FixedWrapper::from(market_maker_max_fee) / FixedWrapper::from(7)) | ||
| * FixedWrapper::from(3) | ||
| * life_ratio; | ||
|
|
||
| (const_part + dynamic_part).try_into_balance().ok() | ||
wer1st marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
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
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,112 @@ | ||
| // This file is part of the SORA network and Polkaswap app. | ||
|
|
||
| // Copyright (c) 2020, 2021, Polka Biome Ltd. All rights reserved. | ||
| // SPDX-License-Identifier: BSD-4-Clause | ||
|
|
||
| // Redistribution and use in source and binary forms, with or without modification, | ||
| // are permitted provided that the following conditions are met: | ||
|
|
||
| // Redistributions of source code must retain the above copyright notice, this list | ||
| // of conditions and the following disclaimer. | ||
| // Redistributions in binary form must reproduce the above copyright notice, this | ||
| // list of conditions and the following disclaimer in the documentation and/or other | ||
| // materials provided with the distribution. | ||
| // | ||
| // All advertising materials mentioning features or use of this software must display | ||
| // the following acknowledgement: This product includes software developed by Polka Biome | ||
| // Ltd., SORA, and Polkaswap. | ||
| // | ||
| // Neither the name of the Polka Biome Ltd. nor the names of its contributors may be used | ||
| // to endorse or promote products derived from this software without specific prior written permission. | ||
|
|
||
| // THIS SOFTWARE IS PROVIDED BY Polka Biome Ltd. AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
| // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Polka Biome Ltd. BE LIABLE FOR ANY | ||
| // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
| // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | ||
| // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | ||
| // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
|
||
| #![cfg(feature = "wip")] // order-book | ||
|
|
||
| use common::balance; | ||
| use common::prelude::constants::SMALL_FEE; | ||
| use framenode_runtime::order_book::fee_calculator::FeeCalculator; | ||
| use framenode_runtime::order_book::Config; | ||
| use framenode_runtime::Runtime; | ||
|
|
||
| #[test] | ||
| fn should_calculate_place_limit_order_fee() { | ||
| let max_lifetime = <Runtime as Config>::MAX_ORDER_LIFESPAN; | ||
|
|
||
| assert_eq!( | ||
| FeeCalculator::<Runtime>::place_limit_order_fee(Some(0), true, false).unwrap(), | ||
| balance!(0.0002) | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| FeeCalculator::<Runtime>::place_limit_order_fee(Some(max_lifetime / 10), true, false) | ||
| .unwrap(), | ||
| balance!(0.000215) | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| FeeCalculator::<Runtime>::place_limit_order_fee(Some(max_lifetime / 5), true, false) | ||
| .unwrap(), | ||
| balance!(0.00023) | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| FeeCalculator::<Runtime>::place_limit_order_fee(Some(max_lifetime / 2), true, false) | ||
| .unwrap(), | ||
| balance!(0.000275) | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| FeeCalculator::<Runtime>::place_limit_order_fee(Some(max_lifetime), true, false).unwrap(), | ||
| SMALL_FEE / 2 | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn should_calculate_place_limit_order_fee_without_weight() { | ||
| assert_eq!( | ||
| FeeCalculator::<Runtime>::place_limit_order_fee(Some(1000), false, false).unwrap(), | ||
| SMALL_FEE | ||
| ); | ||
| assert_eq!( | ||
| FeeCalculator::<Runtime>::place_limit_order_fee(Some(0), false, false).unwrap(), | ||
| SMALL_FEE | ||
| ); | ||
| assert_eq!( | ||
| FeeCalculator::<Runtime>::place_limit_order_fee(Some(1000000000000000), false, false) | ||
| .unwrap(), | ||
| SMALL_FEE | ||
| ); | ||
| assert_eq!( | ||
| FeeCalculator::<Runtime>::place_limit_order_fee(None, false, false).unwrap(), | ||
| SMALL_FEE | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn should_calculate_place_limit_order_fee_with_error() { | ||
| assert_eq!( | ||
| FeeCalculator::<Runtime>::place_limit_order_fee(Some(1000), true, true).unwrap(), | ||
| SMALL_FEE | ||
| ); | ||
| assert_eq!( | ||
| FeeCalculator::<Runtime>::place_limit_order_fee(Some(0), true, true).unwrap(), | ||
| SMALL_FEE | ||
| ); | ||
| assert_eq!( | ||
| FeeCalculator::<Runtime>::place_limit_order_fee(Some(1000000000000000), true, true) | ||
| .unwrap(), | ||
| SMALL_FEE | ||
| ); | ||
| assert_eq!( | ||
| FeeCalculator::<Runtime>::place_limit_order_fee(None, true, true).unwrap(), | ||
| SMALL_FEE | ||
| ); | ||
| } |
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 |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ | |
|
|
||
| mod data_layer; | ||
| mod extrinsics; | ||
| mod fee_calculator; | ||
| mod order_book; | ||
| mod orders; | ||
| mod pallet; | ||
|
|
||
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
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
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
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.