This file contains the state machine changes related to token swapping functionality in the Canopy blockchain. It implements a cross-chain token exchange mechanism that allows users to trade tokens between different chains in a secure, deterministic way.
The token swapping system enables:
The order book is a central component that tracks all sell orders for a specific chain. It maintains information about:
The system provides functions to create, edit, lock, reset, and close orders within the order book. Each order has a unique ID and contains details about the assets being exchanged, the parties involved, and the current status of the swap.
The system monitors transactions across different chains to verify when buyers have sent the requested tokens to sellers. This involves:
When a valid payment is detected, the system can close the order and release the escrowed tokens to the buyer.
To ensure secure exchanges, the Root Chain holds tokens in escrow until the swap is completed:
This escrow mechanism provides security for both parties in the exchange.
The token swap process follows these steps:
Order Creation: A seller creates a sell order, specifying the amount of Root Chain tokens for sale and the amount of buyer-chain tokens requested in return. The Root Chain tokens are placed in escrow.
Order Locking: A buyer locks an order by providing their receive address and setting a deadline by which they must complete the payment. This reserves the order for that specific buyer.
Payment: The buyer sends the requested tokens directly to the seller’s address on the buyer chain, including a CloseOrder message in the transaction memo.
Verification: The Committee (validators) witnesses the payment transaction on the buyer chain and verifies that the correct amount was sent to the correct address before the deadline.
Order Closing: If verification is successful, the Committee closes the order and releases the escrowed Root Chain tokens to the buyer’s address.
Order Reset: If the buyer fails to send payment before the deadline, the order is reset and becomes available for other buyers.
This workflow ensures that tokens are exchanged securely without requiring trust between the parties.
The system uses blockchain heights as deadlines rather than timestamps:
Using block heights provides a deterministic way to measure time that all validators can agree on.
When a user wants to sell tokens:
The Committee (validators) plays a crucial role in the token swap process:
This validation ensures that the swap process is secure and that both parties fulfill their obligations.
The token swap system enables communication between different chains:
This cross-chain communication allows for secure token exchanges without requiring direct integration between the chains.