Skip to content

Commit

Permalink
feat: virtual shares in rebase impl
Browse files Browse the repository at this point in the history
  • Loading branch information
hoomp3 committed Feb 2, 2024
1 parent acbc406 commit 85f0391
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 21 additions & 17 deletions packages/math/src/traits/rebase.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
common::{checked_add, checked_sub, muldiv},
common::{checked_add, checked_sub, exp10, muldiv},
U256,
};
use btr_macros::borsh_serde;
Expand All @@ -18,29 +18,33 @@ pub trait Rebase {
/// Calculates the base value in relationship to `elastic` and self
fn to_base(&self, elastic: impl Into<U256> + Copy, round_up: bool) -> StdResult<U256> {
let elastic = elastic.into();
let mut base: U256;
if self.elastic() == 0 {
base = elastic;
} else {
base = muldiv(elastic, self.base(), self.elastic())?;
if round_up && muldiv(base, self.elastic(), self.base())? < elastic {
base += U256::ONE;
}
let mut base;

// Use virtual offset approach in YieldBox to enforce a base conversion rate.
// Because we want to support at most 18 decimal fixed point math, we set the ratio to 1 : 1e18.
let total_shares = self.base() + exp10(18);
let total_amount = self.elastic() + U256::ONE;

base = muldiv(elastic, total_shares, total_amount)?;
if round_up && muldiv(base, total_amount, total_shares)? < elastic {
base += U256::ONE;
}
Ok(base)
}

/// Calculates the elastic value in relationship to `base` and self
fn to_elastic(&self, base: impl Into<U256> + Copy, round_up: bool) -> StdResult<U256> {
let base = base.into();
let mut elastic: U256;
if self.base() == 0 {
elastic = base;
} else {
elastic = muldiv(base, self.elastic(), self.base())?;
if round_up && muldiv(elastic, self.base(), self.elastic())? < base {
elastic += U256::ONE;
}
let mut elastic;

// Use virtual offset approach in YieldBox to enforce a base conversion rate.
// Because we want to support at most 18 decimal fixed point math, we set the ratio to 1 : 1e18.
let total_shares = self.base() + exp10(18);
let total_amount = self.elastic() + U256::ONE;

elastic = muldiv(base, total_amount, total_shares)?;
if round_up && muldiv(elastic, total_shares, total_amount)? < base {
elastic += U256::ONE;
}
Ok(elastic)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/query-authentication/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ serde = { workspace = true }
thiserror = { workspace = true }
remain = "0.2.2"
ripemd160 = "0.9.1"
secp256k1 = "0.20.3"
secp256k1 = "0.27.0"
bech32 = "0.8.1"

sha2 = { version = "0.9.1", default-features = false }
sha2 = { version = "0.9.1", default-features = false }

0 comments on commit 85f0391

Please sign in to comment.