use super::pubkey::Pubkey;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[repr(C)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct AccountMeta {
#[cfg_attr(feature = "serde", serde(rename = "p"))]
pub pubkey: Pubkey,
#[cfg_attr(feature = "serde", serde(rename = "s"))]
pub is_signer: bool,
#[cfg_attr(feature = "serde", serde(rename = "w"))]
pub is_writable: bool,
}
impl AccountMeta {
pub fn new(pubkey: Pubkey, is_signer: bool) -> Self {
Self {
pubkey,
is_signer,
is_writable: true,
}
}
pub fn new_readonly(pubkey: Pubkey, is_signer: bool) -> Self {
Self {
pubkey,
is_signer,
is_writable: false,
}
}
}
#[cfg(feature = "solana-instruction-convert-v2")]
impl From<solana_instruction_v2::AccountMeta> for AccountMeta {
#[inline]
fn from(value: solana_instruction_v2::AccountMeta) -> Self {
Self {
pubkey: value.pubkey.into(),
is_signer: value.is_signer,
is_writable: value.is_writable,
}
}
}
#[cfg(feature = "solana-instruction-convert-v2")]
impl From<AccountMeta> for solana_instruction_v2::AccountMeta {
#[inline]
fn from(value: AccountMeta) -> Self {
Self {
pubkey: value.pubkey.into(),
is_signer: value.is_signer,
is_writable: value.is_writable,
}
}
}
#[cfg(feature = "solana-instruction-convert-v3")]
impl From<solana_instruction_v3::AccountMeta> for AccountMeta {
#[inline]
fn from(value: solana_instruction_v3::AccountMeta) -> Self {
Self {
pubkey: value.pubkey.into(),
is_signer: value.is_signer,
is_writable: value.is_writable,
}
}
}
#[cfg(feature = "solana-instruction-convert-v3")]
impl From<AccountMeta> for solana_instruction_v3::AccountMeta {
#[inline]
fn from(value: AccountMeta) -> Self {
Self {
pubkey: value.pubkey.into(),
is_signer: value.is_signer,
is_writable: value.is_writable,
}
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Instruction {
#[cfg_attr(feature = "serde", serde(rename = "p"))]
pub program_id: Pubkey,
#[cfg_attr(feature = "serde", serde(rename = "a"))]
pub accounts: Vec<AccountMeta>,
#[cfg_attr(feature = "serde", serde(with = "serde_bytes", rename = "d"))]
pub data: Vec<u8>,
}
#[cfg(feature = "solana-instruction-convert-v2")]
impl From<solana_instruction_v2::Instruction> for Instruction {
fn from(value: solana_instruction_v2::Instruction) -> Self {
Self {
program_id: value.program_id.into(),
accounts: value.accounts.into_iter().map(Into::into).collect(),
data: value.data,
}
}
}
#[cfg(feature = "solana-instruction-convert-v2")]
impl From<Instruction> for solana_instruction_v2::Instruction {
fn from(value: Instruction) -> Self {
Self {
program_id: value.program_id.into(),
accounts: value.accounts.into_iter().map(Into::into).collect(),
data: value.data,
}
}
}
#[cfg(feature = "solana-instruction-convert-v3")]
impl From<solana_instruction_v3::Instruction> for Instruction {
fn from(value: solana_instruction_v3::Instruction) -> Self {
Self {
program_id: value.program_id.into(),
accounts: value.accounts.into_iter().map(Into::into).collect(),
data: value.data,
}
}
}
#[cfg(feature = "solana-instruction-convert-v3")]
impl From<Instruction> for solana_instruction_v3::Instruction {
fn from(value: Instruction) -> Self {
Self {
program_id: value.program_id.into(),
accounts: value.accounts.into_iter().map(Into::into).collect(),
data: value.data,
}
}
}
#[cfg(test)]
mod test {
use super::AccountMeta;
use crate::common::pubkey::{Pubkey, PUBKEY_BYTES};
const SOL_ADDR_BYTES: [u8; PUBKEY_BYTES] = [
6, 155, 136, 87, 254, 171, 129, 132, 251, 104, 127, 99, 70, 24, 192, 53, 218, 196, 57, 220,
26, 235, 59, 85, 152, 160, 240, 0, 0, 0, 0, 0,
];
const SOL_PUBKEY: Pubkey = Pubkey::new_from_array(SOL_ADDR_BYTES);
#[cfg(feature = "serde")]
#[test]
fn test_accountmeta_serialize_messagepack_unnamed() {
let account = AccountMeta {
pubkey: SOL_PUBKEY,
is_signer: false,
is_writable: true,
};
let encoded = rmp_serde::encode::to_vec(&account).expect("should encode via messagepack");
assert_eq!(encoded.len(), 37);
assert_eq!(encoded[0], 0x93_u8); assert_eq!(encoded[1..3], [0xc4_u8, 32_u8]); assert_eq!(encoded[3..35], SOL_ADDR_BYTES); assert_eq!(encoded[35], 0xc2_u8); assert_eq!(encoded[36], 0xc3_u8); }
#[cfg(feature = "serde")]
#[test]
fn test_accountmeta_deserialize_messagepack_unnamed() {
let expected = AccountMeta {
pubkey: SOL_PUBKEY,
is_signer: false,
is_writable: true,
};
let encoded: [u8; 37] = [
0x93, 0xc4, 32, 6, 155, 136, 87, 254, 171, 129, 132, 251, 104, 127, 99, 70, 24, 192, 53, 218, 196, 57,
220, 26, 235, 59, 85, 152, 160, 240, 0, 0, 0, 0, 0, 0xc2, 0xc3, ];
let decoded = rmp_serde::decode::from_slice::<AccountMeta>(&encoded)
.expect("should decode via messagepack");
assert_eq!(decoded, expected);
}
#[cfg(feature = "serde")]
#[test]
fn test_accountmeta_deserialize_messagepack_extra_field_unnamed() {
let encoded: [u8; 38] = [
0x94, 0xc4, 32, 6, 155, 136, 87, 254, 171, 129, 132, 251, 104, 127, 99, 70, 24, 192, 53, 218, 196, 57,
220, 26, 235, 59, 85, 152, 160, 240, 0, 0, 0, 0, 0, 0xc2, 0xc3, 0xc0, ];
let decoded = rmp_serde::decode::from_slice::<AccountMeta>(&encoded)
.expect_err("should fail to decode");
match decoded {
rmp_serde::decode::Error::LengthMismatch(_) => (),
other => panic!("unexpected error: {}", other),
}
}
#[cfg(feature = "serde")]
#[test]
fn test_accountmeta_serialize_messagepack_named() {
let account = AccountMeta {
pubkey: SOL_PUBKEY,
is_signer: false,
is_writable: true,
};
let encoded =
rmp_serde::encode::to_vec_named(&account).expect("should encode via messagepack");
assert_eq!(encoded.len(), 43);
assert_eq!(encoded[0], 0x83_u8); assert_eq!(encoded[1..3], [0xa1_u8, 0x70_u8]); assert_eq!(encoded[3..5], [0xc4_u8, 32_u8]); assert_eq!(encoded[5..37], SOL_ADDR_BYTES); assert_eq!(encoded[37..39], [0xa1_u8, 0x73_u8]); assert_eq!(encoded[39], 0xc2_u8); assert_eq!(encoded[40..42], [0xa1_u8, 0x77_u8]); assert_eq!(encoded[42], 0xc3_u8); }
#[cfg(feature = "serde")]
#[test]
fn test_accountmeta_deserialize_messagepack_named() {
let expected = AccountMeta {
pubkey: SOL_PUBKEY,
is_signer: false,
is_writable: true,
};
let encoded: [u8; 43] = [
0x83, 0xa1, 0x70, 0xc4, 32, 6, 155, 136, 87, 254, 171, 129, 132, 251, 104, 127, 99, 70, 24, 192, 53, 218, 196, 57,
220, 26, 235, 59, 85, 152, 160, 240, 0, 0, 0, 0, 0, 0xa1, 0x73, 0xc2, 0xa1, 0x77, 0xc3, ];
let decoded = rmp_serde::decode::from_slice::<AccountMeta>(&encoded)
.expect("should decode via messagepack");
assert_eq!(decoded, expected);
}
#[cfg(feature = "serde")]
#[test]
fn test_accountmeta_deserialize_messagepack_extra_field_named() {
let expected = AccountMeta {
pubkey: SOL_PUBKEY,
is_signer: false,
is_writable: true,
};
let encoded: [u8; 46] = [
0x84, 0xa1, 0x70, 0xc4, 32, 6, 155, 136, 87, 254, 171, 129, 132, 251, 104, 127, 99, 70, 24, 192, 53, 218, 196, 57,
220, 26, 235, 59, 85, 152, 160, 240, 0, 0, 0, 0, 0, 0xa1, 0x73, 0xc2, 0xa1, 0x77, 0xc3, 0xa1, 0x65, 0xc0, ];
let decoded = rmp_serde::decode::from_slice::<AccountMeta>(&encoded)
.expect("should decode via messagepack");
assert_eq!(decoded, expected);
}
}