Skip to content

Commit

Permalink
Merge pull request #5 from apollodao/pacman/fee-msgs-from-coin
Browse files Browse the repository at this point in the history
Implement `fee_msgs_from_coin`
  • Loading branch information
pacmanifold authored May 9, 2024
2 parents ce74e49 + 9dce77f commit 2e3457a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

7 changes: 7 additions & 0 deletions packages/fee-config/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

## [0.1.2] - 2024-05-09

### Added

- Implemented public function `fee_msgs_from_coin` on `FeeConfig<Addr>`
2 changes: 1 addition & 1 deletion packages/fee-config/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cw-fee-config"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
description = "Fee configuration for CosmWasm contracts"
license = { workspace = true }
Expand Down
40 changes: 40 additions & 0 deletions packages/fee-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,25 @@ impl FeeConfig<Addr> {

Ok((fee_msgs, coins_after_fees))
}

/// Calculates the fee from the input coin and returns messages to send it
/// to the fee recipients.
///
/// # Arguments
/// * `coin` - The coin to take the fee from.
///
/// # Returns
/// * `Vec<CosmosMsg>` - The messages to send the fees to the fee
/// recipients.
/// * `Coin` - The asset after the fee has been taken.
pub fn fee_msgs_from_coin(&self, coin: Coin, env: &Env) -> StdResult<(Vec<CosmosMsg>, Coin)> {
let (msgs, coins_after_fee) =
self.fee_msgs_from_coins(&Coins::try_from(vec![coin.clone()])?, env)?;
Ok((
msgs,
cosmwasm_std::coin(coins_after_fee.amount_of(&coin.denom).u128(), coin.denom),
))
}
}

impl From<FeeConfig<Addr>> for FeeConfig<String> {
Expand Down Expand Up @@ -266,6 +285,27 @@ pub mod tests {
assert_eq!(asset_after_fee.amount, Uint128::new(99));
}

#[test]
fn fee_msgs_from_coin_works() {
let env = mock_env();

let fee_config = super::FeeConfig {
fee_rate: Decimal::percent(1),
fee_recipients: vec![(Addr::unchecked("addr1"), Decimal::percent(100))],
};
let coin = coin(100u128, "uusdc");
let (msgs, coin_after_fee) = fee_config.fee_msgs_from_coin(coin.clone(), &env).unwrap();
assert_eq!(msgs.len(), 1);
assert_eq!(
msgs[0],
CosmosMsg::Bank(BankMsg::Send {
to_address: "addr1".to_string(),
amount: vec![cosmwasm_std::coin(1u128, "uusdc".to_string())]
})
);
assert_eq!(coin_after_fee, cosmwasm_std::coin(99u128, "uusdc"));
}

#[test]
fn fee_msg_from_coins_works() {
let env = mock_env();
Expand Down

0 comments on commit 2e3457a

Please sign in to comment.