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
The amount of tokens to sell (in Wei)
The Ethereum account to sign the order with
The blockchain network to execute the swap on
The address of the token to sell
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
Unix timestamp until which the order is valid. If not specified, uses the quote’s validity period.
The environment to use (“prod”, “staging”, etc.)
Slippage tolerance as a decimal (e.g., 0.005 = 0.5%)
Whether the order can be partially filled
Returns
An object containing the order UID and explorer URL Show CompletedOrder fields
The unique identifier for the created order
The CoW Explorer URL where the order can be tracked
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
The unique identifier for the order (56 bytes)
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}