Market Making
Last updated
Last updated
Cross-chain orders are a fundamental interaction with the Mach protocol. The following example shows you how to implement a cross-chain order contract by first identifying market participants, and illustrating several key functions.
We'll start by identifying the three necessary participants in these interactions:
Order Takers (Taker)
Takers are the Users - who place trade orders on the platform (usually by front-end). The one and only thing the taker does is specify their intent to buy/sell assets in placeOrder
.
Market Makers (Maker)
Makers are entities that fill taker orders on the destination chain. Makers match the taker's orders with their own inventory or liquidity in executeMatch
.
Collateral Providers (Guarantor)
The guarantor orchestrates the cross-chain swap and provides collateral to guarantee the fulfillment of the takers' orders. They initiate the match in createMatch
and post collateral to add an initiative for 3rd parties to dispute bad matches. Often, the maker and guarantor will be the same entity.
For a cross-chain order that is processed without issue, these are the several key functions necessary for their success:
placeOrder
createMatch
executeMatch
confirmMatch
placeOrder
The placeOrder
function writes an order intent on-chain without moving funds. This function can be called by both takers and makers.
Core Event: OrderPlaced
Callable By: Taker, Maker
createMatch
The createMatch
function writes a match struct and pulls in funds from the guarantor. This function can only be called by the bonder.
Core Event: MatchProposed
Callable By: Only Guarantor
executeMatch
The executeMatch
function writes the match on-chain, pulls in funds, and pays out the funds. This function can only be called by the maker.
Core Event: MatchExecuted
Callable By: Only Maker
confirmMatch
The confirmMatch
function confirms the match and disallows any future challenge. This function can be called by both the guarantor and the maker.
Core Event: MatchConfirmed
Callable By: Guarantor, Maker
Taker submits intent: The taker places an order on the source chain using placeOrder
.
Maker places an equal and opposite order: The maker places an order on the destination chain using placeOrder
.
Guarantor matches taker and maker: The bonder matches the taker and maker by calling createMatch
on the source chain.
Funds are pulled from the guarantor (approximately 10% of the taker size, variable).
Funds are pulled from the taker.
Maker pays the taker: The maker pays the taker on the destination chain using executeMatch
.
Funds are moved from the maker to the taker via the contract.
Finalize the match: The guarantor or maker finalizes the match by calling finalizeMatch
on the source chain.
The guarantor or maker waits (>min_block_stability), pays the maker, and returns the bond with interest.
Funds are released to the maker.
Bond and fee are sent to the guarantor.
For simplification, the example workflow assumes the functions are called in the correct order, but the contract could be modified to handle various scenarios and conditions per transaction. This system includes multiple scenarios that we will cover under Challenge Process.