Skip to main content

CowSwapConfig

Struct CowSwapConfig 

Source
pub struct CowSwapConfig {
    pub chain_id: SupportedChainId,
    pub env: Env,
    pub sell_token: Address,
    pub sell_token_decimals: u8,
    pub tokens: TokenRegistry,
    pub slippage_bps: u32,
    pub order_valid_secs: u32,
    pub receiver: Option<Address>,
}
Expand description

Configuration for the CoW Protocol swap executor.

Bundles all parameters needed to submit orders: target chain, environment, sell token, slippage, TTL, and a TokenRegistry mapping strategy symbols to on-chain addresses.

Construct via prod or staging, then customise with the with_* builder methods.

§Example

use alloy_primitives::Address;
use cow_chains::{CowSwapConfig, SupportedChainId, TokenRegistry};

let empty: Vec<(&str, Address)> = vec![];
let config = CowSwapConfig::prod(
    SupportedChainId::Mainnet,
    Address::ZERO, // sell token
    TokenRegistry::new(empty),
    50,   // 0.5% slippage
    1800, // 30 min TTL
);
assert!(config.env.is_prod());
assert_eq!(config.slippage_bps, 50);

Fields§

§chain_id: SupportedChainId

Target chain.

§env: Env

API environment (Prod or Staging).

§sell_token: Address

The token used as the quote / sell currency (e.g. USDC on Sepolia).

§sell_token_decimals: u8

Decimal count for Self::sell_token (e.g. 6 for USDC, 18 for WETH).

§tokens: TokenRegistry

Registry mapping strategy asset symbols to their on-chain token addresses and decimal counts.

§slippage_bps: u32

Slippage tolerance in basis points (e.g. 50 = 0.5 %).

§order_valid_secs: u32

Default order TTL in seconds (e.g. 1800 = 30 min).

§receiver: Option<Address>

Optional override for the buy-token receiver address.

When None the order receiver defaults to the signing wallet address.

Implementations§

Source§

impl CowSwapConfig

Source

pub const fn prod( chain_id: SupportedChainId, sell_token: Address, tokens: TokenRegistry, slippage_bps: u32, order_valid_secs: u32, ) -> Self

Convenience constructor defaulting to the production environment.

Self::sell_token_decimals defaults to 18; use with_sell_token_decimals for tokens such as USDC (6) or WBTC (8).

§Parameters
  • chain_id — the target SupportedChainId.
  • sell_token — the ERC-20 Address of the sell (quote) currency.
  • tokens — the TokenRegistry mapping strategy symbols to tokens.
  • slippage_bps — slippage tolerance in basis points (e.g. 50 = 0.5 %).
  • order_valid_secs — order TTL in seconds (e.g. 1800 = 30 min).
§Returns

A new CowSwapConfig targeting Env::Prod with no custom receiver.

Source

pub const fn staging( chain_id: SupportedChainId, sell_token: Address, tokens: TokenRegistry, slippage_bps: u32, order_valid_secs: u32, ) -> Self

Convenience constructor defaulting to the staging (barn) environment.

Same parameters as prod but targets Env::Staging (barn.api.cow.fi). Self::sell_token_decimals defaults to 18.

§Parameters

See prod for parameter descriptions.

§Returns

A new CowSwapConfig targeting Env::Staging.

Source

pub const fn with_sell_token(self, token: Address) -> Self

Override the sell token address.

§Arguments
  • token — the new sell token Address.
§Returns

self with the updated sell token.

Source

pub const fn with_chain_id(self, chain_id: SupportedChainId) -> Self

Override the chain ID.

§Arguments
§Returns

self with the updated chain ID.

Source

pub const fn with_env(self, env: Env) -> Self

Override the API environment (Prod or Staging).

§Arguments
  • env — the new Env value.
§Returns

self with the updated environment.

Source

pub const fn with_slippage_bps(self, slippage_bps: u32) -> Self

Override the slippage tolerance in basis points.

§Arguments
  • slippage_bps — the new slippage in basis points (e.g. 50 = 0.5 %).
§Returns

self with the updated slippage.

Source

pub const fn with_order_valid_secs(self, secs: u32) -> Self

Override the default order TTL in seconds.

§Arguments
  • secs — the new TTL in seconds (e.g. 1800 = 30 min).
§Returns

self with the updated order TTL.

Source

pub const fn with_sell_token_decimals(self, decimals: u8) -> Self

Override the decimal count for sell_token (defaults to 18).

§Arguments
  • decimals — the decimal precision of the sell token.
§Returns

self with the updated decimal count.

Source

pub const fn with_receiver(self, receiver: Address) -> Self

Override the order receiver address.

§Arguments
  • receiver — the custom receiver Address.
§Returns

self with the receiver override set.

Source

pub const fn has_custom_receiver(&self) -> bool

Returns true if a custom receiver address has been set.

When false, the executor uses the signing wallet address as receiver.

§Returns

true when a receiver override is present.

Source

pub fn effective_receiver(&self, default: Address) -> Address

Return the effective receiver: the override if set, otherwise default.

§Example
use alloy_primitives::{Address, address};
use cow_chains::{CowSwapConfig, SupportedChainId, TokenRegistry};

let wallet = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
let empty: Vec<(&str, Address)> = vec![];
let config = CowSwapConfig::prod(
    SupportedChainId::Mainnet,
    Address::ZERO,
    TokenRegistry::new(empty),
    50,
    1800,
);
assert_eq!(config.effective_receiver(wallet), wallet);

let override_addr = address!("0000000000000000000000000000000000000001");
let with_recv = config.with_receiver(override_addr);
assert_eq!(with_recv.effective_receiver(wallet), override_addr);

Trait Implementations§

Source§

impl Debug for CowSwapConfig

Source§

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

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

impl Display for CowSwapConfig

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