Have Shorthand::Raw contain RefundableInInstruction, not an encoded RII

This commit is contained in:
Luke Parker 2023-07-26 14:00:30 -04:00
parent f8afb040dc
commit e00aa3031c
No known key found for this signature in database
3 changed files with 19 additions and 17 deletions
docs/integrations
substrate/in-instructions/primitives/src

View file

@ -67,9 +67,8 @@ Shorthand is an enum which expands to an Refundable In Instruction.
##### Raw ##### Raw
Raw Shorthand encodes a raw Refundable In Instruction in a Data, with no further Raw Shorthand contains a Refundable In Instruction directly. This is a verbose
processing. This is a verbose fallback option for infrequent use cases not fallback option for infrequent use cases not covered by Shorthand.
covered by Shorthand.
##### Swap ##### Swap

View file

@ -30,7 +30,18 @@ pub enum InInstruction {
Dex(Data), Dex(Data),
} }
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Encode, Decode, TypeInfo, RuntimeDebug)] #[derive(
Clone,
PartialEq,
Eq,
Serialize,
Deserialize,
Encode,
Decode,
MaxEncodedLen,
TypeInfo,
RuntimeDebug,
)]
#[cfg_attr(feature = "std", derive(Zeroize))] #[cfg_attr(feature = "std", derive(Zeroize))]
pub struct RefundableInInstruction { pub struct RefundableInInstruction {
pub origin: Option<ExternalAddress>, pub origin: Option<ExternalAddress>,

View file

@ -6,7 +6,7 @@ use serde::{Serialize, Deserialize};
use scale::{Encode, Decode, MaxEncodedLen}; use scale::{Encode, Decode, MaxEncodedLen};
use scale_info::TypeInfo; use scale_info::TypeInfo;
use serai_primitives::{Coin, Amount, SeraiAddress, ExternalAddress, Data}; use serai_primitives::{Coin, Amount, SeraiAddress, ExternalAddress};
use tokens_primitives::OutInstruction; use tokens_primitives::OutInstruction;
@ -19,7 +19,7 @@ use crate::InInstruction;
)] )]
#[cfg_attr(feature = "std", derive(Zeroize))] #[cfg_attr(feature = "std", derive(Zeroize))]
pub enum Shorthand { pub enum Shorthand {
Raw(Data), Raw(RefundableInInstruction),
Swap { Swap {
origin: Option<ExternalAddress>, origin: Option<ExternalAddress>,
coin: Coin, coin: Coin,
@ -36,14 +36,8 @@ pub enum Shorthand {
impl Shorthand { impl Shorthand {
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub fn transfer(origin: Option<ExternalAddress>, address: SeraiAddress) -> Option<Self> { pub fn transfer(origin: Option<ExternalAddress>, address: SeraiAddress) -> Self {
Some(Self::Raw( Self::Raw(RefundableInInstruction { origin, instruction: InInstruction::Transfer(address) })
Data::new(
(RefundableInInstruction { origin, instruction: InInstruction::Transfer(address) })
.encode(),
)
.ok()?,
))
} }
} }
@ -51,9 +45,7 @@ impl TryFrom<Shorthand> for RefundableInInstruction {
type Error = &'static str; type Error = &'static str;
fn try_from(shorthand: Shorthand) -> Result<RefundableInInstruction, &'static str> { fn try_from(shorthand: Shorthand) -> Result<RefundableInInstruction, &'static str> {
Ok(match shorthand { Ok(match shorthand {
Shorthand::Raw(raw) => { Shorthand::Raw(instruction) => instruction,
RefundableInInstruction::decode(&mut raw.data()).map_err(|_| "invalid raw instruction")?
}
Shorthand::Swap { .. } => todo!(), Shorthand::Swap { .. } => todo!(),
Shorthand::AddLiquidity { .. } => todo!(), Shorthand::AddLiquidity { .. } => todo!(),
}) })