Skip to content

Commit

Permalink
fix: resolved PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alenmestrov committed Dec 19, 2024
1 parent a5e2931 commit 784d579
Showing 1 changed file with 41 additions and 38 deletions.
79 changes: 41 additions & 38 deletions crates/context/config/src/client/env/proxy/types/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ impl From<StarknetProposalActionWithArgs> for ProposalAction {
amount,
calldata,
) => ProposalAction::ExternalFunctionCall {
receiver_id: format!("0x{}", hex::encode(contract.to_bytes_be())),
method_name: format!("0x{}", hex::encode(selector.to_bytes_be())),
receiver_id: contract.to_string(),
method_name: selector.to_string(),
args: calldata
.iter()
.map(|felt| format!("0x{}", hex::encode(felt.to_bytes_be())))
Expand Down Expand Up @@ -522,43 +522,44 @@ impl From<(Vec<Felt>, Vec<Felt>)> for ContextStorageEntry {

struct ValueCodec<'a>(&'a serde_json::Value);

impl<'a> Encode for ValueCodec<'a> {
fn encode<W: FeltWriter>(&self, writer: &mut W) -> Result<(), Error> {
match self.0 {
serde_json::Value::Bool(b) => {
writer.write(Felt::from(*b as u64));
Ok(())
impl<'a> ValueCodec<'a> {
fn encode_string<W: FeltWriter>(s: &str, writer: &mut W) -> Result<(), Error> {
if s.starts_with("0x") {
// Attempt to handle hex string directly as a single Felt
if let Ok(felt) = Felt::from_hex(s) {
writer.write(felt);
return Ok(());
}
serde_json::Value::String(s) => {
if s.starts_with("0x") {
// Handle hex string directly as a single Felt
writer.write(
Felt::from_hex(&s)
.map_err(|e| Error::custom(&format!("Invalid hex string: {}", e)))?,
);
} else {
// Regular string - split into chunks
let chunk_size = 31;
let chunks: Vec<_> = s.as_bytes().chunks(chunk_size).collect();
}
// Regular string - split into chunks
let chunk_size = 31;
let bytes = s.as_bytes();

// Write number of chunks first
writer.write(Felt::from(chunks.len()));
// Write number of chunks first
writer.write(Felt::from(bytes.len() / chunk_size));

// Write each chunk as a Felt
for chunk in chunks {
writer.write(Felt::from_bytes_be_slice(chunk));
}
// Write each chunk as a Felt
for chunk in bytes.chunks(chunk_size) {
writer.write(Felt::from_bytes_be_slice(chunk));
}
Ok(())
}
}

impl<'a> Encode for ValueCodec<'a> {
fn encode<W: FeltWriter>(&self, writer: &mut W) -> Result<(), Error> {
match self.0 {
serde_json::Value::String(s) => Self::encode_string(s, writer),
serde_json::Value::Object(obj) => {
writer.write(Felt::from(obj.len()));
for (key, value) in obj {
Self::encode_string(key, writer)?;
ValueCodec(value).encode(writer)?;
}
Ok(())
}
serde_json::Value::Array(arr) => {
// Write array length first
writer.write(Felt::from(arr.len()));

// Encode each array element
for item in arr {
ValueCodec(item).encode(writer)?;
}
serde_json::Value::Bool(b) => {
writer.write(Felt::from(*b as u64));
Ok(())
}
serde_json::Value::Number(n) => {
Expand All @@ -571,11 +572,13 @@ impl<'a> Encode for ValueCodec<'a> {
}
Ok(())
}
serde_json::Value::Object(obj) => {
writer.write(Felt::from(obj.len()));
for (key, value) in obj {
ValueCodec(&serde_json::Value::String(key.clone())).encode(writer)?;
ValueCodec(value).encode(writer)?;
serde_json::Value::Array(arr) => {
// Write array length first
writer.write(Felt::from(arr.len()));

// Encode each array element
for item in arr {
ValueCodec(item).encode(writer)?;
}
Ok(())
}
Expand Down

0 comments on commit 784d579

Please sign in to comment.