Skip to content

Commit

Permalink
pr nits
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOsiris committed Aug 30, 2024
1 parent 74ab4cd commit 309e64e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ alloy = { version = "0.2", features = [
"rpc-types-eth",
"signer-local",
] }
reqwest = "0.12"
rug = "1.24.1"

[features]
Expand Down
37 changes: 21 additions & 16 deletions src/amm/balancer_v2/bmath.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use alloy::primitives::U256;
use rug::Float;

use crate::amm::consts::{BONE, DECIMAL_RADIX, MPFR_T_PRECISION, U256_2};
use crate::amm::consts::{BONE, DECIMAL_RADIX, MPFR_T_PRECISION, U256_1, U256_10E_10, U256_2};

use super::error::BMathError;

pub fn btoi(a: U256) -> U256 {
a / BONE
}

#[inline]
pub fn badd(a: U256, b: U256) -> Result<U256, BMathError> {
let c = a + b;
if c < a {
Expand All @@ -17,44 +18,43 @@ pub fn badd(a: U256, b: U256) -> Result<U256, BMathError> {
Ok(c)
}

#[inline]
pub fn bpowi(a: U256, n: U256) -> Result<U256, BMathError> {
let mut z = if n % U256::from(2u64) != U256::ZERO {
a
} else {
BONE
};
let mut z = if n % U256_2 != U256::ZERO { a } else { BONE };

let mut a = a;
let mut n = n / U256::from(2u64);
let mut n = n / U256_2;
while n != U256::ZERO {
a = bmul(a, a)?;
if n % U256::from(2u64) != U256::ZERO {
if n % U256_2 != U256::ZERO {
z = bmul(z, a)?;
}
n /= U256::from(2u64);
n /= U256_2;
}
Ok(z)
}

#[inline]
pub fn bpow(base: U256, exp: U256) -> Result<U256, BMathError> {
let whole = bfloor(exp);
let remain = bsub(exp, whole)?;
let whole_pow = bpowi(base, btoi(whole))?;
if remain == U256::ZERO {
return Ok(whole_pow);
}
let precision = BONE / U256::from(10_u64.pow(10));
let precision = BONE / U256_10E_10;
let partial_result = bpow_approx(base, remain, precision)?;
bmul(whole_pow, partial_result)
}

#[inline]
pub fn bpow_approx(base: U256, exp: U256, precision: U256) -> Result<U256, BMathError> {
let a = exp;
let (x, xneg) = bsub_sign(base, BONE);
let mut term = BONE;
let mut sum = term;
let mut negative = false;
let mut i = 1;
let mut i = U256_1;
while term >= precision {
let big_k = U256::from(i) * BONE;
let (c, cneg) = bsub_sign(a, bsub(big_k, BONE)?);
Expand All @@ -63,23 +63,25 @@ pub fn bpow_approx(base: U256, exp: U256, precision: U256) -> Result<U256, BMath
if term == U256::ZERO {
break;
}
negative ^= xneg ^ cneg;
negative ^= xneg ^ cneg;
if negative {
sum = bsub(sum, term)?;
} else {
sum = badd(sum, term)?;
}
i += 1;
i += U256_1;
}
Ok(sum)
}

#[inline]
pub fn bfloor(a: U256) -> U256 {
btoi(a) * BONE
}

// Reference:
// https://github.com/balancer/balancer-core/blob/f4ed5d65362a8d6cec21662fb6eae233b0babc1f/contracts/BNum.sol#L75
#[inline]
pub fn bdiv(a: U256, b: U256) -> Result<U256, BMathError> {
if b == U256::ZERO {
return Err(BMathError::DivZero);
Expand All @@ -97,6 +99,7 @@ pub fn bdiv(a: U256, b: U256) -> Result<U256, BMathError> {

// Reference:
// https://github.com/balancer/balancer-core/blob/f4ed5d65362a8d6cec21662fb6eae233b0babc1f/contracts/BNum.sol#L43
#[inline]
pub fn bsub(a: U256, b: U256) -> Result<U256, BMathError> {
let (c, flag) = bsub_sign(a, b);
if flag {
Expand All @@ -107,6 +110,7 @@ pub fn bsub(a: U256, b: U256) -> Result<U256, BMathError> {

// Reference:
// https://github.com/balancer/balancer-core/blob/f4ed5d65362a8d6cec21662fb6eae233b0babc1f/contracts/BNum.sol#L52
#[inline]
pub fn bsub_sign(a: U256, b: U256) -> (U256, bool) {
if a >= b {
(a - b, false)
Expand All @@ -117,6 +121,7 @@ pub fn bsub_sign(a: U256, b: U256) -> (U256, bool) {

// Reference:
// https://github.com/balancer/balancer-core/blob/f4ed5d65362a8d6cec21662fb6eae233b0babc1f/contracts/BNum.sol#L63C4-L73C6
#[inline]
pub fn bmul(a: U256, b: U256) -> Result<U256, BMathError> {
let c0 = a * b;
if a != U256::ZERO && c0 / a != b {
Expand Down Expand Up @@ -174,9 +179,9 @@ pub fn calculate_out_given_in(
let adjusted_in = bsub(BONE, swap_fee)?;
let adjusted_in = bmul(token_amount_in, adjusted_in)?;
let y = bdiv(token_balance_in, badd(token_balance_in, adjusted_in)?)?;
let foo = bpow(y, weight_ratio)?;
let bar = bsub(BONE, foo)?;
bmul(token_balance_out, bar)
let x = bpow(y, weight_ratio)?;
let z = bsub(BONE, x)?;
bmul(token_balance_out, z)
}

/// Converts a `U256` into a `Float` with a high precision.
Expand Down
15 changes: 9 additions & 6 deletions src/amm/balancer_v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,9 @@ mod tests {
address: address!("8a649274E4d777FFC6851F13d23A86BBFA2f2Fbf"),
..Default::default()
};
let provider =
Arc::new(ProviderBuilder::new().on_http(env!("ETHEREUM_PROVIDER").parse().unwrap()));
let provider = Arc::new(
ProviderBuilder::new().on_http(env!("ETHEREUM_RPC_ENDPOINT").parse().unwrap()),
);
balancer_v2_pool
.populate_data(Some(20487793), provider.clone())
.await
Expand All @@ -335,8 +336,9 @@ mod tests {

#[tokio::test]
pub async fn test_calculate_price() {
let provider =
Arc::new(ProviderBuilder::new().on_http(env!("ETHEREUM_PROVIDER").parse().unwrap()));
let provider = Arc::new(
ProviderBuilder::new().on_http(env!("ETHEREUM_RPC_ENDPOINT").parse().unwrap()),
);
let mut balancer_v2_pool = super::BalancerV2Pool {
address: address!("8a649274E4d777FFC6851F13d23A86BBFA2f2Fbf"),
..Default::default()
Expand All @@ -358,8 +360,9 @@ mod tests {

#[tokio::test]
pub async fn test_simulate_swap() {
let provider =
Arc::new(ProviderBuilder::new().on_http(env!("ETHEREUM_PROVIDER").parse().unwrap()));
let provider = Arc::new(
ProviderBuilder::new().on_http(env!("ETHEREUM_RPC_ENDPOINT").parse().unwrap()),
);
let mut balancer_v2_pool = super::BalancerV2Pool {
address: address!("8a649274E4d777FFC6851F13d23A86BBFA2f2Fbf"),
..Default::default()
Expand Down
1 change: 1 addition & 0 deletions src/amm/consts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloy::primitives::U256;

// commonly used U256s
pub const U256_10E_10: U256 = U256::from_limbs([10000000000, 0, 0, 0]);
pub const U256_0X100000000: U256 = U256::from_limbs([4294967296, 0, 0, 0]);
pub const U256_0X10000: U256 = U256::from_limbs([65536, 0, 0, 0]);
pub const U256_0X100: U256 = U256::from_limbs([256, 0, 0, 0]);
Expand Down

0 comments on commit 309e64e

Please sign in to comment.