Skip to content

Commit

Permalink
feat(torii): fetch and process erc721 metadata and image
Browse files Browse the repository at this point in the history
commit-id:274aaa3a
  • Loading branch information
lambda-0x committed Oct 23, 2024
1 parent 79572d8 commit 624919a
Show file tree
Hide file tree
Showing 26 changed files with 1,551 additions and 259 deletions.
455 changes: 450 additions & 5 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,16 @@ clap_complete = "4.3"
console = "0.15.7"
convert_case = "0.6.0"
crypto-bigint = { version = "0.5.3", features = [ "serde" ] }
data-url = "0.3"
derive_more = "0.99.17"
flate2 = "1.0.24"
fluent-uri = "0.3"
futures = "0.3.30"
futures-util = "0.3.30"
hashlink = "0.9.1"
hex = "0.4.3"
http = "0.2.9"
image = "0.25.2"
indexmap = "2.2.5"
indoc = "1.0.7"
itertools = "0.12.1"
Expand Down Expand Up @@ -209,14 +212,17 @@ tracing-log = "0.1.3"
tracing-subscriber = { version = "0.3.16", features = [ "env-filter", "json" ] }
url = { version = "2.4.0", features = [ "serde" ] }
walkdir = "2.5.0"
# TODO: see if we still need the git version
ipfs-api-backend-hyper = { git = "https://github.com/ferristseng/rust-ipfs-api", rev = "af2c17f7b19ef5b9898f458d97a90055c3605633", features = [ "with-hyper-rustls", "with-send-sync" ] }
mime_guess = "2.0"

# server
hyper = "0.14.27"
warp = "0.3"

# gRPC
prost = "0.12"
tonic = { version = "0.11", features = [ "tls", "tls-roots", "gzip" ] }
tonic = { version = "0.11", features = [ "gzip", "tls", "tls-roots" ] }
tonic-build = "0.11"
tonic-reflection = "0.11"
tonic-web = "0.11"
Expand Down
49 changes: 42 additions & 7 deletions bin/torii/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::sync::Arc;
use std::time::Duration;

use anyhow::Context;
use camino::Utf8PathBuf;
use clap::{ArgAction, Parser};
use dojo_metrics::exporters::prometheus::PrometheusRecorder;
use dojo_utils::parse::{parse_socket_address, parse_url};
Expand All @@ -30,7 +31,7 @@ use sqlx::SqlitePool;
use starknet::core::types::Felt;
use starknet::providers::jsonrpc::HttpTransport;
use starknet::providers::JsonRpcClient;
use tempfile::NamedTempFile;
use tempfile::{NamedTempFile, TempDir};
use tokio::sync::broadcast;
use tokio::sync::broadcast::Sender;
use tokio_stream::StreamExt;
Expand Down Expand Up @@ -146,6 +147,10 @@ struct Args {
/// Configuration file
#[arg(long)]
config: Option<PathBuf>,

/// Path to a directory to store ERC artifacts
#[arg(long)]
artifacts_path: Option<Utf8PathBuf>,
}

