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

fix!: WithFee::with_fee method #263

Merged
merged 4 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ['GalacticCouncil']
edition = '2018'
name = 'primitives'
version = '3.1.0'
version = '4.0.0'

[build-dependencies]
substrate-wasm-builder = {package = 'substrate-wasm-builder', version = '3.0.0'}
Expand Down
27 changes: 26 additions & 1 deletion primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,22 @@ pub mod fee {
Self: Sized,
{
fn with_fee(&self, fee: Fee) -> Option<Self>;
fn without_fee(&self, fee: Fee) -> Option<Self>;
fn just_fee(&self, fee: Fee) -> Option<Self>;
fn discounted_fee(&self) -> Option<Self>;
}

impl WithFee for Balance {
fn with_fee(&self, fee: Fee) -> Option<Self> {
self.checked_mul(fee.denominator as Self - fee.numerator as Self)?
self.checked_mul(fee.denominator as Self + fee.numerator as Self)?
.checked_div(fee.denominator as Self)
}

fn without_fee(&self, fee: Fee) -> Option<Self> {
self.checked_mul(fee.denominator as Self)?
.checked_div(fee.denominator as Self + fee.numerator as Self)
}

fn just_fee(&self, fee: Fee) -> Option<Self> {
self.checked_mul(fee.numerator as Self)?
.checked_div(fee.denominator as Self)
Expand All @@ -137,3 +143,22 @@ pub mod fee {
}
}
}

#[cfg(test)]
mod tests {
use super::fee::*;

#[test]
// This function tests that fee calculations return correct amounts
fn fee_calculations_should_work() {
let fee = Fee{
numerator: 2,
denominator: 1_000,
};

assert_eq!(1_000.with_fee(fee), Some(1_002));
assert_eq!(1_002.without_fee(fee), Some(1_000));
assert_eq!(1_000.just_fee(fee), Some(2));
assert_eq!(1_000_000.discounted_fee(), Some(700));
}
}
2 changes: 1 addition & 1 deletion runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ homepage = 'https://github.com/galacticcouncil/hydradx-node'
license = 'Apache 2.0'
name = 'hydra-dx-runtime'
repository = 'https://github.com/galacticcouncil/hydradx-node'
version = '14.1.0'
version = '14.2.0'

[package.metadata.docs.rs]
targets = ['x86_64-unknown-linux-gnu']
Expand Down