Skip to content

Commit

Permalink
Update the fee_percentage type (#69)
Browse files Browse the repository at this point in the history
Co-authored-by: drewstone <[email protected]>
  • Loading branch information
duguorong009 and drewstone authored Aug 22, 2022
1 parent 5656499 commit 74db63e
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 145 deletions.
6 changes: 3 additions & 3 deletions contracts/tokenwrapper/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn instantiate(
let fee_recipient = deps.api.addr_validate(msg.fee_recipient.as_str())?;
if msg.fee_percentage > WRAP_FEE_CALC_DENOMINATOR {
return Err(ContractError::Std(StdError::generic_err(
"Fee percenage cannot be greater than 100",
"Fee percentage cannot be greater than 10000",
)));
}
let fee_percentage = msg.fee_percentage;
Expand Down Expand Up @@ -458,7 +458,7 @@ fn update_config(
governor: Option<String>,
is_native_allowed: Option<bool>,
wrapping_limit: Option<Uint128>,
fee_percentage: Option<u8>,
fee_percentage: Option<u16>,
fee_recipient: Option<String>,
) -> Result<Response, ContractError> {
let mut config = CONFIG.load(deps.storage)?;
Expand All @@ -483,7 +483,7 @@ fn update_config(
if let Some(fee_percentage) = fee_percentage {
if fee_percentage > WRAP_FEE_CALC_DENOMINATOR {
return Err(ContractError::Std(StdError::generic_err(
"Fee percenage cannot be greater than 100",
"Fee percentage cannot be greater than 10000",
)));
}
config.fee_percentage = fee_percentage;
Expand Down
2 changes: 1 addition & 1 deletion contracts/tokenwrapper/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Config {
pub governor: Addr,
pub native_token_denom: String,
pub fee_recipient: Addr,
pub fee_percentage: u8,
pub fee_percentage: u16,
pub is_native_allowed: bool,
pub wrapping_limit: Uint128,
pub proposal_nonce: u64,
Expand Down
46 changes: 23 additions & 23 deletions contracts/tokenwrapper/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const NAME: &str = "Webb-WRAP";
const SYMBOL: &str = "WWRP";
const DECIMALS: u8 = 6;
const FEE_RECIPIENT: &str = "terra1qca9hs2qk2w29gqduaq9k720k9293qt7q8nszl";
const FEE_PERCENTAGE: u8 = 1_u8;
const FEE_PERCENTAGE: u16 = 1_u16;
const NATIVE_TOKEN_DENOM: &str = "uusd";
const CW20_TOKEN: &str = "cw20_token";
const WRAPPING_LIMIT: u128 = 5000000;
Expand Down Expand Up @@ -90,7 +90,7 @@ fn test_wrap_native() {
let mut deps = init_tokenwrapper([].to_vec());

// Try the wrapping the native token
let info = mock_info("anyone", &coins(100, "uusd"));
let info = mock_info("anyone", &coins(10000, "uusd"));
let wrap_msg = ExecuteMsg::Wrap {
sender: Some("owner".to_string()),
recipient: Some("recipient".to_string()),
Expand All @@ -104,7 +104,7 @@ fn test_wrap_native() {
attr("from", "anyone"),
attr("owner", "owner"),
attr("to", "recipient"),
attr("minted", "99"),
attr("minted", "9999"),
attr("fee", "1"),
]
);
Expand All @@ -121,7 +121,7 @@ fn test_wrap_native() {
)
.unwrap();
let token_balance: BalanceResponse = from_binary(&query).unwrap();
assert_eq!(token_balance.balance.u128(), 99);
assert_eq!(token_balance.balance.u128(), 9999);
}

#[test]
Expand All @@ -130,7 +130,7 @@ fn test_unwrap_native() {
let mut deps = init_tokenwrapper(ctx_coins);

// Try the wrapping the native token
let info = mock_info("anyone", &coins(100, "uusd"));
let info = mock_info("anyone", &coins(10000, "uusd"));
let wrap_msg = ExecuteMsg::Wrap {
sender: None,
recipient: None,
Expand All @@ -141,7 +141,7 @@ fn test_unwrap_native() {
let info = mock_info("anyone", &[]);
let unwrap_msg = ExecuteMsg::Unwrap {
token: None,
amount: Uint128::from(80_u128),
amount: Uint128::from(8000_u128),
sender: None,
recipient: None,
};
Expand All @@ -154,8 +154,8 @@ fn test_unwrap_native() {
attr("from", "anyone"),
attr("owner", "anyone"),
attr("to", "anyone"),
attr("unwrap", "80"),
attr("refund", "80"),
attr("unwrap", "8000"),
attr("refund", "8000"),
]
);

Expand All @@ -169,7 +169,7 @@ fn test_unwrap_native() {
)
.unwrap();
let token_balance: BalanceResponse = from_binary(&query).unwrap();
assert_eq!(token_balance.balance.u128(), 19);
assert_eq!(token_balance.balance.u128(), 1999);
}

#[test]
Expand All @@ -188,7 +188,7 @@ fn test_wrap_cw20() {
let info = mock_info(CW20_TOKEN, &[]);
let wrap_msg = ExecuteMsg::Receive(Cw20ReceiveMsg {
sender: "anyone".to_string(),
amount: Uint128::from(100_u128),
amount: Uint128::from(10000_u128),
msg: to_binary(&Cw20HookMsg::Wrap {
sender: None,
recipient: None,
Expand All @@ -204,7 +204,7 @@ fn test_wrap_cw20() {
attr("from", "anyone"),
attr("owner", "anyone"),
attr("to", "anyone"),
attr("minted", "99"),
attr("minted", "9999"),
attr("fee", "1")
]
);
Expand All @@ -219,7 +219,7 @@ fn test_wrap_cw20() {
)
.unwrap();
let token_balance: BalanceResponse = from_binary(&query).unwrap();
assert_eq!(token_balance.balance.u128(), 99);
assert_eq!(token_balance.balance.u128(), 9999);
}

#[test]
Expand All @@ -238,7 +238,7 @@ fn test_unwrap_cw20() {
let info = mock_info(CW20_TOKEN, &[]);
let wrap_msg = ExecuteMsg::Receive(Cw20ReceiveMsg {
sender: "anyone".to_string(),
amount: Uint128::from(100_u128),
amount: Uint128::from(10000_u128),
msg: to_binary(&Cw20HookMsg::Wrap {
sender: None,
recipient: None,
Expand All @@ -251,7 +251,7 @@ fn test_unwrap_cw20() {
let info = mock_info("anyone", &[]);
let unwrap_msg = ExecuteMsg::Unwrap {
token: Some(Addr::unchecked(CW20_TOKEN.to_string())),
amount: Uint128::from(80_u128),
amount: Uint128::from(8000_u128),
sender: None,
recipient: None,
};
Expand All @@ -264,8 +264,8 @@ fn test_unwrap_cw20() {
attr("from", "anyone"),
attr("owner", "anyone"),
attr("to", "anyone"),
attr("unwrap", "80"),
attr("refund", "80"),
attr("unwrap", "8000"),
attr("refund", "8000"),
]
);

Expand All @@ -279,7 +279,7 @@ fn test_unwrap_cw20() {
)
.unwrap();
let token_balance: BalanceResponse = from_binary(&res).unwrap();
assert_eq!(token_balance.balance.u128(), 19);
assert_eq!(token_balance.balance.u128(), 1999);
}

#[test]
Expand All @@ -291,13 +291,13 @@ fn test_query_fee_from_wrap_amt() {
deps.as_ref(),
mock_env(),
QueryMsg::FeeFromAmount {
amount_to_wrap: "222".to_string(),
amount_to_wrap: "10000".to_string(),
},
)
.unwrap();
let fee_response: FeeFromAmountResponse = from_binary(&query_bin).unwrap();
assert_eq!(fee_response.amount_to_wrap, "222".to_string());
assert_eq!(fee_response.fee_amt, "2".to_string());
assert_eq!(fee_response.amount_to_wrap, "10000".to_string());
assert_eq!(fee_response.fee_amt, "1".to_string());
}

#[test]
Expand All @@ -309,13 +309,13 @@ fn test_query_amt_to_wrap_from_target_amount() {
deps.as_ref(),
mock_env(),
QueryMsg::GetAmountToWrap {
target_amount: "222".to_string(),
target_amount: "10000".to_string(),
},
)
.unwrap();
let fee_response: GetAmountToWrapResponse = from_binary(&query_bin).unwrap();
assert_eq!(fee_response.target_amount, "222".to_string());
assert_eq!(fee_response.amount_to_wrap, "224".to_string());
assert_eq!(fee_response.target_amount, "10000".to_string());
assert_eq!(fee_response.amount_to_wrap, "10001".to_string());
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions contracts/tokenwrapper/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ pub fn is_valid_unwrap_amount(deps: DepsMut, sender: &str, amount: Uint128) -> b
}

// Calculates the "fee" from "wrap_amount".
pub fn get_fee_from_amount(amount_to_wrap: Uint128, fee_perc: u8) -> Uint128 {
pub fn get_fee_from_amount(amount_to_wrap: Uint128, fee_perc: u16) -> Uint128 {
amount_to_wrap.multiply_ratio(fee_perc, WRAP_FEE_CALC_DENOMINATOR)
}

// Calculate the "amount_to_send" from "deposit_target" amount.
pub fn get_amount_to_wrap(target_amount: Uint128, fee_perc: u8) -> Uint128 {
pub fn get_amount_to_wrap(target_amount: Uint128, fee_perc: u16) -> Uint128 {
target_amount.multiply_ratio(
WRAP_FEE_CALC_DENOMINATOR,
WRAP_FEE_CALC_DENOMINATOR - fee_perc,
Expand Down
8 changes: 4 additions & 4 deletions packages/protocol_cosmwasm/src/token_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use cw20::{Cw20ReceiveMsg, Expiration};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

pub const WRAP_FEE_CALC_DENOMINATOR: u8 = 100_u8;
pub const WRAP_FEE_CALC_DENOMINATOR: u16 = 10_000_u16;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct InstantiateMsg {
/// name of the Wrapping target token
Expand All @@ -18,8 +18,8 @@ pub struct InstantiateMsg {
pub governor: Option<String>,
/// addr of fee recipient
pub fee_recipient: String,
/// fee_percentage( 0 ~ 100 )
pub fee_percentage: u8,
/// fee_percentage( 0 ~ 10,000 )
pub fee_percentage: u16,
/// native token denom string to be wrapped
pub native_token_denom: String,
/// flag of is_native_allowed
Expand Down Expand Up @@ -65,7 +65,7 @@ pub enum ExecuteMsg {
ConfigureFeeRecipient { fee_recipient: Option<String> },

/// Update the `fee_percentage`
ConfigureFeePercentage { fee_percentage: Option<u8> },
ConfigureFeePercentage { fee_percentage: Option<u16> },

/// Add cw20 token address to wrapping list
AddCw20TokenAddr { token: String, nonce: u64 },
Expand Down
1 change: 0 additions & 1 deletion packages/protocol_cosmwasm/src/treasury.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use cosmwasm_std::Uint128;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

pub const WRAP_FEE_CALC_DENOMINATOR: u8 = 100_u8;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct InstantiateMsg {
/// Address of "treasury_handler"
Expand Down
Loading

0 comments on commit 74db63e

Please sign in to comment.