4 releases

0.1.3 Aug 7, 2022
0.1.2 Jul 30, 2022
0.1.1 Jul 30, 2022
0.1.0 Jul 30, 2022

#56 in #cpi

Custom license

10KB

marginfi

License

marginfi.com

marginfi is a decentralized portfolio margining protocol for trading on Solana. The protocol gives traders a unified account to access margin, compose a portfolio, and improve capital efficiency across underlying trading protocols.

Overview

This crate provides a CPI interface to the marginfi program. It leverages the anchor-gen tool to generate it from the program IDL.

Usage

use anchor_lang::prelude::*;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[program]
pub mod example_cpi {

    use super::*;

    /// -----------------------------------------------------------------------
    /// INITIALIZE
    /// -----------------------------------------------------------------------

    #[derive(Accounts)]
    pub struct InitializeAccount<'info> {
        /// CHECK: no validation, for educational purpose only
        pub marginfi_program: AccountInfo<'info>,

        /// CHECK: no validation, for educational purpose only
        pub marginfi_group: AccountInfo<'info>,

        /// CHECK: no validation, for educational purpose only
        #[account(zero)]
        pub marginfi_account: AccountInfo<'info>,

        #[account(mut)]
        signer: Signer<'info>,

        pub system_program: Program<'info, System>,
    }

    pub fn initialize_account(ctx: Context<InitializeAccount>) -> Result<()> {
        let ctx = CpiContext::new(
            ctx.accounts.marginfi_program.to_account_info(),
            marginfi_cpi::cpi::accounts::InitMarginfiAccount {
                authority: ctx.accounts.signer.to_account_info(),
                marginfi_account: ctx.accounts.marginfi_account.to_account_info(),
                marginfi_group: ctx.accounts.marginfi_group.to_account_info(),
                system_program: ctx.accounts.system_program.to_account_info(),
            },
        );
        marginfi_cpi::cpi::init_marginfi_account(ctx)?;
        Ok(())
    }
}

For a more complete example usage, check out the sample program

Dependencies

~18–27MB
~463K SLoC