Skip to main content

Swap

The swap module provides utilities for encoding Balancer swap requests within CoW Protocol. It enables direct order settlement against Balancer pools via the swap() function on the GPv2Settlement contract.

Interfaces

Swap

Represents a single Balancer pool interaction:

BatchSwapStep

The encoded form passed to the settlement contract, using token array indices rather than addresses for gas efficiency:

SwapExecution

Provides limitAmount for solvers to enforce stricter slippage protection than the original order:

EncodedSwap

A three-element tuple:

SwapEncoder Class

Builder class that constructs encoded swap calldata progressively.

Methods

encodeSwapStep

Appends one or more swaps to the encoder’s internal state:

encodeTrade

Incorporates a pre-signed order into the encoding:

signEncodeTrade

Signs an order and encodes it simultaneously:

encodedSwap

Returns the finalized tuple:

encodeSwap (static)

One-shot utility method with multiple overloads for convenience:

Examples

Single-Pool Swap

Multi-Hop Swap

MEV Mitigation

This module specifically handles direct Balancer settlement. For complex scenarios involving multiple orders and contract interactions, use the SettlementEncoder class instead. Most standard Balancer pools omit the userData field.
Last modified on March 4, 2026