The marginfi Rust CLI

With the marginfi CLI tool, you can interact with the marginfi protocol directly from your terminal, taking advantage of its borrowing and lending services. Built in Rust, this command-line interface simplifies the process of managing your marginfi activities. Whether you want to create a new bank, make deposits to existing banks, or perform other operations, the marginfi CLI provides a streamlined experience.


Installation

To get started, install the latest stable Rust toolchain and set it to default:

rustup default stable-x86_64-apple-darwin
rustc --version

Then, download the CLI tool, navigate inside the folder, and execute the build with the --all-features flag and target x86_64:

git clone https://github.com/mrgnlabs/marginfi-v2.git
cd marginfi-v2/clients/rust/marginfi-cli
cargo install --path . --target x86_64-apple-darwin --all-features

You can then test if the CLI has been installed correctly by running mfi. You should see output like so:

mfi
marginfi-v2-cli 0.1.0

USAGE:
    mfi [OPTIONS] <SUBCOMMAND>

OPTIONS:
        --dry-run              Dry run for any transactions involved
    -h, --help                 Print help information
    -V, --version              Print version information
    -y, --skip-confirmation

SUBCOMMANDS:
    account
    bank
    group
    help                        Print this message or the help of the given subcommand(s)
    inspect-padding
    inspect-size
    inspect-switchboard-feed
    lip
    profile

Getting Started

Step 1: Create a Profile

First, we must create a profile which we will use to interact with the marginfi protocol. To create a profile, use the profile subcommand:

mfi profile create --name <NAME> --cluster <CLUSTER> --keypair-path <KEYPAIR_PATH> --rpc-url <RPC_URL>

For example:

mfi profile create --name masterzorgon --cluster mainnet --keypair-path /Users/MasterZorgon/Documents/Keypairs/UPX866A3Lrdghw1BUdxBkCUMpVEZaokZqFxLskUTiMV.json --rpc-url https://api.mainnet-beta.solana.com

Confirm that you’ve successfully created a profile like so:

mfi profile show

The output should look like the following:

Profile:
    Name: masterzorgon
    Program: MFv2hWf31Z9kbCa1snEPYctwafyhdvnV7FZnsebVacA
    Marginfi Group: None
    Marginfi Account: None
    Cluster: mainnet
    Rpc URL: https://api.mainnet-beta.solana.com
    Fee Payer: UPX866A3Lrdghw1BUdxBkCUMpVEZaokZqFxLskUTiMV
    Authority: UPX866A3Lrdghw1BUdxBkCUMpVEZaokZqFxLskUTiMV
    Keypair: /Users/MasterZorgon/Documents/Keypairs/UPX866A3Lrdghw1BUdxBkCUMpVEZaokZqFxLskUTiMV.json
    Multisig: None

Step 2: Create a Group

A group represents a lending pool with a set of rules and configurations. It acts as a container for multiple lending banks, each with its own settings. The group defines the overall parameters for the lending and borrowing activities within its scope.

You can create a group using the group subcommand:

mfi group create

You will then be shown the profile you recently created. Type the name of your profile to continue:

Command: Create

Profile:
    Name: masterzorgon
    Program: MFv2hWf31Z9kbCa1snEPYctwafyhdvnV7FZnsebVacA
    Marginfi Group: None
    Marginfi Account: None
    Cluster: mainnet
    Rpc URL: https://api.mainnet-beta.solana.com
    Fee Payer: UPX866A3Lrdghw1BUdxBkCUMpVEZaokZqFxLskUTiMV
    Authority: UPX866A3Lrdghw1BUdxBkCUMpVEZaokZqFxLskUTiMV
    Keypair: /Users/MasterZorgon/Documents/Keypairs/UPX866A3Lrdghw1BUdxBkCUMpVEZaokZqFxLskUTiMV.json
    Multisig: None
        
Type the name of the profile [masterzorgon] to continue  

Step 3: Create an Account

An account represents an individual user's position within a group. Each user has their own account, which manages their deposits, borrows, and collateral levels. Accounts interact with the lending banks within a group to perform actions like depositing funds, borrowing assets, or repaying loans.

Before you can start borrowing and lending via the CLI, you must set an account and link it to your profile:

mfi account create

You will then be shown the profile you recently created. Type the name of your profile to continue:

Command: Create

Profile:
    Name: masterzorgon
    Program: MFv2hWf31Z9kbCa1snEPYctwafyhdvnV7FZnsebVacA
    Marginfi Group: None
    Marginfi Account: None
    Cluster: mainnet
    Rpc URL: https://api.mainnet-beta.solana.com
    Fee Payer: UPX866A3Lrdghw1BUdxBkCUMpVEZaokZqFxLskUTiMV
    Authority: UPX866A3Lrdghw1BUdxBkCUMpVEZaokZqFxLskUTiMV
    Keypair: /Users/MasterZorgon/Documents/Keypairs/UPX866A3Lrdghw1BUdxBkCUMpVEZaokZqFxLskUTiMV.json
    Multisig: None
        
Type the name of the profile [masterzorgon] to continue

Step 4: Deposit to a Bank

Now that you’ve created your account, you can interact with the marginfi protocol. Let’s start with lending to a marginfi bank. In this example, we’re going to deposit 1 USDC into the USDC pool 3uxNepDbmkDNq6JhRja5Z8QwbTrfmkKP8AKZV5chYDGG:

