Skip to main content

TwapData

Struct TwapData 

Source
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: Address

Token to sell.

§buy_token: Address

Token to buy.

§receiver: Address

Address to receive bought tokens (use Address::ZERO for the order owner).

§sell_amount: U256

Total amount to sell across all parts.

§buy_amount: U256

Minimum total amount to buy across all parts.

§start_time: TwapStartTime

When to start the TWAP.

§part_duration: u32

Duration of each part in seconds.

§num_parts: u32

Number of parts to split the order into.

§app_data: B256

App-data hash (use B256::ZERO for none).

§partially_fillable: bool

Whether each individual part may be partially filled.

§kind: OrderKind

Order kind (Sell or Buy).

§duration_of_part: DurationOfPart

How long each part remains valid within its window.

Defaults to DurationOfPart::Auto (full window, span = 0).

Implementations§

Source§

impl TwapData

Source

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.

Source

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).

Source

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());
Source

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());
Source

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));
Source

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 of sell_token to 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.

Source

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 of buy_token to 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.

Source

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).

Source

pub const fn with_buy_amount(self, buy_amount: U256) -> Self

Set the minimum amount of buy_token to receive across all parts.

Useful when building a sell-kind order to set a price floor.

§Returns

The modified TwapData with the updated buy amount (builder pattern).

Source

pub const fn with_sell_amount(self, sell_amount: U256) -> Self

Set the maximum amount of sell_token to sell across all parts.

Useful when building a buy-kind order to cap spending.

§Returns

The modified TwapData with the updated sell amount (builder pattern).

Source

pub const fn with_start_time(self, start_time: TwapStartTime) -> Self

Set when the TWAP order starts executing.

§Returns

The modified TwapData with the updated start time (builder pattern).

Source

pub const fn with_app_data(self, app_data: B256) -> Self

Attach an app-data hash to the order.

§Returns

The modified TwapData with the updated app-data hash (builder pattern).

Source

pub const fn with_partially_fillable(self, partially_fillable: bool) -> Self

Allow each individual part to be partially filled.

§Returns

The modified TwapData with the updated partial-fill setting (builder pattern).

Source

pub const fn with_duration_of_part( self, duration_of_part: DurationOfPart, ) -> Self

Restrict each part to a shorter validity window within its overall interval.

§Returns

The modified TwapData with the updated duration-of-part setting (builder pattern).

Source

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 Clone for TwapData

Source§

fn clone(&self) -> TwapData

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TwapData

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for TwapData

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for TwapData

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&TwapStruct> for TwapData

Source§

fn from(s: &TwapStruct) -> Self

Convert an ABI-level TwapStruct back into a high-level TwapData.

Delegates to crate::struct_to_data.

Source§

impl Serialize for TwapData

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl TryFrom<&TwapData> for TwapStruct

Source§

fn try_from(d: &TwapData) -> Result<Self, Self::Error>

Convert a high-level TwapData into the ABI-level TwapStruct.

Delegates to crate::data_to_struct.

Source§

type Error = CowError

The type returned in the event of a conversion error.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T> ToStringFallible for T
where T: Display,

Source§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<'de, T> BorrowedRpcObject<'de> for T
where T: RpcBorrow<'de> + RpcSend,

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<'de, T> RpcBorrow<'de> for T
where T: Deserialize<'de> + Debug + Send + Sync + Unpin,

Source§

impl<T> RpcObject for T
where T: RpcSend + RpcRecv,

Source§

impl<T> RpcRecv for T
where T: DeserializeOwned + Debug + Send + Sync + Unpin + 'static,

Source§

impl<T> RpcSend for T
where T: Serialize + Clone + Debug + Send + Sync + Unpin,