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: Splitter Send Config #686

Merged
merged 13 commits into from
Dec 6, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added Validator Staking ADO [(#330)](https://github.com/andromedaprotocol/andromeda-core/pull/330)
- Added Restake and Redelegate to Validator Staking [(#622)](https://github.com/andromedaprotocol/andromeda-core/pull/622)
- Added andromeda-math and andromeda-account packages[(#672)](https://github.com/andromedaprotocol/andromeda-core/pull/672)
- Added optional config for Send in Splitter contracts [(#686)](https://github.com/andromedaprotocol/andromeda-core/pull/686)
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like it's under the wrong section? Release 3 was completed so this will be under Unreleased



### Changed
Expand Down
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contracts/finance/andromeda-set-amount-splitter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "andromeda-set-amount-splitter"
version = "1.0.3-beta"
version = "1.1.0-beta"
edition = "2021"
rust-version = "1.75.0"

Expand Down
15 changes: 11 additions & 4 deletions contracts/finance/andromeda-set-amount-splitter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ pub fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Respon
let res = match msg {
ExecuteMsg::UpdateRecipients { recipients } => execute_update_recipients(ctx, recipients),
ExecuteMsg::UpdateLock { lock_time } => execute_update_lock(ctx, lock_time),
ExecuteMsg::Send {} => execute_send(ctx),
ExecuteMsg::Send { config } => execute_send(ctx, config),
_ => ADOContract::default().execute(ctx, msg),
}?;
Ok(res
Expand All @@ -133,7 +133,10 @@ pub fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Respon
.add_events(action_response.events))
}

fn execute_send(ctx: ExecuteContext) -> Result<Response, ContractError> {
fn execute_send(
ctx: ExecuteContext,
config: Option<Vec<AddressAmount>>,
) -> Result<Response, ContractError> {
let ExecuteContext { deps, info, .. } = ctx;

ensure!(
Expand All @@ -159,7 +162,11 @@ fn execute_send(ctx: ExecuteContext) -> Result<Response, ContractError> {
denom_set.insert(coin.denom);
}

let splitter = SPLITTER.load(deps.storage)?;
let splitter = if let Some(config) = config {
config
} else {
SPLITTER.load(deps.storage)?.recipients
};

let mut msgs: Vec<SubMsg> = Vec::new();
let mut amp_funds: Vec<Coin> = Vec::new();
Expand All @@ -171,7 +178,7 @@ fn execute_send(ctx: ExecuteContext) -> Result<Response, ContractError> {
let mut remainder_funds = coin.amount;
let denom = coin.denom;

for recipient in &splitter.recipients {
for recipient in &splitter {
// Find the recipient's corresponding denom for the current iteration of the sent funds
let recipient_coin = recipient
.coins
Expand Down
14 changes: 10 additions & 4 deletions contracts/finance/andromeda-set-amount-splitter/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ impl MockSetAmountSplitter {
Self(res.unwrap())
}

pub fn execute_send(&self, app: &mut MockApp, sender: Addr, funds: &[Coin]) -> ExecuteResult {
let msg = mock_set_amount_splitter_send_msg();
pub fn execute_send(
&self,
app: &mut MockApp,
sender: Addr,
funds: &[Coin],
config: Option<Vec<AddressAmount>>,
) -> ExecuteResult {
let msg = mock_set_amount_splitter_send_msg(config);

self.execute(app, &msg, sender, funds)
}
Expand All @@ -55,6 +61,6 @@ pub fn mock_set_amount_splitter_instantiate_msg(
}
}

pub fn mock_set_amount_splitter_send_msg() -> ExecuteMsg {
ExecuteMsg::Send {}
pub fn mock_set_amount_splitter_send_msg(config: Option<Vec<AddressAmount>>) -> ExecuteMsg {
ExecuteMsg::Send { config }
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,15 @@ fn test_execute_send() {

let recip_address2 = "address2".to_string();

let recip_address3 = "address3".to_string();

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

let recip3 = Recipient::from_string(recip_address3);
let config_recipient = vec![AddressAmount {
recipient: recip3.clone(),
coins: vec![coin(1_u128, "uandr"), coin(30_u128, "usdc")],
}];
let recipient = vec![
AddressAmount {
recipient: recip1.clone(),
Expand All @@ -189,7 +195,7 @@ fn test_execute_send() {
coins: vec![coin(1_u128, "uandr"), coin(20_u128, "usdc")],
},
];
let msg = ExecuteMsg::Send {};
let msg = ExecuteMsg::Send { config: None };

let amp_msg_1 = recip1
.generate_amp_msg(&deps.as_ref(), Some(vec![Coin::new(1, "uandr")]))
Expand Down Expand Up @@ -230,7 +236,7 @@ fn test_execute_send() {

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

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

let expected_res = Response::new()
.add_submessages(vec![
Expand All @@ -247,6 +253,56 @@ fn test_execute_send() {
.add_submessage(generate_economics_message(OWNER, "Send"));

assert_eq!(res, expected_res);

// Test with config
let msg = ExecuteMsg::Send {
config: Some(config_recipient),
};

let amp_msg_1 = recip3
.generate_amp_msg(&deps.as_ref(), Some(vec![Coin::new(1, "uandr")]))
.unwrap();

let amp_msg_2 = recip3
.generate_amp_msg(&deps.as_ref(), Some(vec![Coin::new(30, "usdc")]))
.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(1, "uandr"), Coin::new(30, "usdc")]),
1,
)
.unwrap();

let expected_res = Response::new()
.add_submessages(vec![
SubMsg::new(
// refunds remainder to sender
CosmosMsg::Bank(BankMsg::Send {
to_address: OWNER.to_string(),
amount: vec![Coin::new(9999, "uandr")],
}),
),
SubMsg::new(
// refunds remainder to sender
CosmosMsg::Bank(BankMsg::Send {
to_address: OWNER.to_string(),
amount: vec![Coin::new(20, "usdc")],
}),
),
amp_msg,
])
.add_attributes(vec![attr("action", "send"), attr("sender", "creator")])
.add_submessage(generate_economics_message(OWNER, "Send"));
let res = execute(deps.as_mut(), env, info, msg).unwrap();

assert_eq!(res, expected_res);
}

#[test]
Expand Down Expand Up @@ -275,7 +331,7 @@ fn test_execute_send_ado_recipient() {
coins: coins(1_u128, "uandr"),
},
];
let msg = ExecuteMsg::Send {};
let msg = ExecuteMsg::Send { config: None };

let amp_msg_1 = recip1
.generate_amp_msg(&deps.as_ref(), Some(vec![Coin::new(1, "uandr")]))
Expand Down Expand Up @@ -349,7 +405,7 @@ fn test_handle_packet_exit_with_error_true() {
"cosmos2contract",
vec![AMPMsg::new(
recip_address1,
to_json_binary(&ExecuteMsg::Send {}).unwrap(),
to_json_binary(&ExecuteMsg::Send { config: None }).unwrap(),
Some(vec![Coin::new(0, "uandr")]),
)],
);
Expand Down Expand Up @@ -425,7 +481,7 @@ fn test_execute_send_error() {
coins: coins(1_u128, "uandr"),
},
];
let msg = ExecuteMsg::Send {};
let msg = ExecuteMsg::Send { config: None };

let splitter = Splitter {
recipients: recipient,
Expand Down
2 changes: 1 addition & 1 deletion contracts/finance/andromeda-splitter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "andromeda-splitter"
version = "2.1.4"
version = "2.2.0"
edition = "2021"
rust-version = "1.75.0"

Expand Down
15 changes: 11 additions & 4 deletions contracts/finance/andromeda-splitter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Respon
let res = match msg {
ExecuteMsg::UpdateRecipients { recipients } => execute_update_recipients(ctx, recipients),
ExecuteMsg::UpdateLock { lock_time } => execute_update_lock(ctx, lock_time),
ExecuteMsg::Send {} => execute_send(ctx),
ExecuteMsg::Send { config } => execute_send(ctx, config),
_ => ADOContract::default().execute(ctx, msg),
}?;
Ok(res
Expand All @@ -114,7 +114,10 @@ pub fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Respon
.add_events(action_response.events))
}

fn execute_send(ctx: ExecuteContext) -> Result<Response, ContractError> {
fn execute_send(
ctx: ExecuteContext,
config: Option<Vec<AddressPercent>>,
) -> Result<Response, ContractError> {
let ExecuteContext { deps, info, .. } = ctx;
ensure!(
!info.funds.is_empty(),
Expand All @@ -131,7 +134,11 @@ fn execute_send(ctx: ExecuteContext) -> Result<Response, ContractError> {
);
}

let splitter = SPLITTER.load(deps.storage)?;
let splitter = if let Some(config) = config {
config
} else {
SPLITTER.load(deps.storage)?.recipients
};
joemonem marked this conversation as resolved.
Show resolved Hide resolved

let mut msgs: Vec<SubMsg> = Vec::new();
let mut amp_funds: Vec<Coin> = Vec::new();
Expand All @@ -148,7 +155,7 @@ fn execute_send(ctx: ExecuteContext) -> Result<Response, ContractError> {

let mut pkt = AMPPkt::from_ctx(ctx.amp_ctx, ctx.env.contract.address.to_string());

for recipient_addr in &splitter.recipients {
for recipient_addr in &splitter {
let recipient_percent = recipient_addr.percent;
let mut vec_coin: Vec<Coin> = Vec::new();
for (i, coin) in info.funds.clone().iter().enumerate() {
Expand Down
14 changes: 10 additions & 4 deletions contracts/finance/andromeda-splitter/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ impl MockSplitter {
Self(res.unwrap())
}

pub fn execute_send(&self, app: &mut MockApp, sender: Addr, funds: &[Coin]) -> ExecuteResult {
let msg = mock_splitter_send_msg();
pub fn execute_send(
&self,
app: &mut MockApp,
sender: Addr,
funds: &[Coin],
config: Option<Vec<AddressPercent>>,
) -> ExecuteResult {
let msg = mock_splitter_send_msg(config);

self.execute(app, &msg, sender, funds)
}
Expand All @@ -54,6 +60,6 @@ pub fn mock_splitter_instantiate_msg(
}
}

pub fn mock_splitter_send_msg() -> ExecuteMsg {
ExecuteMsg::Send {}
pub fn mock_splitter_send_msg(config: Option<Vec<AddressPercent>>) -> ExecuteMsg {
ExecuteMsg::Send { config }
}
Loading
Loading