๐Ÿ๏ธAutomated Vaults

This document aims to allow external developers to integrate with Mito's Vaults from within their dApps. We will only show the message examples, how will you integrate them within your dApp is your own choice. Documentation on broadcasting the transactions containing these messages can be found here.

Query

Fetch Vaults

import { IndexerGrpcMitoApi } from '@injectivelabs/sdk-ts'

const MITO_API_ENDPOINT = 'https://k8s.mainnet.mito.grpc-web.injective.network' /** for mainnet */
const mitoApi = new IndexerGrpcMitoApi(MITO_API_ENDPOINT)

const VAULT_CONTRACT_ADDRESS = 'inj1..'

const { ido: launchpad } = await mitoApi.fetchVault({
    contractAddress: VAULT_CONTRACT_ADDRESS,
})

console.log(launchpad)

Fetch LP Holders

import { IndexerGrpcMitoApi } from '@injectivelabs/sdk-ts'

const MITO_API_ENDPOINT = 'https://k8s.mainnet.mito.grpc-web.injective.network' /** for mainnet */
const mitoApi = new IndexerGrpcMitoApi(MITO_API_ENDPOINT)

const VAULT_CONTRACT_ADDRESS = 'inj1..'
const STAKING_CONTRACT_ADDRESS = `inj1gtze7qm07nky47n7mwgj4zatf2s77xqvh3k2n8` /** staking contract address for mainnet */

const { holders } = await mitoApi.fetchLPHolders({
    vaultAddress: VAULT_CONTRACT_ADDRESS,
    stakingContractAddress: STAKING_CONTRACT_ADDRESS,
})

console.log(launchpad)

Transactions

Subscribe to Spot Vault

import { 
  MsgPrivilegedExecuteContract,
  getDefaultSubaccountId,
  ExecPrivilegedArgVaultSubscribe,
  spotQuantityToChainQuantityToFixed 
} from '@injectivelabs/sdk-ts'

enum VaultContractType {
  ManagedVault = 'crates.io:managed-vault',
  CPMM = 'crates.io:vault-cpmm-spot',
  ASMMSpot = 'crates.io:vault-asmm-spot',
  ASMMPerp = 'crates.io:vault-asmm-perp'
}

enum SpotRedemptionType {
  BaseOnly = 'BaseOnly',
  QuoteOnly = 'QuoteOnly',
  BaseAndQuote = 'BaseAndQuote',
  FixedBaseAndQuote = 'FixedBaseAndQuote',
  VariableBaseAndQuote = 'VariableBaseAndQuote'
}

/** For example QUNT/INJ market, it's recommended to fetch the market from chain */
const market = {
  baseDenom: 'factory/...',
  baseDecimals: 6,
  quoteDecimals: 18,
  quoteDenom 'INJ'
}
const sender = `inj1...`
const baseAmount = 10000 /** subscribing 1000 QUNT */
const quoteAmount = 0.15 /** subscribing 0.15 INJ */
const vaultSubaccountId = '0x..' /* fetched from Vault details */
const slippage = {
  max_penalty: '0.1'
}
const VAULT_MASTER_ADDRESS = 'inj1..' /* fetched from Vault details */

const data = ExecPrivilegedArgVaultSubscribe.fromJSON({
  args: vault.vaultType === VaultContractType.CPMM ? { slippage } : {},
  origin: sender,
  vaultSubaccountId,
  traderSubaccountId: getDefaultSubaccountId(sender),
})

const formattedBaseAmount =
  subscriptionType !== SpotRedemptionType.QuoteOnly
    ? `${spotQuantityToChainQuantityToFixed({
        value: baseAmount,
        baseDecimals: market.baseDecimals
      })} ${market.baseDenom}`
    : ''

const formattedQuoteAmount =
  subscriptionType !== SpotRedemptionType.BaseOnly
    ? `${spotQuantityToChainQuantityToFixed({
        value: quoteAmount,
        baseDecimals: market.quoteDecimals
      })} ${market.quoteDenom}`
    : ''

const funds = [formattedBaseAmount, formattedQuoteAmount]
  .filter((amount) => amount)
  .join(', ')

const message = MsgPrivilegedExecuteContract.fromJSON({
  data,
  sender,
  funds,
  contractAddress: VAULT_MASTER_ADDRESS,
})

/** (Prepare) Broadcast the transaction */

