|
| 1 | +use std::ops::Deref; |
| 2 | + |
| 3 | +use anchor_lang::{prelude::*, Discriminator}; |
| 4 | +use token_router::state::PreparedFillInfo; |
| 5 | +use wormhole_io::{Readable, TypePrefixedPayload}; |
| 6 | + |
| 7 | +#[derive(Debug, AnchorSerialize, AnchorDeserialize, Clone)] |
| 8 | +pub struct PreparedFill<P: TypePrefixedPayload> { |
| 9 | + pub info: PreparedFillInfo, |
| 10 | + pub message_size: u32, |
| 11 | + pub redeemer_message: P, |
| 12 | +} |
| 13 | + |
| 14 | +impl<P: TypePrefixedPayload> Owner for PreparedFill<P> { |
| 15 | + fn owner() -> Pubkey { |
| 16 | + token_router::ID |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +impl<P: TypePrefixedPayload> Discriminator for PreparedFill<P> { |
| 21 | + const DISCRIMINATOR: [u8; 8] = token_router::state::PreparedFill::DISCRIMINATOR; |
| 22 | +} |
| 23 | + |
| 24 | +impl<P: TypePrefixedPayload> AccountSerialize for PreparedFill<P> { |
| 25 | + fn try_serialize<W: std::io::prelude::Write>(&self, writer: &mut W) -> Result<()> { |
| 26 | + Self::DISCRIMINATOR.serialize(writer)?; |
| 27 | + self.info.serialize(writer)?; |
| 28 | + self.message_size.serialize(writer)?; |
| 29 | + self.redeemer_message.write(writer)?; |
| 30 | + Ok(()) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl<P: TypePrefixedPayload> AccountDeserialize for PreparedFill<P> { |
| 35 | + fn try_deserialize(buf: &mut &[u8]) -> Result<Self> { |
| 36 | + let disc_len = Self::DISCRIMINATOR.len(); |
| 37 | + if buf.len() < disc_len { |
| 38 | + return err!(ErrorCode::AccountDidNotDeserialize); |
| 39 | + }; |
| 40 | + if Self::DISCRIMINATOR != buf[..disc_len] { |
| 41 | + return err!(ErrorCode::AccountDidNotDeserialize); |
| 42 | + } |
| 43 | + Self::try_deserialize_unchecked(buf) |
| 44 | + } |
| 45 | + |
| 46 | + fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self> { |
| 47 | + let mut data = &buf[Self::DISCRIMINATOR.len()..]; |
| 48 | + Ok(Self { |
| 49 | + info: AnchorDeserialize::deserialize(&mut data)?, |
| 50 | + message_size: AnchorDeserialize::deserialize(&mut data)?, |
| 51 | + redeemer_message: Readable::read(&mut data)?, |
| 52 | + }) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl<P: TypePrefixedPayload> Deref for PreparedFill<P> { |
| 57 | + type Target = PreparedFillInfo; |
| 58 | + |
| 59 | + fn deref(&self) -> &Self::Target { |
| 60 | + &self.info |
| 61 | + } |
| 62 | +} |
0 commit comments