Documentation
SocialsStatisticsLaunch App
  • Welcome to Mars Protocol
  • Getting Started
    • How to set up a Wallet
    • Connect your wallet
    • Using a Credit Account
  • Credit Accounts
  • Perpetual Futures (Perps)
    • Perps Vault (Counterparty Vault)
    • Funding Rate Mechanism
    • Price Impact
    • Open Interest Caps
    • Vault Solvency Protection
    • Health Factor
    • Liquidations
  • Spot & Margin Trading
  • Lending & Borrowing
  • Leveraged Yield Farming
  • High Leverage Strategies
  • Managed Vaults
    • Creating a Vault
      • How to Resume Vault Creation After Failed Transaction
    • Managing a Vault
    • Depositing into Vault
  • Risk Methodology
    • Asset Listing
    • Protocol Risk Framework
    • Perps Risk Framework
      • Maximum Leverage & LTVs
      • SkewScale
      • Open Interest Caps
      • Maximum Funding Velocity
      • Mitigating Risks of Static Parameters
    • Deposit Caps Risk Framework
  • Governance
    • MARS Token
  • Smart Contracts
    • Address Provider
    • Account NFT
    • Credit Manager
    • Health
    • Incentives
    • Oracle
    • Params
    • Red Bank
    • Rewards Collector
    • Swapper
    • Zapper
    • Perps
  • Brand kit
    • The Mars Brand
  • Legal
    • Mars FUD Bible
    • Terms of Service
    • Privacy Policy
    • Cookie Policy
Powered by GitBook
On this page
  • Deployments
  • Types
  • Conditions
  • Actions
  • Queries
  • Methods
Edit on GitHub
Export as PDF
  1. Smart Contracts

Credit Manager

The Credit Manager Contract is the centerpiece of the Mars Protocol v2. It serves as a proxy between the user, the Red Bank, the liquidation engine, and the Perps contracts.

Last updated 25 days ago

Deployments

Neutron:

Osmosis:


Types

The types of the Credit Manager Contract can be found .

For reference on the Queries and Methods:

Base Types
type AccountKind =
  | ('default' | 'high_levered_strategy')
  | {
      fund_manager: {
        vault_addr: string
      }
    }
type ActionKind = 'default' | 'liquidation'
type Expiration =
  | {
      at_height: number
    }
  | {
      at_time: Timestamp
    }
  | {
      never: {}
    }
type HealthState =
  | 'healthy'
  | {
      unhealthy: {
        max_ltv_health_factor: Decimal
      }
    }
type Int128 = string    
type Uint128 = string
Coin Types
interface Coin {
  amount: Uint128
  denom: string
  [k: string]: unknown
}

interface ActionCoin {
  amount: 'account_balance' | { exact: Uint128 }
  denom: string
}

Conditions

Conditions are used to create triggers for trigger orders. They are referenced as the Condition type in the Methods below.

health_factor

{
    health_factor: {
        comparison: 'greater_than' | 'less_than'
        threshold: Decimal
    }
}

oracle_price

{
    oracle_price: {
        comparison: 'greater_than' | 'less_than'
        denom: string
        price: Decimal
    }
}

relative_price

{
    relative_price: {
        base_price_denom: string
        comparison: 'greater_than' | 'less_than'
        price: Decimal
        quote_price_denom: string
    }
}

Actions

A central part of the Credit Manager are the Actions. Actions can be combined into powerful sequences by lining them up in an array (they will be executed in a top to down maner). They are referenced via the Action type in the Methods below.

borrow

{
    borrow: Coin
}

claim_astro_lp_rewards

{
    claim_astro_lp_rewards: {
        lp_denom: string
    }
}

claim_rewards

{
    claim_rewards: {}
}

create_trigger_order

{
    create_trigger_order: {
        actions: Action[]
        conditions: Condition[]
        keeper_fee: Coin
    }
}

delete_trigger_order

{
    delete_trigger_order: {
        trigger_order_id: string
    }
}

deposit

{
    deposit: Coin
}

deposit_to_perp_vault

{
    deposit_to_perp_vault: {
        coin: ActionCoin
        max_receivable_shares?: Uint128 | null
    }
}

enter_vault

{
    enter_vault: {
        coin: ActionCoin
        vault: string
    }
}

execute_perp_order

{
    execute_perp_order: {
        denom: string
        order_size: Int128
        reduce_only?: boolean | null
    }
}

exit_vault

{
    exit_vault: {
        amount: Uint128
        vault: string
    }
}

exit_vault_unlocked

{
    exit_vault_unlocked: {
        id: number
        vault: VaultBaseForString
    }
}

lend

