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:
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.
// filename: Services.tsimport { ChainGrpcBankApi, IndexerGrpcSpotApi, IndexerGrpcDerivativesApi,} from'@injectivelabs/sdk-ts'import { getNetworkEndpoints, Network } from'@injectivelabs/networks'// Getting the pre-defined endpoints for the Testnet environment// (using TestnetK8s here because we want to use the Kubernetes infra)exportconstNETWORK=Network.TestnetexportconstENDPOINTS=getNetworkEndpoints(NETWORK)exportconstchainBankApi=newChainGrpcBankApi(ENDPOINTS.grpc)exportconstindexerSpotApi=newIndexerGrpcSpotApi(ENDPOINTS.indexer)exportconstindexerDerivativesApi=newIndexerGrpcDerivativesApi(ENDPOINTS.indexer,)exportconstindexerSpotStream=newIndexerGrpcDerivativeStream(ENDPOINTS.indexer,)exportconstindexerDerivativeStream=newIndexerGrpcDerivativeStream(ENDPOINTS.indexer,)
Then, we also need to setup a wallet connection to allow the user to connect to our DEX and start signing transactions. To make this happen we are going to use our @injectivelabs/wallet-ts package which allows users to connect with a various of different wallet providers and use them to sign transactions on Injective.
Since we are using the WalletStrategy 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.
// filename: WalletConnection.tsimport { WalletException, UnspecifiedErrorCode, ErrorType,} from'@injectivelabs/exceptions'import { Wallet } from'@injectivelabs/wallet-ts'import { walletStrategy } from'./Wallet.ts'exportconstgetAddresses=async (wallet:Wallet):Promise<string[]> => {walletStrategy.setWallet(wallet)constaddresses=awaitwalletStrategy.getAddresses()if (addresses.length===0) {thrownewWalletException(newError('There are no addresses linked in this wallet.'), { code: UnspecifiedErrorCode, type:ErrorType.WalletError, }, ) }if (!addresses.every((address) =>!!address)) {thrownewWalletException(newError('There are no addresses linked in this wallet.'), { code: UnspecifiedErrorCode, type:ErrorType.WalletError, }, ) }// If we are using Ethereum native wallets the 'addresses' are the hex addresses// If we are using Cosmos native wallets the 'addresses' are bech32 injective addresses,return addresses}
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.
Once we have these functions we can call them anywhere in our application (usually the centralized state management services like Pinia in Nuxt, or Context providers in React, etc).
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.
// filename: Transactions.tsimport { BigNumberInWei } from'@injectivelabs/utils'import { MsgSend, MsgCreateSpotLimitOrder, spotPriceToChainPriceToFixed, MsgCreateDerivativeMarketOrder, spotQuantityToChainQuantityToFixed} from'@injectivelabs/sdk-ts'// used to send assets from one address to anotherexportconstmakeMsgSend= ({ sender: string, recipient: string, amount: string,// human readable amount denom: string}) => {constamount= { denom, amount:newBigNumberInBase(amount).toWei(/** denom's decimals */) }returnMsgSend.fromJSON({ amount, srcInjectiveAddress: sender, dstInjectiveAddress: recipient, })}// used to create a spot limit orderexportconstmakeMsgCreateSpotLimitOrder= ({ price,// human readable number quantity,// human readable number orderType,// OrderType enum injectiveAddress,}) => {constsubaccountId=getDefaultSubaccountId(injectiveAddress)constmarket= { marketId:'0x...', baseDecimals:18, quoteDecimals:6, minPriceTickSize:'',/* fetched from the chain */ minQuantityTickSize:'',/* fetched from the chain */ priceTensMultiplier:'',/** can be fetched from getDerivativeMarketTensMultiplier */ quantityTensMultiplier:'',/** can be fetched from getDerivativeMarketTensMultiplier */ }returnMsgCreateSpotLimitOrder.fromJSON({ subaccountId, injectiveAddress, orderType: orderType, price:spotPriceToChainPriceToFixed({ value: price, tensMultiplier:market.priceTensMultiplier, baseDecimals:market.baseDecimals, quoteDecimals:market.quoteDecimals }), quantity:spotQuantityToChainQuantityToFixed({ value: quantity, tensMultiplier:market.quantityTensMultiplier, baseDecimals:market.baseDecimals }), marketId:market.marketId, feeRecipient: injectiveAddress, })}// used to create a derivative market orderexportconstmakeMsgCreateDerivativeMarketOrder= ({ price,// human readable number margin,// human readable number quantity,// human readable number orderType,// OrderType enum injectiveAddress,}) => {constsubaccountId=getDefaultSubaccountId(injectiveAddress)constmarket= { marketId:'0x...', baseDecimals:18, quoteDecimals:6, minPriceTickSize:'',/* fetched from the chain */ minQuantityTickSize:'',/* fetched from the chain */ priceTensMultiplier:'',/** can be fetched from getDerivativeMarketTensMultiplier */ quantityTensMultiplier:'',/** can be fetched from getDerivativeMarketTensMultiplier */ }returnMsgCreateDerivativeMarketOrder.fromJSON( orderType: orderPrice, triggerPrice: '0', injectiveAddress, price: derivativePriceToChainPriceToFixed({ value:order.price, tensMultiplier:market.priceTensMultiplier, quoteDecimals:market.quoteDecimals }), quantity: derivativeQuantityToChainQuantityToFixed({ value:order.quantity, tensMultiplier:market.quantityTensMultiplier, }), margin: derivativeMarginToChainMarginToFixed({ value:order.margin, quoteDecimals:market.quoteDecimals, tensMultiplier: priceTensMultiplier, }), marketId: market.marketId, feeRecipient: feeRecipient, subaccountId: subaccountI })}
After we have the Messages, you can use the msgBroadcaster client to broadcast these transactions:
constresponse=awaitmsgBroadcaster({ msgs:/** the message here */, injectiveAddress: signersInjectiveAddress,})console.log(response)
Final Thoughts
What's left for you is to build a nice UI around the business logic explained above :)