aleph-solana-contract

Crates.ioaleph-solana-contract
lib.rsaleph-solana-contract
version0.1.1
sourcesrc
created_at2023-04-12 13:14:18.555963
updated_at2023-06-23 14:01:34.207765
descriptionAleph Solana program for handling messages
homepage
repositoryhttps://github.com/ricardocr987/aleph-solana-contract
max_upload_size
id836908
size5,204
Riki (ricardocr987)

documentation

README

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

Commit count: 12

cargo fmt