Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add CallContract starknet event parsing logic #19

Merged
merged 11 commits into from
Apr 30, 2024
Prev Previous commit
Next Next commit
fixes on doc tests
  • Loading branch information
ctoyan committed Apr 29, 2024
commit e278094b848c2c2a41d4bb2f0fa5e401448b83ab
2 changes: 1 addition & 1 deletion ampd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mod handlers;
mod health_check;
mod json_rpc;
mod queue;
mod starknet;
pub mod starknet;
pub mod state;
mod sui;
mod tm_client;
Expand Down
45 changes: 44 additions & 1 deletion ampd/src/starknet/types/array_span.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,50 @@
use starknet_core::types::{FieldElement, ValueOutOfRangeError};
use thiserror::Error;

/// Applies for bot a cairo Array and a Span
/// Represents Cairo's Array and Span types.
/// Implements `TryFrom<Vec<FieldElement>>`, which is the way to create it.
///
/// ## Example usage with the strging "hello"
///
/// ```rust
/// use ampd::starknet::types::array_span::ArraySpan;
/// use std::str::FromStr;
/// use starknet_core::types::FieldElement;
///
/// let data = vec![
/// FieldElement::from_str(
/// "0x0000000000000000000000000000000000000000000000000000000000000005",
/// )
/// .unwrap(),
/// FieldElement::from_str(
/// "0x0000000000000000000000000000000000000000000000000000000000000068",
/// )
/// .unwrap(),
/// FieldElement::from_str(
/// "0x0000000000000000000000000000000000000000000000000000000000000065",
/// )
/// .unwrap(),
/// FieldElement::from_str(
/// "0x000000000000000000000000000000000000000000000000000000000000006c",
/// )
/// .unwrap(),
/// FieldElement::from_str(
/// "0x000000000000000000000000000000000000000000000000000000000000006c",
/// )
/// .unwrap(),
/// FieldElement::from_str(
/// "0x000000000000000000000000000000000000000000000000000000000000006f",
/// )
/// .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");
/// ```
///
/// 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>,
Expand Down
79 changes: 32 additions & 47 deletions ampd/src/starknet/types/byte_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,37 @@ use starknet_core::types::{FieldElement, ValueOutOfRangeError};
use starknet_core::utils::parse_cairo_short_string;
use thiserror::Error;

/// Represents Cairo's ByteArray type.
/// Implements `TryFrom<Vec<FieldElement>>`, which is the way to create it.
///
/// ## Example usage with the strging "hello"
///
/// ```rust
/// use ampd::starknet::types::byte_array::ByteArray;
/// use std::str::FromStr;
/// use starknet_core::types::FieldElement;
///
/// let data = vec![
/// FieldElement::from_str(
/// "0x0000000000000000000000000000000000000000000000000000000000000000",
/// )
/// .unwrap(),
/// FieldElement::from_str(
/// "0x00000000000000000000000000000000000000000000000000000068656c6c6f",
/// )
/// .unwrap(),
/// FieldElement::from_str(
/// "0x0000000000000000000000000000000000000000000000000000000000000005",
/// )
/// .unwrap(),
/// ];
///
/// let byte_array = ByteArray::try_from(data);
/// assert!(byte_array.is_ok());
/// ```
///
/// 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 ByteArray {
/// The data byte array. Contains 31-byte chunks of the byte array.
Expand Down Expand Up @@ -35,35 +66,6 @@ pub enum ByteArrayError {
ToString,
}

/// The Vec<FieldElement> should be the elements representing the ByteArray
/// type as described in this document:
/// https://docs.starknet.io/documentation/architecture_and_concepts/Smart_Contracts/serialization_of_Cairo_types/#serialization_of_byte_arrays
///
/// ## Example usage
///
/// ```rust
/// use amd::starknet::types::ByteArray;
/// use std::str::FromStr;
/// use starknet_core::types::FieldElement;
///
/// let data = vec![
/// FieldElement::from_str(
/// "0x0000000000000000000000000000000000000000000000000000000000000000",
/// )
/// .unwrap(),
/// FieldElement::from_str(
/// "0x00000000000000000000000000000000000000000000000000000068656c6c6f",
/// )
/// .unwrap(),
/// FieldElement::from_str(
/// "0x0000000000000000000000000000000000000000000000000000000000000005",
/// )
/// .unwrap(),
/// ];
///
/// let byte_array = ByteArray::try_from(data).unwrap();
/// assert_eq!("hello", byte_array.is_ok());
/// ```
impl TryFrom<Vec<FieldElement>> for ByteArray {
type Error = ByteArrayError;

Expand Down Expand Up @@ -137,23 +139,6 @@ impl TryFrom<Vec<FieldElement>> for ByteArray {
}

Ok(byte_array)

// TODO:
// - If word count is 0 - convert the pending word to a string
// - If word count > 0:
// - for i=2; i < 2+eventData[1]; i++
// - cut all leading 0s
// - concatenate all field element hex bytes resulting in
// 31_word_bytes
// - parse felt 1 to u32 and take element parsedFelt+2 which is the
// pending_word
// - parse elelemtn parsedFelt+3 as u8, which is
// pending_word_bytes_length
// - take pending_words_byte_length worth of bytes from the
// pending_word
// - take the pending_word bytes and concatenate them with the
// previous 31_word_bytes
// - Convert those bytes to a string
}
}

Expand All @@ -163,7 +148,7 @@ impl ByteArray {
/// ## Example usage with the string "hello"
///
/// ```rust
/// use ampd::starknet::types::ByteArray;
/// use ampd::starknet::types::byte_array::ByteArray;
/// use std::str::FromStr;
/// use starknet_core::types::FieldElement;
///
Expand Down