Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
DOBEN committed Dec 4, 2024
1 parent 59ee671 commit ffc24b8
Showing 1 changed file with 124 additions and 52 deletions.
176 changes: 124 additions & 52 deletions backend-rust/src/graphql_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,45 @@ use transaction_metrics::TransactionMetricsQuery;

const VERSION: &str = clap::crate_version!();

struct SchemaName {
type_name: &'static str,
value_name: &'static str,
}

enum SmartContractSchemaNames {
Event,
InputParameterReceiveFunction,
}

impl SmartContractSchemaNames {
pub const EVENT: SchemaName = SchemaName {
type_name: "event",
value_name: "contract log",
};
pub const INPUT_PARAMETER_RECEIVE_FUNCTION: SchemaName = SchemaName {
type_name: "receive parameter",
value_name: "input parameter of receive function",
};

pub fn value(&self) -> &'static str {
match self {
SmartContractSchemaNames::Event => Self::EVENT.value_name,
SmartContractSchemaNames::InputParameterReceiveFunction => {
Self::INPUT_PARAMETER_RECEIVE_FUNCTION.value_name
}
}
}

pub fn kind(&self) -> &'static str {
match self {
SmartContractSchemaNames::Event => Self::EVENT.type_name,
SmartContractSchemaNames::InputParameterReceiveFunction => {
Self::INPUT_PARAMETER_RECEIVE_FUNCTION.type_name
}
}
}
}

#[derive(clap::Args)]
pub struct ApiServiceConfig {
/// Account(s) that should not be considered in circulation.
Expand Down Expand Up @@ -3765,16 +3804,15 @@ impl SearchResult {

fn decode_value_with_schema(
opt_schema: Option<&Type>,
schema_name: &str,
value: &[u8],
value_name: &str,
schema_name: SmartContractSchemaNames,
) -> String {
let Some(schema) = opt_schema else {
// Note: There could be something better displayed than this string if no schema is
// available for decoding at the frontend long-term.
return format!(
"No embedded {} schema in smart contract available for decoding",
schema_name
schema_name.kind()
);
};

Expand All @@ -3791,7 +3829,9 @@ fn decode_value_with_schema(
// contract developer for debugging purposes here.
format!(
"Failed to deserialize {} with {} schema into string: {:?}",
value_name, schema_name, e
schema_name.value(),
schema_name.kind(),
e
)
})
}
Expand All @@ -3805,8 +3845,8 @@ fn decode_value_with_schema(
// contract developer for debugging purposes here.
format!(
"Failed to deserialize {} with {} schema: {:?}",
value_name,
schema_name,
schema_name.value(),
schema_name.kind(),
e.display(true)
)
}
Expand Down Expand Up @@ -4804,7 +4844,7 @@ impl ContractInitialized {
connection.edges.push(connection::Edge::new(index.to_string(), hex::encode(log)));
});

// Nice-to-have: pagination info but not used at front-end currently.
// TODO: pagination info but not used at front-end currently (issue#318).

Ok(connection)
}
Expand Down Expand Up @@ -4833,24 +4873,33 @@ impl ContractInitialized {
.await?
.ok_or(ApiError::NotFound)?;

let opt_event_schema = row
.display_schema
.as_ref()
.and_then(|schema| VersionedModuleSchema::new(schema, &None).ok())
.and_then(|versioned_schema| {
versioned_schema.get_event_schema(&row.contract_name).ok()
});
// Get the event schema if it exists.
let opt_event_schema = if let Some(event_schema) = row.display_schema.as_ref() {
let versioned_schema =
VersionedModuleSchema::new(event_schema, &None).map_err(|_| {
ApiError::InternalError(
"Database bytes should be a valid VersionedModuleSchema".to_string(),
)
})?;

versioned_schema.get_event_schema(&row.contract_name).ok()
} else {
None
};

let mut connection = connection::Connection::new(true, true);

for (index, log) in self.contract_logs_raw.iter().enumerate() {
let decoded_log =
decode_value_with_schema(opt_event_schema.as_ref(), "event", log, "contract_log");
let decoded_log = decode_value_with_schema(
opt_event_schema.as_ref(),
log,
SmartContractSchemaNames::Event,
);

connection.edges.push(connection::Edge::new(index.to_string(), decoded_log));
}

// Nice-to-have: pagination info but not used at front-end currently.
// TODO: pagination info but not used at front-end currently (issue#318).

Ok(connection)
}
Expand Down Expand Up @@ -5076,7 +5125,7 @@ impl ContractInterrupted {
connection.edges.push(connection::Edge::new(index.to_string(), hex::encode(log)));
});

