Skip to content

Commit

Permalink
Merge pull request #1 from JakeHartnell/example-metadata
Browse files Browse the repository at this point in the history
Fix bug, add example metadata and images
  • Loading branch information
JakeHartnell authored May 14, 2023
2 parents bdd806b + 72c59c0 commit 0c34581
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 13 deletions.
4 changes: 2 additions & 2 deletions contracts/cw721-piggy-bank/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,15 @@ pub fn execute_deposit(
let new_balance = balance.unwrap_or_default() + amount;

// TODO don't hard code
let base_url = "<insert_ipfs_url>";
let base_url = "https://bafybeie2grcflzjvds7i33bxjjgktjdfcp2h2v27gdkbyuiaelvbgtdewy.ipfs.nftstorage.link";

// Native token micro units are typically 6 decimal places
// Check if balance is greater than 1
if new_balance > Uint128::new(1000000) {
token.token_uri = Some(format!("{}/{}/{}", base_url, token_id, "sapling.json"));
} else if new_balance > Uint128::new(10000000) {
token.token_uri = Some(format!("{}/{}/{}", base_url, token_id, "tree.json"));
} else {
} else if new_balance > Uint128::new(100000000) {
token.token_uri = Some(format!("{}/{}/{}", base_url, token_id, "fullgrown.json"));
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/cw721-piggy-bank/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ pub enum ContractError {
/// This inherits from cw721-base::ContractError to handle the base contract errors
#[error("NFT contract error: {0}")]
Cw721Error(#[from] cw721_base::ContractError),
}
}
4 changes: 3 additions & 1 deletion contracts/cw721-piggy-bank/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ pub struct MetadataExt {
#[cw_serde]
pub enum ExecuteExt {
/// Used to deposit funds in a particular NFT
Deposit { token_id: String },
Deposit {
token_id: String,
},
UpdateTokenUri {
token_id: String,
token_uri: String,
Expand Down
4 changes: 2 additions & 2 deletions contracts/cw721-piggy-bank/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_std::Uint128;
use cw_storage_plus::{Map, Item};
use cw_storage_plus::{Item, Map};

/// Map for storing NFT balances (token_id, amount)
pub const BALANCES: Map<&str, Uint128> = Map::new("nft_balances");

pub const DENOM: Item<String> = Item::new("denoms");
pub const DENOM: Item<String> = Item::new("denoms");
45 changes: 38 additions & 7 deletions contracts/cw721-piggy-bank/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::{
contract::{execute, instantiate, CONTRACT_NAME},
msg::{ExecuteExt, ExecuteMsg, InstantiateMsg, MetadataExt},
contract::{execute, instantiate, query, CONTRACT_NAME},
msg::{ExecuteExt, ExecuteMsg, InstantiateMsg, MetadataExt, QueryMsg},
ContractError,
};

use cosmwasm_std::{
coins,
coins, from_binary,
testing::{mock_dependencies, mock_env, mock_info},
BankMsg, CosmosMsg, StdError,
};
use cw721::AllNftInfoResponse;

/// Make sure cw2 version info is properly initialized during instantiation,
/// and NOT overwritten by the base contract.
Expand Down Expand Up @@ -60,7 +61,7 @@ fn happy_path() {
ExecuteMsg::Mint {
token_id: "1".into(),
owner: BOB.into(),
token_uri: Some("https://ipfs.io/cutedog.json".to_string()),
token_uri: Some("https://bafybeie2grcflzjvds7i33bxjjgktjdfcp2h2v27gdkbyuiaelvbgtdewy.ipfs.nftstorage.link/1/seedling.json".to_string()),
extension: MetadataExt {},
},
)
Expand Down Expand Up @@ -111,11 +112,26 @@ fn happy_path() {
})
);

// Calling deposit succeeds with correct token
// Check token URI is seedling state
let token: AllNftInfoResponse<MetadataExt> = from_binary(
&query(
deps.as_ref(),
mock_env(),
QueryMsg::AllNftInfo {
token_id: "1".to_string(),
include_expired: None,
},
)
.unwrap(),
)
.unwrap();
assert_eq!(token.info.token_uri, Some("https://bafybeie2grcflzjvds7i33bxjjgktjdfcp2h2v27gdkbyuiaelvbgtdewy.ipfs.nftstorage.link/1/seedling.json".to_string()));

// Deposit enough to change the token URI
execute(
deps.as_mut(),
mock_env(),
mock_info(BOB, &coins(1000, "ujuno")),
mock_info(BOB, &coins(1000000, "ujuno")),
ExecuteMsg::Extension {
msg: ExecuteExt::Deposit {
token_id: "1".to_string(),
Expand All @@ -124,6 +140,21 @@ fn happy_path() {
)
.unwrap();

// Check token URI is sapling state
let token: AllNftInfoResponse<MetadataExt> = from_binary(
&query(
deps.as_ref(),
mock_env(),
QueryMsg::AllNftInfo {
token_id: "1".to_string(),
include_expired: None,
},
)
.unwrap(),
)
.unwrap();
assert_eq!(token.info.token_uri, Some("https://bafybeie2grcflzjvds7i33bxjjgktjdfcp2h2v27gdkbyuiaelvbgtdewy.ipfs.nftstorage.link/1/seedling.json".to_string()));

// Only owner can burn NFT
execute(
deps.as_mut(),
Expand Down Expand Up @@ -167,7 +198,7 @@ fn happy_path() {
res.messages[0].msg,
CosmosMsg::Bank(BankMsg::Send {
to_address: BOB.to_string(),
amount: coins(1000, "ujuno"),
amount: coins(1000000, "ujuno"),
})
);
}
Binary file added images/1/fullgrown.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/1/sapling.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/1/seedling.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/1/tree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions metadata/1/fullgrown.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "The First Tree",
"description": "The first NFT tree, towering over the others.",
"image": "https://nftstorage.link/ipfs/bafybeigxc7pf4qah4qmy6ssnyuldz5cmfwzypotq4ujewwtnjdbhtl3c6a"
}
5 changes: 5 additions & 0 deletions metadata/1/sapling.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "The First Tree",
"description": "The first NFT tree, just a little sapling.",
"image": "https://nftstorage.link/ipfs/bafybeifhumobsk6bnf2rlkw3ogxttm75vrbdjj5m4tq7ts4po64ytsjbzi"
}
5 changes: 5 additions & 0 deletions metadata/1/seedling.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "The First Tree",
"description": "The first NFT tree, just a little seedling.",
"image": "https://nftstorage.link/ipfs/bafybeibztc6comutebincvmo4mqkhafve2idc7gqx4qiuzrxnc4dyr5uhi"
}
5 changes: 5 additions & 0 deletions metadata/1/tree.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "The First Tree",
"description": "The first NFT tree.",
"image": "https://nftstorage.link/ipfs/bafybeid4ltlxzgojohe2uwl3ebq3f5urmowddky5okl5nol2z65oit35k4"
}

0 comments on commit 0c34581

Please sign in to comment.