Skip to content

Commit a46ad34

Browse files
author
Johnathan Reale
committed
working our way backwards computation
1 parent bd5b6b0 commit a46ad34

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/bindings.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,30 @@ pub fn preview_combined_lock(
12701270
(total_amount, combined_duration)
12711271
}
12721272

1273+
/// Given a desired weighted duration, compute the required additional lock duration.
1274+
///
1275+
/// This inverts the weighted-average equation used by the TokenRegistry so callers can
1276+
/// determine which duration to supply to `manageLock` in order to reach a target lock end.
1277+
/// Returns `None` if the additional amount is zero or if the math underflows.
1278+
pub fn required_additional_duration(
1279+
existing_amount: U256,
1280+
existing_duration: U256,
1281+
additional_amount: U256,
1282+
desired_weighted_duration: U256,
1283+
) -> Option<U256> {
1284+
if additional_amount.is_zero() {
1285+
return None;
1286+
}
1287+
let total_amount = existing_amount + additional_amount;
1288+
let desired_total_weighted = desired_weighted_duration.saturating_mul(total_amount);
1289+
if desired_total_weighted < existing_amount.saturating_mul(existing_duration) {
1290+
return None;
1291+
}
1292+
let numerator =
1293+
desired_total_weighted - existing_amount.saturating_mul(existing_duration);
1294+
Some(numerator / additional_amount)
1295+
}
1296+
12731297
// ... existing code ...
12741298

12751299
impl Serialize for ManifestItem {

0 commit comments

Comments
 (0)