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

fix: Relax CIP15 requirements and log level #290

Merged
merged 1 commit into from
May 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions src/mapper/cip15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@ use serde_json::Value as JsonValue;

use pallas::ledger::primitives::alonzo::Metadatum;

fn extract_json_property<'a>(json: &'a JsonValue, key: &str) -> Result<&'a JsonValue, Error> {
fn extract_json_property<'a>(
json: &'a JsonValue,
key: &str,
) -> Result<Option<&'a JsonValue>, Error> {
let result = json
.as_object()
.ok_or_else(|| Error::from("invalid metadatum object for CIP15"))?
.get(key)
.ok_or_else(|| Error::from("required key not found for CIP15"))?;
.get(key);

Ok(result)
}

fn extract_json_string_property(json: &JsonValue, key: &str) -> Result<String, Error> {
fn extract_json_string_property(json: &JsonValue, key: &str) -> Result<Option<String>, Error> {
let result = extract_json_property(json, key)?
.as_str()
.ok_or_else(|| Error::from("invalid value type for CIP15"))?
.to_string();
.and_then(|x| x.as_str())
.map(|x| x.to_string());

Ok(result)
}

fn extract_json_int_property(json: &JsonValue, key: &str) -> Result<i64, Error> {
let result = extract_json_property(json, key)?
.as_i64()
.ok_or_else(|| Error::from("invalid value type for CIP15"))?;
fn extract_json_int_property(json: &JsonValue, key: &str) -> Result<Option<i64>, Error> {
let result = extract_json_property(json, key)?.and_then(|x| x.as_i64());

Ok(result)
}
Expand All @@ -37,18 +36,20 @@ impl EventWriter {
let raw_json = self.to_metadatum_json(content)?;

Ok(CIP15AssetRecord {
voting_key: extract_json_string_property(&raw_json, "1")?,
stake_pub: extract_json_string_property(&raw_json, "2")?,
reward_address: extract_json_string_property(&raw_json, "3")?,
nonce: extract_json_int_property(&raw_json, "4")?,
voting_key: extract_json_string_property(&raw_json, "1")?
.ok_or_else(|| Error::from("invalid value type for CIP15"))?,
stake_pub: extract_json_string_property(&raw_json, "2")?
.ok_or_else(|| Error::from("invalid value type for CIP15"))?,
reward_address: extract_json_string_property(&raw_json, "3")?.unwrap_or_default(),
nonce: extract_json_int_property(&raw_json, "4")?.unwrap_or_default(),
raw_json,
})
}

pub(crate) fn crawl_metadata_label_61284(&self, content: &Metadatum) -> Result<(), Error> {
match self.to_cip15_asset_record(content) {
Ok(record) => self.append_from(record)?,
Err(err) => log::warn!("error parsing CIP15: {:?}", err),
Err(err) => log::info!("error parsing CIP15: {:?}", err),
}

Ok(())
Expand Down