andromeda_std/common/
rates.rs

1use cosmwasm_std::{BankMsg, CosmosMsg, SubMsg, Uint128};
2
3/// Gets the amount of tax paid by iterating over the `msgs` and comparing it to the
4/// difference between the base amount and the amount left over after royalties.
5/// It is assumed that each bank message has a single Coin to send as transfer
6/// agreements only accept a single Coin. It is also assumed that the result will always be
7/// non-negative.
8///
9/// # Arguments
10///
11/// * `msgs` - The vector of submessages containing fund transfers
12/// * `base_amount` - The amount paid before tax.
13/// * `remaining_amount_after_royalties` - The amount remaining of the base_amount after royalties
14///                                        are applied
15/// Returns the amount of tax necessary to be paid on top of the `base_amount`.
16pub fn get_tax_amount(
17    msgs: &[SubMsg],
18    base_amount: Uint128,
19    remaining_amount_after_royalties: Uint128,
20) -> Uint128 {
21    let deducted_amount = base_amount - remaining_amount_after_royalties;
22    msgs.iter()
23        .map(|msg| {
24            if let CosmosMsg::Bank(BankMsg::Send { amount, .. }) = &msg.msg {
25                amount[0].amount
26            } else {
27                Uint128::zero()
28            }
29        })
30        .reduce(|total, amount| total + amount)
31        .unwrap_or_else(Uint128::zero)
32        - deducted_amount
33}