helpers::math
Adapted from https://github.com/pentagonxyz/movemate/
use 0x1::error;
use 0xc0ded0c0::iterable_table_custom;
When an illegal operation is requested
const EARITHMETIC_ERROR: u64 = 0;
const FLOATING_POINT_SHIFT: u64 = 100;
These parameters may depend on staking_config and epoch length
const FLOATING_POINT_TOLERANCE: u64 = 1000;
Returns
a * b / c
but converts to u128
s for calculations to avoid overflow.c > 0
.a * b / c
fits intou64
.
public fun mul_div(a: u64, b: u64, c: u64): u64
Returns
a * b / c
without overflows but returns 0
when a = 0
.Aborts with
error_code
if c = 0
.public fun safe_mul_div(a: u64, b: u64, c: u64, error_code: u64): u64
Returns
a * b / c
without overflows if a
and c
are not 0
. Returns 0
if a = 0
. Returns a * init_factor
if c = 0
.public fun mul_div_with_init(a: u64, b: u64, c: u64, init_factor: u64): u64
Returns
a - b
if a > b
and 0
otherwise.public fun safe_sub(a: u64, b: u64): u64
Returns
a - b
if a > b
and 0
otherwise for u128
.public fun safe_sub_u128(a: u128, b: u128): u128
Returns either
a + pos_b
or a - neg_b
depending on which is non-zero.If both
pos_b
and neg_b
are non-zero.public fun add_possibly_negative(a: u64, pos_b: u64, neg_b: u64): u64
Returns the index of
a
th entry in an array of b
entries. If a == b
, returns 0
, else returns a
.If
a > b
or b == 0
.public fun round_robin(a: u64, b: u64): u64
Returns the maximum between two
u128
values.public fun max_u128(a: u128, b: u128): u128
Adds an entry to the table with the given key
address
and value u64
. If the entry already existed, then the passed value is added to the existing value. Otherwise, a new entry is created in the table for the given address
.public fun safe_add_to_iterable_table_custom(table: &mut iterable_table_custom::IterableTableCustom<address, u64>, key: address, amount: u64)
public fun safe_add_to_iterable_table_custom(
table: &mut IterableTableCustom<address, u64>,
key: address,
amount: u64
) {
if (amount == 0) {
return
};
if (iterable_table_custom::contains(table, key)) {
let current = iterable_table_custom::borrow_mut(table, key);
*current = *current + amount;
}
else {
iterable_table_custom::add(table, key, amount);
};
}
Remove
amount
from iterable table
from the key
. Delete the key
if its value
goes to 0
. Returns the actual amount removed from the value
. It may be smaller than the amount
passed if the existing value
was smaller.public fun subtract_from_iterable_table_custom(table: &mut iterable_table_custom::IterableTableCustom<address, u64>, key: address, amount: u64): u64
public fun subtract_from_iterable_table_custom(
table: &mut IterableTableCustom<address, u64>,
key: address,
amount: u64,
): u64 {
let current = iterable_table_custom::borrow_mut(table, key);
let current_val = *current;
if (current_val > amount) {
*current = *current - amount;
amount
} else {
iterable_table_custom::remove(table, key);
current_val
}
}
Returns the value of the entry with the given key
address
in the table
. If the entry does not exist, returns 0
.public fun safe_get_from_iterable_table_custom(table: &iterable_table_custom::IterableTableCustom<address, u64>, key: address): u64
This function is used in
tortuga::stake_router
. We verify it here as prover doesn't seem to work well in tortuga::stake_router
.fun calc_shares_to_value(num_shares: u64, total_worth: u64, t_apt_supply: u64): u64
Last modified 7mo ago