{
    lend: ActionCoin
}

liquidate

{
    debt_coin: Coin
    liquidatee_account_id: string
    request: {
        deposit: string
    } | {
        lend: string
    } | {
        vault: {
          position_type: 'u_n_l_o_c_k_e_d' | 'l_o_c_k_e_d' | 'u_n_l_o_c_k_i_n_g'
          request_vault: string
        }
    } | {
        staked_astro_lp: string
    }
}

provide_liquidity

{
    provide_liquidity: {
        coins_in: ActionCoin[]
        lp_token_out: string
        slippage: Decimal
    }
}

reclaim

{
    reclaim: ActionCoin
}

refund_all_coin_balances

{
    refund_all_coin_balances: {}
}

repay

{
    repay: {
        coin: ActionCoin
        recipient_account_id?: string | null
    }
}

request_vault_unlock

{
    request_vault_unlock: {
        amount: Uint128
        vault: string
    }
}

stake_astro_lp

{
    stake_astro_lp: {
        lp_token: ActionCoin
    }
}

swap_exact_in

swap_exact_in: {
    coin_in: ActionCoin
    denom_out: string
    min_receive: Uint128
    route?: {
        astro: {
            swaps: [
                {
                    from: string
                    to: string
                },
                ...
            ]
        }
    }
    | {
        osmo: {
            swaps: [
                {
                    pool_id: number
                    to: string
                },
                ...
            ]
        }
    } | null
}

unlock_from_perp_vault

{
    unlock_from_perp_vault: {
        shares: Uint128
    }
}

unstake_astro_lp

{
    unstake_astro_lp: {
        lp_token: ActionCoin
    }
}

withdraw

{
    withdraw: ActionCoin
}

withdraw_from_perp_vault

{
    withdraw_from_perp_vault: {
        min_receive?: Uint128 | null
    }
}

withdraw_liquidity

{
    withdraw_liquidity: {
        lp_token: ActionCoin
        slippage: Decimal
    }
}

withdraw_to_wallet

{
    withdraw_to_wallet: {
        coin: ActionCoin
        recipient: string
    }
}

Queries

account_kind

Returns the AccountKind of a specific account_id.

Query message
{
    account_kind: {
        account_id: string
    }
}
Return output
{
    data: AccountKind
}

accounts

Query message
{
    accounts: {
        limit?: number | null
        owner: string
        start_after?: string | null
    }  
}
Return output
{
    data: [
        {
            id: string
            kind: AccountKind
        },
        ...
    ]
}

all_account_trigger_orders

Query message
{
    all_account_trigger_orders: {
        account_id: string
        limit?: number | null
        start_after?: string | null
    }
}
Return output
{
    data: {
        data: [
            {
                account_id: string
                order: {
                    actions: Action[]
                    conditions: Condition[]
                    keeper_fee: Coin
                    order_id: string
                }
            },
            ...
        ]
        metadata: {
            has_more: boolean
        }
    }
}

all_coin_balances

Query message
{
    all_coin_balances: {
        limit?: number | null
        start_after?: [string, string] | null
    }    
}
Return output
{
    data: [
        {
            account_id: string
            amount: Uint128
            denom: string
        },
        ...
    ]
}

all_debt_shares

Query message
{
    all_debt_shares: {
        limit?: number | null
        start_after?: [string, string] | null
    }    
}
Return output
{
    data: [
        {
            account_id: string
            shares: Uint128
            denom: string
        },
        ...
    ]
}

all_total_debt_shares

Query message
{
    all_total_debt_shares: {
        limit?: number | null
        start_after?: string | null
    }
}
Return output
{
    data: [
        {
            shares: Uint128
            denom: string
        },
        ...
    ]
}

all_trigger_orders

Query message
{
    all_trigger_orders: {
        limit?: number | null
        start_after?: [string, string] | null
    }
}
Return output
{
    data: {
        data: [
            {
                account_id: string
                order: {
                    actions: Action[]
                    conditions: Condition[]
                    keeper_fee: Coin
                    order_id: string
                }
            },
            ...
        ]
        metadata: {
            has_more: boolean
        }
    }
}

all_vault_positions

Query message
{
    all_vault_positions: {
        limit?: number | null
        start_after?: [string, string] | null
    }
}
Return output
{    
    data: [
        {
            account_id: string
            position: {
                vault_position: {
                    amount: {
                        unlocked: string
                    } | {
                        locking: {
                            locked: string
                            unlocking: [
                                {
                                    coin: Coin
                                    id: number
                                },
                            ...
                            ]
                        }
                    }
                    vault: string
                }
            }
        },
        ...
    ]
}

all_vault_utilizations

