use crate::{
domain::solana::Pubkey,
error::{Error, ValidationError},
};
use base64::{Engine as _, engine::general_purpose::STANDARD};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
#[derive(Clone, Eq, PartialEq)]
pub struct InstructionData(Vec<u8>);
impl InstructionData {
pub const MAX_BYTES: usize = 4096;
pub fn new(bytes: Vec<u8>) -> Result<Self, Error> {
if bytes.len() > Self::MAX_BYTES {
return Err(invalid());
}
Ok(Self(bytes))
}
pub fn parse(value: &str) -> Result<Self, Error> {
if value.len() > Self::MAX_BYTES.div_ceil(3) * 4 {
return Err(invalid());
}
let bytes = STANDARD.decode(value).map_err(|_| invalid())?;
if STANDARD.encode(&bytes) != value {
return Err(invalid());
}
Self::new(bytes)
}
#[must_use]
pub fn bytes(&self) -> &[u8] {
&self.0
}
}
impl fmt::Debug for InstructionData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("InstructionData")
.field("byte_length", &self.0.len())
.finish_non_exhaustive()
}
}
impl Serialize for InstructionData {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&STANDARD.encode(&self.0))
}
}
impl<'de> Deserialize<'de> for InstructionData {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
Self::parse(&String::deserialize(d)?).map_err(serde::de::Error::custom)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AccountMeta {
pub pubkey: Pubkey,
pub is_signer: bool,
pub is_writable: bool,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct InstructionFields {
pub program_id: Pubkey,
pub accounts: Vec<AccountMeta>,
pub data: InstructionData,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "InstructionFields", into = "InstructionFields")]
pub struct Instruction(InstructionFields);
impl Instruction {
pub fn new(v: InstructionFields) -> Result<Self, Error> {
if v.accounts.len() > 256 {
return Err(invalid());
}
Ok(Self(v))
}
#[must_use]
pub const fn fields(&self) -> &InstructionFields {
&self.0
}
}
impl TryFrom<InstructionFields> for Instruction {
type Error = Error;
fn try_from(v: InstructionFields) -> Result<Self, Error> {
Self::new(v)
}
}
impl From<Instruction> for InstructionFields {
fn from(v: Instruction) -> Self {
v.0
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "LookupFields", into = "LookupFields")]
pub struct LookupTable {
address: Pubkey,
entries: Vec<Pubkey>,
}
impl LookupTable {
pub fn new(address: Pubkey, entries: Vec<Pubkey>) -> Result<Self, Error> {
if entries.len() > 256 {
return Err(invalid());
}
Ok(Self { address, entries })
}
#[must_use]
pub const fn address(&self) -> Pubkey {
self.address
}
#[must_use]
pub fn entries(&self) -> &[Pubkey] {
&self.entries
}
}
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct LookupFields {
address: Pubkey,
entries: Vec<Pubkey>,
}
impl TryFrom<LookupFields> for LookupTable {
type Error = Error;
fn try_from(v: LookupFields) -> Result<Self, Error> {
Self::new(v.address, v.entries)
}
}
impl From<LookupTable> for LookupFields {
fn from(v: LookupTable) -> Self {
Self {
address: v.address,
entries: v.entries,
}
}
}
fn invalid() -> Error {
ValidationError::InvalidJupiterBuild.into()
}