Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Multiple Rates #517

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions contracts/data-storage/andromeda-primitive/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ pub fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Respon
ExecuteMsg::SetValue { key, value } => set_value(ctx, key, value, action),
ExecuteMsg::DeleteValue { key } => delete_value(ctx, key),
ExecuteMsg::Rates(rates_message) => match rates_message {
RatesMessage::SetRate { rate, .. } => match rate {
Rate::Local(local_rate) => {
// Percent rates aren't applicable in this case, so we enforce Flat rates
ensure!(local_rate.value.is_flat(), ContractError::InvalidRate {});
ADOContract::default().execute(ctx, msg)
RatesMessage::SetRate { rates, .. } => {
for rate in rates {
match rate {
Rate::Local(local_rate) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Refactor this to a rate.is_flat() and rate.is_local()

ensure!(local_rate.value.is_flat(), ContractError::InvalidRate {});
}
Rate::Contract(_) => (),
}
}
Rate::Contract(_) => ADOContract::default().execute(ctx, msg),
},
ADOContract::default().execute(ctx, msg)
}
RatesMessage::RemoveRate { .. } => ADOContract::default().execute(ctx, msg),
},
_ => ADOContract::default().execute(ctx, msg),
Expand Down
8 changes: 4 additions & 4 deletions contracts/data-storage/andromeda-primitive/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ impl MockPrimitive {
app: &mut MockApp,
sender: Addr,
action: String,
rate: Rate,
rates: Vec<Rate>,
) -> ExecuteResult {
self.execute(app, &mock_set_rate_msg(action, rate), sender, &[])
self.execute(app, &mock_set_rate_msg(action, rates), sender, &[])
}

pub fn query_value(&self, app: &mut MockApp, key: Option<String>) -> GetValueResponse {
Expand Down Expand Up @@ -109,8 +109,8 @@ pub fn mock_store_address_msgs(key: String, address: Addr) -> ExecuteMsg {
}
}

