Skip to content

Commit

Permalink
Merge pull request #263 from galacticcouncil/fix/fee_struct
Browse files Browse the repository at this point in the history
fix!: WithFee::with_fee method
  • Loading branch information
enthusiastmartin authored May 27, 2021
2 parents 694cce6 + 21510e2 commit 75f7ec0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
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

0 comments on commit 75f7ec0

Please sign in to comment.