Within these short series we are going to showcase how easy it is to build a DEX on top of Injective. There is an open-sourced DEX which everyone can reference and use to build on top of Injective. For those who want to start from scratch, this is the right place to start. The series will include:Documentation Index
Fetch the complete documentation index at: https://injectivelabs-mintlify-jp-developers-first-half-1777019423.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
- Setting up the API clients and environment,
- Connecting to the Chain and the Indexer API,
- Connect to a user wallet and get their address,
- Fetching Spot and Derivative markets and their orderbooks,
- Placing market orders on both spot and a derivative market,
- View all positions for an Injective address.
Setup
First, configure your desired UI framework. You can find more details on the configuration here. To get started with the dex, we need to setup the API clients and the environment. To build our DEX we are going to query data from both the Injective Chain and the Indexer API. In this example, we are going to use the existing Testnet environment. Let’s first setup some of the classes we need to query the data.@injectivelabs/wallet-strategy package which allows users to connect with a various of different wallet providers and use them to sign transactions on Injective.
evmOptions within the WalletStrategy constructor.
Finally, to do the whole transaction flow (prepare + sign + broadcast) on Injective we are going to use the MsgBroadcaster class.
Connect to the user’s wallet
Since we are using theWalletStrategy to handle the connection with the user’s wallet, we can use its methods to handle some use cases like getting the user’s addresses, sign/broadcast a transaction, etc. To find out more about the wallet strategy, you can explore the documentation interface and the method the WalletStrategy offers.
Note: We can switch between the “active” wallet within the WalletStrategy using the setWallet method (which is async and requires await).
Querying
After the initial setup is done, let’s see how to query (and stream) markets from the IndexerAPI, as well as user’s balances from the chain directly.Transactions
Finally, let’s make some transactions. For this example, we are going to:- Send assets from one address to another,
- Make a spot limit order,
- Make a derivative market order.
msgBroadcaster client to broadcast these transactions:
