Skip to content

Commit

Permalink
updates to block-parser lib to handle diff record inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
jaketarnow committed Jul 18, 2024
1 parent 77c951b commit b49e5be
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
14 changes: 9 additions & 5 deletions block-parser/src/block_json/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct InputJSON {
// The ID of the input.
id: String,
// The value of the input.
value: String,
value: Option<String>,
}

impl InputJSON {
Expand All @@ -29,9 +29,13 @@ impl InputJSON {
None => bail!("Invalid input ID"),
};
// Get the value of the input.
let value = match json["value"].as_str() {
Some(value) => value.to_string(),
None => bail!("Invalid input value"),
let value = match json
.get("value")
.or_else(|| json.get("tag"))
.and_then(|v| v.as_str())
{
Some(value) => Some(value.to_string()),
None => None,
};
Ok(Self { type_, id, value })
}
Expand All @@ -47,7 +51,7 @@ impl InputJSON {
}

// Returns the value of the input.
pub fn value(&self) -> &str {
pub fn value(&self) -> &Option<String> {
&self.value
}
}
15 changes: 9 additions & 6 deletions block-parser/src/block_json/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ impl TransactionJSON {
None => bail!("Invalid transaction type"),
};
// Get the transitions in the transaction.
let transitions = match json["transaction"]["execution"]["transitions"].as_array() {
Some(transitions) => transitions
.iter()
.map(|transition| TransitionJSON::new(transition.clone()))
.collect::<Result<Vec<_>>>()?,
None => bail!("Invalid transitions"),
let transitions = match json["transaction"]["type"].as_str() {
Some("execute") => match json["transaction"]["execution"]["transitions"].as_array() {
Some(transitions) => transitions
.iter()
.map(|transition| TransitionJSON::new(transition.clone()))
.collect::<Result<Vec<_>>>()?,
None => bail!("Invalid transitions"),
},
_ => Vec::new(),
};
Ok(Self {
json,
Expand Down
28 changes: 21 additions & 7 deletions block-parser/src/decoders.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;

use nom::character::complete::digit1;
use nom::sequence::{tuple};
use nom::sequence::tuple;
use nom::{
bytes::complete::tag,
character::complete::{alphanumeric1, multispace0},
Expand Down Expand Up @@ -142,9 +142,13 @@ pub fn decode_block_unchecked<N: Network>(string: &str) -> Result<(Vec<CreditsOp
// Check that there are 3 inputs.
ensure!(inputs.len() == 3, "Expected 3 inputs");
// Get the validator address, withdrawal address, and amount from the inputs.
let validator = inputs.first().unwrap().value().to_string();
let withdrawal = inputs.get(1).unwrap().value().to_string();
let amount = *U64::<N>::from_str(inputs.get(2).unwrap().value())?;
let validator = inputs.first().unwrap().value().to_owned().unwrap();
let withdrawal = inputs.get(1).unwrap().value().to_owned().unwrap();
let inputs_value = inputs.get(2).unwrap().value();
let amount = match inputs_value {
Some(v) => *U64::<N>::from_str(&v)?,
None => bail!("Invalid JSON object"),
};
// Add the `bond_public` operation to the credits transactions.
credits_transactions.push(CreditsOperations::BondPublic {
id,
Expand All @@ -161,7 +165,10 @@ pub fn decode_block_unchecked<N: Network>(string: &str) -> Result<(Vec<CreditsOp
// Check that there is 1 input.
ensure!(inputs.len() == 1, "Expected 1 input");
// Get the staker address from the inputs.
let staker = inputs.first().unwrap().value().to_string();
let staker = match inputs.first().unwrap().value() {
Some(v) => v.to_string(),
None => bail!("Invalid response for staker address"),
};
// Add the `claim_unbond_public` operation to the credits transactions.
credits_transactions
.push(CreditsOperations::ClaimUnbondPublic { id, staker });
Expand All @@ -174,8 +181,15 @@ pub fn decode_block_unchecked<N: Network>(string: &str) -> Result<(Vec<CreditsOp
// Check that there are 2 inputs.
ensure!(inputs.len() == 2, "Expected 2 inputs");
// Get the staker address and amount from the inputs.
let staker = inputs.first().unwrap().value().to_string();
let amount = *U64::<N>::from_str(inputs.get(1).unwrap().value())?;
let staker = match inputs.first().unwrap().value() {
Some(v) => v.to_string(),
None => bail!("Invalid response for staker address"),
};
let inputs_value = inputs.get(1).unwrap().value();
let amount = match inputs_value {
Some(v) => *U64::<N>::from_str(&v)?,
None => bail!("Invalid JSON object"),
};
// Add the `unbond_public` operation to the credits transactions.
credits_transactions.push(CreditsOperations::UnbondPublic {
id,
Expand Down

0 comments on commit b49e5be

Please sign in to comment.