Exchange

The exchange module is the heart of the Injective Chain which enables fully decentralized spot and derivative exchange. It is the sine qua non module of the chain and integrates tightly with the auction, insurance, oracle, and peggy modules.

The exchange protocol enables traders to create and trade on arbitrary spot and derivative markets. The entire process of orderbook management, trade execution, order matching and settlement occurs on chain through the logic codified by the exchange module.

Messages

Let's explore (and provide examples) the Messages that the Exchange module exports and we can use to interact with the Injective chain.

MsgDeposit

This Message is used to send coins from the Bank module to a wallet's subaccount

import { Network } from "@injectivelabs/networks";
import { toChainFormat } from "@injectivelabs/utils";
import {
  MsgDeposit,
  MsgBroadcasterWithPk,
  getEthereumAddress,
} from "@injectivelabs/sdk-ts";

const privateKey = "0x...";
const injectiveAddress = "inj1...";

const amount = {
  denom: "inj",
  amount: toChainFormat(1),
};

const ethereumAddress = getEthereumAddress(injectiveAddress);
const subaccountIndex = 0;
const suffix = "0".repeat(23) + subaccountIndex;
const subaccountId = ethereumAddress + suffix;

const msg = MsgDeposit.fromJSON({
  amount,
  subaccountId,
  injectiveAddress,
});

const txHash = await new MsgBroadcasterWithPk({
  privateKey,
  network: Network.Testnet,
}).broadcast({
  msgs: msg,
});

console.log(txHash);

MsgWithdraw

This Message is used to send coins from the wallet's subaccount back to the users Bank funds

MsgCreateSpotLimitOrder

This Message is used to create a spot limit order

MsgCreateSpotMarketOrder

This Message is used to create a spot market order

MsgCreateDerivativeLimitOrder

This Message is used to create a derivative limit order

MsgCreateDerivativeMarketOrder

This Message is used to create a derivative market order

MsgBatchUpdateOrders

This Message is used to batch update orders on the chain

MsgBatchCancelSpotOrders

This Message is used to batch cancel spot orders on the chain

This Message is used to batch cancel spot orders on the chain

MsgBatchCancelDerivativeOrders

MsgRewardsOptOut

This Message is used to opt out of the Trade & Earn program.

MsgExternalTransfer

This message is used to transfer balance from one subaccount to another subaccount.

Note:

  • You cannot transfer from your default subaccountId since that balance is now associated with your Injective address in the bank module. Therefore, in order for MsgExternalTransfer to work, you will need to transfer from a non-default subaccountId.

How to find the subaccountId that you will be transferring from:

How to use funds that are currently associated with your Injective Address in bank module:

  • If you have existing non-default subaccounts, you'll want to do a MsgDeposit to one of your existing non-default subaccountIds and use that subaccountId as the srcSubaccountId below.

  • If you don't have existing non-default subaccounts, you can do a MsgDeposit to a new default subaccountId, which would be done via importing getSubaccountId from sdk-ts and setting the subaccountId field in MsgDeposit to getSubaccountId(injectiveAddress, 1).

Last updated