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
impl SettlementEncoder
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty settlement encoder.
§Returns
An empty SettlementEncoder with no tokens, trades, or interactions.
Sourcepub fn set_clearing_price(&mut self, token_index: usize, price: U256)
pub fn set_clearing_price(&mut self, token_index: usize, price: U256)
Sourcepub fn add_trade(
&mut self,
order: &UnsignedOrder,
signature: &SignatureData,
executed_amount: U256,
)
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.
Sourcepub fn add_interaction(
&mut self,
stage: InteractionStage,
target: Address,
value: U256,
calldata: Vec<u8>,
)
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 contractAddressto call.value- ETH value to send with the call (in wei).calldata- ABI-encoded calldata for the interaction.
Sourcepub fn encode_settlement(&self) -> Vec<u8> ⓘ
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>.
Sourcepub fn token_count(&self) -> usize
pub fn token_count(&self) -> usize
Sourcepub const fn trade_count(&self) -> usize
pub const fn trade_count(&self) -> usize
Sourcepub const fn interaction_count(&self, stage: InteractionStage) -> usize
pub const fn interaction_count(&self, stage: InteractionStage) -> usize
Trait Implementations§
Source§impl Clone for SettlementEncoder
impl Clone for SettlementEncoder
Source§fn clone(&self) -> SettlementEncoder
fn clone(&self) -> SettlementEncoder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SettlementEncoder
impl Debug for SettlementEncoder
Auto Trait Implementations§
impl Freeze for SettlementEncoder
impl RefUnwindSafe for SettlementEncoder
impl Send for SettlementEncoder
impl Sync for SettlementEncoder
impl Unpin for SettlementEncoder
impl UnsafeUnpin for SettlementEncoder
impl UnwindSafe for SettlementEncoder
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 more