Skip to content

Commit

Permalink
add conversion from u32 to chain extension outcome enum
Browse files Browse the repository at this point in the history
  • Loading branch information
gianfra-t committed Nov 22, 2023
1 parent b659746 commit 29d1624
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions runtime/common/src/chain_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,59 @@ impl ChainExtensionArithmeticError {
}
}
}

impl TryFrom<u32> for ChainExtensionOutcome {
type Error = DispatchError;

fn try_from(value: u32) -> Result<Self, Self::Error> {
match value {
0 => Ok(ChainExtensionOutcome::Success),
1 => Ok(ChainExtensionOutcome::Other),
2 => Ok(ChainExtensionOutcome::CannotLookup),
3 => Ok(ChainExtensionOutcome::BadOrigin),
4 => Ok(ChainExtensionOutcome::Module),
5 => Ok(ChainExtensionOutcome::ConsumerRemaining),
6 => Ok(ChainExtensionOutcome::NoProviders),
7 => Ok(ChainExtensionOutcome::TooManyConsumers),
8 => Ok(ChainExtensionOutcome::DecodingError),
9 => Ok(ChainExtensionOutcome::WriteError),
10 => Ok(ChainExtensionOutcome::UnimplementedFuncId),
999 => Ok(ChainExtensionOutcome::Unknown),
1000..=1999 => Ok(ChainExtensionOutcome::Token(ChainExtensionTokenError::try_from(value - 1000)?)),
2000..=2999 => Ok(ChainExtensionOutcome::Arithmetic(ChainExtensionArithmeticError::try_from(value - 2000)?)),
_ => Err(DispatchError::Other("Invalid ChainExtensionOutcome value")),
}
}
}

impl TryFrom<u32> for ChainExtensionTokenError {
type Error = DispatchError;

fn try_from(value: u32) -> Result<Self, Self::Error> {
match value {
0 => Ok(ChainExtensionTokenError::NoFunds),
1 => Ok(ChainExtensionTokenError::WouldDie),
2 => Ok(ChainExtensionTokenError::BelowMinimum),
3 => Ok(ChainExtensionTokenError::CannotCreate),
4 => Ok(ChainExtensionTokenError::UnknownAsset),
5 => Ok(ChainExtensionTokenError::Frozen),
6 => Ok(ChainExtensionTokenError::Unsupported),
999 => Ok(ChainExtensionTokenError::Unknown),
_ => Err(DispatchError::Other("Invalid ChainExtensionTokenError value")),
}
}
}

impl TryFrom<u32> for ChainExtensionArithmeticError {
type Error = DispatchError;

fn try_from(value: u32) -> Result<Self, Self::Error> {
match value {
0 => Ok(ChainExtensionArithmeticError::Underflow),
1 => Ok(ChainExtensionArithmeticError::Overflow),
2 => Ok(ChainExtensionArithmeticError::DivisionByZero),
999 => Ok(ChainExtensionArithmeticError::Unknown),
_ => Err(DispatchError::Other("Invalid ChainExtensionArithmeticError value")),
}
}
}

0 comments on commit 29d1624

Please sign in to comment.