# 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](https://docs.ts.injective.network/transactions/msgbroadcaster#msgbroadcaster--wallet-strategy).

### Query

#### Fetch Vaults

```typescript
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

```typescript
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

```typescript
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

```typescript
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

```typescript
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 */
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mito.fi/integration/automated-vaults.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
