pub struct TwapData {
pub sell_token: Address,
pub buy_token: Address,
pub receiver: Address,
pub sell_amount: U256,
pub buy_amount: U256,
pub start_time: TwapStartTime,
pub part_duration: u32,
pub num_parts: u32,
pub app_data: B256,
pub partially_fillable: bool,
pub kind: OrderKind,
pub duration_of_part: DurationOfPart,
}Expand description
Parameters for a Time-Weighted Average Price (TWAP) order.
A TWAP order splits a large trade into num_parts equal parts executed
over num_parts × part_duration seconds, reducing market impact.
Fields§
§sell_token: AddressToken to sell.
buy_token: AddressToken to buy.
receiver: AddressAddress to receive bought tokens (use Address::ZERO for the order owner).
sell_amount: U256Total amount to sell across all parts.
buy_amount: U256Minimum total amount to buy across all parts.
start_time: TwapStartTimeWhen to start the TWAP.
part_duration: u32Duration of each part in seconds.
num_parts: u32Number of parts to split the order into.
app_data: B256App-data hash (use B256::ZERO for none).
partially_fillable: boolWhether each individual part may be partially filled.
kind: OrderKindOrder kind (Sell or Buy).
duration_of_part: DurationOfPartHow long each part remains valid within its window.
Defaults to DurationOfPart::Auto (full window, span = 0).
Implementations§
Source§impl TwapData
impl TwapData
Sourcepub const fn total_duration_secs(&self) -> u64
pub const fn total_duration_secs(&self) -> u64
Total duration of the TWAP order in seconds.
Equals num_parts × part_duration.
§Returns
The total duration in seconds as a u64.
Sourcepub const fn end_time(&self) -> Option<u64>
pub const fn end_time(&self) -> Option<u64>
Absolute Unix timestamp at which the last part expires, if the start time is known.
§Returns
Some(end_timestamp) when start_time is TwapStartTime::At, computed
as start + total_duration_secs(). Returns None when start_time is
TwapStartTime::AtMiningTime (the exact start is only known at mining time).
Sourcepub const fn is_sell(&self) -> bool
pub const fn is_sell(&self) -> bool
Returns true if this is a sell-direction TWAP order.
use alloy_primitives::{Address, U256};
use cow_composable::TwapData;
let twap = TwapData::sell(Address::ZERO, Address::ZERO, U256::ZERO, 4, 3_600);
assert!(twap.is_sell());
assert!(!twap.is_buy());Sourcepub const fn is_buy(&self) -> bool
pub const fn is_buy(&self) -> bool
Returns true if this is a buy-direction TWAP order.
use alloy_primitives::{Address, U256};
use cow_composable::{TwapData, TwapStartTime};
use cow_types::OrderKind;
let mut twap = TwapData::sell(Address::ZERO, Address::ZERO, U256::ZERO, 4, 3_600);
twap.kind = OrderKind::Buy;
assert!(twap.is_buy());
assert!(!twap.is_sell());Sourcepub const fn is_expired(&self, timestamp: u64) -> bool
pub const fn is_expired(&self, timestamp: u64) -> bool
Returns true if the TWAP has fully expired at the given Unix timestamp.
Returns false when start_time is TwapStartTime::AtMiningTime
(the end time is not yet known).
use alloy_primitives::{Address, U256};
use cow_composable::{TwapData, TwapStartTime};
let twap = TwapData::sell(Address::ZERO, Address::ZERO, U256::ZERO, 4, 3_600)
.with_start_time(TwapStartTime::At(1_000_000));
// ends at 1_000_000 + 4 × 3600 = 1_014_400
assert!(!twap.is_expired(1_014_399));
assert!(twap.is_expired(1_014_400));Sourcepub const fn sell(
sell_token: Address,
buy_token: Address,
sell_amount: U256,
num_parts: u32,
part_duration: u32,
) -> Self
pub const fn sell( sell_token: Address, buy_token: Address, sell_amount: U256, num_parts: u32, part_duration: u32, ) -> Self
Create a minimal sell-kind TWAP order.
Defaults: receiver = Address::ZERO, buy_amount = U256::ZERO (no min),
start_time = TwapStartTime::AtMiningTime, app_data = B256::ZERO,
partially_fillable = false, duration_of_part = DurationOfPart::Auto.
Use the with_* builder methods to set optional fields.
§Arguments
sell_token- Address of the token to sell.buy_token- Address of the token to buy.sell_amount- Total amount ofsell_tokento sell across all parts.num_parts- Number of parts to split the order into.part_duration- Duration of each part in seconds.
§Returns
A new TwapData configured as a sell order with sensible defaults.
Sourcepub const fn buy(
sell_token: Address,
buy_token: Address,
buy_amount: U256,
num_parts: u32,
part_duration: u32,
) -> Self
pub const fn buy( sell_token: Address, buy_token: Address, buy_amount: U256, num_parts: u32, part_duration: u32, ) -> Self
Create a minimal buy-kind TWAP order.
Defaults: receiver = Address::ZERO, sell_amount = U256::MAX (unlimited),
start_time = TwapStartTime::AtMiningTime, app_data = B256::ZERO,
partially_fillable = false, duration_of_part = DurationOfPart::Auto.
Use the with_* builder methods to set optional fields.
§Arguments
sell_token- Address of the token to sell.buy_token- Address of the token to buy.buy_amount- Minimum total amount ofbuy_tokento receive across all parts.num_parts- Number of parts to split the order into.part_duration- Duration of each part in seconds.
§Returns
A new TwapData configured as a buy order with sensible defaults.
Sourcepub const fn with_receiver(self, receiver: Address) -> Self
pub const fn with_receiver(self, receiver: Address) -> Self
Set the receiver address for bought tokens.
Address::ZERO means the order owner (default).
§Returns
The modified TwapData with the updated receiver (builder pattern).
Sourcepub const fn with_buy_amount(self, buy_amount: U256) -> Self
pub const fn with_buy_amount(self, buy_amount: U256) -> Self
Sourcepub const fn with_sell_amount(self, sell_amount: U256) -> Self
pub const fn with_sell_amount(self, sell_amount: U256) -> Self
Sourcepub const fn with_start_time(self, start_time: TwapStartTime) -> Self
pub const fn with_start_time(self, start_time: TwapStartTime) -> Self
Sourcepub const fn with_app_data(self, app_data: B256) -> Self
pub const fn with_app_data(self, app_data: B256) -> Self
Sourcepub const fn with_partially_fillable(self, partially_fillable: bool) -> Self
pub const fn with_partially_fillable(self, partially_fillable: bool) -> Self
Sourcepub const fn with_duration_of_part(
self,
duration_of_part: DurationOfPart,
) -> Self
pub const fn with_duration_of_part( self, duration_of_part: DurationOfPart, ) -> Self
Sourcepub fn has_app_data(&self) -> bool
pub fn has_app_data(&self) -> bool
Returns true if a non-zero app-data hash is attached.
The zero hash (B256::ZERO) means no app-data was set.
use alloy_primitives::{Address, B256, U256};
use cow_composable::TwapData;
let twap = TwapData::sell(Address::ZERO, Address::ZERO, U256::ZERO, 4, 3_600);
assert!(!twap.has_app_data());
let with_data = twap.with_app_data(B256::repeat_byte(0x01));
assert!(with_data.has_app_data());Trait Implementations§
Source§impl<'de> Deserialize<'de> for TwapData
impl<'de> Deserialize<'de> for TwapData
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl From<&TwapStruct> for TwapData
impl From<&TwapStruct> for TwapData
Source§fn from(s: &TwapStruct) -> Self
fn from(s: &TwapStruct) -> Self
Convert an ABI-level TwapStruct back into a high-level TwapData.
Delegates to crate::struct_to_data.
Auto Trait Implementations§
impl Freeze for TwapData
impl RefUnwindSafe for TwapData
impl Send for TwapData
impl Sync for TwapData
impl Unpin for TwapData
impl UnsafeUnpin for TwapData
impl UnwindSafe for TwapData
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.