Skip to content

Commit

Permalink
Merge pull request #450 from CosmWasm/440-demo-metadata-extension
Browse files Browse the repository at this point in the history
Demo metadata extension
  • Loading branch information
ethanfrey committed Sep 23, 2021
2 parents 9671cc2 + 749dfc7 commit 4819351
Show file tree
Hide file tree
Showing 24 changed files with 1,353 additions and 20 deletions.
38 changes: 38 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ workflows:
- contract_cw20_staking
- contract_cw20_merkle_airdrop
- contract_cw721_base
- contract_cw721_metadata_uri
- contract_cw1155_base
- package_controllers
- package_cw0
Expand Down Expand Up @@ -548,6 +549,43 @@ jobs:
- target
key: cargocache-cw721-base-rust:1.53.0-{{ checksum "~/project/Cargo.lock" }}


contract_cw721_metadata_uri:
docker:
- image: rust:1.53.0
working_directory: ~/project/contracts/cw721-metadata-uri
steps:
- checkout:
path: ~/project
- run:
name: Version information
command: rustc --version; cargo --version; rustup --version
- restore_cache:
keys:
- cargocache-cw721-metadata-uri-rust:1.53.0-{{ checksum "~/project/Cargo.lock" }}
- run:
name: Unit Tests
environment:
RUST_BACKTRACE: 1
command: cargo unit-test --locked
- run:
name: Build and run schema generator
command: cargo schema --locked
- run:
name: Ensure checked-in schemas are up-to-date
command: |
CHANGES_IN_REPO=$(git status --porcelain)
if [[ -n "$CHANGES_IN_REPO" ]]; then
echo "Repository is dirty. Showing 'git status' and 'git --no-pager diff' for debugging now:"
git status && git --no-pager diff
exit 1
fi
- save_cache:
paths:
- /usr/local/cargo/registry
- target
key: cargocache-cw721-metadata-uri-rust:1.53.0-{{ checksum "~/project/Cargo.lock" }}

contract_cw1155_base:
docker:
- image: rust:1.53.0
Expand Down
16 changes: 16 additions & 0 deletions Cargo.lock

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

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ incremental = false
codegen-units = 1
incremental = false

[profile.release.package.cw20-merkle-airdrop]
codegen-units = 1
incremental = false

[profile.release.package.cw20-staking]
codegen-units = 1
incremental = false
Expand All @@ -53,6 +57,10 @@ incremental = false
codegen-units = 1
incremental = false

[profile.release.package.cw721-metadata-uri]
codegen-units = 1
incremental = false

[profile.release.package.cw1155-base]
codegen-units = 1
incremental = false
Expand Down
3 changes: 1 addition & 2 deletions contracts/cw721-base/examples/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use cw721::{
AllNftInfoResponse, ApprovedForAllResponse, ContractInfoResponse, NftInfoResponse,
NumTokensResponse, OwnerOfResponse, TokensResponse,
};
use cw721_base::entry::Extension;
use cw721_base::msg::{ExecuteMsg, InstantiateMsg, MinterResponse, QueryMsg};
use cw721_base::{ExecuteMsg, Extension, InstantiateMsg, MinterResponse, QueryMsg};

