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

use sg721-metadata-onchain #104

Merged
merged 3 commits into from
Sep 4, 2024
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
32 changes: 32 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ sha2 = "^0.10"
serde = "^1.0"
thiserror = "^1.0"
# Stargaze libs
sg-metadata = "^3.14"
sg-std = "^3.2"
sg-multi-test = "^3.1"
sg721 = "^3.3"
sg721-base = "^3.3"
sg721-base = "^3.14"
sg721-metadata-onchain = "^3.14"
# packages and contracts
cw-cii = { path = "./packages/cw-cii" }
cw-pause-once = { path = "./packages/cw-pause-once" }
Expand Down
2 changes: 2 additions & 0 deletions contracts/sg-ics721/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ cw2 = { workspace = true }
cw721 = { workspace = true }
ics721 = { workspace = true }
ics721-types = { workspace = true }
sg-metadata = { workspace = true}
sg-std = { workspace = true}
sg721 = { workspace = true }
sg721-base = { workspace = true, features = ["library"] }
sg721-metadata-onchain = { workspace = true, features = ["library"] }

[dev-dependencies]
anyhow = { workspace = true }
Expand Down
51 changes: 49 additions & 2 deletions contracts/sg-ics721/src/execute.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use cosmwasm_std::{
from_json, to_json_binary, Addr, Binary, ContractInfoResponse, Deps, DepsMut, Env, StdResult,
};
use cw721::{CollectionExtension, RoyaltyInfo};
use cw721::{CollectionExtension, NftExtension, RoyaltyInfo};
use ics721::{execute::Ics721Execute, state::CollectionData, utils::get_collection_data};
use ics721_types::token_types::Class;

use sg721::RoyaltyInfoResponse;
use sg721_base::msg::{CollectionInfoResponse, QueryMsg};
use sg721_base::msg::CollectionInfoResponse;
use sg721_metadata_onchain::QueryMsg;
use sg_metadata::{Metadata, Trait};

use crate::state::{SgIcs721Contract, STARGAZE_ICON_PLACEHOLDER};

Expand Down Expand Up @@ -113,4 +115,49 @@ impl Ics721Execute for SgIcs721Contract {

to_json_binary(&instantiate_msg)
}

fn mint_msg(
&self,
token_id: String,
token_uri: Option<String>,
owner: String,
data: Option<Binary>,
) -> StdResult<Binary> {
// parse token data and check whether it is of type NftExtension
let extension = data
.and_then(|binary| {
from_json::<NftExtension>(binary).ok().map(|ext| Metadata {
animation_url: ext.animation_url,
attributes: ext.attributes.map(|traits| {
traits
.into_iter()
.map(|t| Trait {
trait_type: t.trait_type,
value: t.value,
display_type: t.display_type,
})
.collect()
}),
background_color: ext.background_color,
description: ext.description,
external_url: ext.external_url,
image: ext.image,
image_data: ext.image_data,
youtube_url: ext.youtube_url,
name: ext.name,
})
})
.unwrap_or(Metadata {
// no onchain metadata (only offchain), in this case empty metadata is created
..Default::default()
});

let msg = sg721_metadata_onchain::ExecuteMsg::Mint {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so my question here is, does every meta data load resolve into a metadata_onchain type message? Wondering if there is a different struct for off-chain metadata?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of offchain metadata:

  • extension is empty onchain Metadata (with None values for each prop like image and attributes)
  • token_uri is used for offchain data, e.g. refers to an IPFS link with offchain metadata. Here JSON file on IPFS is exactly the same type for Metadata (as defined by ERC721).

So SG NFT mint is done by this:

        let msg = cw721_metadata_onchain::msg::ExecuteMsg::Mint {
            token_id,
            token_uri, // holds off-chain metadata
            owner,
            extension, // holds on-chain metadata
        };

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering here, if off chain metadata is minted in this way, shouldn't we not use an execute message from cw721_onchain such as to not confuse the developer? I think the data is fine just coming from the cw721_metadata_onchain directory might be a bit misleading.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my bad (wrong example given above). This is an sg-implementation, proper code is using this:

        let msg = sg721_metadata_onchain::ExecuteMsg::Mint {
            token_id,
            token_uri, // holds off-chain metadata
            owner,
            extension, // holds on-chain metadata
        };
        to_json_binary(&msg)

Please note that mint_msg() returns StdResult<Binary> for ics721 and sg-ics721.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved in discord, name is coming from external repo

token_id,
token_uri, // holds off-chain metadata
owner,
extension, // holds on-chain metadata
};
to_json_binary(&msg)
}
}
Loading
Loading