Skip to main content

TwapOrder

Struct TwapOrder 

Source
pub struct TwapOrder {
    pub data: TwapData,
    pub salt: B256,
}
Expand description

A TWAP order ready to be submitted to ComposableCow.

Fields§

§data: TwapData

The underlying TWAP configuration.

§salt: B256

32-byte salt uniquely identifying this order instance.

Implementations§

Source§

impl TwapOrder

Source

pub fn new(data: TwapData) -> Self

Create a new TWAP order.

The salt is derived deterministically from the order parameters. Use TwapOrder::with_salt to supply an explicit salt.

§Example
use alloy_primitives::{Address, U256};
use cow_composable::{TwapData, TwapOrder};

let data = TwapData::sell(Address::ZERO, Address::ZERO, U256::from(1000u64), 4, 3600);
let order = TwapOrder::new(data);
assert_eq!(order.data.num_parts, 4);
assert_eq!(order.data.part_duration, 3600);
Source

pub const fn with_salt(data: TwapData, salt: B256) -> Self

Create a TWAP order with an explicit salt.

§Arguments
  • data - The TWAP order configuration.
  • salt - A caller-chosen 32-byte salt to uniquely identify this order.
§Returns

A TwapOrder using the provided salt verbatim.

Source

pub fn to_params(&self) -> Result<ConditionalOrderParams, CowError>

Returns the on-chain ConditionalOrderParams for this order.

§Errors

Returns CowError::AppData if ABI encoding fails.

Source

pub fn id(&self) -> Result<B256, CowError>

Unique order ID: keccak256(abi.encode(ConditionalOrderParams)).

§Errors

Returns CowError::AppData if encoding fails.

Source

pub fn is_valid(&self) -> bool

Validate the order parameters.

Mirrors the TypeScript SDK Twap.isValid() logic:

  • Tokens must differ and must not be the zero address
  • sell_amount and buy_amount must be non-zero
  • num_parts must be ≥ 2
  • part_duration must be > 0 and ≤ MAX_FREQUENCY
  • sell_amount must be divisible by num_parts
  • If DurationOfPart::LimitDuration: duration must be ≤ part_duration
Source

pub fn per_part_amounts(&self) -> Result<(U256, U256), CowError>

Return the per-part sell and buy amounts (part_sell, min_part_buy).

These are the amounts used in each individual order slice: sell_amount / num_parts and buy_amount / num_parts.

§Errors

Returns CowError::AppData if num_parts is zero.

§Example
use alloy_primitives::{Address, U256};
use cow_composable::{TwapData, TwapOrder};

let data = TwapData::sell(Address::ZERO, Address::ZERO, U256::from(1000u64), 4, 3600)
    .with_buy_amount(U256::from(800u64));
let order = TwapOrder::new(data);
let (sell, buy) = order.per_part_amounts().unwrap();
assert_eq!(sell, U256::from(250u64));
assert_eq!(buy, U256::from(200u64));
Source

pub fn to_struct(&self) -> Result<TwapStruct, CowError>

Convert this order’s user-facing data into the on-chain TwapStruct representation.

The struct uses per-part amounts and the raw span/t0 fields.

§Errors

Returns CowError::AppData if num_parts is zero.

Source

pub fn poll_validate(&self, block_timestamp: u64) -> PollResult

Check tradability of this TWAP order at the given block timestamp.

Returns PollResult::Success if the order is within its execution window, PollResult::TryAtEpoch if the order has not started yet, or PollResult::DontTryAgain if the order has fully expired.

For TwapStartTime::AtMiningTime orders, always returns PollResult::Success because the start time is not known until mined.

Source§

impl TwapOrder

Source

pub const fn salt_ref(&self) -> &B256

Returns a reference to the 32-byte salt.

§Returns

A shared reference to the B256 salt that uniquely identifies this order instance within a ComposableCow safe.

Source

pub const fn data_ref(&self) -> &TwapData

Returns a reference to the underlying TwapData.

§Returns

A shared reference to the TwapData configuration backing this order.

Source

pub const fn total_sell_amount(&self) -> U256

Returns the total sell amount across all parts.

use alloy_primitives::{Address, U256};
use cow_composable::{TwapData, TwapOrder};

let data = TwapData::sell(Address::ZERO, Address::ZERO, U256::from(1_000u64), 4, 3_600);
let order = TwapOrder::new(data);
assert_eq!(order.total_sell_amount(), U256::from(1_000u64));
Source

pub const fn total_buy_amount(&self) -> U256

Returns the total minimum buy amount across all parts.

use alloy_primitives::{Address, U256};
use cow_composable::{TwapData, TwapOrder};

let data = TwapData::sell(Address::ZERO, Address::ZERO, U256::ZERO, 4, 3_600)
    .with_buy_amount(U256::from(800u64));
let order = TwapOrder::new(data);
assert_eq!(order.total_buy_amount(), U256::from(800u64));
Source

pub const fn is_sell(&self) -> bool

Returns true if this is a sell-direction TWAP order.

§Returns

true when the order kind is OrderKind::Sell, false otherwise.

Source

pub const fn is_buy(&self) -> bool

Returns true if this is a buy-direction TWAP order.

§Returns

true when the order kind is OrderKind::Buy, false otherwise.

Source

pub const fn is_expired_at(&self, block_timestamp: u64) -> bool

Returns true if the order has fully expired at the given Unix timestamp.

Delegates to TwapData::is_expired. Returns false when the start time is TwapStartTime::AtMiningTime (end time is unknown until mined).

use alloy_primitives::{Address, U256};
use cow_composable::{TwapData, TwapOrder, TwapStartTime};

let data = 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
let order = TwapOrder::new(data);
assert!(!order.is_expired_at(1_014_399));
assert!(order.is_expired_at(1_014_400));
Source

pub const fn start_timestamp(&self) -> Option<u32>

Return the fixed start timestamp, or None when the order starts at mining time.

Mirrors TwapData::start_time::timestamp().

Trait Implementations§

Source§

impl Clone for TwapOrder

Source§

fn clone(&self) -> TwapOrder

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 TwapOrder

Source§

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

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

impl Display for TwapOrder

Source§

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

Formats the value using the given formatter. Read more

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