LeafNode: tif field u8 -> u16

Allows for longer TIF durations
This commit is contained in:
Christian Kamm 2022-12-05 15:21:24 +01:00
parent cbaa0098d6
commit a5a015e19f
4 changed files with 17 additions and 14 deletions

View File

@ -545,7 +545,7 @@ pub mod mango_v4 {
//
// Send 0 if you want the order to never expire.
// Timestamps in the past mean the instruction is skipped.
// Timestamps in the future are reduced to now + 255s.
// Timestamps in the future are reduced to now + 65535s.
expiry_timestamp: u64,
// Maximum number of orders from the book to fill.
@ -608,7 +608,7 @@ pub mod mango_v4 {
//
// Send 0 if you want the order to never expire.
// Timestamps in the past mean the instruction is skipped.
// Timestamps in the future are reduced to now + 255s.
// Timestamps in the future are reduced to now + 65535s.
expiry_timestamp: u64,
// Maximum number of orders from the book to fill.

View File

@ -225,7 +225,7 @@ mod tests {
let mut fixed = new_order_tree(order_tree_type);
let mut oracle_pegged = new_order_tree(order_tree_type);
let new_node = |key: u128, tif: u8, peg_limit: i64| {
let new_node = |key: u128, tif: u16, peg_limit: i64| {
LeafNode::new(
0,
key,
@ -238,11 +238,11 @@ mod tests {
peg_limit,
)
};
let mut add_fixed = |price: i64, tif: u8| {
let mut add_fixed = |price: i64, tif: u16| {
let key = new_node_key(side, fixed_price_data(price).unwrap(), 0);
fixed.insert_leaf(&new_node(key, tif, -1)).unwrap();
};
let mut add_pegged = |price_offset: i64, tif: u8, peg_limit: i64| {
let mut add_pegged = |price_offset: i64, tif: u16, peg_limit: i64| {
let key = new_node_key(side, oracle_pegged_price_data(price_offset), 0);
oracle_pegged
.insert_leaf(&new_node(key, tif, peg_limit))

View File

@ -127,11 +127,13 @@ pub struct LeafNode {
pub owner_slot: u8,
pub order_type: PostOrderType, // this was added for TradingView move order
pub padding: [u8; 1],
/// Time in seconds after `timestamp` at which the order expires.
/// A value of 0 means no expiry.
pub time_in_force: u8,
pub time_in_force: u16,
pub padding: [u8; 4],
pub padding2: [u8; 2],
/// The binary tree key
pub key: u128,
@ -165,15 +167,16 @@ impl LeafNode {
client_order_id: u64,
timestamp: u64,
order_type: PostOrderType,
time_in_force: u8,
time_in_force: u16,
peg_limit: i64,
) -> Self {
Self {
tag: NodeTag::LeafNode.into(),
owner_slot,
order_type,
time_in_force,
padding: Default::default(),
time_in_force,
padding2: Default::default(),
key,
owner,
quantity,

View File

@ -20,7 +20,7 @@ pub struct Order {
pub reduce_only: bool,
/// Number of seconds the order shall live, 0 meaning forever
pub time_in_force: u8,
pub time_in_force: u16,
/// Order type specific params
pub params: OrderParams,
@ -45,16 +45,16 @@ pub enum OrderParams {
impl Order {
/// Convert an input expiry timestamp to a time_in_force value
pub fn tif_from_expiry(expiry_timestamp: u64) -> Option<u8> {
pub fn tif_from_expiry(expiry_timestamp: u64) -> Option<u16> {
let now_ts: u64 = Clock::get().unwrap().unix_timestamp.try_into().unwrap();
if expiry_timestamp != 0 {
// If expiry is far in the future, clamp to 255 seconds
let tif = expiry_timestamp.saturating_sub(now_ts).min(255);
// If expiry is far in the future, clamp to u16::MAX seconds
let tif = expiry_timestamp.saturating_sub(now_ts).min(u16::MAX.into());
if tif == 0 {
// If expiry is in the past, ignore the order
return None;
}
Some(tif as u8)
Some(tif as u16)
} else {
// Never expire
Some(0)