Skip to content

Commit

Permalink
handle proportional case
Browse files Browse the repository at this point in the history
  • Loading branch information
joksas committed Aug 31, 2024
1 parent 074723e commit 5b87589
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
authors = ["RSS Blue", "Dovydas Joksas"]
name = "v4v"
version = "0.5.16"
version = "0.5.17"
edition = "2021"
description = "Value-for-value helper utilities for Podcasting 2.0"
license = "MIT OR Apache-2.0"
Expand Down
11 changes: 10 additions & 1 deletion src/pc20/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,16 @@ pub fn compute_sat_recipients(splits: &[u64], total_sats: u64) -> Vec<u64> {
return vec![];
}

// Create a vector of (index, split) pairs and sort it by split in descending order
// Check if all splits are non-zero and total_sats is a multiple of total_split
if !splits.contains(&0) && (total_sats as u128 % total_split == 0) {
// Distribute sats proportionally
return splits
.iter()
.map(|&split| (split as u128 * total_sats as u128 / total_split) as u64)
.collect();
}

// Original logic for other cases
let mut indexed_splits: Vec<(usize, u128)> = splits
.iter()
.enumerate()
Expand Down
15 changes: 15 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,21 @@ compute_sat_recipients_tests! {
total_sats: 0,
expected_sats: vec![0, 0],
},
case_25: TestCase {
splits: vec![50, 40, 3, 2, 2, 1, 2],
total_sats: 100,
expected_sats: vec![50, 40, 3, 2, 2, 1, 2],
},
case_26: TestCase {
splits: vec![50, 40, 3, 2, 2, 1, 2],
total_sats: 1000,
expected_sats: vec![500, 400, 30, 20, 20, 10, 20],
},
case_27: TestCase {
splits: vec![50, 40, 3, 2, 2, 1, 2],
total_sats: 10,
expected_sats: vec![3, 2, 1, 1, 1, 1, 1],
},
}

macro_rules! compute_sat_recipients_generic_tests {
Expand Down

0 comments on commit 5b87589

Please sign in to comment.