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

refactor: Splitter Adjustments #457

Merged
merged 3 commits into from
May 20, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Splitter: avoid zero send messages, owner updates lock any time [(#457)](https://github.com/andromedaprotocol/andromeda-core/pull/457)

Comment on lines +24 to +25
Copy link
Contributor

Choose a reason for hiding this comment

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

Correct the grammar and punctuation in the change description.

- Splitter: avoid zero send messages, owner updates lock any time [(#457)](https://github.com/andromedaprotocol/andromeda-core/pull/457)
+ Splitter: avoid zero send messages; owner can update lock at any time [(#457)](https://github.com/andromedaprotocol/andromeda-core/pull/457)

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
- Splitter: avoid zero send messages, owner updates lock any time [(#457)](https://github.com/andromedaprotocol/andromeda-core/pull/457)
Splitter: avoid zero send messages; owner can update lock at any time [(#457)](https://github.com/andromedaprotocol/andromeda-core/pull/457)

### Removed

- Schemas are no longer tracked [(#430)](https://github.com/andromedaprotocol/andromeda-core/pull/430)
41 changes: 20 additions & 21 deletions contracts/finance/andromeda-splitter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,22 @@ fn execute_send(ctx: ExecuteContext) -> Result<Response, ContractError> {
let recipient_percent = recipient_addr.percent;
let mut vec_coin: Vec<Coin> = Vec::new();
for (i, coin) in info.funds.clone().iter().enumerate() {
let mut recip_coin: Coin = coin.clone();
recip_coin.amount = coin.amount * recipient_percent;
remainder_funds[i].amount = remainder_funds[i].amount.checked_sub(recip_coin.amount)?;
vec_coin.push(recip_coin.clone());
amp_funds.push(recip_coin);
let amount_owed = coin.amount.mul_floor(recipient_percent);
if !amount_owed.is_zero() {
let mut recip_coin: Coin = coin.clone();
recip_coin.amount = amount_owed;
remainder_funds[i].amount =
remainder_funds[i].amount.checked_sub(recip_coin.amount)?;
vec_coin.push(recip_coin.clone());
amp_funds.push(recip_coin);
}
Comment on lines +171 to +179
Copy link
Contributor

Choose a reason for hiding this comment

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

Optimize nested loops to improve performance.

Consider using a HashMap to store and update remainder_funds to avoid nested loops and improve performance.

let mut remainder_funds_map: HashMap<String, Uint128> = info.funds.iter().map(|coin| (coin.denom.clone(), coin.amount)).collect();

for recipient_addr in &splitter.recipients {
    let recipient_percent = recipient_addr.percent;
    let mut vec_coin: Vec<Coin> = Vec::new();
    for coin in info.funds.iter() {
        let amount_owed = coin.amount.mul_floor(recipient_percent);
        if !amount_owed.is_zero() {
            let mut recip_coin = coin.clone();
            recip_coin.amount = amount_owed;
            if let Some(remainder) = remainder_funds_map.get_mut(&coin.denom) {
                *remainder = remainder.checked_sub(amount_owed)?;
            }
            vec_coin.push(recip_coin.clone());
            amp_funds.push(recip_coin);
        }
    }
    if !vec_coin.is_empty() {
        let amp_msg = recipient_addr.recipient.generate_amp_msg(&deps.as_ref(), Some(vec_coin))?;
        pkt = pkt.add_message(amp_msg);
    }
}

}
if !vec_coin.is_empty() {
let amp_msg = recipient_addr
.recipient
.generate_amp_msg(&deps.as_ref(), Some(vec_coin))?;
pkt = pkt.add_message(amp_msg);
}

// let direct_message = recipient_addr
// .recipient
// .generate_direct_msg(&deps.as_ref(), vec_coin)?;
let amp_msg = recipient_addr
.recipient
.generate_amp_msg(&deps.as_ref(), Some(vec_coin))?;
pkt = pkt.add_message(amp_msg);
}
remainder_funds.retain(|x| x.amount > Uint128::zero());

Expand All @@ -197,8 +199,11 @@ fn execute_send(ctx: ExecuteContext) -> Result<Response, ContractError> {
})));
}
let kernel_address = ADOContract::default().get_kernel_address(deps.as_ref().storage)?;
let distro_msg = pkt.to_sub_msg(kernel_address, Some(amp_funds), 1)?;
msgs.push(distro_msg);

if !pkt.messages.is_empty() {
let distro_msg = pkt.to_sub_msg(kernel_address, Some(amp_funds), 1)?;
msgs.push(distro_msg);
}

Ok(Response::new()
.add_submessages(msgs)
Expand Down Expand Up @@ -259,12 +264,6 @@ fn execute_update_lock(

let mut splitter = SPLITTER.load(deps.storage)?;

// Can't call this function while the lock isn't expired

ensure!(
splitter.lock.is_expired(&env.block),
ContractError::ContractLocked {}
);
// Get current time
let current_time = Milliseconds::from_seconds(env.block.time.seconds());

Expand Down
Loading