Skip to content

Commit

Permalink
feat: rework resource metadata to match artifacts format
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Jun 6, 2024
1 parent e834232 commit 5a091cd
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 22 deletions.
59 changes: 54 additions & 5 deletions crates/dojo-world/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,31 @@ pub fn dojo_metadata_from_workspace(ws: &Workspace<'_>) -> DojoMetadata {
if let Ok(manifest) = BaseManifest::load_from_path(&manifest_dir.join(BASE_DIR)) {
for model in manifest.models {
let name = model.name.to_string();
dojo_metadata.artifacts.insert(
dojo_metadata.resources_artifacts.insert(
name.clone(),
build_artifact_from_name(&sources_dir, &abis_dir.join("models"), &name),
ResourceMetadata {
name: name.clone(),
artifacts: build_artifact_from_name(
&sources_dir,
&abis_dir.join("models"),
&name,
),
},
);
}

for contract in manifest.contracts {
let name = contract.name.to_string();
dojo_metadata.artifacts.insert(
dojo_metadata.resources_artifacts.insert(
name.clone(),
build_artifact_from_name(&sources_dir, &abis_dir.join("contracts"), &name),
ResourceMetadata {
name: name.clone(),
artifacts: build_artifact_from_name(
&sources_dir,
&abis_dir.join("contracts"),
&name,
),
},
);
}
}
Expand All @@ -135,12 +149,19 @@ pub struct ProjectMetadata {
pub env: Option<Environment>,
}

/// Metadata for a user defined resource (models, contracts).
#[derive(Default, Serialize, Deserialize, Debug, Clone)]

Check warning on line 153 in crates/dojo-world/src/metadata.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-world/src/metadata.rs#L153

Added line #L153 was not covered by tests
pub struct ResourceMetadata {
pub name: String,
pub artifacts: ArtifactMetadata,
}

/// Metadata collected from the project configuration and the Dojo workspace
#[derive(Default, Deserialize, Debug, Clone)]
pub struct DojoMetadata {
pub world: WorldMetadata,
pub env: Option<Environment>,
pub artifacts: HashMap<String, ArtifactMetadata>,
pub resources_artifacts: HashMap<String, ResourceMetadata>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -342,6 +363,34 @@ impl ArtifactMetadata {
}
}

impl ResourceMetadata {
pub async fn upload(&self) -> Result<String> {
let mut meta = self.clone();
let client =
IpfsClient::from_str(IPFS_CLIENT_URL)?.with_credentials(IPFS_USERNAME, IPFS_PASSWORD);

Check warning on line 370 in crates/dojo-world/src/metadata.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-world/src/metadata.rs#L367-L370

Added lines #L367 - L370 were not covered by tests

if let Some(Uri::File(abi)) = &self.artifacts.abi {
let abi_data = std::fs::read(abi)?;
let reader = Cursor::new(abi_data);
let response = client.add(reader).await?;
meta.artifacts.abi = Some(Uri::Ipfs(format!("ipfs://{}", response.hash)))
};

Check warning on line 377 in crates/dojo-world/src/metadata.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-world/src/metadata.rs#L372-L377

Added lines #L372 - L377 were not covered by tests

if let Some(Uri::File(source)) = &self.artifacts.source {
let source_data = std::fs::read(source)?;
let reader = Cursor::new(source_data);
let response = client.add(reader).await?;
meta.artifacts.source = Some(Uri::Ipfs(format!("ipfs://{}", response.hash)))
};

Check warning on line 384 in crates/dojo-world/src/metadata.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-world/src/metadata.rs#L379-L384

Added lines #L379 - L384 were not covered by tests

let serialized = json!(meta).to_string();
let reader = Cursor::new(serialized);
let response = client.add(reader).await?;

Check warning on line 388 in crates/dojo-world/src/metadata.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-world/src/metadata.rs#L386-L388

Added lines #L386 - L388 were not covered by tests

Ok(response.hash)
}

Check warning on line 391 in crates/dojo-world/src/metadata.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-world/src/metadata.rs#L390-L391

Added lines #L390 - L391 were not covered by tests
}

impl DojoMetadata {
pub fn env(&self) -> Option<&Environment> {
self.env.as_ref()
Expand Down
13 changes: 9 additions & 4 deletions crates/dojo-world/src/metadata_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,18 @@ async fn get_full_dojo_metadata_from_workspace() {

dbg!(&artifacts);
for (abi_subdir, name) in artifacts {
let artifact = dojo_metadata.artifacts.get(&name);
assert!(artifact.is_some(), "bad artifact for {}", name);
let artifact = artifact.unwrap();
let resource = dojo_metadata.resources_artifacts.get(&name);
assert!(resource.is_some(), "bad resource metadata for {}", name);
let resource = resource.unwrap();

let sanitized_name = name.replace("::", "_");

check_artifact(artifact.clone(), sanitized_name, &abis_dir.join(abi_subdir), &sources_dir);
check_artifact(
resource.artifacts.clone(),
sanitized_name,
&abis_dir.join(abi_subdir),
&sources_dir,
);
}
}

Expand Down
15 changes: 6 additions & 9 deletions crates/sozo/ops/src/migration/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use dojo_world::manifest::{
AbiFormat, BaseManifest, DeploymentManifest, DojoContract, DojoModel, Manifest,
ManifestMethods, WorldContract as ManifestWorldContract, WorldMetadata,
};
use dojo_world::metadata::{dojo_metadata_from_workspace, ArtifactMetadata};
use dojo_world::metadata::{dojo_metadata_from_workspace, ResourceMetadata};
use dojo_world::migration::class::ClassMigration;
use dojo_world::migration::contract::ContractMigration;
use dojo_world::migration::strategy::{generate_salt, prepare_for_migration, MigrationStrategy};
Expand Down Expand Up @@ -255,13 +255,12 @@ where
/// on success.
async fn upload_on_ipfs_and_create_resource(
ui: &Ui,
element_name: String,
resource_id: FieldElement,
artifact: ArtifactMetadata,
metadata: ResourceMetadata,

Check warning on line 259 in crates/sozo/ops/src/migration/migrate.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migration/migrate.rs#L259

Added line #L259 was not covered by tests
) -> Result<world::ResourceMetadata> {
match artifact.upload().await {
match metadata.upload().await {

Check warning on line 261 in crates/sozo/ops/src/migration/migrate.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migration/migrate.rs#L261

Added line #L261 was not covered by tests
Ok(hash) => {
ui.print_sub(format!("{}: ipfs://{}", element_name, hash));
ui.print_sub(format!("{}: ipfs://{}", metadata.name, hash));

Check warning on line 263 in crates/sozo/ops/src/migration/migrate.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migration/migrate.rs#L263

Added line #L263 was not covered by tests
create_resource_metadata(resource_id, hash)
}
Err(_) => Err(anyhow!("Failed to upload IPFS resource.")),
Expand Down Expand Up @@ -330,10 +329,9 @@ where
// models
if !migration_output.models.is_empty() {
for model_name in migration_output.models {
if let Some(m) = dojo_metadata.artifacts.get(&model_name) {
if let Some(m) = dojo_metadata.resources_artifacts.get(&model_name) {

Check warning on line 332 in crates/sozo/ops/src/migration/migrate.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migration/migrate.rs#L332

Added line #L332 was not covered by tests
ipfs.push(upload_on_ipfs_and_create_resource(
&ui,
model_name.clone(),
get_selector_from_name(&model_name).expect("ASCII model name"),
m.clone(),
));
Expand All @@ -346,10 +344,9 @@ where

if !migrated_contracts.is_empty() {
for contract in migrated_contracts {
if let Some(m) = dojo_metadata.artifacts.get(&contract.name) {
if let Some(m) = dojo_metadata.resources_artifacts.get(&contract.name) {

Check warning on line 347 in crates/sozo/ops/src/migration/migrate.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/migration/migrate.rs#L347

Added line #L347 was not covered by tests
ipfs.push(upload_on_ipfs_and_create_resource(
&ui,
contract.name.clone(),
contract.contract_address,
m.clone(),
));
Expand Down
8 changes: 4 additions & 4 deletions crates/sozo/ops/src/tests/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,19 +484,19 @@ async fn check_artifact_metadata<P: starknet::providers::Provider + Sync>(
) {
let resource = world_reader.metadata(&resource_id).call().await.unwrap();

let expected_artifact = dojo_metadata.artifacts.get(element_name);
let expected_resource = dojo_metadata.resources_artifacts.get(element_name);
assert!(
expected_artifact.is_some(),
expected_resource.is_some(),
"Unable to find local artifact metadata for {}",
element_name
);
let expected_artifact = expected_artifact.unwrap();
let expected_resource = expected_resource.unwrap();

check_ipfs_metadata(
client,
element_name,
&resource.metadata_uri.to_string().unwrap(),
expected_artifact,
&expected_resource.artifacts,
)
.await;
}

0 comments on commit 5a091cd

Please sign in to comment.