Skip to content

Commit

Permalink
Fix ReScript Contract Import Templates (#294)
Browse files Browse the repository at this point in the history
* Fix ReScript contract import template

* Run format

* Rename helper closure in get_entity_id_code
  • Loading branch information
JonoPrest authored Oct 25, 2024
1 parent 3685c56 commit 8a5d8e4
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 41 deletions.
20 changes: 9 additions & 11 deletions codegenerator/cli/src/cli_args/interactive_init/evm_prompts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,14 @@ impl ContractImportArgs {
Ok(ContractImportResult::NotVerified) => {
Err("Failed to find the verified contract on a block explorer.".to_string())
}
Ok(ContractImportResult::UnsupportedChain) => {
Err(format!("The \"{network}\" chain doesn't support contract import yet. Let us know if you want it by opening an issue on Github."))
}
Err(e) => {
Err(format!(
"Failed getting the contract ABI with the following error:\n{}",
e
))
}
Ok(ContractImportResult::UnsupportedChain) => Err(format!(
"The \"{network}\" chain doesn't support contract import yet. Let us know if you \
want it by opening an issue on Github."
)),
Err(e) => Err(format!(
"Failed getting the contract ABI with the following error:\n{}",
e
)),
};
let contract_data = match result {
Ok(contract_data) => contract_data,
Expand Down Expand Up @@ -282,8 +281,7 @@ fn get_converter_network_u64(
///be very slow to have the startblock at 0 with rpc 🦶🔫
fn prompt_for_start_block() -> Result<u64> {
let start_block = CustomType::<u64>::new(
"Please provide a start block for this network \
(this can be edited later in config.yaml):",
"Please provide a start block for this network (this can be edited later in config.yaml):",
)
.with_error_message("Invalid start block input, please enter a number 0 or greater")
.prompt()?;
Expand Down
3 changes: 2 additions & 1 deletion codegenerator/cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ pub mod start {

if !exit.success() {
return Err(anyhow!(
"Indexer crashed. For more details see the error logs above the TUI. Can't find them? Restart the indexer with the 'TUI_OFF=true pnpm start' command."
"Indexer crashed. For more details see the error logs above the TUI. Can't find \
them? Restart the indexer with the 'TUI_OFF=true pnpm start' command."
));
}
println!(
Expand Down
3 changes: 2 additions & 1 deletion codegenerator/cli/src/config_parsing/human_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,8 @@ pub mod fuel {
#[serde(rename = "type")]
#[serde(skip_serializing_if = "Option::is_none")]
#[schemars(
description = "Explicitly set the event type you want to index. It's derived from the event name and fallbacks to LogData."
description = "Explicitly set the event type you want to index. It's derived from the \
event name and fallbacks to LogData."
)]
pub type_: Option<EventType>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
26 changes: 17 additions & 9 deletions codegenerator/cli/src/config_parsing/system_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,11 @@ impl SystemConfig {
//there is no config
if !contracts.get(&contract.name).is_some() {
Err(anyhow!(
"Failed to parse contract '{}' for the network '{}'. If you use a global contract definition, please verify that the name reference is correct.",
contract.name, network.id
"Failed to parse contract '{}' for the network '{}'. If you use a \
global contract definition, please verify that the name \
reference is correct.",
contract.name,
network.id
))?;
}
}
Expand Down Expand Up @@ -378,8 +381,11 @@ impl SystemConfig {
//there is no local_contract_config
if !contracts.get(&contract.name).is_some() {
Err(anyhow!(
"Failed to parse contract '{}' for the network '{}'. If you use a global contract definition, please verify that the name reference is correct.",
contract.name, network.id
"Failed to parse contract '{}' for the network '{}'. If you use a \
global contract definition, please verify that the name \
reference is correct.",
contract.name,
network.id
))?;
}
}
Expand Down Expand Up @@ -980,7 +986,8 @@ impl Event {
};
if event_config.log_id.is_some() && event_type != EventType::LogData {
return Err(anyhow!(
"Event '{}' has both 'logId' and '{}' type set. Only one of them can be used at once.",
"Event '{}' has both 'logId' and '{}' type set. Only one of them can be used \
at once.",
event_config.name,
event_type
));
Expand All @@ -990,10 +997,11 @@ impl Event {
let log = match &event_config.log_id {
None => {
let logged_type = fuel_abi
.get_type_by_struct_name(event_config.name.clone())
.context(
"Failed to derive the event configuration from the name. Use the logId, mint, or burn options to set it explicitly.",
)?;
.get_type_by_struct_name(event_config.name.clone())
.context(
"Failed to derive the event configuration from the name. Use \
the logId, mint, or burn options to set it explicitly.",
)?;
fuel_abi.get_log_by_type(logged_type.id)?
}
Some(log_id) => fuel_abi.get_log(&log_id)?,
Expand Down
51 changes: 32 additions & 19 deletions codegenerator/cli/src/hbs_templating/contract_import_templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,24 +250,24 @@ pub struct Event {
}

impl Event {
fn get_entity_id_code(event_var_name: String, is_fuel: bool, language: &Language) -> String {
let to_string_code = match language {
Language::ReScript => "->Belt.Int.toString",
Language::TypeScript => "",
Language::JavaScript => "",
}
.to_string();
/// Returns the code to get the entity id from an event
pub fn get_entity_id_code(is_fuel: bool, language: &Language) -> String {
let int_as_string = |int_var_name| match language {
Language::ReScript => format!("{int_var_name}->Belt.Int.toString"),
Language::TypeScript | Language::JavaScript => int_var_name,
};
let int_event_prop_as_string = |event_prop: &str| int_as_string(format!("event.{event_prop}"));
let chain_id_str = int_event_prop_as_string("chainId");

let block_number_field = match is_fuel {
true => "height",
false => "number",
true => "block.height",
false => "block.number",
};
let block_number_str = int_event_prop_as_string(block_number_field);

format!(
"`${{{event_var_name}.chainId}}_${{{event_var_name}.block.\
{block_number_field}}}_${{{event_var_name}.logIndex{}}}`",
to_string_code
)
let log_index_str = int_event_prop_as_string("logIndex");

format!("`${{{chain_id_str}}}_${{{block_number_str}}}_${{{log_index_str}}}`",)
}

fn get_create_mock_code(
Expand Down Expand Up @@ -315,11 +315,7 @@ impl Event {

Ok(Event {
name: event.name.to_string(),
entity_id_from_event_code: Event::get_entity_id_code(
"event".to_string(),
is_fuel,
&language,
),
entity_id_from_event_code: Event::get_entity_id_code(is_fuel, &language),
create_mock_code: Event::get_create_mock_code(&event, &contract, is_fuel, &language),
params,
})
Expand Down Expand Up @@ -452,6 +448,7 @@ impl AutoSchemaHandlerTemplate {
mod test {
use super::*;
use ethers::abi::EventParam;
use pretty_assertions::assert_eq;

#[test]
fn flatten_event_with_tuple() {
Expand Down Expand Up @@ -558,4 +555,20 @@ mod test {

assert_eq!(expected_event_keys, actual_event_keys);
}

#[test]
fn test_get_entity_id_code() {
const IS_FUEL: bool = true;
assert_eq!(
Event::get_entity_id_code(!IS_FUEL, &Language::ReScript),
"`${event.chainId->Belt.Int.toString}_${event.block.number->Belt.Int.\
toString}_${event.logIndex->Belt.Int.toString}`"
.to_string()
);

assert_eq!(
Event::get_entity_id_code(IS_FUEL, &Language::TypeScript),
"`${event.chainId}_${event.block.height}_${event.logIndex}`".to_string()
);
}
}

0 comments on commit 8a5d8e4

Please sign in to comment.