-
Notifications
You must be signed in to change notification settings - Fork 180
/
lib.rs
198 lines (179 loc) · 6.17 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
pub mod error;
pub mod msg;
pub mod state;
use cw721::traits::{Cw721Execute, Cw721Query};
// Version info for migration
const CONTRACT_NAME: &str = "crates.io:cw721-metadata-onchain";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
/// This is an opionated `cw721` explicitly defining `NftExtension` for metadata on chain for ease of use.
/// There are 2 possibiities for using metadata on chain:
///
/// cw721-metadata-onchain:
///
/// ```rust
/// // instantiate:
/// let contract = Cw721MetadataContract::default();
/// let info = mock_info(CREATOR, &[]);
/// let init_msg = InstantiateMsg {
/// name: "SpaceShips".to_string(),
/// symbol: "SPACE".to_string(),
/// collection_info_extension: None,
/// minter: None,
/// creator: None,
/// withdraw_address: None,
/// };
/// // ...
/// // mint:
/// let token_id = "Enterprise";
/// let token_uri = Some("https://starships.example.com/Starship/Enterprise.json".into());
/// let extension = Some(NftExtensionMsg {
/// description: Some("description1".into()),
/// name: Some("name1".to_string()),
/// attributes: Some(vec![Trait {
/// display_type: None,
/// trait_type: "type1".to_string(),
/// value: "value1".to_string(),
/// }]),
/// ..NftExtensionMsg::default()
/// });
/// let exec_msg = ExecuteMsg::Mint {
/// token_id: token_id.to_string(),
/// owner: "john".to_string(),
/// token_uri: token_uri.clone(),
/// extension: extension.clone(),
/// };
/// // ...
/// ```
pub type Cw721MetadataContract<'a> = cw721::extension::Cw721OnchainExtensions<'a>;
pub mod entry {
use super::*;
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Response};
use cw721::msg::Cw721MigrateMsg;
use error::ContractError;
use msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
mut deps: DepsMut,
env: Env,
info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
Cw721MetadataContract::default().instantiate_with_version(
deps.branch(),
&env,
&info,
msg,
CONTRACT_NAME,
CONTRACT_VERSION,
)
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
Cw721MetadataContract::default().execute(deps, &env, &info, msg)
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary, ContractError> {
Cw721MetadataContract::default().query(deps, &env, msg)
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(
deps: DepsMut,
env: Env,
msg: Cw721MigrateMsg,
) -> Result<Response, ContractError> {
let contract = Cw721MetadataContract::default();
contract.migrate(deps, env, msg, CONTRACT_NAME, CONTRACT_VERSION)
}
}
#[cfg(test)]
mod tests {
use super::*;
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cw721::{msg::NftExtensionMsg, state::Trait, NftExtension};
use msg::{ExecuteMsg, InstantiateMsg};
const CREATOR: &str = "creator";
/// Make sure cw2 version info is properly initialized during instantiation,
/// and NOT overwritten by the base contract.
#[test]
fn proper_cw2_initialization() {
let mut deps = mock_dependencies();
entry::instantiate(
deps.as_mut(),
mock_env(),
mock_info("sender", &[]),
InstantiateMsg {
name: "collection_name".into(),
symbol: "collection_symbol".into(),
collection_info_extension: None,
minter: None,
creator: None,
withdraw_address: None,
},
)
.unwrap();
let version = cw2::get_contract_version(deps.as_ref().storage).unwrap();
assert_eq!(version.contract, CONTRACT_NAME);
}
#[test]
fn use_metadata_extension() {
let mut deps = mock_dependencies();
let contract = Cw721MetadataContract::default();
let info = mock_info(CREATOR, &[]);
let init_msg = InstantiateMsg {
name: "SpaceShips".to_string(),
symbol: "SPACE".to_string(),
collection_info_extension: None,
minter: None,
creator: None,
withdraw_address: None,
};
contract
.instantiate(deps.as_mut(), &mock_env(), &info.clone(), init_msg)
.unwrap();
let token_id = "Enterprise";
let token_uri = Some("https://starships.example.com/Starship/Enterprise.json".into());
let extension = Some(NftExtensionMsg {
description: Some("description1".into()),
name: Some("name1".to_string()),
attributes: Some(vec![Trait {
display_type: None,
trait_type: "type1".to_string(),
value: "value1".to_string(),
}]),
..NftExtensionMsg::default()
});
let exec_msg = ExecuteMsg::Mint {
token_id: token_id.to_string(),
owner: "john".to_string(),
token_uri: token_uri.clone(),
extension: extension.clone(),
};
contract
.execute(deps.as_mut(), &mock_env(), &info, exec_msg)
.unwrap();
let res = contract
.query_nft_info(deps.as_ref().storage, token_id.into())
.unwrap();
assert_eq!(res.token_uri, token_uri);
assert_eq!(
res.extension,
Some(NftExtension {
description: Some("description1".into()),
name: Some("name1".to_string()),
attributes: Some(vec![Trait {
display_type: None,
trait_type: "type1".to_string(),
value: "value1".to_string(),
}]),
..NftExtension::default()
})
);
}
}