#[tokio::main]
Expand Down Expand Up @@ -213,7 +218,18 @@ async fn main() -> anyhow::Result<()> {
let contracts =
config.contracts.iter().map(|contract| (contract.address, contract.r#type)).collect();

let (mut executor, sender) = Executor::new(pool.clone(), shutdown_tx.clone()).await?;
let temp_dir = TempDir::new()?;
let artifacts_path =
args.artifacts_path.unwrap_or_else(|| Utf8PathBuf::from(temp_dir.path().to_str().unwrap()));

let (mut executor, sender) = Executor::new(
pool.clone(),
shutdown_tx.clone(),
&artifacts_path,
provider.clone(),
args.max_concurrent_tasks,
)
.await?;

Check warning on line 232 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L221-L232

Added lines #L221 - L232 were not covered by tests
tokio::spawn(async move {
executor.run().await.unwrap();
});
Expand Down Expand Up @@ -254,10 +270,20 @@ async fn main() -> anyhow::Result<()> {
Arc::new(contracts),
);

let shutdown_rx = shutdown_tx.subscribe();
let (grpc_addr, grpc_server) =
torii_grpc::server::new(shutdown_rx, &pool, block_rx, world_address, Arc::clone(&provider))
.await?;
let (grpc_addr, grpc_server) = torii_grpc::server::new(
shutdown_tx.subscribe(),
&pool,
block_rx,
world_address,
Arc::clone(&provider),
)
.await?;

tokio::fs::create_dir_all(&artifacts_path).await?;
let absolute_path = artifacts_path.canonicalize_utf8()?;

let (artifacts_addr, artifacts_server) =
torii_server::artifacts::new(shutdown_tx.subscribe(), &absolute_path, pool.clone()).await?;

Check warning on line 286 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L273-L286

Added lines #L273 - L286 were not covered by tests

let mut libp2p_relay_server = torii_relay::server::Relay::new(
db,
Expand All @@ -270,7 +296,13 @@ async fn main() -> anyhow::Result<()> {
)
.expect("Failed to start libp2p relay server");

let proxy_server = Arc::new(Proxy::new(args.addr, args.allowed_origins, Some(grpc_addr), None));
let proxy_server = Arc::new(Proxy::new(
args.addr,
args.allowed_origins,
Some(grpc_addr),
None,
Some(artifacts_addr),
));

Check warning on line 305 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L299-L305

Added lines #L299 - L305 were not covered by tests

let graphql_server = spawn_rebuilding_graphql_server(
shutdown_tx.clone(),
Expand All @@ -288,6 +320,7 @@ async fn main() -> anyhow::Result<()> {
info!(target: LOG_TARGET, endpoint = %endpoint, "Starting torii endpoint.");
info!(target: LOG_TARGET, endpoint = %gql_endpoint, "Serving Graphql playground.");
info!(target: LOG_TARGET, url = %explorer_url, "Serving World Explorer.");
info!(target: LOG_TARGET, path = %artifacts_path, "Serving ERC artifacts at path");

Check warning on line 323 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L323

Added line #L323 was not covered by tests

if args.explorer {
if let Err(e) = webbrowser::open(&explorer_url) {
Expand All @@ -308,13 +341,15 @@ async fn main() -> anyhow::Result<()> {
let graphql_server_handle = tokio::spawn(graphql_server);
let grpc_server_handle = tokio::spawn(grpc_server);
let libp2p_relay_server_handle = tokio::spawn(async move { libp2p_relay_server.run().await });
let artifacts_server_handle = tokio::spawn(artifacts_server);

Check warning on line 344 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L344

Added line #L344 was not covered by tests

tokio::select! {
res = engine_handle => res??,
res = proxy_server_handle => res??,
res = graphql_server_handle => res?,
res = grpc_server_handle => res??,
res = libp2p_relay_server_handle => res?,
res = artifacts_server_handle => res?,

Check warning on line 352 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L352

Added line #L352 was not covered by tests
_ = dojo_utils::signal::wait_signals() => {},
};

Expand Down
6 changes: 3 additions & 3 deletions crates/dojo-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ anyhow.workspace = true
async-trait.workspace = true
cairo-lang-filesystem.workspace = true
cairo-lang-project.workspace = true
cairo-lang-starknet.workspace = true
cairo-lang-starknet-classes.workspace = true
cairo-lang-starknet.workspace = true
camino.workspace = true
convert_case.workspace = true
dojo-utils = { workspace = true, optional = true }
Expand All @@ -22,16 +22,16 @@ serde.workspace = true
serde_json.workspace = true
serde_with.workspace = true
smol_str.workspace = true
starknet.workspace = true
starknet-crypto.workspace = true
starknet.workspace = true
thiserror.workspace = true
topological-sort.workspace = true
tracing.workspace = true

cainome.workspace = true
dojo-types = { path = "../dojo-types", optional = true }
http = { workspace = true, optional = true }
ipfs-api-backend-hyper = { git = "https://github.com/ferristseng/rust-ipfs-api", rev = "af2c17f7b19ef5b9898f458d97a90055c3605633", features = [ "with-hyper-rustls" ], optional = true }
ipfs-api-backend-hyper = { workspace = true, optional = true }
scarb = { workspace = true, optional = true }
tokio = { version = "1.32.0", features = [ "time" ], default-features = false, optional = true }
toml.workspace = true
Expand Down
10 changes: 5 additions & 5 deletions crates/sozo/ops/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ cairo-lang-defs.workspace = true
cairo-lang-filesystem.workspace = true
cairo-lang-plugins.workspace = true
cairo-lang-project.workspace = true
cairo-lang-sierra.workspace = true
cairo-lang-sierra-to-casm.workspace = true
cairo-lang-starknet.workspace = true
cairo-lang-sierra.workspace = true
cairo-lang-starknet-classes.workspace = true
cairo-lang-starknet.workspace = true
cairo-lang-test-plugin.workspace = true
cairo-lang-utils.workspace = true
camino.workspace = true
Expand All @@ -36,16 +36,16 @@ num-bigint = "0.4.6"
num-traits.workspace = true
reqwest.workspace = true
rpassword.workspace = true
scarb.workspace = true
scarb-ui.workspace = true
scarb.workspace = true
semver.workspace = true
serde.workspace = true
serde_json.workspace = true
serde_with.workspace = true
smol_str.workspace = true
sozo-walnut = { workspace = true, optional = true }
starknet.workspace = true
starknet-crypto.workspace = true
starknet.workspace = true
thiserror.workspace = true
tokio.workspace = true
toml.workspace = true
Expand All @@ -58,7 +58,7 @@ katana-runner = { workspace = true, optional = true }
[dev-dependencies]
assert_fs.workspace = true
dojo-test-utils = { workspace = true, features = [ "build-examples" ] }
ipfs-api-backend-hyper = { git = "https://github.com/ferristseng/rust-ipfs-api", rev = "af2c17f7b19ef5b9898f458d97a90055c3605633", features = [ "with-hyper-rustls" ] }
ipfs-api-backend-hyper.workspace = true
katana-runner.workspace = true

[features]
Expand Down
4 changes: 4 additions & 0 deletions crates/torii/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ async-trait.workspace = true
base64.workspace = true
bitflags = "2.6.0"
cainome.workspace = true
camino.workspace = true
chrono.workspace = true
crypto-bigint.workspace = true
data-url.workspace = true
dojo-types.workspace = true
dojo-world = { workspace = true, features = [ "contracts", "manifest" ] }
# fluent-uri.workspace = true
futures-channel = "0.3.0"
futures-util.workspace = true
hashlink.workspace = true
Expand All @@ -33,6 +36,7 @@ starknet.workspace = true
thiserror.workspace = true
tokio = { version = "1.32.0", features = [ "sync" ], default-features = true }
# tokio-stream = "0.1.11"
ipfs-api-backend-hyper.workspace = true
tokio-util.workspace = true
toml.workspace = true
tracing.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion crates/torii/core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,9 @@ impl<P: Provider + Send + Sync + std::fmt::Debug + 'static> Engine<P> {

match self.process(fetch_result).await {
Ok(_) => {
self.db.execute().await?;
self.db.flush().await?;
self.db.apply_cache_diff().await?;
self.db.execute().await?;
},
Err(e) => {
error!(target: LOG_TARGET, error = %e, "Processing fetched data.");
Expand Down
Loading

0 comments on commit 624919a

Please sign in to comment.