Create your Swap Contract
Last updated
Last updated
The allows an instant swap between two different tokens. Under the hood, it uses atomic orders to place market orders in one or more spot markets.
Anyone can instantiate an instance of the swap contract. There is a version of this contract uploaded on Injective mainnet already, and can be found .
Before instantiating the contract, as the contract owner, you have three questions to answer:
Since orders placed by the swap contract are orders in the Injective Exchange Module, this means each order can have a fee recipient which can receive 40% of the trading fee. Typically, Exchange dApps will set the fee recipient as their own addresses.
Every token available in the contract must have a route defined. Route refers to which markets token A
will go through in order to get token B
. For example, if you would like to support swapping between ATOM and INJ, then you would have to set route by providing the contract the market IDs of ATOM/USDT and INJ/USDT, so that it knows the route of swapping between ATOM and INJ would be ATOM ⇔ USDT ⇔ INJ.
At this moment, the contract can only support markets quoted in USDT.
As the contract owner, you also have to provide funds to the contract which will be used when the swap happens. The buffer is used by the contract when it places orders. If the user wants to swap a big amount or swap in an illiquid market, then more buffer is required. An error will occur when the contract buffer cannot satisfy the user's input amount.
At this moment, the buffer should only be USDT.
Initializes the contract state with the contract version and configuration details. The config includes an administrator address and a fee recipient address.
Handles different types of transactions and admin functions:
SwapMinOutput: Swap with the minimum output quantity.
SwapExactOutput: Swap with an exact output quantity.
SetRoute: Set a swap route.
DeleteRoute: Delete a swap route.
UpdateConfig: Update the contract configuration.
WithdrawSupportFunds: Withdraw the support funds from the contract.
Handles the replies from other contracts or transactions.
Handles various queries to the contract:
GetRoute: Get a specific swap route.
GetOutputQuantity: Get the output quantity for a given input quantity.
GetInputQuantity: Get the input quantity for a given output quantity.
GetAllRoutes: Get all available swap routes.
The complete GitHub repository for the swap contract can be found .