Skip to content

Commit

Permalink
fix: fix orderId bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
JhChoy committed Jun 19, 2024
1 parent d144fc9 commit 0146520
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
3 changes: 2 additions & 1 deletion src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
pub mod libraries {
pub mod lockers;
pub mod currency_delta;
pub mod fee_policy;
pub mod lockers;
pub mod order_id;
pub mod tick;
}

Expand Down
64 changes: 35 additions & 29 deletions src/libraries/order_id.cairo
Original file line number Diff line number Diff line change
@@ -1,71 +1,77 @@
use starknet::storage_access::{StorePacking};

const MASK_BOOK_ID: u256 = consteval_int!(2 * *192);
const MASK_TICK: u256 = consteval_int!(2 * *32);
const MASK_INDEX: u256 = consteval_int!(2 * *40);

const TWO_POW_72: u256 = consteval_int!(2 * *72);
const TWO_POW_40: u256 = consteval_int!(2 * *40);
const TWO_POW_192: u256 = 0x1000000000000000000000000000000000000000000000000; // 2**192
const TWO_POW_64: u256 = 0x10000000000000000; // 2**64
const TWO_POW_40: u64 = 0x10000000000; // 2**40

#[derive(Copy, Drop, Serde, Debug)]
pub struct OrderId {
pub book_id: u256, // u192
pub tick: i32, // u32
pub book_id: felt252, // u192
pub tick: u32, // todo: change to Tick struct
pub index: u64, // u40
}

#[generate_trait]
impl OrderIdImpl of OrderIdTrait {
fn encode(self: OrderId) -> u256 {
assert(self.book_id < MASK_BOOK_ID, "book_id overflow");
assert(self.tick < MASK_TICK, "tick overflow");
assert(self.index < MASK_INDEX, "index overflow");
return self.book_id * TWO_POW_72 + self.tick * TWO_POW_40 + self.index;
fn encode(self: OrderId) -> felt252 {
assert(self.book_id.into() < TWO_POW_192, 'book_id overflow');
assert(self.index < TWO_POW_40, 'index overflow');
return self.book_id.try_into().unwrap() * TWO_POW_64.try_into().unwrap()
+ self.tick.into() * TWO_POW_40.try_into().unwrap()
+ self.index.into();
}
}

impl OrderIdStoragePacking of StoragePacking<OrderId, u256> {
fn pack(value: OrderId) -> u256 {
impl OrderIdStoragePacking of StorePacking<OrderId, felt252> {
fn pack(value: OrderId) -> felt252 {
value.encode()
}

fn unpack(value: u256) -> OrderId {
let book_id = value / TWO_POW_72;
let tick = (value % TWO_POW_72) / TWO_POW_40;
let index = value % TWO_POW_40;
OrderId { book_id, tick, index }
fn unpack(value: felt252) -> OrderId {
let casted_value: u256 = value.into();
let book_id: u256 = casted_value / TWO_POW_64.into();
let tick_felt252: felt252 = (casted_value % TWO_POW_64.into() / TWO_POW_40.into())
.try_into()
.unwrap();
let index: u64 = (casted_value % TWO_POW_40.into()).try_into().unwrap();
OrderId {
book_id: book_id.try_into().unwrap(), tick: tick_felt252.try_into().unwrap(), index
}
}
}

#[cfg(test)]
mod tests {
use super::OrderId;
use super::OrderIdImpl;
use super::StoragePacking;
use super::StorePacking;

// todo delete this
const TWO_POW_24: u32 = 0x1000000; // 2**24

#[test]
fn encode() {
let order_id = OrderId { book_id: 1, tick: 2, index: 3 };
let encoded = order_id.encode();
assert_eq!(encoded, 0x10000020000000003_u256);
assert_eq!(encoded, 0x10000020000000003_felt252);

let order_id = OrderId { book_id: 1, tick: -2, index: 3 };
let order_id = OrderId { book_id: 1, tick: TWO_POW_24 - 2, index: 3 };
let encoded = order_id.encode();
assert_eq!(encoded, 0x1fffffe0000000003_u256);
assert_eq!(encoded, 0x1fffffe0000000003_felt252);
}

#[test]
fn unpack() {
let encoded = 0x10000020000000003_u256;
let order_id = OrderId::unpack(encoded);
let encoded = 0x10000020000000003_felt252;
let order_id: OrderId = StorePacking::unpack(encoded);
assert_eq!(order_id.book_id, 1);
assert_eq!(order_id.tick, 2);
assert_eq!(order_id.index, 3);

let encoded = 0x1fffffe0000000003_u256;
let order_id = OrderId::unpack(encoded);
let encoded = 0x1fffffe0000000003_felt252;
let order_id: OrderId = StorePacking::unpack(encoded);
assert_eq!(order_id.book_id, 1);
assert_eq!(order_id.tick, -2);
assert_eq!(order_id.tick, TWO_POW_24 - 2);
assert_eq!(order_id.index, 3);
}
}

0 comments on commit 0146520

Please sign in to comment.