Skip to main content

swap_tokens

Swap tokens using the CoW Protocol. This is the main high-level function for executing token swaps.
CowContractAddress.VAULT_RELAYER needs to be approved to spend the sell token before calling this function.
async def swap_tokens(
    amount: Wei,
    account: LocalAccount,
    chain: Chain,
    sell_token: ChecksumAddress,
    buy_token: ChecksumAddress,
    safe_address: ChecksumAddress | None = None,
    app_data: str = DEFAULT_APP_DATA_HASH,
    valid_to: int | None = None,
    env: Envs = "prod",
    slippage_tolerance: float = 0.005,
    partially_fillable: bool = False,
) -> CompletedOrder

Parameters

amount
Wei
required
The amount of tokens to sell (in Wei)
account
LocalAccount
required
The Ethereum account to sign the order with
chain
Chain
required
The blockchain network to execute the swap on
sell_token
ChecksumAddress
required
The address of the token to sell
buy_token
ChecksumAddress
required
The address of the token to buy
safe_address
ChecksumAddress | None
default:"None"
Optional Safe (multi-sig) address if executing through a Safe. If provided, a pre-signature scheme will be used.
app_data
str
default:"DEFAULT_APP_DATA_HASH"
Application-specific metadata hash to attach to the order
valid_to
int | None
default:"None"
Unix timestamp until which the order is valid. If not specified, uses the quote’s validity period.
env
Envs
default:"prod"
The environment to use (“prod”, “staging”, etc.)
slippage_tolerance
float
default:"0.005"
Slippage tolerance as a decimal (e.g., 0.005 = 0.5%)
partially_fillable
bool
default:"False"
Whether the order can be partially filled

Returns

CompletedOrder
CompletedOrder
An object containing the order UID and explorer URL

Usage Example

from web3 import Web3
from eth_account import Account
from cowdao_cowpy.cow.swap import swap_tokens
from cowdao_cowpy.common.chains import Chain

# Initialize account
private_key = "your_private_key"
account = Account.from_key(private_key)

# Token addresses (example: USDC to WETH on mainnet)
usdc_address = Web3.to_checksum_address("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48")
weth_address = Web3.to_checksum_address("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2")

# Execute swap
result = await swap_tokens(
    amount=Web3.to_wei(1000, "mwei"),  # 1000 USDC (6 decimals)
    account=account,
    chain=Chain.MAINNET,
    sell_token=usdc_address,
    buy_token=weth_address,
    slippage_tolerance=0.01,  # 1% slippage
)

print(f"Order UID: {result.uid}")
print(f"Track order: {result.url}")

With Safe Multi-Sig

from web3 import Web3

# Safe address
safe_address = Web3.to_checksum_address("0x...")

result = await swap_tokens(
    amount=Web3.to_wei(1000, "mwei"),
    account=account,
    chain=Chain.MAINNET,
    sell_token=usdc_address,
    buy_token=weth_address,
    safe_address=safe_address,  # Use Safe for execution
    slippage_tolerance=0.01,
)

CompletedOrder

A Pydantic model representing a successfully created order.
class CompletedOrder(BaseModel):
    uid: UID
    url: str

Fields

uid
UID
The unique identifier for the order (56 bytes)
url
str
The CoW Explorer URL for tracking the order status

Usage

result = await swap_tokens(...)

# Access order details
order_id = result.uid
explorer_url = result.url

# The URL points to CoW Explorer based on the chain
# Examples:
# - Mainnet: https://explorer.cow.fi/ethereum/orders/{uid}
# - Gnosis Chain: https://explorer.cow.fi/gc/orders/{uid}
# - Arbitrum: https://explorer.cow.fi/arb1/orders/{uid}

Build docs developers (and LLMs) love