-
Notifications
You must be signed in to change notification settings - Fork 176
/
lib.rs
255 lines (221 loc) · 8.88 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
pub mod msg;
pub mod query;
use cosmwasm_std::to_binary;
pub use query::{check_royalties, query_royalties_info};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::msg::Cw2981QueryMsg;
use cosmwasm_std::Empty;
use cw721_base::Cw721Contract;
pub use cw721_base::{ContractError, InstantiateMsg, MintMsg, MinterResponse};
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug, Default)]
pub struct Trait {
pub display_type: Option<String>,
pub trait_type: String,
pub value: String,
}
// see: https://docs.opensea.io/docs/metadata-standards
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug, Default)]
pub struct Metadata {
pub image: Option<String>,
pub image_data: Option<String>,
pub external_url: Option<String>,
pub description: Option<String>,
pub name: Option<String>,
pub attributes: Option<Vec<Trait>>,
pub background_color: Option<String>,
pub animation_url: Option<String>,
pub youtube_url: Option<String>,
/// specify whether royalties are set on this token
pub royalty_payments: bool,
/// This is how much the minter takes as a cut when sold
pub royalty_percentage: Option<u64>,
/// The payment address, may be different to or the same
/// as the minter addr
/// question: how do we validate this?
pub royalty_payment_address: Option<String>,
}
pub type Extension = Option<Metadata>;
pub type MintExtension = Option<Extension>;
pub type Cw2981Contract<'a> = Cw721Contract<'a, Extension, Empty>;
pub type ExecuteMsg = cw721_base::ExecuteMsg<Extension>;
#[cfg(not(feature = "library"))]
pub mod entry {
use super::*;
use cosmwasm_std::entry_point;
use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult};
#[entry_point]
pub fn instantiate(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: InstantiateMsg,
) -> StdResult<Response> {
Cw2981Contract::default().instantiate(deps, env, info, msg)
}
#[entry_point]
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
Cw2981Contract::default().execute(deps, env, info, msg)
}
#[entry_point]
pub fn query(deps: Deps, env: Env, msg: Cw2981QueryMsg) -> StdResult<Binary> {
match msg {
Cw2981QueryMsg::RoyaltyInfo {
token_id,
sale_price,
} => to_binary(&query_royalties_info(deps, token_id, sale_price)?),
Cw2981QueryMsg::CheckRoyalties {} => to_binary(&check_royalties(deps)?),
_ => Cw2981Contract::default()
.query(deps, env, msg.into())
.map_err(|err| err),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::msg::{CheckRoyaltiesResponse, RoyaltiesInfoResponse};
use cosmwasm_std::{from_binary, Uint128};
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cw721::Cw721Query;
const CREATOR: &str = "creator";
#[test]
fn use_metadata_extension() {
let mut deps = mock_dependencies();
let contract = Cw2981Contract::default();
let info = mock_info(CREATOR, &[]);
let init_msg = InstantiateMsg {
name: "SpaceShips".to_string(),
symbol: "SPACE".to_string(),
minter: CREATOR.to_string(),
};
entry::instantiate(deps.as_mut(), mock_env(), info.clone(), init_msg).unwrap();
let token_id = "Enterprise";
let mint_msg = MintMsg {
token_id: token_id.to_string(),
owner: "john".to_string(),
token_uri: Some("https://starships.example.com/Starship/Enterprise.json".into()),
extension: Some(Metadata {
description: Some("Spaceship with Warp Drive".into()),
name: Some("Starship USS Enterprise".to_string()),
..Metadata::default()
}),
};
let exec_msg = ExecuteMsg::Mint(mint_msg.clone());
entry::execute(deps.as_mut(), mock_env(), info, exec_msg).unwrap();
let res = contract.nft_info(deps.as_ref(), token_id.into()).unwrap();
assert_eq!(res.token_uri, mint_msg.token_uri);
assert_eq!(res.extension, mint_msg.extension);
}
#[test]
fn check_royalties_response() {
let mut deps = mock_dependencies();
let _contract = Cw2981Contract::default();
let info = mock_info(CREATOR, &[]);
let init_msg = InstantiateMsg {
name: "SpaceShips".to_string(),
symbol: "SPACE".to_string(),
minter: CREATOR.to_string(),
};
entry::instantiate(deps.as_mut(), mock_env(), info.clone(), init_msg).unwrap();
let token_id = "Enterprise";
let mint_msg = MintMsg {
token_id: token_id.to_string(),
owner: "john".to_string(),
token_uri: Some("https://starships.example.com/Starship/Enterprise.json".into()),
extension: Some(Metadata {
description: Some("Spaceship with Warp Drive".into()),
name: Some("Starship USS Enterprise".to_string()),
..Metadata::default()
}),
};
let exec_msg = ExecuteMsg::Mint(mint_msg);
entry::execute(deps.as_mut(), mock_env(), info, exec_msg).unwrap();
let expected = CheckRoyaltiesResponse {
royalty_payments: true,
};
let res = check_royalties(deps.as_ref()).unwrap();
assert_eq!(res, expected);
// also check the longhand way
let query_msg = Cw2981QueryMsg::CheckRoyalties {};
let query_res: CheckRoyaltiesResponse =
from_binary(&entry::query(deps.as_ref(), mock_env(), query_msg).unwrap()).unwrap();
assert_eq!(query_res, expected);
}
#[test]
fn check_token_royalties() {
let mut deps = mock_dependencies();
let info = mock_info(CREATOR, &[]);
let init_msg = InstantiateMsg {
name: "SpaceShips".to_string(),
symbol: "SPACE".to_string(),
minter: CREATOR.to_string(),
};
entry::instantiate(deps.as_mut(), mock_env(), info.clone(), init_msg).unwrap();
let token_id = "Enterprise";
let mint_msg = MintMsg {
token_id: token_id.to_string(),
owner: "JeanLuc".to_string(),
token_uri: Some("https://starships.example.com/Starship/Enterprise.json".into()),
extension: Some(Metadata {
description: Some("Spaceship with Warp Drive".into()),
name: Some("Starship USS Enterprise".to_string()),
royalty_payment_address: Some("JeanLuc".to_string()),
royalty_percentage: Some(10),
..Metadata::default()
}),
};
let exec_msg = ExecuteMsg::Mint(mint_msg.clone());
entry::execute(deps.as_mut(), mock_env(), info.clone(), exec_msg).unwrap();
let expected = RoyaltiesInfoResponse {
address: mint_msg.owner,
royalty_amount: Uint128::new(10),
};
let res =
query_royalties_info(deps.as_ref(), token_id.to_string(), Uint128::new(100)).unwrap();
assert_eq!(res, expected);
// also check the longhand way
let query_msg = Cw2981QueryMsg::RoyaltyInfo {
token_id: token_id.to_string(),
sale_price: Uint128::new(100),
};
let query_res: RoyaltiesInfoResponse =
from_binary(&entry::query(deps.as_ref(), mock_env(), query_msg).unwrap()).unwrap();
assert_eq!(query_res, expected);
// check for rounding down
// which is the default behaviour
let voyager_token_id = "Voyager";
let second_mint_msg = MintMsg {
token_id: voyager_token_id.to_string(),
owner: "Janeway".to_string(),
token_uri: Some("https://starships.example.com/Starship/Voyager.json".into()),
extension: Some(Metadata {
description: Some("Spaceship with Warp Drive".into()),
name: Some("Starship USS Voyager".to_string()),
royalty_payment_address: Some("Janeway".to_string()),
royalty_percentage: Some(4),
..Metadata::default()
}),
};
let voyager_exec_msg = ExecuteMsg::Mint(second_mint_msg.clone());
entry::execute(deps.as_mut(), mock_env(), info, voyager_exec_msg).unwrap();
// 43 x 0.04 (i.e., 4%) should be 1.72
// we expect this to be rounded down to 1
let voyager_expected = RoyaltiesInfoResponse {
address: second_mint_msg.owner,
royalty_amount: Uint128::new(1),
};
let res = query_royalties_info(
deps.as_ref(),
voyager_token_id.to_string(),
Uint128::new(43),
)
.unwrap();
assert_eq!(res, voyager_expected);
}
}