Swap Process
The swap process is initiated by a user. They enter the parameters of a swap (such as tokens, networks, amounts, etc.) and send them to the query service, which is a part of decentralized Kinetex Nodes powered by Maintainers. This service queries registered resolvers for suitable swap orders they can provide.
Each Resolver can provide its own rates and set faster execution times, securing orders by higher collateral amounts. Each Resolver also has a score calculated in a decentralized way by Maintainers, according to order execution success rate, total trading volume, etc. This score, along with the parameters mentioned above, is an important criterion for choosing the most suitable order.
Once the best suitable order is chosen, it is important to ensure that Resolver has sufficient unlocked collateral to fulfill it. To achieve this, the collateralUnlocked
field in the Order
structure should contain an unlockCounter
value from the CollateralManager
. It will enable the OrderReceiver
contract to verify that the collateral can cover the order.
After that, the user signs the order with the private key associated with the address they hold the assets to swap on. The required signature is of EIP-712 standard for the Order
typed data structure.
Next, the signature is passed to the Resolver to execute the order via the contracts.
In general, the first stage of successful order execution looks like the following:
A User sets order parameters and sends them to Maintainers.
Maintainers query the registered Resolvers and pick the best order according to the rate, execution time, and resolver score.
If the User agrees to this order, they check the collateral state of the Resolver.
If the Resolver has enough collateral, the User can sign the order.
The User approves the transfer of the order asset to the Resolver.
The Resolver receives an order from the User in the initial network.
The order is received by the
OrderReceiver
smart contract that immediately transfers the User asset to the Resolver.
If receive is not performed by the Resolver (within the deadline specified in the order), the swap is considered canceled. Continuous cancellations can affect the Resolver score and limit its ability to execute orders.
Once the Resolver calls the OrderReceiver
contract, it computes the locked collateral for the order. The locked collateral is the sum of the order amount and the liquidation bounty. The liquidation bounty is an amount set aside to incentivize Liquidators to step in and complete the order if the Resolver fails to do so. The size of the collateral is always higher than one and is calculated depending on the assets and the execution time.
If the unlocked collateral (the Resolver's balance not currently involved in securing transactions) is smaller than the required collateral (collateral to be locked), a transaction revert will occur, and the Resolver will fail to confirm the order.
If the unlocked collateral is bigger than the required collateral amount, the OrderReceiver
updates the state and emits an AssetReceive
event, signaling that the order assets have been received successfully. This event may be used as part of the proof of order execution, checked by Kinetex Light Clients, to unlock the collateral.
After successfully receiving the order asset in the initial blockchain, the Resolver fills the order in the destination blockchain:
The Resolver calls the
OrderSender
contract with the order structure and transfers funds to the contract. Usage of Trusted Forwarders allows for more complicated transactions involving borrowed liquidity or swapped assets.The order is checked by the
OrderSender
contract, and then the Resolver’s assets are transferred to the User.After transferring the assets to the User, the
OrderSender
emits anAssetSend
event, signaling that the order assets have been sent successfully. This event may be used as proof to unlock the collateral.
Last updated