On a low level, solana programs take instructions which only have 3 fields, an account vec, a bytearray of data, and the programID. This makes it easy to invoke programs from other programs by generating those fields and calling solana_program::program::invoke. However, with anchor, program modules have a context field which has Accounts structs to validate accounts, and any number of other arguments. I'm wondering how to invoke these types of programs using base solana_program::program::invoke calls. As an example, consider the following swap instruction which has input fields like so
pub fn swap<'info>(
ctx: Context<'_, '_, '_, 'info, Swap<'info>>,
side: Side,
amount: u64,
min_exchange_rate: ExchangeRate,
)
Where the swap account is defined as follows
pub struct Swap<'info> {
pub market: MarketAccounts<'info>,
#[account(signer)]
pub authority: AccountInfo<'info>,
#[account(mut, constraint = pc_wallet.key != &empty::ID)]
pub pc_wallet: AccountInfo<'info>,
pub dex_program: AccountInfo<'info>,
pub token_program: AccountInfo<'info>,
pub rent: AccountInfo<'info>,
}
pub struct MarketAccounts<'info> {
#[account(mut)]
pub market: AccountInfo<'info>,
#[account(mut)]
pub open_orders: AccountInfo<'info>,
#[account(mut)]
pub request_queue: AccountInfo<'info>,
#[account(mut)]
pub event_queue: AccountInfo<'info>,
#[account(mut)]
pub bids: AccountInfo<'info>,
#[account(mut)]
pub asks: AccountInfo<'info>,
#[account(mut, constraint = order_payer_token_account.key != &empty::ID)]
pub order_payer_token_account: AccountInfo<'info>,
#[account(mut)]
pub coin_vault: AccountInfo<'info>,
#[account(mut)]
pub pc_vault: AccountInfo<'info>,
pub vault_signer: AccountInfo<'info>,
#[account(mut, constraint = coin_wallet.key != &empty::ID)]
pub coin_wallet: AccountInfo<'info>,
}
My assumption is that you can simply serialize the data fields in a single struct, but I'm struggling on how you should interact with the accounts. In the accounts vector, would I simply list them like they are ordered within the struct (so I would first do the market accounts, like [market, open_orders, ..., coin_wallet, authority, ..., rent]) or is there a better method?
Custom program error: 0x65
, which corresponds to"Fallback functions are not supported"
I think this means anchor does not know which instruction I'm trying to execute. So I tried adding a byte to the beginning of the data instruction (hoping that perhaps processes are called by some sort of enum), but this also didn't work.Sha256("<namespace>:<rust-identifier>")[..8]
as mentioned at github.com/coral-xyz/anchor/blob/master/lang/syn/src/codegen/…anchor expand
to see what's happening