๐Ÿช„Permissionless Vaults

This document aims to allow external developers to integrate with Mito's Permissionless Vaults from within their dApps. We will only show the message examples, how will you integrate them within your dApp is your own choice.

Instantiating a CPMM Vault

To instantiate a CPMM vault, you will need to set up the necessary environment, configure your wallet, and broadcast the instantiation message. Below is an example of how to instantiate a CPMM vault using the Injective Labs SDK.

import { NETWORK } from '@injectivelabs/networks'
import { MsgBroadcaster } from "@injectivelabs/wallet-ts"
import {
  Coin,
  toUtf8,
  toBase64,
  IndexerGrpcMitoApi,
  ChainGrpcTokenFactoryApi,
  MsgExecuteContractCompat,
} from "@injectivelabs/sdk-ts";

export const msgBroadcastClient = new MsgBroadcaster({
  walletStrategy /* instantiated wallet strategy */,
  network: NETWORK.Devnet,
});

// Devnet configuration
const CPMM_CONTRACT_CODE = 5;
const MITO_MASTER_CONTRACT_ADDRESS = "inj1wug8sewp6cedgkmrmvhl3lf3tulagm9h5uhctd";

// Mainnet configuration
// const CPMM_CONTRACT_CODE = 540;
// const MITO_MASTER_CONTRACT_ADDRESS = "inj1vcqkkvqs7prqu70dpddfj7kqeqfdz5gg662qs3";

const funds = [
  {
    denom: 'inj',
    amount: '70000000000000000000' // 70 INJ
  },
  {
    denom: 'factory/creatorAddress/subDenom',
    amount: '2100000000' // 2100 of the custom token
  }
];

const senderWalletAddress = 'inj...';
const marketId = '0x....';
const feeBps = 100; // 1%
const baseDecimals = 6;
const quoteDecimals = 18;

export const instantiateCPMMVault = async () => {
  const msgs = MsgExecuteContractCompat.fromJSON({
    contractAddress: MITO_MASTER_CONTRACT_ADDRESS,
    funds,
    exec: {
      action: 'register_vault',
      msg: {
        is_subscribing_with_funds: true,
        registration_mode: {
          permissionless: {
            whitelisted_vault_code_id: CPMM_CONTRACT_CODE,
          },
        },
        instantiate_vault_msg: {
          Amm: {
            owner: senderWalletAddress,
            master_address: MITO_MASTER_CONTRACT_ADDRESS,
            notional_value_cap: '100000000000000000000000', // 100,000 INJ
            market_id: marketId,
            pricing_strategy: {
              SmoothingPricingWithRelativePriceRange: {
                bid_range: '0.8',
                ask_range: '0.8',
              },
            },
            max_invariant_sensitivity_bps: '5',
            max_price_sensitivity_bps: '5',
            fee_bps: 100,
            order_type: 'Vanilla',
            config_owner: senderWalletAddress,
            base_decimals: baseDecimals,
            quote_decimals: quoteDecimals,
          },
        },
      },
    },
    sender: senderWalletAddress,
  });

  await msgBroadcastClient.broadcastV2({
    address: senderWalletAddress,
    msgs,
  });
};

Querying Vaults

To query existing vaults, you can use the IndexerGrpcMitoApi from the Injective Labs SDK. Below is an example of how to fetch vaults.

import { IndexerGrpcMitoApi } from "@injectivelabs/sdk-ts";

const MITO_API_ENDPOINT = 'https://devnet.api.ninja.injective.dev';
// const MITO_API_ENDPOINT = 'https://k8s.mainnet.mito.grpc-web.injective.network'; // mainnet

const mitoApi = new IndexerGrpcMitoApi(MITO_API_ENDPOINT);

const fetchVaults = async () => {
  const { vaults } = await mitoApi.fetchVaults({});
  return vaults;
};

Querying Vault Creation Fee

To query the vault creation fee, you can use the ChainGrpcTokenFactoryApi to fetch module parameters and the vault registration configuration.

import { ChainGrpcTokenFactoryApi, toBase64, toUtf8, Coin } from "@injectivelabs/sdk-ts";

const GRPC_ENDPOINT = "https://devnet.grpc.injective.dev";
// const GRPC_ENDPOINT = 'https://sentry.chain.grpc-web.injective.network'; // mainnet

const tokenFactoryApi = new ChainGrpcTokenFactoryApi(GRPC_ENDPOINT);

const fetchVaultCreationFee = async () => {
  // LP creation fee
  const { denomCreationFee } = await tokenFactoryApi.fetchModuleParams();
  const [fee] = denomCreationFee;

  // Vault creation fee
  const queryConfigPayload = toBase64({ config: {} });
  const response = await tokenFactoryApi.queryContractSmart(MITO_MASTER_CONTRACT_ADDRESS, queryConfigPayload);

  const config = JSON.parse(toUtf8(response.data)) as {
    permissionless_vault_registration_fee: Coin[];
  };

  const permissionlessVaultRegistrationFee = config.permissionless_vault_registration_fee.find(
    ({ denom }) => denom === 'inj'
  );

  return (permissionlessVaultRegistrationFee?.amount || 0) + (fee?.amount || 0);
};

Last updated