Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get market id? #267

Open
winktool opened this issue Jun 14, 2024 · 3 comments
Open

How to get market id? #267

winktool opened this issue Jun 14, 2024 · 3 comments

Comments

@winktool
Copy link

I want to write a Solana smart contract to get the market id via Cross Program Invocation (CPI) by providing the address of the tokens ,is this achievable, is there a simple example please? Thanks.

@mubarizkyc
Copy link

Yes it is

use serum_dex::market::Market;

pub mod my_program {
    use super::*;

    pub fn get_market_id(ctx: Context<GetMarketId>, token_mint: Pubkey) -> Result<()> {
        let market = Market::load(ctx.accounts.market.to_account_info(), false)?;

        let token_account = TokenAccount::load(ctx.accounts.token_account.to_account_info())?;
        require_eq!(token_account.mint, token_mint, MarketError::InvalidTokenMint);

        let token_program = Token::load(ctx.accounts.token_program.to_account_info())?;

        let market_info = market.get_market_info(&token_program, &token_account.mint)?;

        msg!("Market ID: {}", market_info.market_index);

        Ok(())
    }
}

#[derive(Accounts)]
pub struct GetMarketId<'info> {
    #[account(address = serum_dex::id())]
    pub serum_program: AccountInfo<'info>,

    #[account(address = token::ID)]
    pub token_program: AccountInfo<'info>,

    #[account(address = spl_associated_token_account::ID)]
    pub associated_token_program: AccountInfo<'info>,

    #[account(address = serum_dex::market::ID)]
    pub market: AccountInfo<'info>,

    #[account(
        mut,
        associated_token::mint = token_mint,
        associated_token::authority = user
    )]
    pub token_account: AccountInfo<'info>,

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

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

@gildastone
Copy link

@mubarizkyc Any link to a repository with the full rust project? Lot of context is missing. Thanks.

@winktool
Copy link
Author

ctx.accounts.market.to_account_info()
ctx.accounts.market.to_account_info()

???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants