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

feat(core): add indexer table #86

Merged
merged 12 commits into from
Sep 8, 2023
40 changes: 31 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
regex = "1.5.4"
anyhow = "1.0"
tokio = { version = "1", features = ["full"] }
reqwest = { version = "0.11", features = ["json"] }
Expand Down
3 changes: 2 additions & 1 deletion crates/ark-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ version = "0.1.0"
edition = "2021"

[dependencies]
chrono = "0.4.19"
anyhow = "1.0"
serde = "1.0.164"
log = "0.4"
ark-metadata = { path = "../ark-metadata" }
aws-sdk-dynamodb = "0.29.0"
dotenv = "0.15.0"
dotenv = "0.15.0"
38 changes: 0 additions & 38 deletions crates/ark-db/src/block/create.rs

This file was deleted.

41 changes: 0 additions & 41 deletions crates/ark-db/src/block/get.rs

This file was deleted.

20 changes: 0 additions & 20 deletions crates/ark-db/src/block/update.rs

This file was deleted.

39 changes: 39 additions & 0 deletions crates/ark-db/src/indexer/get.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use aws_sdk_dynamodb::types::AttributeValue;
use aws_sdk_dynamodb::{Client, Error};
use std::env;

pub async fn get_block(dynamo_client: &Client, block_number: u64) -> Result<bool, Error> {
let indexer_table_name =
env::var("ARK_INDEXER_TABLE_NAME").expect("ARK_INDEXER_TABLE_NAME must be set");
let partition_key = format!("BLOCK#{}", block_number);

let request = dynamo_client
.query()
.table_name(indexer_table_name)
.key_condition_expression("#PK = :PK")
.expression_attribute_names("#PK", "PK")
.expression_attribute_values(":PK", AttributeValue::S(partition_key));

match request.send().await {
Ok(value) => {
if let Some(items) = value.items() {
if let Some(item) = items.first() {
if let Some(is_fetched) = item.get("is_fetched") {
match is_fetched.as_bool() {
Ok(is_fetched_bool) => return Ok(*is_fetched_bool),
Err(_) => return Ok(false),
}
}
}
}
}
Err(e) => {
println!(
"Error requesting block number {} to the list of fetched blocks: {:?}",
block_number, e
);
}
}

Ok(false)
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod create;
pub mod get;
pub mod update;
69 changes: 69 additions & 0 deletions crates/ark-db/src/indexer/update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::env;

use anyhow::Result;
use aws_sdk_dynamodb::types::AttributeValue;
use aws_sdk_dynamodb::Client;
use chrono::Utc;

pub async fn update_indexer(
dynamo_client: &Client,
task_id: &str,
status: String,
from: Option<u64>,
to: Option<u64>,
indexation_progress: Option<String>,
) -> Result<()> {
let indexer_table_name =
env::var("ARK_INDEXER_TABLE_NAME").expect("ARK_INDEXER_TABLE_NAME must be set");
let indexer_version = env::var("ARK_INDEXER_VERSION").unwrap_or(String::from("undefined"));
let now = Utc::now();
let unix_timestamp = now.timestamp();

let mut request = dynamo_client
.put_item()
.table_name(indexer_table_name)
.item("PK", AttributeValue::S(String::from("INDEXER")))
.item("SK", AttributeValue::S(format!("TASK#{}", task_id)))
.item("status", AttributeValue::S(status))
.item("last_update", AttributeValue::N(unix_timestamp.to_string()))
.item("version", AttributeValue::S(indexer_version))
.item("task_id", AttributeValue::S(task_id.to_string()));

if let Some(value) = from {
request = request.item("from", AttributeValue::N(value.to_string()));
}

if let Some(value) = to {
request = request.item("to", AttributeValue::N(value.to_string()));
}

if let Some(value) = indexation_progress {
request = request.item("indexation_progress", AttributeValue::N(value.to_string()));
}

request.send().await?;

Ok(())
}

pub async fn update_block(dynamo_client: &Client, task_id: &str, block_number: u64) -> Result<()> {
let indexer_table_name =
env::var("ARK_INDEXER_TABLE_NAME").expect("ARK_INDEXER_TABLE_NAME must be set");
let indexer_version = env::var("ARK_INDEXER_VERSION").unwrap_or(String::from("undefined"));
let now = Utc::now();
let unix_timestamp = now.timestamp();

dynamo_client
.put_item()
.table_name(indexer_table_name)
.item("PK", AttributeValue::S(format!("BLOCK#{}", block_number)))
.item("SK", AttributeValue::S(format!("TASK#{}", task_id)))
.item("is_fetched", AttributeValue::Bool(true))
.item("last_update", AttributeValue::N(unix_timestamp.to_string()))
.item("task_id", AttributeValue::S(task_id.to_string()))
.item("version", AttributeValue::S(indexer_version))
.send()
.await?;

Ok(())
}
2 changes: 1 addition & 1 deletion crates/ark-db/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod block;
pub mod collection;
pub mod contract;
pub mod indexer;
pub mod owners;
pub mod token;
pub mod token_event;
2 changes: 1 addition & 1 deletion crates/ark-starknet/src/collection_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl CollectionManager {
effective_block_id,
)
.await
.or_else(|_| Err(anyhow!("Failed to get token owner"))),
.map_err(|_| anyhow!("Failed to get token owner")),
}
}

Expand Down
Loading
Loading