Skip to content

Commit

Permalink
Make ArraySpan generic
Browse files Browse the repository at this point in the history
  • Loading branch information
ctoyan committed Apr 29, 2024
1 parent 070048b commit da82021
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
24 changes: 23 additions & 1 deletion ampd/src/starknet/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub mod contract_call;
static CALL_CONTRACT_FELT: OnceLock<FieldElement> = OnceLock::new();

/// All Axelar event types supported by starknet
#[derive(Eq, PartialEq)]
#[derive(Debug)]
pub enum EventType {
ContractCall,
}
Expand All @@ -27,3 +27,25 @@ impl EventType {
}
}
}

#[cfg(test)]
mod event_type_tests {
use starknet_core::utils::starknet_keccak;

use crate::starknet::events::EventType;

#[test]
fn parse_contract_call() {
let contract_call_felt = starknet_keccak("ContractCall".as_bytes());
assert!(matches!(
EventType::parse(contract_call_felt),
Some(EventType::ContractCall)
));
}

#[test]
fn parse_unknown_event() {
let contract_call_felt = starknet_keccak("UnknownEvent".as_bytes());
assert!(EventType::parse(contract_call_felt).is_none());
}
}
30 changes: 15 additions & 15 deletions ampd/src/starknet/types/array_span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ use thiserror::Error;
/// .unwrap(),
/// ];
///
/// let array_span = ArraySpan::try_from(data).unwrap();
/// assert_eq!(array_span.bytes, vec![104, 101, 108, 108, 111]);
/// assert_eq!(String::from_utf8(array_span.bytes).unwrap(), "hello");
/// let array_span = ArraySpan::<u8>::try_from(data).unwrap();
/// assert_eq!(array_span.data, vec![104, 101, 108, 108, 111]);
/// assert_eq!(String::from_utf8(array_span.data).unwrap(), "hello");
/// ```
///
/// For more info:
/// https://docs.starknet.io/documentation/architecture_and_concepts/Smart_Contracts/serialization_of_Cairo_types/#serialization_of_byte_arrays
#[derive(Debug)]
pub struct ArraySpan {
pub bytes: Vec<u8>,
pub struct ArraySpan<T> {
pub data: Vec<T>,
}

#[derive(Error, Debug)]
Expand All @@ -58,7 +58,7 @@ pub enum ArraySpanError {
ParsingFelt(#[from] ValueOutOfRangeError),
}

impl TryFrom<Vec<FieldElement>> for ArraySpan {
impl TryFrom<Vec<FieldElement>> for ArraySpan<u8> {
type Error = ArraySpanError;

fn try_from(data: Vec<FieldElement>) -> Result<Self, Self::Error> {
Expand All @@ -79,7 +79,7 @@ impl TryFrom<Vec<FieldElement>> for ArraySpan {
.map(|e| e.try_into().map_err(ArraySpanError::ParsingFelt))
.collect();

Ok(ArraySpan { bytes: bytes? })
Ok(ArraySpan { data: bytes? })
}
}

Expand All @@ -99,8 +99,8 @@ mod array_span_tests {
)
.unwrap()];

let array_span = ArraySpan::try_from(data).unwrap();
assert_eq!(array_span.bytes, Vec::<u8>::new());
let array_span = ArraySpan::<u8>::try_from(data).unwrap();
assert_eq!(array_span.data, Vec::<u8>::new());
}

#[test]
Expand Down Expand Up @@ -133,7 +133,7 @@ mod array_span_tests {
.unwrap(),
];

let array_span = ArraySpan::try_from(data);
let array_span = ArraySpan::<u8>::try_from(data);
assert!(array_span.is_err());
}

Expand Down Expand Up @@ -167,7 +167,7 @@ mod array_span_tests {
.unwrap(),
];

let array_span = ArraySpan::try_from(data);
let array_span = ArraySpan::<u8>::try_from(data);
assert!(array_span.is_err());
}

Expand Down Expand Up @@ -197,7 +197,7 @@ mod array_span_tests {
.unwrap(),
];

let array_span = ArraySpan::try_from(data);
let array_span = ArraySpan::<u8>::try_from(data);
assert!(array_span.is_err());
}

Expand Down Expand Up @@ -232,7 +232,7 @@ mod array_span_tests {
.unwrap(),
];

let array_span = ArraySpan::try_from(data);
let array_span = ArraySpan::<u8>::try_from(data);
assert!(array_span.is_err());
}

Expand Down Expand Up @@ -266,7 +266,7 @@ mod array_span_tests {
.unwrap(),
];

let array_span = ArraySpan::try_from(data).unwrap();
assert_eq!(array_span.bytes, vec![104, 101, 108, 108, 111]);
let array_span = ArraySpan::<u8>::try_from(data).unwrap();
assert_eq!(array_span.data, vec![104, 101, 108, 108, 111]);
}
}

0 comments on commit da82021

Please sign in to comment.