Skip to content

Commit

Permalink
feat(torii/graphql): cleanup names and add imagePath and metadata to …
Browse files Browse the repository at this point in the history
…erc_token

commit-id:21a1c0a5
  • Loading branch information
lambda-0x committed Nov 3, 2024
1 parent ee7257b commit 712afc4
Show file tree
Hide file tree
Showing 16 changed files with 193 additions and 89 deletions.
5 changes: 2 additions & 3 deletions bin/torii/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,7 @@ async fn main() -> anyhow::Result<()> {
args.max_concurrent_tasks,
)
.await?;
tokio::spawn(async move {
executor.run().await.unwrap();
});
let executor_handle = tokio::spawn(async move { executor.run().await });

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

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L228

Added line #L228 was not covered by tests

let db = Sql::new(pool.clone(), sender.clone(), &contracts).await?;

Expand Down Expand Up @@ -344,6 +342,7 @@ async fn main() -> anyhow::Result<()> {

tokio::select! {
res = engine_handle => res??,
res = executor_handle => res??,

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

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L345

Added line #L345 was not covered by tests
res = proxy_server_handle => res??,
res = graphql_server_handle => res?,
res = grpc_server_handle => res??,
Expand Down
3 changes: 3 additions & 0 deletions crates/torii/core/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub const TOKEN_BALANCE_TABLE: &str = "token_balances";
pub const TOKEN_TRANSFER_TABLE: &str = "token_transfers";
pub const TOKENS_TABLE: &str = "tokens";
7 changes: 4 additions & 3 deletions crates/torii/core/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use tokio::task::JoinSet;
use tokio::time::Instant;
use tracing::{debug, error};

use crate::constants::TOKENS_TABLE;
use crate::simple_broker::SimpleBroker;
use crate::sql::utils::{felt_to_sql_string, I256};
use crate::types::{
Expand Down Expand Up @@ -600,9 +601,9 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
QueryType::RegisterErc721Token(register_erc721_token) => {
let semaphore = self.semaphore.clone();
let provider = self.provider.clone();
let res = sqlx::query_as::<_, (String, String)>(
"SELECT name, symbol FROM tokens WHERE contract_address = ?",
)
let res = sqlx::query_as::<_, (String, String)>(&format!(
"SELECT name, symbol FROM {TOKENS_TABLE} WHERE contract_address = ?"
))

Check warning on line 606 in crates/torii/core/src/executor/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/mod.rs#L604-L606

Added lines #L604 - L606 were not covered by tests
.bind(felt_to_sql_string(&register_erc721_token.contract_address))
.fetch_one(&mut **tx)
.await;
Expand Down
1 change: 1 addition & 0 deletions crates/torii/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![warn(unused_crate_dependencies)]

pub mod constants;
pub mod engine;
pub mod error;
pub mod executor;
Expand Down
16 changes: 7 additions & 9 deletions crates/torii/core/src/sql/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use sqlx::{Pool, Sqlite, SqlitePool};
use starknet_crypto::Felt;
use tokio::sync::RwLock;

use crate::constants::TOKEN_BALANCE_TABLE;
use crate::error::{Error, ParseError, QueryError};
use crate::model::{parse_sql_model_members, SqlModelMember};
use crate::sql::utils::I256;
Expand Down Expand Up @@ -133,27 +134,24 @@ pub struct LocalCache {

impl Clone for LocalCache {
fn clone(&self) -> Self {
Self { erc_cache: HashMap::new(), token_id_registry: HashSet::new() }
Self { erc_cache: HashMap::new(), token_id_registry: self.token_id_registry.clone() }
}
}

impl LocalCache {
pub async fn new(pool: Pool<Sqlite>) -> Self {
// read existing token_id's from balances table and cache them
let token_id_registry: Vec<(String,)> = sqlx::query_as("SELECT token_id FROM balances")
.fetch_all(&pool)
.await
.expect("Should be able to read token_id's from blances table");
let token_id_registry: Vec<(String,)> =
sqlx::query_as(&format!("SELECT token_id FROM {TOKEN_BALANCE_TABLE}"))
.fetch_all(&pool)
.await
.expect("Should be able to read token_id's from blances table");

let token_id_registry = token_id_registry.into_iter().map(|token_id| token_id.0).collect();

Self { erc_cache: HashMap::new(), token_id_registry }
}

pub fn empty() -> Self {
Self { erc_cache: HashMap::new(), token_id_registry: HashSet::new() }
}

pub fn contains_token_id(&self, token_id: &str) -> bool {
self.token_id_registry.contains(token_id)
}
Expand Down
8 changes: 5 additions & 3 deletions crates/torii/core/src/sql/erc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use starknet::providers::Provider;

use super::utils::{u256_to_sql_string, I256};
use super::{Sql, FELT_DELIMITER};
use crate::constants::TOKEN_TRANSFER_TABLE;
use crate::executor::{
ApplyBalanceDiffQuery, Argument, QueryMessage, QueryType, RegisterErc20TokenQuery,
RegisterErc721TokenQuery,
Expand Down Expand Up @@ -249,9 +250,10 @@ impl Sql {
block_timestamp: u64,
event_id: &str,
) -> Result<()> {
let insert_query = "INSERT INTO erc_transfers (id, contract_address, from_address, \
to_address, amount, token_id, executed_at) VALUES (?, ?, ?, ?, ?, ?, \
?)";
let insert_query = format!(
"INSERT INTO {TOKEN_TRANSFER_TABLE} (id, contract_address, from_address, to_address, \
amount, token_id, executed_at) VALUES (?, ?, ?, ?, ?, ?, ?)"
);

Check warning on line 256 in crates/torii/core/src/sql/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/sql/erc.rs#L253-L256

Added lines #L253 - L256 were not covered by tests

self.executor.send(QueryMessage::new(
insert_query.to_string(),
Expand Down
1 change: 0 additions & 1 deletion crates/torii/core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use ipfs_api_backend_hyper::{IpfsApi, IpfsClient, TryFromUri};
use tokio_util::bytes::Bytes;
use tracing::info;

// pub const IPFS_URL: &str = "https://cartridge.infura-ipfs.io/ipfs/";
pub const IPFS_URL: &str = "https://ipfs.io/ipfs/";
pub const MAX_RETRY: u8 = 3;

Expand Down
16 changes: 8 additions & 8 deletions crates/torii/graphql/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ pub const EVENT_MESSAGE_TABLE: &str = "event_messages";
pub const MODEL_TABLE: &str = "models";
pub const TRANSACTION_TABLE: &str = "transactions";
pub const METADATA_TABLE: &str = "metadata";
pub const ERC_BALANCE_TABLE: &str = "balances";
pub const ERC_TRANSFER_TABLE: &str = "erc_transfers";

pub const ID_COLUMN: &str = "id";
pub const EVENT_ID_COLUMN: &str = "event_id";
Expand All @@ -35,9 +33,10 @@ pub const QUERY_TYPE_NAME: &str = "World__Query";
pub const SUBSCRIPTION_TYPE_NAME: &str = "World__Subscription";
pub const MODEL_ORDER_TYPE_NAME: &str = "World__ModelOrder";
pub const MODEL_ORDER_FIELD_TYPE_NAME: &str = "World__ModelOrderField";
pub const ERC_BALANCE_TYPE_NAME: &str = "ERC__Balance";
pub const ERC_TRANSFER_TYPE_NAME: &str = "ERC__Transfer";
pub const ERC_TOKEN_TYPE_NAME: &str = "ERC__Token";
pub const TOKEN_BALANCE_TYPE_NAME: &str = "Token__Balance";
pub const TOKEN_TRANSFER_TYPE_NAME: &str = "Token__Transfer";
pub const TOKEN_TYPE_NAME: &str = "ERC__Token";
pub const ERC721_METADATA_TYPE_NAME: &str = "ERC721__Metadata";

// objects' single and plural names
pub const ENTITY_NAMES: (&str, &str) = ("entity", "entities");
Expand All @@ -49,10 +48,11 @@ pub const CONTENT_NAMES: (&str, &str) = ("content", "contents");
pub const METADATA_NAMES: (&str, &str) = ("metadata", "metadatas");
pub const TRANSACTION_NAMES: (&str, &str) = ("transaction", "transactions");
pub const PAGE_INFO_NAMES: (&str, &str) = ("pageInfo", "");
pub const TOKEN_NAME: (&str, &str) = ("token", "tokens");
pub const TOKEN_BALANCE_NAME: (&str, &str) = ("", "tokenBalances");
pub const TOKEN_TRANSFER_NAME: (&str, &str) = ("", "tokenTransfers");

pub const ERC_BALANCE_NAME: (&str, &str) = ("ercBalance", "");
pub const ERC_TOKEN_NAME: (&str, &str) = ("ercToken", "");
pub const ERC_TRANSFER_NAME: (&str, &str) = ("ercTransfer", "");
pub const ERC721_METADATA_NAME: (&str, &str) = ("erc721Metadata", "");

// misc
pub const ORDER_DIR_TYPE_NAME: &str = "OrderDirection";
Expand Down
30 changes: 22 additions & 8 deletions crates/torii/graphql/src/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use async_graphql::Name;
use dojo_types::primitive::Primitive;
use lazy_static::lazy_static;

use crate::constants::{CONTENT_TYPE_NAME, ERC_TOKEN_TYPE_NAME, SOCIAL_TYPE_NAME};
use crate::constants::{
CONTENT_TYPE_NAME, ERC721_METADATA_TYPE_NAME, SOCIAL_TYPE_NAME, TOKEN_TYPE_NAME,
};
use crate::types::{GraphqlType, TypeData, TypeMapping};

lazy_static! {
Expand Down Expand Up @@ -145,27 +147,39 @@ lazy_static! {
),
]);

pub static ref ERC_BALANCE_TYPE_MAPPING: TypeMapping = IndexMap::from([
pub static ref TOKEN_BALANCE_TYPE_MAPPING: TypeMapping = IndexMap::from([
(Name::new("balance"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("type"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("tokenMetadata"), TypeData::Simple(TypeRef::named_nn(ERC_TOKEN_TYPE_NAME))),
(Name::new("tokenMetadata"), TypeData::Simple(TypeRef::named_nn(TOKEN_TYPE_NAME))),
]);

pub static ref ERC_TRANSFER_TYPE_MAPPING: TypeMapping = IndexMap::from([
pub static ref TOKEN_TRANSFER_TYPE_MAPPING: TypeMapping = IndexMap::from([
(Name::new("from"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("to"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("amount"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("type"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("executedAt"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("tokenMetadata"), TypeData::Simple(TypeRef::named_nn(ERC_TOKEN_TYPE_NAME))),
(Name::new("tokenMetadata"), TypeData::Simple(TypeRef::named_nn(TOKEN_TYPE_NAME))),
(Name::new("transactionHash"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
]);

pub static ref ERC_TOKEN_TYPE_MAPPING: TypeMapping = IndexMap::from([
pub static ref TOKEN_TYPE_MAPPING: TypeMapping = IndexMap::from([
(Name::new("name"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("symbol"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("tokenId"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("decimals"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("contractAddress"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("decimals"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(
Name::new("erc721"),
TypeData::Nested((TypeRef::named(ERC721_METADATA_TYPE_NAME), IndexMap::new()))
),
]);

pub static ref ERC721_METADATA_TYPE_MAPPING: TypeMapping = IndexMap::from([
(Name::new("tokenId"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("name"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("description"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("attributes"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("imagePath"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("metadata"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
]);
}
29 changes: 24 additions & 5 deletions crates/torii/graphql/src/object/erc/erc_token.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::constants::{ERC_TOKEN_NAME, ERC_TOKEN_TYPE_NAME};
use crate::mapping::ERC_TOKEN_TYPE_MAPPING;
use crate::constants::{
ERC721_METADATA_NAME, ERC721_METADATA_TYPE_NAME, TOKEN_NAME, TOKEN_TYPE_NAME,
};
use crate::mapping::{ERC721_METADATA_TYPE_MAPPING, TOKEN_TYPE_MAPPING};
use crate::object::BasicObject;
use crate::types::TypeMapping;

Expand All @@ -8,14 +10,31 @@ pub struct ErcTokenObject;

impl BasicObject for ErcTokenObject {
fn name(&self) -> (&str, &str) {
ERC_TOKEN_NAME
TOKEN_NAME

Check warning on line 13 in crates/torii/graphql/src/object/erc/erc_token.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/erc_token.rs#L13

Added line #L13 was not covered by tests
}

fn type_name(&self) -> &str {
ERC_TOKEN_TYPE_NAME
TOKEN_TYPE_NAME
}

fn type_mapping(&self) -> &TypeMapping {
&ERC_TOKEN_TYPE_MAPPING
&TOKEN_TYPE_MAPPING
}
}

#[derive(Debug)]
pub struct Erc721MetadataObject;

impl BasicObject for Erc721MetadataObject {
fn name(&self) -> (&str, &str) {
ERC721_METADATA_NAME
}

Check warning on line 31 in crates/torii/graphql/src/object/erc/erc_token.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/erc_token.rs#L29-L31

Added lines #L29 - L31 were not covered by tests

fn type_name(&self) -> &str {
ERC721_METADATA_TYPE_NAME
}

fn type_mapping(&self) -> &TypeMapping {
&ERC721_METADATA_TYPE_MAPPING
}
}
4 changes: 2 additions & 2 deletions crates/torii/graphql/src/object/erc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::connection::cursor;
use crate::query::order::CursorDirection;

pub mod erc_balance;
pub mod erc_token;
pub mod erc_transfer;
pub mod token_balance;
pub mod token_transfer;

fn handle_cursor(
cursor: &str,
Expand Down
Loading

0 comments on commit 712afc4

Please sign in to comment.