#aleph #solana #message #contract #cpi #emit #events

aleph-solana-contract

Aleph Solana program for handling messages

2 releases

0.1.1 Jun 23, 2023
0.1.0 Apr 12, 2023

#12 in #aleph

MIT/Apache

6KB

The program instructions can be called in two ways:

1- via CPI (in this case another program calls to the to do_message instruction). This one is the more efficient. For this you need:

  • add the dependency to your solana program in the Cargo.toml:
aleph-solana-contract = { version="0.1.0", features = [ "no-entrypoint" ] }

https://crates.io/crates/aleph-solana-contract

  • include the program account in the context of the instruction you want to post a message:
/// CHECK: contraint added to force using actual aleph message program
#[account(address = aleph_solana_contract::ID, executable)]
pub messages_program: UncheckedAccount<'info>
  • build the cpi in the instruction logic:
solana_program::program::invoke(
    &Instruction {
        program_id: ctx.accounts.messages_program.key(),
        accounts: vec![
            AccountMeta::new(ctx.accounts.signer.key(), true), 
            AccountMeta::new_readonly(ctx.accounts.messages_program.key(), false)
        ],
        data: aleph_solana_contract::instruction::DoMessage {
            msgtype: "aggregate".to_string(),
            msgcontent: "content".to_string(),
        }
        .data(),
    },
    &[
        ctx.accounts.signer.to_account_info().clone(),
        ctx.accounts.messages_program.to_account_info().clone(),
    ],
)?;
  • you need to import in the instruction: anchor_lang::InstructionData (to be able to use the data trait when building the instruction)

  • in local you should add this to your Anchor.toml:

[test.validator]
url = "https://api.devnet.solana.com"

[[test.validator.clone]]
address = "ALepH1n9jxScbz45aZhBYVa35zxBNbKSvL6rWQpb4snc"

EXAMPLE

2- including the aleph message instruction inside the transaction in the client, so its creating an specific instruction inside an transaction.

current related discussion in the anchor repo to improve this: add emit_cpi to allow programs to store logs in CPI data:

https://github.com/coral-xyz/anchor/pull/2438

https://github.com/coral-xyz/anchor/issues/2408

https://github.com/coral-xyz/anchor/pull/2438#discussion_r1155121788

Anchor (Solana framework) events are emitted through logs. There are two options:

  • with the emit! macro: more efficient, it serializes the data
  • with the msg! macro: creates a log with the raw json without serialization

Benchmark of these options: https://github.com/coral-xyz/anchor/issues/863#issuecomment-1101779506

Dependencies

~18–28MB
~446K SLoC