pub fn mock_set_rate_msg(action: String, rate: Rate) -> ExecuteMsg {
ExecuteMsg::Rates(RatesMessage::SetRate { action, rate })
pub fn mock_set_rate_msg(action: String, rates: Vec<Rate>) -> ExecuteMsg {
ExecuteMsg::Rates(RatesMessage::SetRate { action, rates })
}

pub fn mock_primitive_get_value(key: Option<String>) -> QueryMsg {
Expand Down
18 changes: 9 additions & 9 deletions contracts/data-storage/andromeda-primitive/src/testing/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ fn test_set_value_with_tax() {
// Set percent rates
let set_percent_rate_msg = ExecuteMsg::Rates(RatesMessage::SetRate {
action: "PrimitiveSetValue".to_string(),
rate: Rate::Local(LocalRate {
rates: vec![Rate::Local(LocalRate {
rate_type: LocalRateType::Additive,
recipients: vec![],
value: LocalRateValue::Percent(PercentRate {
percent: Decimal::one(),
}),
description: None,
}),
})],
});

let err = execute(
Expand All @@ -91,16 +91,16 @@ fn test_set_value_with_tax() {
assert_eq!(err, ContractError::InvalidRate {});

// Make sure sender is set as recipient when the recipients vector is empty
let rate: Rate = Rate::Local(LocalRate {
let rates = vec![Rate::Local(LocalRate {
rate_type: LocalRateType::Additive,
recipients: vec![],
value: LocalRateValue::Flat(coin(20_u128, "uandr")),
description: None,
});
})];

let msg = ExecuteMsg::Rates(RatesMessage::SetRate {
action: "SetValue".to_string(),
rate,
rates,
});
execute(deps.as_mut(), mock_env(), info.clone(), msg).unwrap();

Expand All @@ -109,15 +109,15 @@ fn test_set_value_with_tax() {
.unwrap();
assert_eq!(
queried_rates.unwrap(),
Rate::Local(LocalRate {
vec![Rate::Local(LocalRate {
rate_type: LocalRateType::Additive,
recipients: vec![Recipient::new(AndrAddr::from_string("creator"), None)],
value: LocalRateValue::Flat(coin(20_u128, "uandr")),
description: None,
})
})]
);

let rate: Rate = Rate::Local(LocalRate {
let rate = vec![Rate::Local(LocalRate {
rate_type: LocalRateType::Additive,
recipients: vec![Recipient {
address: AndrAddr::from_string(tax_recipient.to_string()),
Expand All @@ -126,7 +126,7 @@ fn test_set_value_with_tax() {
}],
value: LocalRateValue::Flat(coin(20_u128, "uandr")),
description: None,
});
})];

// Set rates
ADOContract::default()
Expand Down
8 changes: 4 additions & 4 deletions contracts/fungible-tokens/andromeda-cw20/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ impl MockCW20 {
app: &mut MockApp,
sender: Addr,
action: String,
rate: Rate,
rates: Vec<Rate>,
) -> ExecuteResult {
self.execute(app, &mock_set_rate_msg(action, rate), sender, &[])
self.execute(app, &mock_set_rate_msg(action, rates), sender, &[])
}

pub fn query_balance(&self, app: &MockApp, address: impl Into<String>) -> Uint128 {
Expand Down Expand Up @@ -220,6 +220,6 @@ pub fn mock_cw20_increase_allowance(spender: String, amount: Uint128) -> Execute
}
}

pub fn mock_set_rate_msg(action: String, rate: Rate) -> ExecuteMsg {
ExecuteMsg::Rates(RatesMessage::SetRate { action, rate })
pub fn mock_set_rate_msg(action: String, rates: Vec<Rate>) -> ExecuteMsg {
ExecuteMsg::Rates(RatesMessage::SetRate { action, rates })
}
8 changes: 4 additions & 4 deletions contracts/fungible-tokens/andromeda-cw20/src/testing/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn test_transfer() {
};

// Set a royalty of 10% to be paid to royalty_recipient
let rate = Rate::Local(LocalRate {
let rate = vec![Rate::Local(LocalRate {
rate_type: LocalRateType::Deductive,
recipients: vec![Recipient {
address: AndrAddr::from_string("royalty_recipient".to_string()),
Expand All @@ -98,7 +98,7 @@ fn test_transfer() {
percent: Decimal::percent(10),
}),
description: None,
});
})];

// Set rates
ADOContract::default()
Expand Down Expand Up @@ -193,7 +193,7 @@ fn test_send() {
.unwrap()
);

let rate = Rate::Local(LocalRate {
let rate = vec![Rate::Local(LocalRate {
rate_type: LocalRateType::Additive,
recipients: vec![Recipient {
address: AndrAddr::from_string("rates_recipient".to_string()),
Expand All @@ -204,7 +204,7 @@ fn test_send() {
percent: Decimal::percent(10),
}),
description: None,
});
})];

// Set rates
ADOContract::default()
Expand Down
8 changes: 4 additions & 4 deletions contracts/non-fungible-tokens/andromeda-auction/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ impl MockAuction {
app: &mut MockApp,
sender: Addr,
action: String,
rate: Rate,
rates: Vec<Rate>,
) -> ExecuteResult {
self.execute(app, &mock_set_rate_msg(action, rate), sender, &[])
self.execute(app, &mock_set_rate_msg(action, rates), sender, &[])
}

pub fn execute_set_permission(
Expand Down Expand Up @@ -229,8 +229,8 @@ pub fn mock_update_auction(
}
}

pub fn mock_set_rate_msg(action: String, rate: Rate) -> ExecuteMsg {
ExecuteMsg::Rates(RatesMessage::SetRate { action, rate })
pub fn mock_set_rate_msg(action: String, rates: Vec<Rate>) -> ExecuteMsg {
ExecuteMsg::Rates(RatesMessage::SetRate { action, rates })
}

pub fn mock_set_permission(actor: AndrAddr, action: String, permission: Permission) -> ExecuteMsg {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,7 @@ fn execute_claim_with_tax() {
let _res = init(deps.as_mut());
let tax_recipient = "tax_recipient";

let rate: Rate = Rate::Local(LocalRate {
let rate = vec![Rate::Local(LocalRate {
rate_type: LocalRateType::Additive,
recipients: vec![Recipient {
address: AndrAddr::from_string(tax_recipient.to_string()),
Expand All @@ -1247,7 +1247,7 @@ fn execute_claim_with_tax() {
}],
value: LocalRateValue::Flat(coin(20_u128, "uusd")),
description: None,
});
})];

// Set rates
ADOContract::default()
Expand Down Expand Up @@ -1325,7 +1325,7 @@ fn execute_claim_with_royalty() {
let _res = init(deps.as_mut());
let royalty_recipient = "royalty_recipient";

let rate: Rate = Rate::Local(LocalRate {
let rate = vec![Rate::Local(LocalRate {
rate_type: LocalRateType::Deductive,
recipients: vec![Recipient {
address: AndrAddr::from_string(royalty_recipient.to_string()),
Expand All @@ -1334,7 +1334,7 @@ fn execute_claim_with_royalty() {
}],
value: LocalRateValue::Flat(coin(20_u128, "uusd")),
description: None,
});
})];

// Set rates
ADOContract::default()
Expand Down Expand Up @@ -1487,7 +1487,7 @@ fn execute_claim_cw20_with_tax() {
let mut env = mock_env();
let _res = init_cw20(deps.as_mut(), None);
let tax_recipient = "tax_recipient";
let rate: Rate = Rate::Local(LocalRate {
let rate = vec![Rate::Local(LocalRate {
rate_type: LocalRateType::Additive,
recipients: vec![Recipient {
address: AndrAddr::from_string(tax_recipient.to_string()),
Expand All @@ -1498,7 +1498,7 @@ fn execute_claim_cw20_with_tax() {
percent: Decimal::percent(20),
}),
description: None,
});
})];

// Set rates
ADOContract::default()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ impl MockMarketplace {
app: &mut MockApp,
sender: Addr,
action: impl Into<String>,
rate: Rate,
rates: Vec<Rate>,
) -> ExecuteResult {
self.execute(app, &mock_set_rates(action, rate), sender, &[])
self.execute(app, &mock_set_rates(action, rates), sender, &[])
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -162,10 +162,10 @@ pub fn mock_buy_token(token_address: impl Into<String>, token_id: impl Into<Stri
}
}

pub fn mock_set_rates(action: impl Into<String>, rate: Rate) -> ExecuteMsg {
pub fn mock_set_rates(action: impl Into<String>, rates: Vec<Rate>) -> ExecuteMsg {
ExecuteMsg::Rates(RatesMessage::SetRate {
action: action.into(),
rate,
rates,
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ fn test_execute_buy_with_tax_and_royalty_insufficient_funds() {
start_sale(deps.as_mut(), Asset::NativeToken("uusd".to_string()));
assert_sale_created(deps.as_ref(), mock_env(), "uusd".to_string(), false);

let rate = Rate::Local(LocalRate {
let rate = vec![Rate::Local(LocalRate {
rate_type: LocalRateType::Additive,
recipients: vec![Recipient {
address: AndrAddr::from_string("tax_recipient".to_string()),
Expand All @@ -666,7 +666,7 @@ fn test_execute_buy_with_tax_and_royalty_insufficient_funds() {
percent: Decimal::percent(50),
}),
description: None,
});
})];

// Set rates
ADOContract::default()
Expand Down Expand Up @@ -710,7 +710,7 @@ fn test_execute_buy_with_tax_and_royalty_insufficient_funds_cw20() {
uses_cw20,
);

let rate = Rate::Local(LocalRate {
let rate = vec![Rate::Local(LocalRate {
rate_type: LocalRateType::Additive,
recipients: vec![Recipient {
address: AndrAddr::from_string("tax_recipient".to_string()),
Expand All @@ -721,7 +721,7 @@ fn test_execute_buy_with_tax_and_royalty_insufficient_funds_cw20() {
percent: Decimal::percent(50),
}),
description: None,
});
})];

// Set rates
ADOContract::default()
Expand Down Expand Up @@ -787,7 +787,7 @@ fn test_execute_buy_with_tax_and_royalty_works() {
token_address: MOCK_TOKEN_ADDR.to_string(),
};

let rate = Rate::Local(LocalRate {
let rate = vec![Rate::Local(LocalRate {
rate_type: LocalRateType::Additive,
recipients: vec![Recipient {
address: AndrAddr::from_string("tax_recipient".to_string()),
Expand All @@ -798,7 +798,7 @@ fn test_execute_buy_with_tax_and_royalty_works() {
percent: Decimal::percent(50),
}),
description: None,
});
})];

// Set rates
ADOContract::default()
Expand Down
4 changes: 2 additions & 2 deletions packages/std/src/ado_base/rates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Default for RatesResponse {

#[cw_serde]
pub enum RatesMessage {
SetRate { action: String, rate: Rate },
SetRate { action: String, rates: Vec<Rate> },
RemoveRate { action: String },
}

Expand Down Expand Up @@ -248,5 +248,5 @@ pub fn calculate_fee(fee_rate: LocalRateValue, payment: &Coin) -> Result<Coin, C

#[cw_serde]
pub struct AllRatesResponse {
pub all_rates: Vec<(String, Rate)>,
pub all_rates: Vec<(String, Vec<Rate>)>,
}
8 changes: 7 additions & 1 deletion packages/std/src/ado_contract/execute.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::ado_base::rates::RatesMessage;
use crate::ado_contract::ADOContract;
use crate::amp::addresses::AndrAddr;
use crate::amp::messages::AMPPkt;
Expand Down Expand Up @@ -100,7 +101,12 @@ impl<'a> ADOContract<'a> {
self.execute_update_app_contract(ctx.deps, ctx.info, address, None)
}
#[cfg(feature = "rates")]
AndromedaMsg::Rates(rates_message) => self.execute_rates(ctx, rates_message),
AndromedaMsg::Rates(rates_message) => match rates_message {
RatesMessage::SetRate { action, rates } => {
self.execute_set_rates(ctx, action, rates)
}
RatesMessage::RemoveRate { action } => self.execute_remove_rates(ctx, action),
},
AndromedaMsg::UpdateKernelAddress { address } => {
self.update_kernel_address(ctx.deps, ctx.info, address)
}
Expand Down
Loading
Loading