Query message
{
    all_vault_utilizations: {
        limit?: number | null
        start_after?: string | null
    }
}
Return output
{
    data: {
        data: [
            {
                utilization: Coin
                vault: string
            },
            ...
        ]
        metadata: {
            has_more: boolean
        }
    }
}

config

Query message
{
    config: {}    
}
Return output
{
    data: {
        account_nft?: string | null
        health_contract: string
        incentives: string
        keeper_fee_config: {
            min_fee: Coin
        }
        max_slippage: Decimal
        max_unlocking_positions: Uint128
        oracle: string
        ownership: {
            abolished: boolean
            emergency_owner?: string | null
            initialized: boolean
            owner?: string | null
            proposed?: string | null
        }
        params: string
        perps: string
        perps_liquidation_bonus_ratio: Decimal
        red_bank: string
        rewards_collector?: {
          account_id: string
          address: string
        } | null
        swapper: string
        zapper: string
    }
}

estimate_provide_liquidity

Query message
{
    estimate_provide_liquidity: {
        coins_in: Coin[]
        lp_token_out: string
    }
}
Return output
{
    data: Uint128
}

estimate_withdraw_liquidity

Query message
{
    estimate_withdraw_liquidity: {
        lp_token: Coin
    }
}
Return output
{
    data: Coin[]
}

positions

Query message
{
    positions: {
        account_id: string
        action?: ActionKind | null
    }
}
Return output
{
    data: {
        account_id: string
        account_kind: AccountKind
        debts: [
            {
                amount: Uint128
                denom: string
                shares: Uint128
            },
            ...
        ]
        deposits: Coin[]
        lends: Coin[]
        perps: [
            {
                base_denom: string
                current_exec_price: Decimal
                current_price: Decimal
                denom: string
                entry_exec_price: Decimal
                entry_price: Decimal
                realized_pnl: {
                    accrued_funding: Int128
                    closing_fee: Int128
                    opening_fee: Int128
                    pnl: Int128
                    price_pnl: Int128
                }
                size: Int128
                unrealized_pnl: {
                    accrued_funding: Int128
                    closing_fee: Int128
                    opening_fee: Int128
                    pnl: Int128
                    price_pnl: Int128
                }
            },
            ...
        ]
        staked_astro_lps: Coin[]
        vaults: [
            {
                amount: {
                    unlocked: string
                } | {
                    locking: {
                        locked: string
                        unlocking: [
                            {
                                coin: Coin
                                id: number
                            },
                        ...
                        ]
                    }
                }
                vault: string
            },
            ...
        ]
    }
}

total_debt_shares

Query message
{
    total_debt_shares: string
}
Return output
{
    data: {
        shares: Uint128
        denom: string
    }
}

vault_bindings

Query message
{
    vault_bindings: {
        limit?: number | null
        start_after?: string | null
    }
}
Return output
{
    data: {
        data: [
            {
                account_id: string
                vault_address: string
            },
            ...
        ]
        metadata: {
            has_more: boolean
        }
    }
}

vault_position_value

Query message
{
    vault_position_value: {
        vault_position: {
            amount: {
                unlocked: string
            } | {
                locking: {
                    locked: string
                    unlocking: [
                        {
                            coin: Coin
                            id: number
                        },
                    ...
                    ]
                }
            }
            vault: string
        }
    }
}
Return output
{
    data: {
        base_coin: {
            amount: Uint128
            denom: string
            value: Uint128
        }
        vault_coin: {
            amount: Uint128
            denom: string
            value: Uint128
        }
    }
}

vault_utilization

Query message
{
    vault_utilization: {
        vault: string
    }
}
Return output
{
    data: {
          utilization: Coin
          vault: string
    }
}

Methods

create_credit_account

Execution message
{
    create_credit_account: AccountKind
}

execute_trigger_order

Execution message
{
    execute_trigger_order: {
        account_id: string
        trigger_order_id: string
    }
}

repay_from_wallet

Execution message
{
    repay_from_wallet: {
        account_id: string
    }
}

update_balances_after_deleverage

Execution message
{
    update_balance_after_deleverage: {
        account_id: string
        pnl:  'break_even' | { 
            profit: Coin
        } | {
            loss: Coin
        }
    }
}

update_credit_account

Execution message
{
    update_credit_account: {
        account_id?: string | null
        account_kind?: AccountKind | null
        actions: Action[]
    }
}
neutron1qdzn3l4kn7gsjna2tfpg3g3mwd6kunx4p50lfya59k02846xas6qslgs3r
osmo1f2m24wktq0sw3c0lexlg7fv4kngwyttvzws3a3r3al9ld2s2pvds87jqvf
here