-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
106 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
.env | ||
.env.* | ||
*.log | ||
config.toml |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[state_indexer] | ||
accounts = ["test.near"] # Empty list means all accounts | ||
changes = ["state"] # ["state", "access_key", "contract_code", "account"] Empty list means all changes | ||
|
||
[tx_indexer] | ||
# Not yet implemented | ||
|
||
[rpc_server] | ||
# Not yet implemented |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,89 @@ | ||
use near_indexer_primitives::views::StateChangeValueView; | ||
use serde_derive::Deserialize; | ||
use std::path::Path; | ||
|
||
#[derive(Deserialize)] | ||
#[derive(Deserialize, Debug, Clone, Default)] | ||
pub struct Config { | ||
pub state_indexer: StateIndexerConfig, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
#[derive(Deserialize, Debug, Clone, Default)] | ||
pub struct StateIndexerConfig { | ||
pub accounts: Vec<String>, | ||
pub changes: Vec<String>, | ||
} | ||
|
||
async fn read_toml_file() -> anyhow::Result<Config> { | ||
let filename = "config.toml"; | ||
impl StateIndexerConfig { | ||
fn contains_account(&self, account_id: &str) -> bool { | ||
if self.accounts.is_empty() { | ||
true | ||
} else { | ||
self.accounts.contains(&account_id.to_string()) | ||
} | ||
} | ||
|
||
let contents = match std::fs::read_to_string(filename) { | ||
Ok(content) => content, | ||
Err(err) => { | ||
anyhow::bail!("Could not read file: {}.\n Error: {}", filename, err); | ||
fn contains_change(&self, change_type: &str) -> bool { | ||
if self.changes.is_empty() { | ||
true | ||
} else { | ||
self.changes.contains(&change_type.to_string()) | ||
} | ||
} | ||
pub fn should_be_indexed(&self, state_change_value: &StateChangeValueView) -> bool { | ||
match state_change_value { | ||
StateChangeValueView::DataUpdate { account_id, .. } | ||
| StateChangeValueView::DataDeletion { account_id, .. } => { | ||
self.contains_account(account_id) && self.contains_change("state") | ||
} | ||
StateChangeValueView::AccessKeyUpdate { account_id, .. } | ||
| StateChangeValueView::AccessKeyDeletion { account_id, .. } => { | ||
self.contains_account(account_id) && self.contains_change("access_key") | ||
} | ||
StateChangeValueView::ContractCodeUpdate { account_id, .. } | ||
| StateChangeValueView::ContractCodeDeletion { account_id, .. } => { | ||
self.contains_account(account_id) && self.contains_change("contract_code") | ||
} | ||
StateChangeValueView::AccountUpdate { account_id, .. } | ||
| StateChangeValueView::AccountDeletion { account_id, .. } => { | ||
self.contains_account(account_id) && self.contains_change("account") | ||
} | ||
} | ||
}; | ||
} | ||
} | ||
|
||
let config: Config = match toml::from_str::<Config>(&contents) { | ||
Ok(config) => config, | ||
async fn read_toml_file(path_file: &Path) -> anyhow::Result<Config> { | ||
match std::fs::read_to_string(path_file) { | ||
Ok(content) => match toml::from_str::<Config>(&content) { | ||
Ok(config) => Ok(config), | ||
Err(err) => { | ||
anyhow::bail!( | ||
"Unable to load data from: {:?}.\n Error: {}", | ||
path_file.to_str(), | ||
err | ||
); | ||
} | ||
}, | ||
Err(err) => { | ||
anyhow::bail!("Unable to load data from: {}.\n Error: {}", filename, err); | ||
anyhow::bail!( | ||
"Could not read file: {:?}.\n Error: {}", | ||
path_file.to_str(), | ||
err | ||
); | ||
} | ||
}; | ||
} | ||
} | ||
|
||
Ok(config) | ||
pub async fn read_configuration_from_file(path_file: &str) -> anyhow::Result<Config> { | ||
let path_file = Path::new(path_file); | ||
read_toml_file(path_file).await | ||
} | ||
|
||
pub async fn read_configuration() -> anyhow::Result<Config> { | ||
let config = read_toml_file().await?; | ||
|
||
Ok(config) | ||
let mut path_root = project_root::get_project_root()?; | ||
path_root.push("config.toml"); | ||
if path_root.exists() { | ||
read_toml_file(path_root.as_path()).await | ||
} else { | ||
Ok(Config::default()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters