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

View file

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

View file

@ -30,7 +30,18 @@ pub enum InInstruction {
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))]
pub struct RefundableInInstruction {
pub origin: Option<ExternalAddress>,

View file

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