Skip to content

Commit

Permalink
split destinations
Browse files Browse the repository at this point in the history
  • Loading branch information
codehans committed Apr 12, 2024
1 parent 6176790 commit 06045bc
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion artifacts/checksums.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
791f7e2cdb823ac017a34503fa9f5a2a6c04eb007e0ba34d4724747cd7de89d1 kujira_revenue_converter.wasm
3e32932f38991ab7fb572aaa0aea4792b6d72ff034b7a9927dfdcd790e8b940b kujira_revenue_converter.wasm
Binary file modified artifacts/kujira_revenue_converter.wasm
Binary file not shown.
34 changes: 26 additions & 8 deletions src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_json_binary, Addr, Binary, CosmosMsg, Deps, DepsMut, Env, Event, MessageInfo, Reply,
Response, StdResult, SubMsg,
to_json_binary, Addr, Binary, CosmosMsg, Decimal, Deps, DepsMut, Env, Event, MessageInfo,
Reply, Response, StdResult, SubMsg,
};
use kujira::Denom;

Expand Down Expand Up @@ -118,9 +118,27 @@ pub fn reply(deps: DepsMut, env: Env, _msg: Reply) -> Result<Response, ContractE
let send = if balance.amount.is_zero() {
vec![]
} else {
vec![config
.target_denom
.send(&config.target_address, &balance.amount)]
let total_weight = config.target_addresses.iter().fold(0, |a, e| e.1 + a);
let mut sends = vec![];
let mut remaining = balance.amount;
let mut targets = config.target_addresses.iter().peekable();

while let Some((addr, weight)) = targets.next() {
let amount = if targets.peek().is_none() {
remaining
} else {
balance
.amount
.mul_floor(Decimal::from_ratio(*weight, total_weight))
};
if amount.is_zero() {
continue;
}
remaining -= amount;
sends.push(config.target_denom.send(&addr, &balance.amount));

Check failure on line 138 in src/contract.rs

View workflow job for this annotation

GitHub Actions / Lints

this expression creates a reference which is immediately dereferenced by the compiler
}

sends
};
Ok(Response::default()
.add_messages(send)
Expand Down Expand Up @@ -161,7 +179,7 @@ mod tests {
let msg = InstantiateMsg {
owner: Addr::unchecked("owner"),
target_denom: Denom::from("ukuji"),
target_address: fee_address(),
target_addresses: vec![(fee_address(), 1)],
executor: Addr::unchecked("executor"),
};
instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
Expand All @@ -183,7 +201,7 @@ mod tests {
let msg = InstantiateMsg {
owner: Addr::unchecked("owner"),
target_denom: Denom::from("ukuji"),
target_address: fee_address(),
target_addresses: vec![(fee_address(), 1)],
executor: Addr::unchecked("executor"),
};
instantiate(deps.as_mut(), mock_env(), info.clone(), msg).unwrap();
Expand Down Expand Up @@ -291,7 +309,7 @@ mod tests {
let msg = InstantiateMsg {
owner: Addr::unchecked("owner"),
target_denom: Denom::from("ukuji"),
target_address: fee_address(),
target_addresses: vec![(fee_address(), 1)],
executor: Addr::unchecked("executor"),
};
instantiate(deps.as_mut(), mock_env(), info.clone(), msg).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct InstantiateMsg {
pub owner: Addr,
pub executor: Addr,
pub target_denom: Denom,
pub target_address: Addr,
pub target_addresses: Vec<(Addr, u8)>,
}

#[cw_serde]
Expand Down Expand Up @@ -37,7 +37,7 @@ pub struct ConfigResponse {
pub owner: Addr,
pub executor: Addr,
pub target_denom: Denom,
pub target_address: Addr,
pub target_addresses: Vec<(Addr, u8)>,
}

#[cw_serde]
Expand Down
8 changes: 4 additions & 4 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub struct Config {
/// The denom that is transferred to the fee_collector at the end of every execution
pub target_denom: Denom,

/// The final destination that `target_denom` is sent to
pub target_address: Addr,
/// The final destinations that `target_denom` is sent to (address, weight)
pub target_addresses: Vec<(Addr, u8)>,
}

impl Config {
Expand All @@ -43,7 +43,7 @@ impl From<InstantiateMsg> for Config {
owner: value.owner,
executor: value.executor,
target_denom: value.target_denom,
target_address: value.target_address,
target_addresses: value.target_addresses,
}
}
}
Expand All @@ -54,7 +54,7 @@ impl From<Config> for ConfigResponse {
owner: value.owner,
executor: value.executor,
target_denom: value.target_denom,
target_address: value.target_address,
target_addresses: value.target_addresses,
}
}
}
Expand Down

0 comments on commit 06045bc

Please sign in to comment.