multiliquid-svm-sdk 0.1.2

Unsigned transaction builders, account codecs, and quoting for Multiliquid on Solana
Documentation
//! Unsigned version-0 transactions without compute-budget or submission policy.

use solana_sdk::{
    instruction::Instruction,
    message::{v0, VersionedMessage},
    pubkey::Pubkey,
    signature::Signature,
    transaction::VersionedTransaction,
};

use crate::{
    error::{Result, SdkError},
    rpc::Rpc,
};

/// Compile native instructions with the RPC's recent blockhash and zero-filled
/// signatures for every required signer. No address lookup tables are used.
pub async fn build_unsigned_transaction<R: Rpc + ?Sized>(
    rpc: &R,
    payer: &Pubkey,
    instructions: &[Instruction],
) -> Result<VersionedTransaction> {
    let blockhash = rpc.get_latest_blockhash().await?;
    let message = v0::Message::try_compile(payer, instructions, &[], blockhash)
        .map_err(|error| SdkError::InvalidInput(format!("Cannot compile transaction: {error}")))?;
    let signatures =
        vec![Signature::default(); usize::from(message.header.num_required_signatures)];
    Ok(VersionedTransaction {
        signatures,
        message: VersionedMessage::V0(message),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::rpc::{RpcFilter, SimulationResult};
    use async_trait::async_trait;
    use solana_sdk::{account::Account, hash::Hash, instruction::AccountMeta};

    struct BlockhashRpc(Hash);

    #[async_trait]
    impl Rpc for BlockhashRpc {
        async fn get_account(&self, _: &Pubkey) -> Result<Option<Account>> {
            panic!("unexpected account lookup")
        }
        async fn get_program_accounts(
            &self,
            _: &Pubkey,
            _: &[RpcFilter],
        ) -> Result<Vec<(Pubkey, Account)>> {
            panic!("unexpected program account query")
        }
        async fn get_slot(&self) -> Result<u64> {
            panic!("unexpected slot query")
        }
        async fn get_block_time(&self, _: u64) -> Result<Option<i64>> {
            panic!("unexpected block time query")
        }
        async fn get_latest_blockhash(&self) -> Result<Hash> {
            Ok(self.0)
        }
        async fn get_transaction_logs(&self, _: &str) -> Result<Option<Vec<String>>> {
            panic!("unexpected transaction query")
        }
        async fn simulate_transaction(&self, _: &VersionedTransaction) -> Result<SimulationResult> {
            panic!("unsigned construction must not simulate")
        }
    }

    #[tokio::test]
    async fn compiles_payer_signers_privileges_and_data_without_signing() {
        let payer = Pubkey::new_from_array([1; 32]);
        let signer = Pubkey::new_from_array([2; 32]);
        let writable = Pubkey::new_from_array([3; 32]);
        let program = Pubkey::new_from_array([4; 32]);
        let blockhash = Hash::new_from_array([5; 32]);
        let instruction = Instruction {
            program_id: program,
            accounts: vec![
                AccountMeta::new_readonly(signer, true),
                AccountMeta::new(writable, false),
                AccountMeta::new_readonly(payer, false),
                AccountMeta::new_readonly(writable, false),
            ],
            data: vec![8, 0, 255, 9],
        };
        let transaction =
            build_unsigned_transaction(&BlockhashRpc(blockhash), &payer, &[instruction])
                .await
                .unwrap();
        assert_eq!(transaction.signatures, vec![Signature::default(); 2]);
        let VersionedMessage::V0(message) = &transaction.message else {
            panic!("expected a version-0 message");
        };
        assert_eq!(message.recent_blockhash, blockhash);
        assert_eq!(message.account_keys, vec![payer, signer, writable, program]);
        assert_eq!(message.header.num_required_signatures, 2);
        assert_eq!(message.header.num_readonly_signed_accounts, 1);
        assert_eq!(message.header.num_readonly_unsigned_accounts, 1);
        assert!(message.address_table_lookups.is_empty());
        assert_eq!(message.instructions.len(), 1);
        assert_eq!(message.instructions[0].program_id_index, 3);
        assert_eq!(message.instructions[0].accounts, vec![1, 2, 0, 2]);
        assert_eq!(message.instructions[0].data, vec![8, 0, 255, 9]);
        let wire = bincode::serialize(&transaction).unwrap();
        let decoded: VersionedTransaction = bincode::deserialize(&wire).unwrap();
        assert_eq!(decoded, transaction);
    }
}