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

ParameterTypeRequiredDefaultDescription
amountWeiYes-Amount of tokens to sell (in Wei)
accountLocalAccountYes-Ethereum account to sign the order with
chainChainYes-Blockchain network to execute on
sell_tokenChecksumAddressYes-Address of the token to sell
buy_tokenChecksumAddressYes-Address of the token to buy
safe_addressChecksumAddress | NoneNoNoneSafe address (uses pre-signature scheme if provided)
app_datastrNoDEFAULT_APP_DATA_HASHApplication metadata hash
valid_toint | NoneNoNoneOrder expiration (Unix timestamp). Uses quote validity if not set
envEnvsNo"prod"Environment ("prod", "staging")
slippage_tolerancefloatNo0.005Slippage tolerance (0.005 = 0.5%)
partially_fillableboolNoFalseAllow partial fills

Returns

Returns a CompletedOrder object.

Usage

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

account = Account.from_key("your_private_key")

usdc = Web3.to_checksum_address("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48")
weth = Web3.to_checksum_address("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2")

result = await swap_tokens(
    amount=Web3.to_wei(1000, "mwei"),
    account=account,
    chain=Chain.MAINNET,
    sell_token=usdc,
    buy_token=weth,
    slippage_tolerance=0.01,
)

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

With Safe Multi-Sig

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,
    buy_token=weth,
    safe_address=safe_address,
)

CompletedOrder

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

Fields

FieldTypeDescription
uidUIDUnique order identifier (56 bytes)
urlstrCoW Explorer URL for tracking

Explorer URLs

The URL points to CoW Explorer based on the chain:
  • 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}
result = await swap_tokens(...)

order_id = result.uid
explorer_url = result.url
Last modified on March 11, 2026