An example is a thousand word. Is something like this possible:
#[program]
pub mod cross_chain_swap {
use super::*;
pub fn my_fn(Either<Context<Instruction1>, Context<Instruction2>>) -> Result<()> {...}
Basically, I'm implementing kind of a "swap" program. I have to deal with either native SOL or arbitrary ERC20 tokens. So, I have two instruction:
#[derive(Accounts)]
pub struct Native<'info> {
#[account(mut)]
pub program_token_account: SystemAccount<'info>,
#[account(mut)]
pub user_token_account: SystemAccount<'info>,
}
#[derive(Accounts)]
pub struct Erc20<'info> {
pub user: SystemAccount<'info>,
pub token_mint: Account<'info, Mint>,
#[account(
mut,
associated_token::mint = token_mint,
associated_token::authority = user
)]
pub signer_token_account: Account<'info, TokenAccount>,
#[account(
mut,
associated_token::mint = token_mint,
associated_token::authority = my_program_account,
)]
pub program_token_account: Box<Account<'info, TokenAccount>>,
}
Both these instructions essentially does the same thing, but it mean that I need two different functions for the same behavior. I would like to have a function that can either take the "Native" Instruction, or the "Erc20" one