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

fix(sdk): Id generation + space import #20

Merged
merged 3 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions Cargo.lock

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

13 changes: 6 additions & 7 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use clap::{Args, Parser, Subcommand};
use futures::{stream, StreamExt, TryStreamExt};
use ipfs::IpfsClient;
use sdk::mapping::{Entity, Named};
use sdk::{ids, pb};
use sdk::{ids, neo4rs, pb};
use sink::bootstrap::constants;
use sink::kg;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;

Expand All @@ -21,7 +20,7 @@ async fn main() -> anyhow::Result<()> {
init_tracing();
let args = AppArgs::parse();

let kg = kg::Client::new(
let neo4j: neo4rs::Graph = neo4rs::Graph::new(
&args.neo4j_args.neo4j_uri,
&args.neo4j_args.neo4j_user,
&args.neo4j_args.neo4j_pass,
Expand All @@ -45,7 +44,7 @@ async fn main() -> anyhow::Result<()> {
unimplemented!()
}
Command::Describe { id, space_id } => {
let entity = Entity::<Named>::find_by_id(&kg.neo4j, &id, &space_id)
let entity = Entity::<Named>::find_by_id(&neo4j, &id, &space_id)
.await?
.expect("Entity not found");

Expand Down Expand Up @@ -77,12 +76,12 @@ async fn main() -> anyhow::Result<()> {
}
Command::ImportSpace {
ipfs_hash,
space_id,
space_id: _,
} => {
let ops = import_space(&ipfs_client, &ipfs_hash).await?;
let _ = import_space(&ipfs_client, &ipfs_hash).await?;
// let rollups = conversions::batch_ops(ops);

kg.process_ops(&Default::default(), &space_id, ops).await?
// neo4j.process_ops(&Default::default(), &space_id, ops).await?
}
Command::CreateEntityId { n } => {
for _ in 0..n {
Expand Down
8 changes: 4 additions & 4 deletions codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use futures::{stream, StreamExt, TryStreamExt};
use sdk::mapping::{Entity, Named};
use sdk::system_ids;
use sdk::{neo4rs, system_ids};
use swc::config::SourceMapsConfig;
use swc::PrintArgs;
use swc_common::{sync::Lrc, SourceMap, Span};
Expand Down Expand Up @@ -160,7 +160,7 @@ impl EntityExt for Entity<Named> {

/// Generate a TypeScript class declaration from an entity.
/// Note: The entity must be a `Type` entity.
pub async fn gen_type(_kg: &sink::kg::Client, entity: &Entity<Named>) -> anyhow::Result<Decl> {
pub async fn gen_type(_kg: &neo4rs::Graph, entity: &Entity<Named>) -> anyhow::Result<Decl> {
// let attrs = kg.attribute_nodes::<Named>(entity.id()).await?;
let attrs = vec![]; // FIXME: Temporary while we figure out what to do with codegen

Expand Down Expand Up @@ -236,7 +236,7 @@ pub async fn gen_type(_kg: &sink::kg::Client, entity: &Entity<Named>) -> anyhow:
}

/// Generate a TypeScript module containing class definitions from all types in the knowledge graph.
pub async fn gen_types(kg: &sink::kg::Client) -> anyhow::Result<Program> {
pub async fn gen_types(kg: &neo4rs::Graph) -> anyhow::Result<Program> {
let import_stmts = vec![
quote!("import { Driver, Node } from 'neo4j-driver';" as ModuleItem),
quote!("import { Entity } from './kg';" as ModuleItem),
Expand Down Expand Up @@ -271,7 +271,7 @@ pub async fn gen_types(kg: &sink::kg::Client) -> anyhow::Result<Program> {
}

/// Generate and render TypeScript code from the knowledge graph.
pub async fn codegen(kg: &sink::kg::Client) -> anyhow::Result<String> {
pub async fn codegen(kg: &neo4rs::Graph) -> anyhow::Result<String> {
let cm: Lrc<SourceMap> = Default::default();
let compiler = swc::Compiler::new(cm.clone());

Expand Down
12 changes: 10 additions & 2 deletions sdk/src/ids/base58.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,24 @@ mod tests {
#[test]
fn test_base58_encoding() {
assert_eq!(
encode_uuid_to_base58("1cc6995f-6cc2-4c7a-9592-1466bf95f6be"),
"4Z6VLmpipszCVZb21Fey5F",
encode_uuid_to_base58("1cc6995f-6cc2-4c7a-9592-1466bf95f6be")
)
}

#[test]
fn test_base58_encoding_2() {
assert_eq!(
encode_uuid_to_base58("08c4f093-7858-4b7c-9b94-b82e448abcff"),
"25omwWh6HYgeRQKCaSpVpa",
)
}

#[test]
fn test_base58_decoding() {
assert_eq!(
decode_base58_to_uuid("4Z6VLmpipszCVZb21Fey5F").unwrap(),
"1cc6995f-6cc2-4c7a-9592-1466bf95f6be",
decode_base58_to_uuid("4Z6VLmpipszCVZb21Fey5F").unwrap()
)
}

Expand Down
23 changes: 20 additions & 3 deletions sdk/src/ids/id.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Display;

use md5::{Digest, Md5};
use uuid::Uuid;
use uuid::Builder;

use super::base58::encode_uuid_to_base58;

Expand Down Expand Up @@ -58,12 +58,29 @@ pub fn create_space_id(network: &str, address: &str) -> String {
pub fn create_id_from_unique_string(text: &str) -> String {
let mut hasher = Md5::new();
hasher.update(text);
let hashed = hasher.finalize();
let hashed: [u8; 16] = hasher.finalize().into();

let uuid = Uuid::from_slice(&hashed[..]).unwrap();
let uuid = Builder::from_random_bytes(hashed).into_uuid();
encode_uuid_to_base58(&uuid.to_string())
}

pub fn create_geo_id() -> String {
encode_uuid_to_base58(&uuid::Uuid::new_v4().to_string())
}

#[cfg(test)]
mod tests {
use super::*;
use crate::network_ids;

#[test]
fn test_space_id() {
assert_eq!(
create_space_id(
network_ids::GEO,
"0xcD48eF54771d9cf7dDA324c64bF4e53C161aF294"
),
"25omwWh6HYgeRQKCaSpVpa"
)
}
}
4 changes: 2 additions & 2 deletions sdk/src/models/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct GeoAccount {

impl GeoAccount {
pub fn new(address: String, block: &BlockMetadata) -> Entity<Self> {
let checksummed_address = checksum_address(&address, None);
let checksummed_address = checksum_address(&address);
Entity::new(
&ids::create_id_from_unique_string(&checksummed_address),
indexer_ids::INDEXER_SPACE_ID,
Expand All @@ -25,6 +25,6 @@ impl GeoAccount {
}

pub fn new_id(address: &str) -> String {
ids::create_id_from_unique_string(&checksum_address(address, None))
ids::create_id_from_unique_string(&checksum_address(address))
}
}
2 changes: 1 addition & 1 deletion sdk/src/models/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl Proposal {

let query = neo4rs::query(QUERY)
.param("proposal_id", proposal_id)
.param("plugin_address", checksum_address(plugin_address, None));
.param("plugin_address", checksum_address(plugin_address));

#[derive(Debug, Deserialize)]
struct ResultRow {
Expand Down
25 changes: 12 additions & 13 deletions sdk/src/models/space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct Space {

impl Space {
pub fn new_id(network: &str, address: &str) -> String {
ids::create_id_from_unique_string(&format!("{network}:{}", checksum_address(address, None)))
ids::create_id_from_unique_string(&format!("{network}:{}", checksum_address(address)))
}

pub fn builder(id: &str, dao_contract_address: &str, block: &BlockMetadata) -> SpaceBuilder {
Expand All @@ -57,7 +57,7 @@ impl Space {

let query = neo4rs::query(QUERY).param(
"dao_contract_address",
checksum_address(dao_contract_address, None),
checksum_address(dao_contract_address),
);

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -89,7 +89,7 @@ impl Space {

let query = neo4rs::query(QUERY).param(
"space_plugin_address",
checksum_address(space_plugin_address, None),
checksum_address(space_plugin_address),
);

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -121,7 +121,7 @@ impl Space {

let query = neo4rs::query(QUERY).param(
"voting_plugin_address",
checksum_address(voting_plugin_address, None),
checksum_address(voting_plugin_address),
);

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -153,7 +153,7 @@ impl Space {

let query = neo4rs::query(QUERY).param(
"member_access_plugin",
checksum_address(member_access_plugin, None),
checksum_address(member_access_plugin),
);

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -185,7 +185,7 @@ impl Space {

let query = neo4rs::query(QUERY).param(
"personal_space_admin_plugin",
checksum_address(personal_space_admin_plugin, None),
checksum_address(personal_space_admin_plugin),
);

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -256,7 +256,7 @@ impl SpaceBuilder {
block: block.clone(),
network: network_ids::GEO.to_string(),
r#type: SpaceType::Public,
dao_contract_address: checksum_address(dao_contract_address, None),
dao_contract_address: checksum_address(dao_contract_address),
space_plugin_address: None,
voting_plugin_address: None,
member_access_plugin: None,
Expand All @@ -275,28 +275,27 @@ impl SpaceBuilder {
}

pub fn dao_contract_address(mut self, dao_contract_address: &str) -> Self {
self.dao_contract_address = checksum_address(dao_contract_address, None);
self.dao_contract_address = checksum_address(dao_contract_address);
self
}

pub fn space_plugin_address(mut self, space_plugin_address: &str) -> Self {
self.space_plugin_address = Some(checksum_address(space_plugin_address, None));
self.space_plugin_address = Some(checksum_address(space_plugin_address));
self
}

pub fn voting_plugin_address(mut self, voting_plugin_address: &str) -> Self {
self.voting_plugin_address = Some(checksum_address(voting_plugin_address, None));
self.voting_plugin_address = Some(checksum_address(voting_plugin_address));
self
}

pub fn member_access_plugin(mut self, member_access_plugin: &str) -> Self {
self.member_access_plugin = Some(checksum_address(member_access_plugin, None));
self.member_access_plugin = Some(checksum_address(member_access_plugin));
self
}

pub fn personal_space_admin_plugin(mut self, personal_space_admin_plugin: &str) -> Self {
self.personal_space_admin_plugin =
Some(checksum_address(personal_space_admin_plugin, None));
self.personal_space_admin_plugin = Some(checksum_address(personal_space_admin_plugin));
self
}

Expand Down
1 change: 1 addition & 0 deletions sink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ substreams-utils = { version = "0.1.0", path = "../substreams-utils" }
sdk = { version = "0.1.0", path = "../sdk" }
ipfs = { version = "0.1.0", path = "../ipfs" }
web3-utils = { version = "0.1.0", path = "../web3-utils" }
tracing-appender = "0.2.3"

[dev-dependencies]
serde_path_to_error = "0.1.16"
2 changes: 1 addition & 1 deletion sink/src/bootstrap/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub const ROOT_SPACE_CREATED_AT: u32 = 1670280473;
pub const ROOT_SPACE_CREATED_AT_BLOCK: u32 = 620;
pub const ROOT_SPACE_CREATED_BY_ID: &str = "0x66703c058795B9Cb215fbcc7c6b07aee7D216F24";

pub const ROOT_SPACE_ID: &str = "BJqiLPcSgfF8FRxkFr76Uy";
pub const ROOT_SPACE_ID: &str = "EHoZ9qvSPmzxNmReVcCTSw";
pub const ROOT_SPACE_DAO_ADDRESS: &str = "0xB3191d353c4e409Add754112544296449B18c1Af";
pub const ROOT_SPACE_PLUGIN_ADDRESS: &str = "0x2a2d20e5262b27e6383da774E942dED3e4Bf5FaF";
pub const ROOT_SPACE_MAIN_VOTING_ADDRESS: &str = "0x9445A38102792654D92F1ba76Ee26a52Aa1E466e";
Expand Down
Loading