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

Removed Rates module from Point ADO #761

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Weighted Splitter: Replace Recipient with AndrAddr in RemoveRecipient and GetUserWeight [(#739)](https://github.com/andromedaprotocol/andromeda-core/pull/739)
- feat: IBC packet tracking adjustments [#748](https://github.com/andromedaprotocol/andromeda-core/pull/748)
- ref: Rename Set Amount Splitter to Fixed Amount Splitter [(#754)](https://github.com/andromedaprotocol/andromeda-core/pull/754)
- Point ADO: remove Rates module from the contract[(#761)](https://github.com/andromedaprotocol/andromeda-core/pull/761)

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion contracts/math/andromeda-point/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ cosmwasm-schema = { workspace = true }
cw-storage-plus = { workspace = true }
cw-utils = { workspace = true }

andromeda-std = { workspace = true, features = ["rates"] }
andromeda-std = { workspace = true }
andromeda-math = { workspace = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
94 changes: 13 additions & 81 deletions contracts/math/andromeda-point/src/execute.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use andromeda_math::point::{ExecuteMsg, PointCoordinate, PointRestriction};
use andromeda_std::{
ado_base::rates::{Rate, RatesMessage},
ado_contract::ADOContract,
common::{actions::call_action, context::ExecuteContext, rates::get_tax_amount, Funds},
common::{actions::call_action, context::ExecuteContext},
error::ContractError,
};
use cosmwasm_std::{
coin, ensure, BankMsg, Coin, CosmosMsg, Deps, MessageInfo, Response, SubMsg, Uint128,
};
use cosmwasm_std::{ensure, Response};
use cw_utils::nonpayable;

use crate::{
Expand All @@ -16,32 +13,25 @@ use crate::{
};

pub fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Response, ContractError> {
let action = msg.as_ref().to_string();
call_action(
let action_response = call_action(
&mut ctx.deps,
&ctx.info,
&ctx.env,
&ctx.amp_ctx,
msg.as_ref(),
)?;

match msg.clone() {
let res = match msg.clone() {
ExecuteMsg::UpdateRestriction { restriction } => update_restriction(ctx, restriction),
ExecuteMsg::SetPoint { point } => set_point(ctx, point, action),
ExecuteMsg::SetPoint { point } => set_point(ctx, point),
ExecuteMsg::DeletePoint {} => delete_point(ctx),
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)
}
Rate::Contract(_) => ADOContract::default().execute(ctx, msg),
},
RatesMessage::RemoveRate { .. } => ADOContract::default().execute(ctx, msg),
},
_ => ADOContract::default().execute(ctx, msg),
}
}?;

Ok(res
.add_submessages(action_response.messages)
.add_attributes(action_response.attributes)
.add_events(action_response.events))
}

pub fn update_restriction(
Expand All @@ -60,40 +50,23 @@ pub fn update_restriction(
.add_attribute("sender", sender))
}

pub fn set_point(
ctx: ExecuteContext,
point: PointCoordinate,
action: String,
) -> Result<Response, ContractError> {
pub fn set_point(ctx: ExecuteContext, point: PointCoordinate) -> Result<Response, ContractError> {
let sender = ctx.info.sender.clone();
ensure!(
has_permission(ctx.deps.storage, &sender)?,
ContractError::Unauthorized {}
);

let tax_response = tax_set_value(ctx.deps.as_ref(), &ctx.info, action)?;

point.validate()?;

DATA.save(ctx.deps.storage, &point.clone())?;
DATA_OWNER.save(ctx.deps.storage, &sender)?;

let mut response = Response::new()
let response = Response::new()
.add_attribute("method", "set_point")
.add_attribute("sender", sender)
.add_attribute("point", format!("{point:?}"));

if let Some(tax_response) = tax_response {
response = response.add_submessages(tax_response.1);
let refund = tax_response.0.try_get_coin()?;
if !refund.amount.is_zero() {
return Ok(response.add_message(CosmosMsg::Bank(BankMsg::Send {
to_address: ctx.info.sender.into_string(),
amount: vec![refund],
})));
}
}

Ok(response)
}

Expand All @@ -110,44 +83,3 @@ pub fn delete_point(ctx: ExecuteContext) -> Result<Response, ContractError> {
.add_attribute("method", "delete_point")
.add_attribute("sender", sender))
}

fn tax_set_value(
deps: Deps,
info: &MessageInfo,
action: String,
) -> Result<Option<(Funds, Vec<SubMsg>)>, ContractError> {
let default_coin = coin(0_u128, "uandr".to_string());
let sent_funds = info.funds.first().unwrap_or(&default_coin);

let transfer_response = ADOContract::default().query_deducted_funds(
deps,
action,
Funds::Native(sent_funds.clone()),
)?;

if let Some(transfer_response) = transfer_response {
let remaining_funds = transfer_response.leftover_funds.try_get_coin()?;
let tax_amount = get_tax_amount(
&transfer_response.msgs,
remaining_funds.amount,
remaining_funds.amount,
);

let refund = if sent_funds.amount > tax_amount {
sent_funds.amount.checked_sub(tax_amount)?
} else {
Uint128::zero()
};

let after_tax_payment = Coin {
denom: remaining_funds.denom,
amount: refund,
};
Ok(Some((
Funds::Native(after_tax_payment),
transfer_response.msgs,
)))
} else {
Ok(None)
}
}
15 changes: 0 additions & 15 deletions contracts/math/andromeda-point/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::contract::{execute, instantiate, query};
use andromeda_math::point::{
ExecuteMsg, GetDataOwnerResponse, InstantiateMsg, PointCoordinate, PointRestriction, QueryMsg,
};
use andromeda_std::ado_base::rates::{Rate, RatesMessage};
use andromeda_testing::mock::MockApp;
use andromeda_testing::{
mock_ado,
Expand Down Expand Up @@ -53,16 +52,6 @@ impl MockPoint {
}
}

pub fn execute_add_rate(
&self,
app: &mut MockApp,
sender: Addr,
action: String,
rate: Rate,
) -> ExecuteResult {
self.execute(app, &mock_set_rate_msg(action, rate), sender, &[])
}

pub fn query_point(&self, app: &mut MockApp) -> PointCoordinate {
let msg = mock_point_get_point();
let res: PointCoordinate = self.query(app, msg);
Expand Down Expand Up @@ -98,10 +87,6 @@ pub fn mock_point_set_point_msg(point: PointCoordinate) -> ExecuteMsg {
ExecuteMsg::SetPoint { point }
}

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

pub fn mock_point_get_point() -> QueryMsg {
QueryMsg::GetPoint {}
}
Expand Down
15 changes: 1 addition & 14 deletions contracts/math/andromeda-point/src/testing/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use andromeda_std::{
use cosmwasm_std::{
from_json,
testing::{mock_env, mock_info, MockApi, MockStorage},
Coin, Deps, DepsMut, MessageInfo, OwnedDeps, Response,
Deps, DepsMut, MessageInfo, OwnedDeps, Response,
};

use crate::contract::{execute, instantiate, query};
Expand Down Expand Up @@ -48,19 +48,6 @@ pub fn set_point(
execute(deps, mock_env(), info, msg)
}

pub fn set_point_with_funds(
deps: DepsMut<'_>,
point: &PointCoordinate,
sender: &str,
coin: Coin,
) -> Result<Response, ContractError> {
let msg = ExecuteMsg::SetPoint {
point: point.clone(),
};
let info = mock_info(sender, &[coin]);
execute(deps, mock_env(), info, msg)
}

pub fn delete_point(deps: DepsMut<'_>, sender: &str) -> Result<Response, ContractError> {
let msg = ExecuteMsg::DeletePoint {};
let info = mock_info(sender, &[]);
Expand Down
125 changes: 7 additions & 118 deletions contracts/math/andromeda-point/src/testing/tests.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
use crate::contract::{execute, query};
use andromeda_math::point::{
ExecuteMsg, GetDataOwnerResponse, PointCoordinate, PointRestriction, QueryMsg,
};
use cosmwasm_std::{
coin, from_json, testing::mock_env, BankMsg, CosmosMsg, Decimal, Response, SubMsg,
};

use andromeda_std::{
ado_base::rates::{LocalRate, LocalRateType, LocalRateValue, PercentRate, Rate, RatesMessage},
ado_contract::ADOContract,
amp::{AndrAddr, Recipient},
error::ContractError,
};

use super::mock::{
delete_point, proper_initialization, query_point, set_point, set_point_with_funds,
};
use crate::contract::query;
use andromeda_math::point::{GetDataOwnerResponse, PointCoordinate, PointRestriction, QueryMsg};
use cosmwasm_std::{from_json, testing::mock_env};

use andromeda_std::{amp::AndrAddr, error::ContractError};

use super::mock::{delete_point, proper_initialization, query_point, set_point};

#[test]
fn test_instantiation() {
Expand All @@ -42,106 +31,6 @@ fn test_set_and_update_point() {
assert_eq!(point, query_res);
}

#[test]
fn test_set_point_with_tax() {
let (mut deps, info) = proper_initialization(PointRestriction::Private);
let point = PointCoordinate::from_f64(10_f64, 10_f64, Some(10_f64));
point.validate().unwrap();
let tax_recipient = "tax_recipient";

// Set percent rates
let set_percent_rate_msg = ExecuteMsg::Rates(RatesMessage::SetRate {
action: "PointSetPoint".to_string(),
rate: Rate::Local(LocalRate {
rate_type: LocalRateType::Additive,
recipient: Recipient {
address: AndrAddr::from_string(String::default()),
msg: None,
ibc_recovery_address: None,
},
value: LocalRateValue::Percent(PercentRate {
percent: Decimal::one(),
}),
description: None,
}),
});

let err = execute(
deps.as_mut(),
mock_env(),
info.clone(),
set_percent_rate_msg,
)
.unwrap_err();

assert_eq!(err, ContractError::InvalidRate {});

let rate: Rate = Rate::Local(LocalRate {
rate_type: LocalRateType::Additive,
recipient: Recipient {
address: AndrAddr::from_string(tax_recipient.to_string()),
msg: None,
ibc_recovery_address: None,
},
value: LocalRateValue::Flat(coin(20_u128, "uandr")),
description: None,
});

// Set rates
ADOContract::default()
.set_rates(deps.as_mut().storage, "SetPoint", rate)
.unwrap();

// Sent the exact amount required for tax
let res = set_point_with_funds(
deps.as_mut(),
&point,
info.sender.as_ref(),
coin(20_u128, "uandr".to_string()),
)
.unwrap();
let expected_response: Response = Response::new()
.add_submessage(SubMsg::new(CosmosMsg::Bank(BankMsg::Send {
to_address: tax_recipient.to_string(),
amount: vec![coin(20, "uandr")],
})))
.add_attributes(vec![("method", "set_point"), ("sender", "creator")])
.add_attribute("point", format!("{point:?}"));
assert_eq!(expected_response, res);

// Sent less than amount required for tax
let err = set_point_with_funds(
deps.as_mut(),
&point,
info.sender.as_ref(),
coin(19_u128, "uandr".to_string()),
)
.unwrap_err();
assert_eq!(err, ContractError::InsufficientFunds {});

// Sent more than required amount for tax
let res = set_point_with_funds(
deps.as_mut(),
&point,
info.sender.as_ref(),
coin(200_u128, "uandr".to_string()),
)
.unwrap();
let expected_response: Response = Response::new()
.add_submessage(SubMsg::new(CosmosMsg::Bank(BankMsg::Send {
to_address: tax_recipient.to_string(),
amount: vec![coin(20, "uandr")],
})))
// 200 was sent, but the tax is only 20, so we send back the difference
.add_submessage(SubMsg::new(CosmosMsg::Bank(BankMsg::Send {
to_address: "creator".to_string(),
amount: vec![coin(180, "uandr")],
})))
.add_attributes(vec![("method", "set_point"), ("sender", "creator")])
.add_attribute("point", format!("{point:?}"));
assert_eq!(expected_response, res);
}

struct TestHandlePointCoordinate {
name: &'static str,
point_coordinate: PointCoordinate,
Expand Down
6 changes: 4 additions & 2 deletions contracts/os/andromeda-adodb/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,9 +718,11 @@ fn test_all_ado_types() {

let mut code_id = 1;

let ados = [ADOVersion::from_string("[email protected]".to_string()),
let ados = [
ADOVersion::from_string("[email protected]".to_string()),
ADOVersion::from_string("[email protected]".to_string()),
ADOVersion::from_string("[email protected]".to_string())];
ADOVersion::from_string("[email protected]".to_string()),
];

ados.iter().for_each(|ado_version| {
let msg = ExecuteMsg::Publish {
Expand Down
Loading
Loading