-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathcontract.rs
291 lines (255 loc) · 9.76 KB
/
contract.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use crate::error::ContractError;
use crate::msg::{ConfigResponse, ExecuteMsg};
use crate::state::{increment_token_index, Config, COLLECTION_ADDRESS, CONFIG, STATUS};
use base_factory::msg::{BaseMinterCreateMsg, ParamsResponse};
use base_factory::state::Extension;
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_json_binary, Addr, Binary, CosmosMsg, Decimal, Deps, DepsMut, Empty, Env, MessageInfo,
Reply, Response, StdResult, SubMsg, Timestamp, WasmMsg,
};
use cw2::set_contract_version;
use cw_utils::{must_pay, nonpayable, parse_reply_instantiate_data};
use sg1::transfer_funds_to_launchpad_dao;
use sg2::query::Sg2QueryMsg;
use sg4::{QueryMsg, Status, StatusResponse, SudoMsg};
use sg721::{ExecuteMsg as Sg721ExecuteMsg, InstantiateMsg as Sg721InstantiateMsg};
use sg721_base::msg::{CollectionInfoResponse, QueryMsg as Sg721QueryMsg};
use sg_utils::NATIVE_DENOM;
use url::Url;
const CONTRACT_NAME: &str = "crates.io:sg-base-minter";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
const INSTANTIATE_SG721_REPLY_ID: u64 = 1;
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: BaseMinterCreateMsg,
) -> Result<Response, ContractError> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
let factory = info.sender.clone();
// set default status so it can be queried without failing
STATUS.save(deps.storage, &Status::default())?;
// Make sure the sender is the factory contract
// This will fail if the sender cannot parse a response from the factory contract
let factory_params: ParamsResponse = deps
.querier
.query_wasm_smart(factory.clone(), &Sg2QueryMsg::Params {})?;
let config = Config {
factory: factory.clone(),
collection_code_id: msg.collection_params.code_id,
// assume the mint price is the minimum mint price
// 100% is fair burned
mint_price: factory_params.params.min_mint_price,
extension: Empty {},
};
// Use default start trading time if not provided
let mut collection_info = msg.collection_params.info.clone();
let offset = factory_params.params.max_trading_offset_secs;
let start_trading_time = msg
.collection_params
.info
.start_trading_time
.or_else(|| Some(env.block.time.plus_seconds(offset)));
collection_info.start_trading_time = start_trading_time;
CONFIG.save(deps.storage, &config)?;
let wasm_msg = WasmMsg::Instantiate {
code_id: msg.collection_params.code_id,
msg: to_json_binary(&Sg721InstantiateMsg {
name: msg.collection_params.name.clone(),
symbol: msg.collection_params.symbol,
minter: env.contract.address.to_string(),
collection_info,
})?,
funds: info.funds,
admin: Some(
deps.api
.addr_validate(&msg.collection_params.info.creator)?
.to_string(),
),
label: format!(
"SG721-{}-{}",
msg.collection_params.code_id,
msg.collection_params.name.trim()
),
};
let submsg = SubMsg::reply_on_success(wasm_msg, INSTANTIATE_SG721_REPLY_ID);
Ok(Response::new()
.add_attribute("action", "instantiate")
.add_attribute("contract_name", CONTRACT_NAME)
.add_attribute("contract_version", CONTRACT_VERSION)
.add_attribute("sender", factory)
.add_submessage(submsg))
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::Mint { token_uri } => execute_mint_sender(deps, env, info, token_uri),
ExecuteMsg::UpdateStartTradingTime(time) => {
execute_update_start_trading_time(deps, env, info, time)
}
}
}
pub fn execute_mint_sender(
deps: DepsMut,
_env: Env,
info: MessageInfo,
token_uri: String,
) -> Result<Response, ContractError> {
let config = CONFIG.load(deps.storage)?;
let collection_address = COLLECTION_ADDRESS.load(deps.storage)?;
// This is a 1:1 minter, minted at min_mint_price
// Should mint and then list on the marketplace for secondary sales
let collection_info: CollectionInfoResponse = deps.querier.query_wasm_smart(
collection_address.clone(),
&Sg721QueryMsg::CollectionInfo {},
)?;
// allow only sg721 creator address to mint
if collection_info.creator != info.sender {
return Err(ContractError::Unauthorized(
"Sender is not sg721 creator".to_owned(),
));
};
// Token URI must be a valid URL (ipfs, https, etc.)
Url::parse(&token_uri).map_err(|_| ContractError::InvalidTokenURI {})?;
let mut res = Response::new();
let factory: ParamsResponse = deps
.querier
.query_wasm_smart(config.factory, &Sg2QueryMsg::Params {})?;
let factory_params = factory.params;
let funds_sent = must_pay(&info, NATIVE_DENOM)?;
// Create network fee msgs
let mint_fee_percent = Decimal::bps(factory_params.mint_fee_bps);
let network_fee = config.mint_price.amount * mint_fee_percent;
// For the base 1/1 minter, the entire mint price should be Fair Burned
if network_fee != funds_sent {
return Err(ContractError::InvalidMintPrice {});
}
transfer_funds_to_launchpad_dao(&info, network_fee.u128(), NATIVE_DENOM, &mut res)?;
// Create mint msgs
let mint_msg = Sg721ExecuteMsg::<Extension, Empty>::Mint {
token_id: increment_token_index(deps.storage)?.to_string(),
owner: info.sender.to_string(),
token_uri: Some(token_uri.clone()),
extension: None,
};
let msg = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: collection_address.to_string(),
msg: to_json_binary(&mint_msg)?,
funds: vec![],
});
res = res.add_message(msg);
Ok(res
.add_attribute("action", "mint")
.add_attribute("sender", info.sender)
.add_attribute("token_uri", token_uri)
.add_attribute("network_fee", network_fee.to_string()))
}
pub fn execute_update_start_trading_time(
deps: DepsMut,
env: Env,
info: MessageInfo,
start_time: Option<Timestamp>,
) -> Result<Response, ContractError> {
nonpayable(&info)?;
let sg721_contract_addr = COLLECTION_ADDRESS.load(deps.storage)?;
let collection_info: CollectionInfoResponse = deps.querier.query_wasm_smart(
sg721_contract_addr.clone(),
&Sg721QueryMsg::CollectionInfo {},
)?;
if info.sender != collection_info.creator {
return Err(ContractError::Unauthorized(
"Sender is not creator".to_owned(),
));
}
// add custom rules here
if let Some(start_time) = start_time {
if env.block.time > start_time {
return Err(ContractError::InvalidStartTradingTime(
env.block.time,
start_time,
));
}
}
// execute sg721 contract
let msg = WasmMsg::Execute {
contract_addr: sg721_contract_addr.to_string(),
msg: to_json_binary(
&Sg721ExecuteMsg::<Extension, Empty>::UpdateStartTradingTime(start_time),
)?,
funds: vec![],
};
Ok(Response::new()
.add_attribute("action", "update_start_time")
.add_attribute("sender", info.sender)
.add_message(msg))
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn sudo(deps: DepsMut, _env: Env, msg: SudoMsg) -> Result<Response, ContractError> {
match msg {
SudoMsg::UpdateStatus {
is_verified,
is_blocked,
is_explicit,
} => update_status(deps, is_verified, is_blocked, is_explicit)
.map_err(|_| ContractError::UpdateStatus {}),
}
}
/// Only governance can update contract params
pub fn update_status(
deps: DepsMut,
is_verified: bool,
is_blocked: bool,
is_explicit: bool,
) -> StdResult<Response> {
let mut status = STATUS.load(deps.storage)?;
status.is_verified = is_verified;
status.is_blocked = is_blocked;
status.is_explicit = is_explicit;
STATUS.save(deps.storage, &status)?;
Ok(Response::new().add_attribute("action", "sudo_update_status"))
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::Config {} => to_json_binary(&query_config(deps)?),
QueryMsg::Status {} => to_json_binary(&query_status(deps)?),
}
}
fn query_config(deps: Deps) -> StdResult<ConfigResponse> {
let config = CONFIG.load(deps.storage)?;
let collection_address = COLLECTION_ADDRESS.load(deps.storage)?;
Ok(ConfigResponse {
collection_address: collection_address.to_string(),
config,
})
}
pub fn query_status(deps: Deps) -> StdResult<StatusResponse> {
let status = STATUS.load(deps.storage)?;
Ok(StatusResponse { status })
}
// Reply callback triggered from sg721 contract instantiation in instantiate()
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractError> {
if msg.id != INSTANTIATE_SG721_REPLY_ID {
return Err(ContractError::InvalidReplyID {});
}
let reply = parse_reply_instantiate_data(msg);
match reply {
Ok(res) => {
let collection_address = res.contract_address;
COLLECTION_ADDRESS.save(deps.storage, &Addr::unchecked(collection_address.clone()))?;
Ok(Response::default()
.add_attribute("action", "instantiate_sg721_reply")
.add_attribute("sg721_address", collection_address))
}
Err(_) => Err(ContractError::InstantiateSg721Error {}),
}
}