Skip to content

Commit

Permalink
ref: Send Economics Call Conditionally (#764)
Browse files Browse the repository at this point in the history
  • Loading branch information
joemonem authored Jan 20, 2025
1 parent aa5ffd4 commit 0f37694
Show file tree
Hide file tree
Showing 26 changed files with 522 additions and 912 deletions.
24 changes: 6 additions & 18 deletions contracts/app/andromeda-app-contract/src/testing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
use crate::state::{ADO_DESCRIPTORS, ADO_IDX};

use super::{contract::*, state::ADO_ADDRESSES};
use crate::state::{ADO_DESCRIPTORS, ADO_IDX};
use andromeda_app::app::{AppComponent, ComponentType, ExecuteMsg, InstantiateMsg};
use andromeda_std::ado_base::ownership::OwnershipMessage;
// use andromeda_std::amp::AndrAddr;
// use andromeda_std::common::reply::ReplyId;
// use andromeda_std::os::vfs::{convert_component_name, ExecuteMsg as VFSExecuteMsg};
use andromeda_std::testing::mock_querier::{
mock_dependencies_custom, MOCK_ANCHOR_CONTRACT, MOCK_CW20_CONTRACT, MOCK_KERNEL_CONTRACT,
};

use andromeda_std::{ado_base::AndromedaMsg, error::ContractError};

use andromeda_testing::economics_msg::generate_economics_message;
use cosmwasm_std::{
attr,
testing::{mock_env, mock_info},
Expand Down Expand Up @@ -332,8 +325,7 @@ fn test_claim_ownership_empty() {
};

let res = execute(deps.as_mut(), env, info, msg).unwrap();
// The message is for the economics contract
assert_eq!(1, res.messages.len());
assert_eq!(0, res.messages.len());
}

#[test]
Expand Down Expand Up @@ -371,8 +363,7 @@ fn test_claim_ownership_all() {
};

let res = execute(deps.as_mut(), env, info, msg).unwrap();
// The 3rd message is for the economics contract
assert_eq!(3, res.messages.len());
assert_eq!(2, res.messages.len());
}

#[test]
Expand Down Expand Up @@ -410,8 +401,7 @@ fn test_claim_ownership() {
};

let res = execute(deps.as_mut(), env, info, msg).unwrap();
// The 2nd message is for the economics contract
assert_eq!(2, res.messages.len());
assert_eq!(1, res.messages.len());

let exec_submsg: SubMsg<Empty> = SubMsg {
id: 200,
Expand All @@ -429,8 +419,7 @@ fn test_claim_ownership() {
};
let expected = Response::new()
.add_submessage(exec_submsg)
.add_attributes(vec![attr("method", "claim_ownership")])
.add_submessage(generate_economics_message("creator", "ClaimOwnership"));
.add_attributes(vec![attr("method", "claim_ownership")]);

assert_eq!(expected, res)
}
Expand Down Expand Up @@ -528,8 +517,7 @@ fn test_proxy_message() {
.add_attributes(vec![
attr("method", "app_message"),
attr("recipient", "token"),
])
.add_submessage(generate_economics_message("creator", "ProxyMessage"));
]);

assert_eq!(expected, res)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::contract::{execute, query};
use andromeda_data_storage::primitive::{
ExecuteMsg, GetValueResponse, Primitive, PrimitiveRestriction, QueryMsg,
};
use andromeda_testing::economics_msg::generate_economics_message;
use cosmwasm_std::{
coin, from_json, testing::mock_env, BankMsg, Binary, CosmosMsg, Decimal, Response, SubMsg,
};
Expand Down Expand Up @@ -161,8 +160,7 @@ fn test_set_value_with_tax() {
("sender", "creator"),
("key", "key"),
])
.add_attribute("value", format!("{value:?}"))
.add_submessage(generate_economics_message("creator", "SetValue"));
.add_attribute("value", format!("{value:?}"));
assert_eq!(expected_response, res);

// Sent less than amount required for tax
Expand Down Expand Up @@ -200,8 +198,7 @@ fn test_set_value_with_tax() {
("sender", "creator"),
("key", "key"),
])
.add_attribute("value", format!("{value:?}"))
.add_submessage(generate_economics_message("creator", "SetValue"));
.add_attribute("value", format!("{value:?}"));
assert_eq!(expected_response, res);
}

