Skip to content

Commit

Permalink
Add Mainnet selection in Fuel contract import (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh authored Oct 25, 2024
1 parent 51811db commit e828ef5
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 48 deletions.
34 changes: 17 additions & 17 deletions codegenerator/cli/CommandLineHelp.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ This document contains the help content for the `envio` command-line program.
* [`envio init contract-import local`](#envio-init-contract-import-local)
* [`envio init template`](#envio-init-template)
* [`envio init fuel`](#envio-init-fuel)
* [`envio init fuel template`](#envio-init-fuel-template)
* [`envio init fuel contract-import`](#envio-init-fuel-contract-import)
* [`envio init fuel contract-import local`](#envio-init-fuel-contract-import-local)
* [`envio init fuel template`](#envio-init-fuel-template)
* [`envio dev`](#envio-dev)
* [`envio stop`](#envio-stop)
* [`envio codegen`](#envio-codegen)
Expand Down Expand Up @@ -150,23 +150,8 @@ Initialization option for creating Fuel indexer

###### **Subcommands:**

* `template` — Initialize Fuel indexer from an example template
* `contract-import` — Initialize Fuel indexer by importing config from a contract for a given chain



## `envio init fuel template`

Initialize Fuel indexer from an example template

**Usage:** `envio init fuel template [OPTIONS]`

###### **Options:**

* `-t`, `--template <TEMPLATE>` — Name of the template to be used in initialization

Possible values: `greeter`

* `template` — Initialize Fuel indexer from an example template



Expand Down Expand Up @@ -201,6 +186,21 @@ Initialize from a local json ABI file



## `envio init fuel template`

Initialize Fuel indexer from an example template

**Usage:** `envio init fuel template [OPTIONS]`

###### **Options:**

* `-t`, `--template <TEMPLATE>` — Name of the template to be used in initialization

Possible values: `greeter`




## `envio dev`

Development commands for starting, stopping, and restarting the indexer with automatic codegen for any changed files
Expand Down
4 changes: 2 additions & 2 deletions codegenerator/cli/src/cli_args/clap_definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,11 @@ pub mod fuel {

#[derive(Subcommand, Debug, EnumIter, Display, EnumString, Clone)]
pub enum InitFlow {
///Initialize Fuel indexer from an example template
Template(TemplateArgs),
///Initialize Fuel indexer by importing config from a contract for a given chain
#[strum(serialize = "Contract Import")]
ContractImport(ContractImportArgs),
///Initialize Fuel indexer from an example template
Template(TemplateArgs),
}

#[derive(Args, Debug, Default, Clone)]
Expand Down
81 changes: 55 additions & 26 deletions codegenerator/cli/src/cli_args/init_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,17 @@ pub mod evm {
}

pub mod fuel {
use std::collections::HashMap;

use clap::ValueEnum;
use serde::{Deserialize, Serialize};
use strum::{Display, EnumIter, EnumString};
use strum::{Display, EnumIter, EnumString, IntoEnumIterator};

use crate::{
config_parsing::human_config::{
fuel::{ContractConfig, EcosystemTag, EventConfig, HumanConfig, Network},
fuel::{
ContractConfig, EcosystemTag, EventConfig, HumanConfig, Network as NetworkConfig,
},
NetworkContract,
},
fuel::{abi::FuelAbi, address::Address},
Expand All @@ -219,12 +223,19 @@ pub mod fuel {
Greeter,
}

#[derive(Clone, Debug, Display, Eq, Hash, PartialEq, EnumIter)]
pub enum Network {
Mainnet = 9889,
Testnet = 0,
}

#[derive(Clone, Debug)]
pub struct SelectedContract {
pub name: String,
pub addresses: Vec<Address>,
pub abi: FuelAbi,
pub selected_events: Vec<EventConfig>,
pub network: Network,
}

impl SelectedContract {
Expand All @@ -240,37 +251,55 @@ pub mod fuel {

impl ContractImportSelection {
pub fn to_human_config(self: &Self, init_config: &InitConfig) -> HumanConfig {
let mut contracts_by_network: HashMap<Network, Vec<SelectedContract>> = HashMap::new();

for contract in self.contracts.clone() {
match contracts_by_network.get_mut(&contract.network) {
None => {
contracts_by_network.insert(contract.network.clone(), vec![contract]);
}
Some(contracts) => contracts.push(contract),
}
}

let mut network_configs = vec![];
for network in Network::iter() {
match contracts_by_network.get(&network) {
None => (),
Some(contracts) => network_configs.push(NetworkConfig {
id: network as u64,
start_block: 0,
end_block: None,
hyperfuel_config: None,
contracts: contracts
.iter()
.map(|selected_contract| NetworkContract {
name: selected_contract.name.clone(),
address: selected_contract
.addresses
.iter()
.map(|a| a.to_string())
.collect::<Vec<String>>()
.into(),
config: Some(ContractConfig {
abi_file_path: selected_contract.get_vendored_abi_file_path(),
handler: init_config.language.get_event_handler_directory(),
events: selected_contract.selected_events.clone(),
}),
})
.collect(),
}),
}
}

HumanConfig {
name: init_config.name.clone(),
description: None,
ecosystem: EcosystemTag::Fuel,
schema: None,
contracts: None,
raw_events: None,
networks: vec![Network {
id: 0,
start_block: 0,
end_block: None,
hyperfuel_config: None,
contracts: self
.contracts
.iter()
.map(|selected_contract| NetworkContract {
name: selected_contract.name.clone(),
address: selected_contract
.addresses
.iter()
.map(|a| a.to_string())
.collect::<Vec<String>>()
.into(),
config: Some(ContractConfig {
abi_file_path: selected_contract.get_vendored_abi_file_path(),
handler: init_config.language.get_event_handler_directory(),
events: selected_contract.selected_events.clone(),
}),
})
.collect(),
}],
networks: network_configs,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ fn prompt_for_network_id(
//Action prompt
let choose_from_networks = Select::new("Choose network:", options)
.prompt()
.context("Failed during prompt for abi file path")?;
.context("Failed during prompt for network")?;

let selected = match choose_from_networks.as_str() {
//If the user's choice evaluates to the enter network id option, prompt them for
Expand Down
16 changes: 14 additions & 2 deletions codegenerator/cli/src/cli_args/interactive_init/fuel_prompts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
},
config_parsing::human_config::fuel::EventConfig,
fuel::abi::{FuelAbi, BURN_EVENT_NAME, CALL_EVENT_NAME, MINT_EVENT_NAME, TRANSFER_EVENT_NAME},
init_config::fuel::{ContractImportSelection, InitFlow, SelectedContract, Template},
init_config::fuel::{ContractImportSelection, InitFlow, Network, SelectedContract, Template},
};
use anyhow::{Context, Result};
use inquire::{validator::Validation, Select};
Expand Down Expand Up @@ -92,7 +92,13 @@ fn prompt_event_selection(events: Vec<EventConfig>) -> Result<Vec<EventConfig>>

impl Contract for SelectedContract {
fn get_network_name(&self) -> Result<String> {
Ok("Fuel".to_string())
Ok(format!(
"Fuel {}",
match self.network {
Network::Mainnet => "Mainnet",
Network::Testnet => "Testnet",
}
))
}

fn get_name(&self) -> String {
Expand Down Expand Up @@ -150,13 +156,19 @@ async fn get_contract_import_selection(args: ContractImportArgs) -> Result<Selec

let name = get_contract_name(&local_import_args).context("Failed getting contract name")?;

let choose_from_networks =
Select::new("Choose network:", vec![Network::Mainnet, Network::Testnet])
.prompt()
.context("Failed during prompt for network")?;

let addresses = vec![prompt_contract_address(None)?];

Ok(SelectedContract {
name,
addresses,
abi,
selected_events,
network: choose_from_networks,
})
}

Expand Down

0 comments on commit e828ef5

Please sign in to comment.