Skip to content

Commit

Permalink
Add calculate_fee_rate_and_remainder (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
shapeshed committed Apr 30, 2024
1 parent 3fd7377 commit 8cf05d1
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.1] - 2014-04-30

### Added

- Add `calculate_fee_rate_and_remainder`

## [0.2.0] - 2014-04-29

### Added
Expand Down Expand Up @@ -100,3 +106,7 @@ and this project adheres to
[0.0.7]: https://github.com/margined-protocol/vaultenator/0.0.6...0.0.7
[0.0.8]: https://github.com/margined-protocol/vaultenator/0.0.7...0.0.8
[0.0.9]: https://github.com/margined-protocol/vaultenator/0.0.8...0.0.9
[0.1.0]: https://github.com/margined-protocol/vaultenator/0.0.9...0.1.0
[0.1.1]: https://github.com/margined-protocol/vaultenator/0.1.0...0.1.1
[0.2.0]: https://github.com/margined-protocol/vaultenator/0.1.1...0.2.0
[0.2.1]: https://github.com/margined-protocol/vaultenator/0.2.0...0.2.1
6 changes: 3 additions & 3 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ readme = "README.md"
repository = "https://github.com/margined-protocol/vaultenator"
resolver = "2"
rust-version = "1.77.2"
version = "0.2.0"
version = "0.2.1"

[lib]
crate-type = ["cdylib", "rlib"]
Expand Down
57 changes: 56 additions & 1 deletion src/maths.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{StdError, Uint128};
use cosmwasm_std::{Decimal, StdError, StdResult, Uint128};

pub const DEFAULT_STRATEGY_TOKENS_PER_DEPOSITED_BASE_TOKEN: Uint128 = Uint128::new(1_000_000);

Expand All @@ -16,8 +16,19 @@ pub fn calculate_strategy_tokens(
Ok(strategy_tokens)
}

pub fn calculate_fee_and_remainder(
amount: Uint128,
fee_rate: Decimal,
) -> StdResult<(Uint128, Uint128)> {
let remainder = amount * (Decimal::one() - fee_rate);
let fee = amount.checked_sub(remainder)?;
Ok((fee, remainder))
}

#[cfg(test)]
mod tests {
use std::str::FromStr;

use super::*;

#[test]
Expand Down Expand Up @@ -55,4 +66,48 @@ mod tests {
.unwrap();
assert_eq!(result, Uint128::new(1_000_000)); // Should match the strategy token supply because ratio is 1:1
}

#[test]
fn test_calculate_fee_and_remainder_with_basic_fee() {
let fee_rate = Decimal::percent(10);
let amount = Uint128::new(1000);
let expected_fee = Uint128::new(100);
let expected_remainder = Uint128::new(900);
let (fee, remainder) = calculate_fee_and_remainder(amount, fee_rate).unwrap();
assert_eq!(fee, expected_fee);
assert_eq!(remainder, expected_remainder);
}

#[test]
fn test_calculate_fee_and_remainder_with_zero_fee_rate() {
let fee_rate = Decimal::zero();
let amount = Uint128::new(1000);
let expected_fee = Uint128::new(0);
let expected_remainder = Uint128::new(1000);
let (fee, remainder) = calculate_fee_and_remainder(amount, fee_rate).unwrap();
assert_eq!(fee, expected_fee);
assert_eq!(remainder, expected_remainder);
}

#[test]
fn test_calculate_fee_and_remainder_with_100_percent_fee_rate() {
let fee_rate = Decimal::percent(100);
let amount = Uint128::new(1000);
let expected_fee = Uint128::new(1000);
let expected_remainder = Uint128::zero();
let (fee, remainder) = calculate_fee_and_remainder(amount, fee_rate).unwrap();
assert_eq!(fee, expected_fee);
assert_eq!(remainder, expected_remainder);
}

#[test]
fn test_calculate_fee_and_remainder_with_small_fractional_fee_rate() {
let fee_rate = Decimal::from_str("0.05").unwrap();
let amount = Uint128::new(1000);
let expected_fee = Uint128::new(50);
let expected_remainder = Uint128::new(950);
let (fee, remainder) = calculate_fee_and_remainder(amount, fee_rate).unwrap();
assert_eq!(fee, expected_fee);
assert_eq!(remainder, expected_remainder);
}
}

0 comments on commit 8cf05d1

Please sign in to comment.