Expand Down
178 changes: 83 additions & 95 deletions contracts/finance/andromeda-conditional-splitter/src/testing/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use andromeda_std::{
common::{expiration::Expiry, Milliseconds},
error::ContractError,
};
use andromeda_testing::economics_msg::generate_economics_message;
use cosmwasm_std::{
attr, from_json,
testing::{mock_env, mock_info, MOCK_CONTRACT_ADDR},
Expand Down Expand Up @@ -198,12 +197,10 @@ fn test_execute_update_lock() {
let res = execute(deps.as_mut(), env.clone(), info, msg).unwrap();

assert_eq!(
Response::default()
.add_attributes(vec![
attr("action", "update_lock"),
attr("locked", lock_time.get_time(&env.block).to_string())
])
.add_submessage(generate_economics_message(OWNER, "UpdateLock")),
Response::default().add_attributes(vec![
attr("action", "update_lock"),
attr("locked", lock_time.get_time(&env.block).to_string())
]),
res
);

Expand Down Expand Up @@ -317,9 +314,7 @@ fn test_execute_update_thresholds() {
let info = mock_info(OWNER, &[]);
let res = execute(deps.as_mut(), env, info, msg).unwrap();
assert_eq!(
Response::default()
.add_attributes(vec![attr("action", "update_thresholds")])
.add_submessage(generate_economics_message(OWNER, "UpdateThresholds")),
Response::default().add_attributes(vec![attr("action", "update_thresholds")]),
res
);

Expand Down Expand Up @@ -437,8 +432,7 @@ fn test_execute_send() {
),
amp_msg,
])
.add_attributes(vec![attr("action", "send"), attr("sender", "creator")])
.add_submessage(generate_economics_message(OWNER, "Send"));
.add_attributes(vec![attr("action", "send"), attr("sender", "creator")]);

assert_eq!(res, expected_res);

Expand Down Expand Up @@ -479,8 +473,7 @@ fn test_execute_send() {
),
amp_msg,
])
.add_attributes(vec![attr("action", "send"), attr("sender", "creator")])
.add_submessage(generate_economics_message(OWNER, "Send"));
.add_attributes(vec![attr("action", "send"), attr("sender", "creator")]);

assert_eq!(res, expected_res);