Subscribe to Derivative Vault

import { 
  MsgPrivilegedExecuteContract,
  getDefaultSubaccountId,
  ExecPrivilegedArgVaultSubscribe,
  spotQuantityToChainQuantityToFixed 
} from '@injectivelabs/sdk-ts'

enum VaultContractType {
  ManagedVault = 'crates.io:managed-vault',
  CPMM = 'crates.io:vault-cpmm-spot',
  ASMMSpot = 'crates.io:vault-asmm-spot',
  ASMMPerp = 'crates.io:vault-asmm-perp'
}

const marketQuoteDenom = 'peggy0x..' /** for BTC/USDT Perp it's USDT denom */
const marketQuoteDecimals = 6 /** fetched from the Vault's associated market quote asset, for BTC/USDT Perp it's USDT and it's decimals are 6 */
const sender = `inj1...`
const subscribeAmount = 0.01 /** subscribing 0.01 INJ */
const vaultSubaccountId = '0x..' /* fetched from Vault details */
const slippage = {
  max_penalty: '0.1'
}
const VAULT_MASTER_ADDRESS = 'inj1..' /* fetched from Vault details */

const data = ExecPrivilegedArgVaultSubscribe.fromJSON({
  args: vault.vaultType === VaultContractType.CPMM ? { slippage } : {},
  origin: sender,
  vaultSubaccountId,
  traderSubaccountId: getDefaultSubaccountId(sender),
})

const amount = spotQuantityToChainQuantityToFixed({
  value: subscribeAmount,
  baseDecimals: vault.market.quoteToken.decimals
})

const message = MsgPrivilegedExecuteContract.fromJSON({
  data,
  sender,
  contractAddress: VAULT_MASTER_ADDRESS,
  funds: `${amount} ${marketQuoteDenom}`
})

/** (Prepare) Broadcast the transaction */

Redeem Subscription

import { 
  MsgPrivilegedExecuteContract,
  getDefaultSubaccountId,
  ExecPrivilegedArgVaultRedeem,
  spotQuantityToChainQuantityToFixed 
} from '@injectivelabs/sdk-ts'

enum VaultContractType {
  ManagedVault = 'crates.io:managed-vault',
  CPMM = 'crates.io:vault-cpmm-spot',
  ASMMSpot = 'crates.io:vault-asmm-spot',
  ASMMPerp = 'crates.io:vault-asmm-perp'
}

enum DerivativeRedemptionType {
  QuoteOnly = 'QuoteOnly',
  PositionAndQuote = 'PositionAndQuote'
}

enum SpotRedemptionType {
  BaseOnly = 'BaseOnly',
  QuoteOnly = 'QuoteOnly',
  BaseAndQuote = 'BaseAndQuote',
  FixedBaseAndQuote = 'FixedBaseAndQuote',
  VariableBaseAndQuote = 'VariableBaseAndQuote'
}

enum VaultMarketType {
  Spot = 'Spot',
  Derivative = 'Derivative'
}

const vaultLpDenom = 'factory...' /* fetched from Vault details */
const vaultBaseDecimals = 18
const sender = `inj1...`
const redeemAmount = 0.01 /** subscribing 0.01 INJ */
const vaultSubaccountId = '0x..' /* fetched from Vault details */
const slippage = {
  max_penalty: '0.1'
}
const VAULT_MASTER_ADDRESS = 'inj1..' /* fetched from Vault details */
const redemptionType: SpotRedemptionType | DerivativeRedemptionType = 1 /** SpotRedemptionMap.BaseOnly */
const subscriptionMarketType = VaultMarketType.Spot

const data = ExecPrivilegedArgVaultRedeem.fromJSON({
  origin: sender,
  vaultSubaccountId,
  traderSubaccountId: getDefaultSubaccountId(sender),
  args: {
    ...(subscriptionMarketType === VaultMarketType.Derivative
      ? { slippage }
      : {}),
    redemption_type: redemptionType
  }
})

const amount = spotQuantityToChainQuantityToFixed({
  value: redeemAmount,
  baseDecimals: vaultBaseDecimals
})

const message = MsgPrivilegedExecuteContract.fromJSON({
  data,
  sender,
  contractAddress: VAULT_MASTER_ADDRESS,
  funds: `${amount} ${subscription.vaultInfo.lpDenom}`
})

/** (Prepare) Broadcast the transaction */

Last updated