Skip to content

Commit

Permalink
TryFrom over from
Browse files Browse the repository at this point in the history
  • Loading branch information
birchmd committed Nov 15, 2021
1 parent 6d621a6 commit 339dc43
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
26 changes: 20 additions & 6 deletions engine/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,21 @@ impl AsRef<[u8]> for ERC20Address {
self.0.as_bytes()
}
}
impl From<Vec<u8>> for ERC20Address {
fn from(bytes: Vec<u8>) -> Self {
Self(Address::from_slice(&bytes))
impl TryFrom<Vec<u8>> for ERC20Address {
type Error = AddressParseError;

fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
if bytes.len() == 20 {
Ok(Self(Address::from_slice(&bytes)))
} else {
Err(AddressParseError)
}
}
}
pub struct AddressParseError;
impl AsRef<[u8]> for AddressParseError {
fn as_ref(&self) -> &[u8] {
b"ERR_PARSE_ADDRESS"
}
}

Expand All @@ -218,9 +230,11 @@ impl AsRef<[u8]> for NEP141Account {
self.0.as_bytes()
}
}
impl From<Vec<u8>> for NEP141Account {
fn from(bytes: Vec<u8>) -> Self {
Self(AccountId::try_from(bytes).unwrap())
impl TryFrom<Vec<u8>> for NEP141Account {
type Error = aurora_engine_types::account_id::ParseAccountError;

fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
AccountId::try_from(bytes).map(Self)
}
}

Expand Down
3 changes: 2 additions & 1 deletion engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,8 @@ mod contract {
#[no_mangle]
pub extern "C" fn get_nep141_from_erc20() {
let mut io = Runtime;
let erc20_address: crate::engine::ERC20Address = io.read_input().to_vec().into();
let erc20_address: crate::engine::ERC20Address =
io.read_input().to_vec().try_into().sdk_unwrap();
io.return_output(
Engine::nep141_erc20_map(io)
.lookup_right(&erc20_address)
Expand Down
14 changes: 10 additions & 4 deletions engine/src/map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use crate::prelude::{bytes_to_key, PhantomData, Vec};
pub use crate::prelude::{bytes_to_key, PhantomData, TryFrom, TryInto, Vec};
use aurora_engine_sdk::io::{StorageIntermediate, IO};
use aurora_engine_types::storage::KeyPrefix;

Expand All @@ -12,7 +12,9 @@ pub struct BijectionMap<L, R, I> {
right_phantom: PhantomData<R>,
}

impl<L: AsRef<[u8]> + From<Vec<u8>>, R: AsRef<[u8]> + From<Vec<u8>>, I: IO> BijectionMap<L, R, I> {
impl<L: AsRef<[u8]> + TryFrom<Vec<u8>>, R: AsRef<[u8]> + TryFrom<Vec<u8>>, I: IO>
BijectionMap<L, R, I>
{
pub fn new(left_prefix: KeyPrefix, right_prefix: KeyPrefix, io: I) -> Self {
Self {
left_prefix,
Expand All @@ -33,12 +35,16 @@ impl<L: AsRef<[u8]> + From<Vec<u8>>, R: AsRef<[u8]> + From<Vec<u8>>, I: IO> Bije

pub fn lookup_left(&self, left: &L) -> Option<R> {
let key = self.left_key(left);
self.io.read_storage(&key).map(|v| v.to_vec().into())
self.io
.read_storage(&key)
.and_then(|v| v.to_vec().try_into().ok())
}

pub fn lookup_right(&self, right: &R) -> Option<L> {
let key = self.right_key(right);
self.io.read_storage(&key).map(|v| v.to_vec().into())
self.io
.read_storage(&key)
.and_then(|v| v.to_vec().try_into().ok())
}

fn left_key(&self, left: &L) -> Vec<u8> {
Expand Down

0 comments on commit 339dc43

Please sign in to comment.