mfi account deposit 3uxNepDbmkDNq6JhRja5Z8QwbTrfmkKP8AKZV5chYDGG 1

You will then be shown your profile and asked to confirm the profile you want to interact with by typing the profile name:

Command: Deposit {
    bank: 3uxNepDbmkDNq6JhRja5Z8QwbTrfmkKP8AKZV5chYDGG,
    ui_amount: 1.0,
}

Profile:
    Name: masterzorgon
    Program: MFv2hWf31Z9kbCa1snEPYctwafyhdvnV7FZnsebVacA
    Marginfi Group: None
    Marginfi Account: None
    Cluster: mainnet
    Rpc URL: https://api.mainnet-beta.solana.com
    Fee Payer: UPX866A3Lrdghw1BUdxBkCUMpVEZaokZqFxLskUTiMV
    Authority: UPX866A3Lrdghw1BUdxBkCUMpVEZaokZqFxLskUTiMV
    Keypair: /Users/MasterZorgon/Documents/Keypairs/UPX866A3Lrdghw1BUdxBkCUMpVEZaokZqFxLskUTiMV.json
    Multisig: None
        
Type the name of the profile [masterzorgon] to continue

Subcommands

Group

Manages lending groups in the marginlend protocol.

  • Name
    add-bank
    Description

    Adds a new lending bank to the marginfi group. (Enabled with admin feature)

  • Name
    create
    Description

    Creates a new marginfi group. (Enabled with admin feature)

  • Name
    get
    Description

    Retrieves information about the specified marginfi group.

  • Name
    get-all
    Description

    Retrieves information about all marginfi groups.

  • Name
    handle-bankruptcy
    Description

    Handles bankruptcy for the specified marginfi accounts in the group. (Enabled with admin feature)

  • Name
    update
    Description

    Updates an existing marginfi group. (Enabled with admin feature)

  • Name
    update-lookup-table
    Description

    Updates the token lookup table for the marginfi group. (Enabled with admin feature)

Bank

Handles operations related to lending banks within groups.

  • Name
    collect-fees
    Description

    Collects fees from a lending bank. (Enabled with admin feature)

  • Name
    get
    Description

    Retrieves information about the specified lending bank.

  • Name
    get-all
    Description

    Retrieves information about all lending banks in the specified marginfi group.

  • Name
    inspect-price-oracle
    Description

    Inspects the price oracle for a lending bank.

  • Name
    settle-all-emissions
    Description

    Settles all pending emissions (rewards) for a lending bank. (Enabled with admin feature)

  • Name
    setup-emissions
    Description

    Sets up emissions (rewards) for a lending bank.

  • Name
    update
    Description

    Updates the configuration of a lending bank. (Enabled with admin feature)

  • Name
    update-emissions
    Description

    Updates the emissions (rewards) parameters for a lending bank. (Enabled with admin feature)

  • Name
    withdraw-fees
    Description

    Withdraws fees from a lending bank. (Enabled with admin feature)

  • Name
    withdraw-insurance
    Description

    Withdraws insurance funds from a lending bank. (Enabled with admin feature)

Profile

Manages user profiles or configurations.

  • Name
    create
    Description

    Creates a new profile configuration.

  • Name
    list
    Description

    Lists all available profile configurations.

  • Name
    set
    Description

    Sets the current profile configuration.

  • Name
    show
    Description

    Shows the current profile configuration.

  • Name
    update
    Description

    Updates an existing profile configuration.

InspectPadding

The inspect-padding subcommand, which is compiled only when the dev feature is enabled, is used to inspect the padding or memory layout-related information for development or debugging purposes.

PatchIdl

The patch-idl subcommand, which is compiled only when the dev feature is enabled, is used to modify the JSON Interface Definition Library (IDL) file for the MarginFi program.

InspectSize

The inspect-size subcommand, which is compiled only when the dev feature is enabled, is used to print the size (in bytes) of various data structures used in the application.

Account

Manages user accounts within the marginlend protocol.

  • Name
    borrow
    Description

    Borrows funds from a lending bank.

  • Name
    create
    Description

    Creates a new marginfi account.

  • Name
    deposit
    Description

    Deposits funds into a lending bank.

  • Name
    get
    Description

    Retrieves information about the specified marginfi account or the currently active account.

  • Name
    liquidate
    Description

    Liquidates a marginfi account by repaying its debt and seizing collateral.

  • Name
    list
    Description

    Lists all available marginfi accounts.

  • Name
    set-flag
    Description

    Sets a flag on a marginfi account.

  • Name
    use
    Description

    Sets the currently active marginfi account.

  • Name
    withdraw
    Description

    Withdraws funds from a lending bank.

Lip

The lip subcommand is related to managing LIP (Liquidity Incentive Program) campaigns and deposits.

  • Name
    list-campaigns
    Description

    Lists all LIP campaigns.

  • Name
    list-deposits
    Description

    Lists all deposits made to LIP campaigns.

InspectSwitchboardFeed

The inspect-switchboard-feed subcommand, which is compiled only when the dev feature is enabled, inspects Switchboard oracle feeds, used for obtaining price data on listings.

ShowOracleAges

Displays information about the ages or staleness of oracle data sources, with an option to filter for only stale oracles.

Was this page helpful?