Skip to content

Commit

Permalink
chore: u128 -> u256
Browse files Browse the repository at this point in the history
  • Loading branch information
detectivekim committed Aug 19, 2024
1 parent 2b93005 commit 41854a2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/libraries/book_key.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl BookKeyStorePacking of StorePacking<BookKey, [felt252; 4]> {
fn pack(value: BookKey) -> [felt252; 4] {
let packed: u128 = value.unit_size.into()
+ TWO_POW_64 * value.maker_policy.encode().into()
+ TWO_POW_96 * value.taker_policy.encode().into();
+ TWO_POW_96.try_into().unwrap() * value.taker_policy.encode().into();
[value.base.into(), value.quote.into(), value.hooks.into(), packed.into()]
}

Expand All @@ -41,7 +41,7 @@ impl BookKeyStorePacking of StorePacking<BookKey, [felt252; 4]> {
((packed / TWO_POW_64.into()) % TWO_POW_32.into()).try_into().unwrap()
);
let taker_policy = FeePolicyTrait::decode(
((packed / TWO_POW_96.into()) % TWO_POW_32.into()).try_into().unwrap()
((packed / TWO_POW_96) % TWO_POW_32.into()).try_into().unwrap()
);

BookKey {
Expand Down
46 changes: 23 additions & 23 deletions src/libraries/tick.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -46,61 +46,61 @@ pub impl TickImpl of TickTrait {
let mut price: u256 = if absTick & 0x1 != 0 {
R0
} else {
TWO_POW_96.into()
TWO_POW_96
};
if absTick & 0x2 != 0 {
price = (price * R1) / TWO_POW_96.into();
price = (price * R1) / TWO_POW_96;
}
if absTick & 0x4 != 0 {
price = (price * R2) / TWO_POW_96.into();
price = (price * R2) / TWO_POW_96;
}
if absTick & 0x8 != 0 {
price = (price * R3) / TWO_POW_96.into();
price = (price * R3) / TWO_POW_96;
}
if absTick & 0x10 != 0 {
price = (price * R4) / TWO_POW_96.into();
price = (price * R4) / TWO_POW_96;
}
if absTick & 0x20 != 0 {
price = (price * R5) / TWO_POW_96.into();
price = (price * R5) / TWO_POW_96;
}
if absTick & 0x40 != 0 {
price = (price * R6) / TWO_POW_96.into();
price = (price * R6) / TWO_POW_96;
}
if absTick & 0x80 != 0 {
price = (price * R7) / TWO_POW_96.into();
price = (price * R7) / TWO_POW_96;
}
if absTick & 0x100 != 0 {
price = (price * R8) / TWO_POW_96.into();
price = (price * R8) / TWO_POW_96;
}
if absTick & 0x200 != 0 {
price = (price * R9) / TWO_POW_96.into();
price = (price * R9) / TWO_POW_96;
}
if absTick & 0x400 != 0 {
price = (price * R10) / TWO_POW_96.into();
price = (price * R10) / TWO_POW_96;
}
if absTick & 0x800 != 0 {
price = (price * R11) / TWO_POW_96.into();
price = (price * R11) / TWO_POW_96;
}
if absTick & 0x1000 != 0 {
price = (price * R12) / TWO_POW_96.into();
price = (price * R12) / TWO_POW_96;
}
if absTick & 0x2000 != 0 {
price = (price * R13) / TWO_POW_96.into();
price = (price * R13) / TWO_POW_96;
}
if absTick & 0x4000 != 0 {
price = (price * R14) / TWO_POW_96.into();
price = (price * R14) / TWO_POW_96;
}
if absTick & 0x8000 != 0 {
price = (price * R15) / TWO_POW_96.into();
price = (price * R15) / TWO_POW_96;
}
if absTick & 0x10000 != 0 {
price = (price * R16) / TWO_POW_96.into();
price = (price * R16) / TWO_POW_96;
}
if absTick & 0x20000 != 0 {
price = (price * R17) / TWO_POW_96.into();
price = (price * R17) / TWO_POW_96;
}
if absTick & 0x40000 != 0 {
price = (price * R18) / TWO_POW_96.into();
price = (price * R18) / TWO_POW_96;
}
if self > 0 {
return TWO_POW_192 / price;
Expand All @@ -124,16 +124,16 @@ pub impl TickImpl of TickTrait {
fn base_to_quote(self: Tick, base: u256, rounding_up: bool) -> u256 {
let price: u256 = Self::to_price(self);
if rounding_up {
return (base * price + TWO_POW_96.into() - 1) / TWO_POW_96.into();
return (base * price + TWO_POW_96 - 1) / TWO_POW_96;
}
base * price / TWO_POW_96.into()
base * price / TWO_POW_96
}

fn quote_to_base(self: Tick, quote: u256, rounding_up: bool) -> u256 {
let price: u256 = Self::to_price(self);
if rounding_up {
return (quote * TWO_POW_96.into() + price - 1) / price;
return (quote * TWO_POW_96 + price - 1) / price;
}
quote * TWO_POW_96.into() / price
quote * TWO_POW_96 / price
}
}
2 changes: 1 addition & 1 deletion src/utils/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub(crate) const TWO_POW_248: u256 =
0x100000000000000000000000000000000000000000000000000000000000000; // 2**248
pub(crate) const TWO_POW_192: u256 = 0x1000000000000000000000000000000000000000000000000; // 2**192
pub(crate) const TWO_POW_128: u256 = 0x100000000000000000000000000000000; // 2**128
pub(crate) const TWO_POW_96: u128 = 0x1000000000000000000000000; // 2**96
pub(crate) const TWO_POW_96: u256 = 0x1000000000000000000000000; // 2**96
pub(crate) const TWO_POW_64: u128 = 0x10000000000000000; // 2**64
pub(crate) const TWO_POW_62: u64 = 0x4000000000000000; // 2**62
pub(crate) const TWO_POW_40: u64 = 0x10000000000; // 2**40
Expand Down
14 changes: 7 additions & 7 deletions src/utils/math.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,12 @@ pub fn ln_wad(mut x: u256) -> i128 {
// We leave p in 2**192 basis so we don't need to scale it back up for the division.
// q is monic by convention.
let mut q = x + 5573035233440673466300451813936;
q = (q * x) / TWO_POW_96.into() + 71694874799317883764090561454958;
q = (q * x) / TWO_POW_96.into() + 283447036172924575727196451306956;
q = (q * x) / TWO_POW_96.into() + 401686690394027663651624208769553;
q = (q * x) / TWO_POW_96.into() + 204048457590392012362485061816622;
q = (q * x) / TWO_POW_96.into() + 31853899698501571402653359427138;
q = (q * x) / TWO_POW_96.into() + 909429971244387300277376558375;
q = (q * x) / TWO_POW_96 + 71694874799317883764090561454958;
q = (q * x) / TWO_POW_96 + 283447036172924575727196451306956;
q = (q * x) / TWO_POW_96 + 401686690394027663651624208769553;
q = (q * x) / TWO_POW_96 + 204048457590392012362485061816622;
q = (q * x) / TWO_POW_96 + 31853899698501571402653359427138;
q = (q * x) / TWO_POW_96 + 909429971244387300277376558375;

let x: i257 = x.into();

Expand All @@ -353,7 +353,7 @@ pub fn ln_wad(mut x: u256) -> i128 {
p = (p * x) / TWO_POW_96.into() - 11111509109440967052023855526967.into();
p = (p * x) / TWO_POW_96.into() - 45023709667254063763336534515857.into();
p = (p * x) / TWO_POW_96.into() - 14706773417378608786704636184526.into();
p = p * x - (795164235651350426258249787498_u256 * TWO_POW_96.into()).into();
p = p * x - (795164235651350426258249787498_u256 * TWO_POW_96).into();

let mut r = p / q.into();
r *= 1677202110996718588342820967067443963516166.into();
Expand Down

0 comments on commit 41854a2

Please sign in to comment.