// Nice-to-have: pagination info but not used at front-end currently.
// TODO: pagination info but not used at front-end currently (issue#318).

Ok(connection)
}
Expand Down Expand Up @@ -5105,24 +5154,33 @@ impl ContractInterrupted {
.await?
.ok_or(ApiError::NotFound)?;

let opt_event_schema = row
.display_schema
.as_ref()
.and_then(|schema| VersionedModuleSchema::new(schema, &None).ok())
.and_then(|versioned_schema| {
versioned_schema.get_event_schema(&row.contract_name).ok()
});
// Get the event schema if it exists.
let opt_event_schema = if let Some(event_schema) = row.display_schema.as_ref() {
let versioned_schema =
VersionedModuleSchema::new(event_schema, &None).map_err(|_| {
ApiError::InternalError(
"Database bytes should be a valid VersionedModuleSchema".to_string(),
)
})?;

versioned_schema.get_event_schema(&row.contract_name).ok()
} else {
None
};

let mut connection = connection::Connection::new(true, true);

for (index, log) in self.contract_logs_raw.iter().enumerate() {
let decoded_log =
decode_value_with_schema(opt_event_schema.as_ref(), "event", log, "contract_log");
let decoded_log = decode_value_with_schema(
opt_event_schema.as_ref(),
log,
SmartContractSchemaNames::Event,
);

connection.edges.push(connection::Edge::new(index.to_string(), decoded_log));
}

// Nice-to-have: pagination info but not used at front-end currently.
// TODO: pagination info but not used at front-end currently (issue#318).

Ok(connection)
}
Expand Down Expand Up @@ -5172,24 +5230,29 @@ impl ContractUpdated {
.await?
.ok_or(ApiError::NotFound)?;

let opt_receive_param_schema = row
.display_schema
.as_ref()
.and_then(|schema| VersionedModuleSchema::new(schema, &None).ok())
.and_then(|versioned_schema| {
versioned_schema
.get_receive_param_schema(
&row.contract_name,
ReceiveName::new_unchecked(&self.receive_name).entrypoint_name().into(),
// Get the receive param schema if it exists.
let opt_receive_param_schema = if let Some(event_schema) = row.display_schema.as_ref() {
let versioned_schema =
VersionedModuleSchema::new(event_schema, &None).map_err(|_| {
ApiError::InternalError(
"Database bytes should be a valid VersionedModuleSchema".to_string(),
)
.ok()
});
})?;

versioned_schema
.get_receive_param_schema(
&row.contract_name,
ReceiveName::new_unchecked(&self.receive_name).entrypoint_name().into(),
)
.ok()
} else {
None
};

let decoded_input_parameter = decode_value_with_schema(
opt_receive_param_schema.as_ref(),
"receive param",
&self.input_parameter,
"input parameter of receive function",
SmartContractSchemaNames::InputParameterReceiveFunction,
);

Ok(decoded_input_parameter)
Expand All @@ -5202,7 +5265,7 @@ impl ContractUpdated {
connection.edges.push(connection::Edge::new(index.to_string(), hex::encode(log)));
});

// Nice-to-have: pagination info but not used at front-end currently.
// TODO: pagination info but not used at front-end currently (issue#318).

Ok(connection)
}
Expand Down Expand Up @@ -5231,24 +5294,33 @@ impl ContractUpdated {
.await?
.ok_or(ApiError::NotFound)?;

let opt_event_schema = row
.display_schema
.as_ref()
.and_then(|schema| VersionedModuleSchema::new(schema, &None).ok())
.and_then(|versioned_schema| {
versioned_schema.get_event_schema(&row.contract_name).ok()
});
// Get the event schema if it exists.
let opt_event_schema = if let Some(event_schema) = row.display_schema.as_ref() {
let versioned_schema =
VersionedModuleSchema::new(event_schema, &None).map_err(|_| {
ApiError::InternalError(
"Database bytes should be a valid VersionedModuleSchema".to_string(),
)
})?;

versioned_schema.get_event_schema(&row.contract_name).ok()
} else {
None
};

let mut connection = connection::Connection::new(true, true);

for (index, log) in self.contract_logs_raw.iter().enumerate() {
let decoded_log =
decode_value_with_schema(opt_event_schema.as_ref(), "event", log, "contract_log");
let decoded_log = decode_value_with_schema(
opt_event_schema.as_ref(),
log,
SmartContractSchemaNames::Event,
);

connection.edges.push(connection::Edge::new(index.to_string(), decoded_log));
}

// Nice-to-have: pagination info but not used at front-end currently.
// TODO: pagination info but not used at front-end currently (issue#318).

Ok(connection)
}
Expand Down

0 comments on commit ffc24b8

Please sign in to comment.