From 7bed45c4a718d5389e2b3bf5476d1ec9d7746264 Mon Sep 17 00:00:00 2001 From: Joe Monem Date: Mon, 15 Jul 2024 15:51:50 +0300 Subject: [PATCH 1/6] feat: store vector of rates in ado contract --- Cargo.lock | 2 +- packages/std/src/ado_base/rates.rs | 2 +- packages/std/src/ado_contract/rates.rs | 68 ++++++++++++++++---------- packages/std/src/ado_contract/state.rs | 2 +- 4 files changed, 44 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b462121a7..3244e4268 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -63,7 +63,7 @@ dependencies = [ [[package]] name = "andromeda-app-contract" -version = "1.1.1" +version = "1.1.0" dependencies = [ "andromeda-app", "andromeda-std", diff --git a/packages/std/src/ado_base/rates.rs b/packages/std/src/ado_base/rates.rs index b5dd9f159..7137177dd 100644 --- a/packages/std/src/ado_base/rates.rs +++ b/packages/std/src/ado_base/rates.rs @@ -28,7 +28,7 @@ impl Default for RatesResponse { #[cw_serde] pub enum RatesMessage { - SetRate { action: String, rate: Rate }, + SetRate { action: String, rates: Vec }, RemoveRate { action: String }, } diff --git a/packages/std/src/ado_contract/rates.rs b/packages/std/src/ado_contract/rates.rs index 56112c6ee..f26e1a87f 100644 --- a/packages/std/src/ado_contract/rates.rs +++ b/packages/std/src/ado_contract/rates.rs @@ -5,13 +5,14 @@ use crate::common::context::ExecuteContext; use crate::common::Funds; use crate::error::ContractError; use crate::os::aos_querier::AOSQuerier; +use cosmwasm_std::Uint128; use cosmwasm_std::{coin as create_coin, ensure, Coin, Deps, Response, Storage}; use cw20::Cw20Coin; use cw_storage_plus::Map; use super::ADOContract; -pub fn rates<'a>() -> Map<'a, &'a str, Rate> { +pub fn rates<'a>() -> Map<'a, &'a str, Vec> { Map::new("rates") } @@ -21,10 +22,10 @@ impl<'a> ADOContract<'a> { &self, store: &mut dyn Storage, action: impl Into, - rate: Rate, + rates: Vec, ) -> Result<(), ContractError> { let action: String = action.into(); - self.rates.save(store, &action, &rate)?; + self.rates.save(store, &action, &rates)?; Ok(()) } pub fn execute_rates( @@ -33,7 +34,7 @@ impl<'a> ADOContract<'a> { rates_message: RatesMessage, ) -> Result { match rates_message { - RatesMessage::SetRate { action, rate } => self.execute_set_rates(ctx, action, rate), + RatesMessage::SetRate { action, rates } => self.execute_set_rates(ctx, action, rates), RatesMessage::RemoveRate { action } => self.execute_remove_rates(ctx, action), } } @@ -41,7 +42,7 @@ impl<'a> ADOContract<'a> { &self, ctx: ExecuteContext, action: impl Into, - rate: Rate, + rates: Vec, ) -> Result { ensure!( Self::is_contract_owner(self, ctx.deps.storage, ctx.info.sender.as_str())?, @@ -49,8 +50,10 @@ impl<'a> ADOContract<'a> { ); let action: String = action.into(); // Validate rates - rate.validate_rate(ctx.deps.as_ref())?; - self.set_rates(ctx.deps.storage, action, rate)?; + for rate in &rates { + rate.validate_rate(ctx.deps.as_ref())?; + } + self.set_rates(ctx.deps.storage, action, rates)?; Ok(Response::default().add_attributes(vec![("action", "set_rates")])) } @@ -85,7 +88,7 @@ impl<'a> ADOContract<'a> { &self, deps: Deps, action: impl Into, - ) -> Result, ContractError> { + ) -> Result>, ContractError> { let action: String = action.into(); Ok(rates().may_load(deps.storage, &action)?) } @@ -99,7 +102,7 @@ impl<'a> ADOContract<'a> { let action: String = action.into(); let rate = self.rates.may_load(deps.storage, &action)?; match rate { - Some(rate) => { + Some(rates) => { let (coin, is_native): (Coin, bool) = match funds { Funds::Native(coin) => { ensure!( @@ -123,29 +126,40 @@ impl<'a> ADOContract<'a> { ) } }; - let (msgs, events, leftover_funds) = match rate { - Rate::Local(local_rate) => { - local_rate.generate_response(deps, coin.clone(), is_native)? - } - Rate::Contract(rates_address) => { - // Query rates contract - let addr = rates_address.get_raw_address(&deps)?; - let rate = AOSQuerier::get_rate(&deps.querier, &addr, &action)?; - rate.generate_response(deps, coin.clone(), is_native)? - } - }; - + let mut all_msgs = vec![]; + let mut all_events = vec![]; + let mut all_leftover_funds = vec![]; + for rate in rates { + let (mut msgs, mut events, mut leftover_funds) = match rate { + Rate::Local(local_rate) => { + local_rate.generate_response(deps, coin.clone(), is_native)? + } + Rate::Contract(rates_address) => { + // Query rates contract + let addr = rates_address.get_raw_address(&deps)?; + let rate = AOSQuerier::get_rate(&deps.querier, &addr, &action)?; + rate.generate_response(deps, coin.clone(), is_native)? + } + }; + all_msgs.append(&mut msgs); + all_events.append(&mut events); + all_leftover_funds.append(&mut leftover_funds); + } + let total_funds: Uint128 = all_leftover_funds.iter().map(|x| x.amount).sum(); Ok(Some(RatesResponse { - msgs, + msgs: all_msgs, leftover_funds: if is_native { - Funds::Native(leftover_funds[0].clone()) + Funds::Native(Coin { + denom: coin.denom, + amount: total_funds, + }) } else { Funds::Cw20(Cw20Coin { - amount: leftover_funds[0].amount, + amount: total_funds, address: coin.denom, }) }, - events, + events: all_events, })) } None => Ok(None), @@ -179,7 +193,7 @@ mod tests { .save(deps.as_mut().storage, &Addr::unchecked("owner")) .unwrap(); - let expected_rate = Rate::Local(LocalRate { + let expected_rate = vec![Rate::Local(LocalRate { rate_type: LocalRateType::Additive, recipients: vec![Recipient { address: AndrAddr::from_string("owner".to_string()), @@ -188,7 +202,7 @@ mod tests { }], value: LocalRateValue::Flat(coin(100_u128, "uandr")), description: None, - }); + })]; let action = "deposit"; // set rates diff --git a/packages/std/src/ado_contract/state.rs b/packages/std/src/ado_contract/state.rs index c3f8ab98e..3ee0a34a0 100644 --- a/packages/std/src/ado_contract/state.rs +++ b/packages/std/src/ado_contract/state.rs @@ -13,7 +13,7 @@ pub struct ADOContract<'a> { pub(crate) permissioned_actions: Map<'a, String, bool>, #[cfg(feature = "rates")] /// Mapping of action to rate - pub rates: Map<'a, &'a str, Rate>, + pub rates: Map<'a, &'a str, Vec>, } impl<'a> Default for ADOContract<'a> { From cf456839fd4f64fd770bf739639409a3098d9340 Mon Sep 17 00:00:00 2001 From: Joe Monem Date: Tue, 16 Jul 2024 11:57:06 +0300 Subject: [PATCH 2/6] fix: adapt contracts to use vector of rates --- .../andromeda-primitive/src/execute.rs | 17 ++++++++++------- .../andromeda-primitive/src/mock.rs | 8 ++++---- .../andromeda-primitive/src/testing/tests.rs | 8 ++++---- .../fungible-tokens/andromeda-cw20/src/mock.rs | 8 ++++---- .../andromeda-cw20/src/testing/tests.rs | 8 ++++---- .../andromeda-auction/src/mock.rs | 8 ++++---- .../andromeda-auction/src/testing/tests.rs | 12 ++++++------ .../andromeda-marketplace/src/mock.rs | 8 ++++---- .../andromeda-marketplace/src/testing/tests.rs | 12 ++++++------ tests-integration/tests/auction_app.rs | 4 ++-- tests-integration/tests/cw20_app.rs | 4 ++-- tests-integration/tests/marketplace_app.rs | 8 +++++--- tests-integration/tests/primitive.rs | 8 ++++---- 13 files changed, 59 insertions(+), 54 deletions(-) diff --git a/contracts/data-storage/andromeda-primitive/src/execute.rs b/contracts/data-storage/andromeda-primitive/src/execute.rs index 35d9eaf01..0f0cd611d 100644 --- a/contracts/data-storage/andromeda-primitive/src/execute.rs +++ b/contracts/data-storage/andromeda-primitive/src/execute.rs @@ -34,14 +34,17 @@ pub fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result 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) => { + 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), diff --git a/contracts/data-storage/andromeda-primitive/src/mock.rs b/contracts/data-storage/andromeda-primitive/src/mock.rs index 2243472de..950ac3cbc 100644 --- a/contracts/data-storage/andromeda-primitive/src/mock.rs +++ b/contracts/data-storage/andromeda-primitive/src/mock.rs @@ -61,9 +61,9 @@ impl MockPrimitive { app: &mut MockApp, sender: Addr, action: String, - rate: Rate, + rates: Vec, ) -> 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) -> GetValueResponse { @@ -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) -> ExecuteMsg { + ExecuteMsg::Rates(RatesMessage::SetRate { action, rates }) } pub fn mock_primitive_get_value(key: Option) -> QueryMsg { diff --git a/contracts/data-storage/andromeda-primitive/src/testing/tests.rs b/contracts/data-storage/andromeda-primitive/src/testing/tests.rs index 99cbe84c9..81d829e30 100644 --- a/contracts/data-storage/andromeda-primitive/src/testing/tests.rs +++ b/contracts/data-storage/andromeda-primitive/src/testing/tests.rs @@ -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( @@ -90,7 +90,7 @@ fn test_set_value_with_tax() { assert_eq!(err, ContractError::InvalidRate {}); - 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()), @@ -99,7 +99,7 @@ fn test_set_value_with_tax() { }], value: LocalRateValue::Flat(coin(20_u128, "uandr")), description: None, - }); + })]; // Set rates ADOContract::default() diff --git a/contracts/fungible-tokens/andromeda-cw20/src/mock.rs b/contracts/fungible-tokens/andromeda-cw20/src/mock.rs index c545a18ac..706ea581f 100644 --- a/contracts/fungible-tokens/andromeda-cw20/src/mock.rs +++ b/contracts/fungible-tokens/andromeda-cw20/src/mock.rs @@ -123,9 +123,9 @@ impl MockCW20 { app: &mut MockApp, sender: Addr, action: String, - rate: Rate, + rates: Vec, ) -> 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) -> Uint128 { @@ -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) -> ExecuteMsg { + ExecuteMsg::Rates(RatesMessage::SetRate { action, rates }) } diff --git a/contracts/fungible-tokens/andromeda-cw20/src/testing/tests.rs b/contracts/fungible-tokens/andromeda-cw20/src/testing/tests.rs index a2faed990..921cc59d7 100644 --- a/contracts/fungible-tokens/andromeda-cw20/src/testing/tests.rs +++ b/contracts/fungible-tokens/andromeda-cw20/src/testing/tests.rs @@ -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()), @@ -98,7 +98,7 @@ fn test_transfer() { percent: Decimal::percent(10), }), description: None, - }); + })]; // Set rates ADOContract::default() @@ -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()), @@ -204,7 +204,7 @@ fn test_send() { percent: Decimal::percent(10), }), description: None, - }); + })]; // Set rates ADOContract::default() diff --git a/contracts/non-fungible-tokens/andromeda-auction/src/mock.rs b/contracts/non-fungible-tokens/andromeda-auction/src/mock.rs index dbb4d6b0f..4f099e7b9 100644 --- a/contracts/non-fungible-tokens/andromeda-auction/src/mock.rs +++ b/contracts/non-fungible-tokens/andromeda-auction/src/mock.rs @@ -107,9 +107,9 @@ impl MockAuction { app: &mut MockApp, sender: Addr, action: String, - rate: Rate, + rates: Vec, ) -> 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( @@ -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) -> ExecuteMsg { + ExecuteMsg::Rates(RatesMessage::SetRate { action, rates }) } pub fn mock_set_permission(actor: AndrAddr, action: String, permission: Permission) -> ExecuteMsg { diff --git a/contracts/non-fungible-tokens/andromeda-auction/src/testing/tests.rs b/contracts/non-fungible-tokens/andromeda-auction/src/testing/tests.rs index dd8c5df1d..cca052f56 100644 --- a/contracts/non-fungible-tokens/andromeda-auction/src/testing/tests.rs +++ b/contracts/non-fungible-tokens/andromeda-auction/src/testing/tests.rs @@ -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()), @@ -1247,7 +1247,7 @@ fn execute_claim_with_tax() { }], value: LocalRateValue::Flat(coin(20_u128, "uusd")), description: None, - }); + })]; // Set rates ADOContract::default() @@ -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()), @@ -1334,7 +1334,7 @@ fn execute_claim_with_royalty() { }], value: LocalRateValue::Flat(coin(20_u128, "uusd")), description: None, - }); + })]; // Set rates ADOContract::default() @@ -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()), @@ -1498,7 +1498,7 @@ fn execute_claim_cw20_with_tax() { percent: Decimal::percent(20), }), description: None, - }); + })]; // Set rates ADOContract::default() diff --git a/contracts/non-fungible-tokens/andromeda-marketplace/src/mock.rs b/contracts/non-fungible-tokens/andromeda-marketplace/src/mock.rs index 9e1b5e219..3b43a0bcd 100644 --- a/contracts/non-fungible-tokens/andromeda-marketplace/src/mock.rs +++ b/contracts/non-fungible-tokens/andromeda-marketplace/src/mock.rs @@ -63,9 +63,9 @@ impl MockMarketplace { app: &mut MockApp, sender: Addr, action: impl Into, - rate: Rate, + rates: Vec, ) -> ExecuteResult { - self.execute(app, &mock_set_rates(action, rate), sender, &[]) + self.execute(app, &mock_set_rates(action, rates), sender, &[]) } #[allow(clippy::too_many_arguments)] @@ -150,10 +150,10 @@ pub fn mock_buy_token(token_address: impl Into, token_id: impl Into, rate: Rate) -> ExecuteMsg { +pub fn mock_set_rates(action: impl Into, rates: Vec) -> ExecuteMsg { ExecuteMsg::Rates(RatesMessage::SetRate { action: action.into(), - rate, + rates, }) } diff --git a/contracts/non-fungible-tokens/andromeda-marketplace/src/testing/tests.rs b/contracts/non-fungible-tokens/andromeda-marketplace/src/testing/tests.rs index a3884e7d1..a8c1b805b 100644 --- a/contracts/non-fungible-tokens/andromeda-marketplace/src/testing/tests.rs +++ b/contracts/non-fungible-tokens/andromeda-marketplace/src/testing/tests.rs @@ -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()), @@ -666,7 +666,7 @@ fn test_execute_buy_with_tax_and_royalty_insufficient_funds() { percent: Decimal::percent(50), }), description: None, - }); + })]; // Set rates ADOContract::default() @@ -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()), @@ -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() @@ -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()), @@ -798,7 +798,7 @@ fn test_execute_buy_with_tax_and_royalty_works() { percent: Decimal::percent(50), }), description: None, - }); + })]; // Set rates ADOContract::default() diff --git a/tests-integration/tests/auction_app.rs b/tests-integration/tests/auction_app.rs index 8921c45aa..e159f26d7 100644 --- a/tests-integration/tests/auction_app.rs +++ b/tests-integration/tests/auction_app.rs @@ -117,7 +117,7 @@ fn test_auction_app_modules() { &mut router, owner.clone(), "AuctionClaim".to_string(), - Rate::Local(LocalRate { + vec![Rate::Local(LocalRate { rate_type: LocalRateType::Deductive, recipients: vec![ Recipient::new(recipient_one, None), @@ -127,7 +127,7 @@ fn test_auction_app_modules() { percent: Decimal::percent(25), }), description: None, - }), + })], ) .unwrap(); diff --git a/tests-integration/tests/cw20_app.rs b/tests-integration/tests/cw20_app.rs index 65d7155ec..505f3bb90 100644 --- a/tests-integration/tests/cw20_app.rs +++ b/tests-integration/tests/cw20_app.rs @@ -99,7 +99,7 @@ fn test_cw20_with_rates() { &mut router, owner.clone(), "Cw20TransferFrom".to_string(), - Rate::Local(LocalRate { + vec![Rate::Local(LocalRate { rate_type: LocalRateType::Deductive, recipients: vec![ Recipient::new(recipient_one, None), @@ -109,7 +109,7 @@ fn test_cw20_with_rates() { percent: Decimal::percent(10), }), description: None, - }), + })], ) .unwrap(); diff --git a/tests-integration/tests/marketplace_app.rs b/tests-integration/tests/marketplace_app.rs index a78236472..c2b0aa451 100644 --- a/tests-integration/tests/marketplace_app.rs +++ b/tests-integration/tests/marketplace_app.rs @@ -1,5 +1,7 @@ #![cfg(not(target_arch = "wasm32"))] +use std::vec; + use andromeda_address_list::mock::{ mock_address_list_instantiate_msg, mock_andromeda_address_list, MockAddressList, }; @@ -135,7 +137,7 @@ fn test_marketplace_app() { &mut router, owner.clone(), "MarketplaceBuy", - Rate::Contract(AndrAddr::from_string(rates.addr())), + vec![Rate::Contract(AndrAddr::from_string(rates.addr()))], ) .unwrap(); @@ -563,7 +565,7 @@ fn test_marketplace_app_cw20_restricted() { &mut router, owner.clone(), "MarketplaceBuy", - Rate::Contract(AndrAddr::from_string(rates.addr())), + vec![Rate::Contract(AndrAddr::from_string(rates.addr()))], ) .unwrap(); @@ -861,7 +863,7 @@ fn test_marketplace_app_cw20_unrestricted() { &mut router, owner.clone(), "MarketplaceBuy", - Rate::Contract(AndrAddr::from_string(rates.addr())), + vec![Rate::Contract(AndrAddr::from_string(rates.addr()))], ) .unwrap(); diff --git a/tests-integration/tests/primitive.rs b/tests-integration/tests/primitive.rs index 6af2aad23..55a8a0103 100644 --- a/tests-integration/tests/primitive.rs +++ b/tests-integration/tests/primitive.rs @@ -84,14 +84,14 @@ fn test_primitive() { &mut router, owner.clone(), "PrimitiveSetValue".to_string(), - Rate::Local(LocalRate { + vec![Rate::Local(LocalRate { rate_type: LocalRateType::Deductive, recipients: vec![Recipient::new(recipient_one, None)], value: LocalRateValue::Percent(PercentRate { percent: Decimal::percent(25), }), description: None, - }), + })], ) .unwrap_err() .downcast() @@ -104,12 +104,12 @@ fn test_primitive() { &mut router, owner.clone(), "PrimitiveSetValue".to_string(), - Rate::Local(LocalRate { + vec![Rate::Local(LocalRate { rate_type: LocalRateType::Deductive, recipients: vec![Recipient::new(recipient_one, None)], value: LocalRateValue::Flat(coin(10_u128, "uandr")), description: None, - }), + })], ) .unwrap(); From 6d63b307d842ff18453a6e2fc9086aca3e48aa81 Mon Sep 17 00:00:00 2001 From: Joe Monem Date: Thu, 18 Jul 2024 11:36:14 +0300 Subject: [PATCH 3/6] fix: handle total remaining funds for multiple rates, test multiple rates in auction integration test --- packages/std/src/ado_contract/rates.rs | 6 +++- tests-integration/tests/auction_app.rs | 39 +++++++++++++++++--------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/packages/std/src/ado_contract/rates.rs b/packages/std/src/ado_contract/rates.rs index f26e1a87f..5f792c0ac 100644 --- a/packages/std/src/ado_contract/rates.rs +++ b/packages/std/src/ado_contract/rates.rs @@ -145,7 +145,11 @@ impl<'a> ADOContract<'a> { all_events.append(&mut events); all_leftover_funds.append(&mut leftover_funds); } - let total_funds: Uint128 = all_leftover_funds.iter().map(|x| x.amount).sum(); + let total_dedcuted_funds: Uint128 = all_leftover_funds + .iter() + .map(|x| coin.amount - x.amount) + .sum(); + let total_funds = coin.amount.checked_sub(total_dedcuted_funds)?; Ok(Some(RatesResponse { msgs: all_msgs, leftover_funds: if is_native { diff --git a/tests-integration/tests/auction_app.rs b/tests-integration/tests/auction_app.rs index e159f26d7..55b0bd964 100644 --- a/tests-integration/tests/auction_app.rs +++ b/tests-integration/tests/auction_app.rs @@ -117,17 +117,30 @@ fn test_auction_app_modules() { &mut router, owner.clone(), "AuctionClaim".to_string(), - vec![Rate::Local(LocalRate { - rate_type: LocalRateType::Deductive, - recipients: vec![ - Recipient::new(recipient_one, None), - Recipient::new(recipient_two, None), - ], - value: LocalRateValue::Percent(PercentRate { - percent: Decimal::percent(25), + vec![ + Rate::Local(LocalRate { + rate_type: LocalRateType::Deductive, + recipients: vec![ + Recipient::new(recipient_one, None), + Recipient::new(recipient_two, None), + ], + value: LocalRateValue::Percent(PercentRate { + percent: Decimal::percent(25), + }), + description: None, }), - description: None, - })], + Rate::Local(LocalRate { + rate_type: LocalRateType::Deductive, + recipients: vec![ + Recipient::new(recipient_one, None), + Recipient::new(recipient_two, None), + ], + value: LocalRateValue::Percent(PercentRate { + percent: Decimal::percent(10), + }), + description: None, + }), + ], ) .unwrap(); @@ -222,11 +235,11 @@ fn test_auction_app_modules() { let token_owner = cw721.query_owner_of(&router, "0"); assert_eq!(token_owner, buyer_two); let owner_balance = router.wrap().query_balance(owner, "uandr").unwrap(); - assert_eq!(owner_balance.amount, Uint128::from(50u128)); + assert_eq!(owner_balance.amount, Uint128::from(30u128)); let recipient_one_balance = router.wrap().query_balance(recipient_one, "uandr").unwrap(); - assert_eq!(recipient_one_balance.amount, Uint128::from(25u128)); + assert_eq!(recipient_one_balance.amount, Uint128::from(35u128)); let recipient_two_balance = router.wrap().query_balance(recipient_two, "uandr").unwrap(); - assert_eq!(recipient_two_balance.amount, Uint128::from(25u128)); + assert_eq!(recipient_two_balance.amount, Uint128::from(35u128)); } #[test] From 2b6da664fb3b9ab15fbc79b2c74f64c7e73551b2 Mon Sep 17 00:00:00 2001 From: Joe Monem Date: Tue, 13 Aug 2024 16:10:13 +0300 Subject: [PATCH 4/6] fix: build error potentially from match statement --- packages/std/src/ado_contract/execute.rs | 7 +------ packages/std/src/ado_contract/rates.rs | 12 +++++++++++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/std/src/ado_contract/execute.rs b/packages/std/src/ado_contract/execute.rs index da170f7a5..f7623ede1 100644 --- a/packages/std/src/ado_contract/execute.rs +++ b/packages/std/src/ado_contract/execute.rs @@ -101,12 +101,7 @@ impl<'a> ADOContract<'a> { self.execute_update_app_contract(ctx.deps, ctx.info, address, None) } #[cfg(feature = "rates")] - 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::Rates(rates_message) => self.execute_rates(ctx, rates_message), AndromedaMsg::UpdateKernelAddress { address } => { self.update_kernel_address(ctx.deps, ctx.info, address) } diff --git a/packages/std/src/ado_contract/rates.rs b/packages/std/src/ado_contract/rates.rs index 34eab0498..ed0624386 100644 --- a/packages/std/src/ado_contract/rates.rs +++ b/packages/std/src/ado_contract/rates.rs @@ -1,4 +1,4 @@ -use crate::ado_base::rates::{AllRatesResponse, Rate, RatesResponse}; +use crate::ado_base::rates::{AllRatesResponse, Rate, RatesMessage, RatesResponse}; use crate::amp::Recipient; use crate::common::{context::ExecuteContext, Funds}; use crate::error::ContractError; @@ -15,6 +15,16 @@ pub fn rates<'a>() -> Map<'a, &'a str, Vec> { } impl<'a> ADOContract<'a> { + pub fn execute_rates( + &self, + ctx: ExecuteContext, + rates_message: RatesMessage, + ) -> Result { + match rates_message { + RatesMessage::SetRate { action, rates } => self.execute_set_rates(ctx, action, rates), + RatesMessage::RemoveRate { action } => self.execute_remove_rates(ctx, action), + } + } /// Sets rates pub fn set_rates( &self, From d07a9ed9834fabc0affe1d1196017e1bf3bf1f40 Mon Sep 17 00:00:00 2001 From: Joe Monem Date: Tue, 13 Aug 2024 16:12:04 +0300 Subject: [PATCH 5/6] linting --- packages/std/src/ado_contract/execute.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/std/src/ado_contract/execute.rs b/packages/std/src/ado_contract/execute.rs index f7623ede1..5a56b0724 100644 --- a/packages/std/src/ado_contract/execute.rs +++ b/packages/std/src/ado_contract/execute.rs @@ -1,4 +1,3 @@ -use crate::ado_base::rates::RatesMessage; use crate::ado_contract::ADOContract; use crate::amp::addresses::AndrAddr; use crate::amp::messages::AMPPkt; From 3730fe240274a73217c5dedb68705651b64e5ee4 Mon Sep 17 00:00:00 2001 From: Joe Monem Date: Tue, 13 Aug 2024 16:24:56 +0300 Subject: [PATCH 6/6] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 691b5fe16..190cc9440 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Crowdfund Internal Audit [(#485)](https://github.com/andromedaprotocol/andromeda-core/pull/485) - Auction: Minimum Raise [(#486)](https://github.com/andromedaprotocol/andromeda-core/pull/486) - Version Bump [(#488)](https://github.com/andromedaprotocol/andromeda-core/pull/488) +- Multiple Rates [(#517)](https://github.com/andromedaprotocol/andromeda-core/pull/517) ### Fixed