Skip to main content

SettlementEncoder

Struct SettlementEncoder 

Source
pub struct SettlementEncoder { /* private fields */ }
Expand description

Orchestrates building a complete GPv2Settlement.settle() calldata payload.

Collects tokens, clearing prices, trades, and interactions, then ABI-encodes everything into the format expected by the settlement contract’s settle function.

§Example

use alloy_primitives::{Address, B256, Bytes, U256, address};
use cow_rs::{
    OrderKind, SigningScheme, TokenBalance,
    order_signing::{trade::SignatureData, types::UnsignedOrder},
    settlement::encoder::{InteractionStage, SettlementEncoder},
};

let mut encoder = SettlementEncoder::new();

let sell = address!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
let buy = address!("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");

let sell_idx = encoder.add_token(sell);
let buy_idx = encoder.add_token(buy);

encoder.set_clearing_price(sell_idx, U256::from(1000));
encoder.set_clearing_price(buy_idx, U256::from(900));

assert_eq!(encoder.token_count(), 2);
// Encoder with only tokens/prices but no trades is still "empty".
assert!(encoder.is_empty());

Implementations§

Source§

impl SettlementEncoder

Source

pub fn new() -> Self

Create a new empty settlement encoder.

§Returns

An empty SettlementEncoder with no tokens, trades, or interactions.

Source

pub fn add_token(&mut self, token: Address) -> usize

Register a token in the settlement and return its index.

If the token is already registered, its existing index is returned. New tokens also get a default clearing price of zero.

§Arguments
  • token - The token Address to register.
§Returns

The zero-based index of the token in the settlement.

Source

pub fn set_clearing_price(&mut self, token_index: usize, price: U256)

Set the clearing price for a token at the given index.

The clearing price array is extended with zeros if needed to accommodate the index.

§Arguments
  • token_index - The zero-based index of the token.
  • price - The clearing price as U256.
Source

pub fn add_trade( &mut self, order: &UnsignedOrder, signature: &SignatureData, executed_amount: U256, )

Encode and append a trade to the settlement.

The order’s sell and buy tokens are automatically registered in the token registry if not already present.

§Arguments
  • order - The unsigned order to encode.
  • signature - The signature data (scheme + bytes).
  • executed_amount - The amount already executed for this trade.
Source

pub fn add_interaction( &mut self, stage: InteractionStage, target: Address, value: U256, calldata: Vec<u8>, )

Add an interaction to the specified stage.

§Arguments
  • stage - When the interaction should execute (InteractionStage).
  • target - The contract Address to call.
  • value - ETH value to send with the call (in wei).
  • calldata - ABI-encoded calldata for the interaction.
Source

pub fn encode_settlement(&self) -> Vec<u8>

ABI-encode the full settle(tokens[], clearingPrices[], trades[], interactions[][]) calldata.

Produces the calldata for the GPv2Settlement.settle() function, including the 4-byte function selector.

§Returns

The complete ABI-encoded calldata as a Vec<u8>.

Source

pub fn token_count(&self) -> usize

Return the number of registered tokens.

§Returns

The count of unique tokens in the settlement.

Source

pub const fn trade_count(&self) -> usize

Return the number of trades in the settlement.

§Returns

The count of encoded trades.

Source

pub const fn interaction_count(&self, stage: InteractionStage) -> usize

Return the number of interactions for a given stage.

§Arguments
  • stage - The interaction stage to count.
§Returns

The count of interactions in the specified stage.

Source

pub fn is_empty(&self) -> bool

Check whether the encoder contains any trades or interactions.

§Returns

true if there are no trades and no interactions in any stage.

Source

pub fn clear(&mut self)

Reset the encoder, removing all tokens, prices, trades, and interactions.

Trait Implementations§

Source§

impl Clone for SettlementEncoder

Source§

fn clone(&self) -> SettlementEncoder

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 SettlementEncoder

Source§

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

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

impl Default for SettlementEncoder

Source§

fn default() -> Self

Returns the “default value” for a type. 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, 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<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
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