fn main() {
let mut out_dir = current_dir().unwrap();
Expand Down
5 changes: 3 additions & 2 deletions contracts/cw721-base/src/contract_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use cw721::{
NftInfoResponse, OwnerOfResponse,
};

use crate::entry::Extension;
use crate::{ContractError, Cw721Contract, ExecuteMsg, InstantiateMsg, MintMsg, QueryMsg};
use crate::{
ContractError, Cw721Contract, ExecuteMsg, Extension, InstantiateMsg, MintMsg, QueryMsg,
};

const MINTER: &str = "merlin";
const CONTRACT_NAME: &str = "Magic Power";
Expand Down
29 changes: 15 additions & 14 deletions contracts/cw721-base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,44 @@ pub mod state;
pub use crate::error::ContractError;
pub use crate::msg::{ExecuteMsg, InstantiateMsg, MintMsg, MinterResponse, QueryMsg};
pub use crate::state::Cw721Contract;
use cosmwasm_std::Empty;

// This is a simple type to let us handle empty extensions
pub type Extension = Option<Empty>;

#[cfg(not(feature = "library"))]
pub mod entry {
use super::*;

#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{Binary, Deps, DepsMut, Empty, Env, MessageInfo, Response, StdResult};

// This is a simple type to let us handle empty extensions
pub type Extension = Option<Empty>;
use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult};

// This makes a conscious choice on the various generics used by the contract
#[cfg_attr(not(feature = "library"), entry_point)]
#[entry_point]
pub fn instantiate(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: msg::InstantiateMsg,
msg: InstantiateMsg,
) -> StdResult<Response> {
let tract = state::Cw721Contract::<Extension, Empty>::default();
let tract = Cw721Contract::<Extension, Empty>::default();
tract.instantiate(deps, env, info, msg)
}

#[cfg_attr(not(feature = "library"), entry_point)]
#[entry_point]
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: msg::ExecuteMsg<Extension>,
msg: ExecuteMsg<Extension>,
) -> Result<Response, ContractError> {
let tract = state::Cw721Contract::<Extension, Empty>::default();
let tract = Cw721Contract::<Extension, Empty>::default();
tract.execute(deps, env, info, msg)
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, env: Env, msg: msg::QueryMsg) -> StdResult<Binary> {
let tract = state::Cw721Contract::<Extension, Empty>::default();
#[entry_point]
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
let tract = Cw721Contract::<Extension, Empty>::default();
tract.query(deps, env, msg)
}
}
5 changes: 3 additions & 2 deletions contracts/cw721-base/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use cosmwasm_std::Binary;
use cw721::Expiration;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::Binary;
use cw721::Expiration;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct InstantiateMsg {
/// Name of the NFT contract
Expand Down
5 changes: 5 additions & 0 deletions contracts/cw721-metadata-uri/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[alias]
wasm = "build --release --target wasm32-unknown-unknown"
wasm-debug = "build --target wasm32-unknown-unknown"
unit-test = "test --lib"
schema = "run --example schema"
39 changes: 39 additions & 0 deletions contracts/cw721-metadata-uri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "cw721-metadata-uri"
version = "0.9.0"
authors = ["Ethan Frey <[email protected]>"]
edition = "2018"
description = "Example extending CW721 NFT with ERC20 like metadata_uri"
license = "Apache-2.0"
repository = "https://github.com/CosmWasm/cw-plus"
homepage = "https://cosmwasm.com"
documentation = "https://docs.cosmwasm.com"

exclude = [
# Those files are rust-optimizer artifacts. You might want to commit them for convenience but they should not be part of the source code publication.
"artifacts/*",
]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib", "rlib"]

[features]
# for more explicit tests, cargo test --features=backtraces
backtraces = ["cosmwasm-std/backtraces"]
# use library feature to disable all instantiate/execute/query exports
library = []

[dependencies]
cw0 = { path = "../../packages/cw0", version = "0.9.0" }
cw2 = { path = "../../packages/cw2", version = "0.9.0" }
cw721 = { path = "../../packages/cw721", version = "0.9.0" }
cw721-base = { path = "../cw721-base", version = "0.9.0", features = ["library"] }
cw-storage-plus = { path = "../../packages/storage-plus", version = "0.9.0" }
cosmwasm-std = { version = "0.16.0" }
schemars = "0.8.1"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
thiserror = { version = "1.0.23" }

[dev-dependencies]
cosmwasm-schema = { version = "0.16.0" }
14 changes: 14 additions & 0 deletions contracts/cw721-metadata-uri/NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Cw721_metadata_uri
Copyright (C) 2020 Confio OÜ

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
37 changes: 37 additions & 0 deletions contracts/cw721-metadata-uri/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# CW721 Metadata URI

In Ethereum, the ERC721 standard includes a metadata_uri field to store all metadata offchain.
With CW721-Base in CosmWasm, we allow you to store any data on chain you wish, using a generic `extension: T`.

In order to provide better "out of the box" compatibility for people migrating from the Ethereum ecosystem,
and to demonstrate how to use the extension ability, we have created this simple contract. There is no business
logic here, but looking at `lib.rs` will show you how do define custom data that is included when minting and
available in all queries.

In particular, here we define:

```rust
pub struct Extension {
pub metadata_uri: String,
}
```

This means when you query `NftInfo{name: "Enterprise"}`, you will get something like:

```json
{
"name": "Enterprise",
"description": "USS Starship Enterprise",
"image": null,
"extension": {
"metadata_uri": "http://starships.example.com/Starships/Enterprise.json"
}
}
```

Please look at the test code for an example usage in Rust.

## Notice

Feel free to use this contract out of the box, or as inspiration for further customization of cw721-base.
We will not be adding new features or business logic here.
37 changes: 37 additions & 0 deletions contracts/cw721-metadata-uri/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::env::current_dir;
use std::fs::create_dir_all;

use cosmwasm_schema::{export_schema, export_schema_with_title, remove_schemas, schema_for};

use cw721::{
AllNftInfoResponse, ApprovedForAllResponse, ContractInfoResponse, NftInfoResponse,
NumTokensResponse, OwnerOfResponse, TokensResponse,
};
use cw721_metadata_uri::{ExecuteMsg, Extension, InstantiateMsg, MinterResponse, QueryMsg};

fn main() {
let mut out_dir = current_dir().unwrap();
out_dir.push("schema");
create_dir_all(&out_dir).unwrap();
remove_schemas(&out_dir).unwrap();

export_schema(&schema_for!(InstantiateMsg), &out_dir);
export_schema_with_title(&schema_for!(ExecuteMsg), &out_dir, "ExecuteMsg");
export_schema(&schema_for!(QueryMsg), &out_dir);
export_schema_with_title(
&schema_for!(AllNftInfoResponse<Extension>),
&out_dir,
"AllNftInfoResponse",
);
export_schema(&schema_for!(ApprovedForAllResponse), &out_dir);
export_schema(&schema_for!(ContractInfoResponse), &out_dir);
export_schema(&schema_for!(MinterResponse), &out_dir);
export_schema_with_title(
&schema_for!(NftInfoResponse<Extension>),
&out_dir,
"NftInfoResponse",
);
export_schema(&schema_for!(NumTokensResponse), &out_dir);
export_schema(&schema_for!(OwnerOfResponse), &out_dir);
export_schema(&schema_for!(TokensResponse), &out_dir);
}
Loading

0 comments on commit 4819351

Please sign in to comment.