Expand Down Expand Up @@ -513,8 +506,7 @@ fn test_execute_send() {
let expected_res = Response::new()
// No refund for the sender since the percentages add up to 100
.add_submessage(amp_msg)
.add_attributes(vec![attr("action", "send"), attr("sender", "creator")])
.add_submessage(generate_economics_message(OWNER, "Send"));
.add_attributes(vec![attr("action", "send"), attr("sender", "creator")]);

assert_eq!(res, expected_res);
}
Expand Down Expand Up @@ -580,84 +572,81 @@ fn test_execute_send_threshold_not_found() {
}
);
}
#[test]
fn test_execute_send_ado_recipient() {
let mut deps = mock_dependencies_custom(&[]);
let env = mock_env();
let _res: Response = init(deps.as_mut());

let sender_funds_amount = 10000u128;
let info = mock_info(OWNER, &[Coin::new(sender_funds_amount, "uluna")]);

let recip_address1 = "address1".to_string();
let recip_address2 = "address2".to_string();

let recip1 = Recipient::from_string(recip_address1);
let recip2 = Recipient::from_string(recip_address2);

let msg = ExecuteMsg::Send {};

let amp_msg_1 = recip1
.generate_amp_msg(&deps.as_ref(), Some(vec![Coin::new(1000, "uluna")]))
.unwrap();
let amp_msg_2 = recip2
.generate_amp_msg(&deps.as_ref(), Some(vec![Coin::new(2000, "uluna")]))
.unwrap();
let amp_pkt = AMPPkt::new(
MOCK_CONTRACT_ADDR.to_string(),
MOCK_CONTRACT_ADDR.to_string(),
vec![amp_msg_1, amp_msg_2],
);
let amp_msg = amp_pkt
.to_sub_msg(
MOCK_KERNEL_CONTRACT,
Some(vec![Coin::new(1000, "uluna"), Coin::new(2000, "uluna")]),
1,
)
.unwrap();

// #[test]
// fn test_execute_send_ado_recipient() {
// let mut deps = mock_dependencies_custom(&[]);
// let env = mock_env();
// let _res: Response = init(deps.as_mut());

// let sender_funds_amount = 10000u128;
// let info = mock_info(OWNER, &[Coin::new(sender_funds_amount, "uluna")]);

// let recip_address1 = "address1".to_string();
// let recip_percent1 = 10; // 10%

// let recip_address2 = "address2".to_string();
// let recip_percent2 = 20; // 20%

// let recip1 = Recipient::from_string(recip_address1);
// let recip2 = Recipient::from_string(recip_address2);

// let recipient = vec![
// AddressFunds {
// recipient: recip1.clone(),
// percent: Decimal::percent(recip_percent1),
// },
// AddressFunds {
// recipient: recip2.clone(),
// percent: Decimal::percent(recip_percent2),
// },
// ];
// let msg = ExecuteMsg::Send {};

// let amp_msg_1 = recip1
// .generate_amp_msg(&deps.as_ref(), Some(vec![Coin::new(1000, "uluna")]))
// .unwrap();
// let amp_msg_2 = recip2
// .generate_amp_msg(&deps.as_ref(), Some(vec![Coin::new(2000, "uluna")]))
// .unwrap();
// let amp_pkt = AMPPkt::new(
// MOCK_CONTRACT_ADDR.to_string(),
// MOCK_CONTRACT_ADDR.to_string(),
// vec![amp_msg_1, amp_msg_2],
// );
// let amp_msg = amp_pkt
// .to_sub_msg(
// MOCK_KERNEL_CONTRACT,
// Some(vec![Coin::new(1000, "uluna"), Coin::new(2000, "uluna")]),
// 1,
// )
// .unwrap();

// let splitter = ConditionalSplitter {
// recipients: recipient,
// lock: Milliseconds::default(),
// };

// CONDITIONAL_SPLITTER
// .save(deps.as_mut().storage, &splitter)
// .unwrap();

// let res = execute(deps.as_mut(), env, info.clone(), msg).unwrap();

// let expected_res = Response::new()
// .add_submessages(vec![
// SubMsg::new(
// // refunds remainder to sender
// CosmosMsg::Bank(BankMsg::Send {
// to_address: info.sender.to_string(),
// amount: vec![Coin::new(7000, "uluna")], // 10000 * 0.7 remainder
// }),
// ),
// amp_msg,
// ])
// .add_attribute("action", "send")
// .add_attribute("sender", "creator")
// .add_submessage(generate_economics_message(OWNER, "Send"));

// assert_eq!(res, expected_res);
// }
let splitter = ConditionalSplitter {
thresholds: vec![Threshold::new(
Uint128::zero(),
vec![
AddressPercent::new(
recip1.clone(), // 10%
Decimal::from_ratio(Uint128::one(), Uint128::new(10)),
),
AddressPercent::new(
recip2.clone(), // 20%
Decimal::from_ratio(Uint128::one(), Uint128::new(5)),
),
],
)],
lock_time: Milliseconds::default(),
};

CONDITIONAL_SPLITTER
.save(deps.as_mut().storage, &splitter)
.unwrap();

let res = execute(deps.as_mut(), env, info.clone(), msg).unwrap();

let expected_res = Response::new()
.add_submessages(vec![
SubMsg::new(
// refunds remainder to sender
CosmosMsg::Bank(BankMsg::Send {
to_address: info.sender.to_string(),
amount: vec![Coin::new(7000, "uluna")], // 10000 * 0.7 remainder
}),
),
amp_msg,
])
.add_attribute("action", "send")
.add_attribute("sender", "creator");

assert_eq!(res, expected_res);
}

#[test]
fn test_handle_packet_exit_with_error_true() {
Expand Down Expand Up @@ -804,8 +793,7 @@ fn test_update_app_contract() {
assert_eq!(
Response::new()
.add_attribute("action", "update_app_contract")
.add_attribute("address", "app_contract")
.add_submessage(generate_economics_message(OWNER, "UpdateAppContract")),
.add_attribute("address", "app_contract"),
res
);
}
Expand Down
Loading

0 comments on commit 0f37694

Please sign in to comment.