-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcontract.rs
442 lines (402 loc) · 15 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
from_binary, to_binary, Addr, Binary, Deps, DepsMut, Empty, Env, IbcMsg, MessageInfo, Response,
StdResult, SubMsg, WasmMsg,
};
use cw2::set_contract_version;
use crate::{
error::ContractError,
ibc::{NonFungibleTokenPacketData, INSTANTIATE_CW721_REPLY_ID, INSTANTIATE_PROXY_REPLY_ID},
msg::{CallbackMsg, ExecuteMsg, IbcOutgoingMsg, InstantiateMsg, MigrateMsg, QueryMsg},
state::{
UniversalNftInfoResponse, CLASS_ID_TO_CLASS, CLASS_ID_TO_NFT_CONTRACT, CW721_CODE_ID,
NFT_CONTRACT_TO_CLASS_ID, OUTGOING_CLASS_TOKEN_TO_CHANNEL, PO, PROXY, TOKEN_METADATA,
},
token_types::{Class, ClassId, Token, TokenId, VoucherCreation, VoucherRedemption},
};
const CONTRACT_NAME: &str = "crates.io:cw-ics721-bridge";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
deps: DepsMut,
env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
CW721_CODE_ID.save(deps.storage, &msg.cw721_base_code_id)?;
PROXY.save(deps.storage, &None)?;
PO.set_pauser(deps.storage, deps.api, msg.pauser.as_deref())?;
let proxy_instantiate = msg
.proxy
.map(|m| m.into_wasm_msg(env.contract.address))
.map(|wasm| SubMsg::reply_on_success(wasm, INSTANTIATE_PROXY_REPLY_ID))
.map_or(vec![], |s| vec![s]);
Ok(Response::default()
.add_submessages(proxy_instantiate)
.add_attribute("method", "instantiate")
.add_attribute("cw721_code_id", msg.cw721_base_code_id.to_string()))
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
PO.error_if_paused(deps.storage)?;
match msg {
ExecuteMsg::ReceiveNft(cw721::Cw721ReceiveMsg {
sender,
token_id,
msg,
}) => execute_receive_nft(deps, info, token_id, sender, msg),
ExecuteMsg::ReceiveProxyNft { eyeball, msg } => {
execute_receive_proxy_nft(deps, info, eyeball, msg)
}
ExecuteMsg::Pause {} => execute_pause(deps, info),
ExecuteMsg::Callback(msg) => execute_callback(deps, env, info, msg),
}
}
fn execute_callback(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: CallbackMsg,
) -> Result<Response, ContractError> {
if info.sender != env.contract.address {
Err(ContractError::Unauthorized {})
} else {
match msg {
CallbackMsg::CreateVouchers { receiver, create } => {
callback_create_vouchers(deps, env, receiver, create)
}
CallbackMsg::RedeemVouchers { receiver, redeem } => {
callback_redeem_vouchers(deps, receiver, redeem)
}
CallbackMsg::Mint {
class_id,
tokens,
receiver,
} => callback_mint(deps, class_id, tokens, receiver),
CallbackMsg::Conjunction { operands } => Ok(Response::default().add_messages(operands)),
}
}
}
fn execute_receive_proxy_nft(
deps: DepsMut,
info: MessageInfo,
eyeball: String,
msg: cw721::Cw721ReceiveMsg,
) -> Result<Response, ContractError> {
if PROXY
.load(deps.storage)?
.map_or(true, |proxy| info.sender != proxy)
{
return Err(ContractError::Unauthorized {});
}
let mut info = info;
info.sender = deps.api.addr_validate(&eyeball)?;
let cw721::Cw721ReceiveMsg {
token_id,
sender,
msg,
} = msg;
receive_nft(deps, info, TokenId::new(token_id), sender, msg)
}
fn execute_receive_nft(
deps: DepsMut,
info: MessageInfo,
token_id: String,
sender: String,
msg: Binary,
) -> Result<Response, ContractError> {
if PROXY.load(deps.storage)?.is_some() {
Err(ContractError::Unauthorized {})
} else {
receive_nft(deps, info, TokenId::new(token_id), sender, msg)
}
}
fn execute_pause(deps: DepsMut, info: MessageInfo) -> Result<Response, ContractError> {
PO.pause(deps.storage, &info.sender)?;
Ok(Response::default().add_attribute("method", "pause"))
}
pub(crate) fn receive_nft(
deps: DepsMut,
info: MessageInfo,
token_id: TokenId,
sender: String,
msg: Binary,
) -> Result<Response, ContractError> {
let sender = deps.api.addr_validate(&sender)?;
let msg: IbcOutgoingMsg = from_binary(&msg)?;
let class = match NFT_CONTRACT_TO_CLASS_ID.may_load(deps.storage, info.sender.clone())? {
Some(class_id) => CLASS_ID_TO_CLASS.load(deps.storage, class_id)?,
// No class ID being present means that this is a local NFT
// that has never been sent out of this contract.
None => {
let class = Class {
id: ClassId::new(info.sender.to_string()),
// There is no collection-level uri nor data in the
// cw721 specification so we set those values to
// `None` for local, cw721 NFTs.
uri: None,
data: None,
};
NFT_CONTRACT_TO_CLASS_ID.save(deps.storage, info.sender.clone(), &class.id)?;
CLASS_ID_TO_NFT_CONTRACT.save(deps.storage, class.id.clone(), &info.sender)?;
// Merging and usage of this PR may change that:
// <https://github.com/CosmWasm/cw-nfts/pull/75>
CLASS_ID_TO_CLASS.save(deps.storage, class.id.clone(), &class)?;
class
}
};
let UniversalNftInfoResponse { token_uri, .. } = deps.querier.query_wasm_smart(
info.sender,
&cw721::Cw721QueryMsg::NftInfo {
token_id: token_id.clone().into(),
},
)?;
let token_metadata = TOKEN_METADATA
.may_load(deps.storage, (class.id.clone(), token_id.clone()))?
.flatten();
let ibc_message = NonFungibleTokenPacketData {
class_id: class.id.clone(),
class_uri: class.uri,
class_data: class.data,
token_ids: vec![token_id.clone()],
token_uris: token_uri.map(|uri| vec![uri]),
token_data: token_metadata.map(|metadata| vec![metadata]),
sender: sender.into_string(),
receiver: msg.receiver,
memo: msg.memo,
};
let ibc_message = IbcMsg::SendPacket {
channel_id: msg.channel_id.clone(),
data: to_binary(&ibc_message)?,
timeout: msg.timeout,
};
OUTGOING_CLASS_TOKEN_TO_CHANNEL.save(
deps.storage,
(class.id.clone(), token_id.clone()),
&msg.channel_id,
)?;
Ok(Response::default()
.add_attribute("method", "execute_receive_nft")
.add_attribute("token_id", token_id)
.add_attribute("class_id", class.id)
.add_attribute("channel_id", msg.channel_id)
.add_message(ibc_message))
}
fn callback_mint(
deps: DepsMut,
class_id: ClassId,
tokens: Vec<Token>,
receiver: String,
) -> Result<Response, ContractError> {
let receiver = deps.api.addr_validate(&receiver)?;
let cw721_addr = CLASS_ID_TO_NFT_CONTRACT.load(deps.storage, class_id.clone())?;
let mint = tokens
.into_iter()
.map(|Token { id, uri, data }| {
// We save token metadata here as, ideally, once cw721
// supports on-chain metadata, this is where we will set
// that value on the debt-voucher token. Note that this is
// set for every token, regardless of if data is None.
TOKEN_METADATA.save(deps.storage, (class_id.clone(), id.clone()), &data)?;
let msg = cw721_base::msg::ExecuteMsg::<Empty, Empty>::Mint(cw721_base::MintMsg {
token_id: id.into(),
token_uri: uri,
owner: receiver.to_string(),
extension: Empty::default(),
});
Ok(WasmMsg::Execute {
contract_addr: cw721_addr.to_string(),
msg: to_binary(&msg)?,
funds: vec![],
})
})
.collect::<StdResult<Vec<_>>>()?;
Ok(Response::default()
.add_attribute("method", "callback_mint")
.add_messages(mint))
}
/// Creates the specified debt vouchers by minting cw721 debt-voucher
/// tokens for the receiver. If no debt-voucher collection yet exists
/// a new collection is instantiated before minting the vouchers.
fn callback_create_vouchers(
deps: DepsMut,
env: Env,
receiver: String,
create: VoucherCreation,
) -> Result<Response, ContractError> {
let VoucherCreation { class, tokens } = create;
let instantiate = if CLASS_ID_TO_NFT_CONTRACT.has(deps.storage, class.id.clone()) {
vec![]
} else {
let message = SubMsg::<Empty>::reply_on_success(
WasmMsg::Instantiate {
admin: None,
code_id: CW721_CODE_ID.load(deps.storage)?,
msg: to_binary(&cw721_base::msg::InstantiateMsg {
// Name of the collection MUST be class_id as this is how
// we create a map entry on reply.
name: class.id.clone().into(),
symbol: class.id.clone().into(),
minter: env.contract.address.to_string(),
})?,
funds: vec![],
// Attempting to fit the class ID in the label field
// can make this field too long which causes data
// errors in the SDK.
label: "ics-721 debt-voucher cw-721".to_string(),
},
INSTANTIATE_CW721_REPLY_ID,
);
vec![message]
};
// Store mapping from classID to classURI. Notably, we don't check
// if this has already been set. If a new NFT belonging to a class
// ID we have already seen comes in with new metadata, we assume
// that the metadata has been updated on the source chain and
// update it for the class ID locally as well.
CLASS_ID_TO_CLASS.save(deps.storage, class.id.clone(), &class)?;
let mint = WasmMsg::Execute {
contract_addr: env.contract.address.into_string(),
msg: to_binary(&ExecuteMsg::Callback(CallbackMsg::Mint {
class_id: class.id,
receiver,
tokens,
}))?,
funds: vec![],
};
Ok(Response::default()
.add_attribute("method", "callback_create_vouchers")
.add_submessages(instantiate)
.add_message(mint))
}
/// Performs a recemption of debt vouchers returning the corresponding
/// tokens to the receiver.
fn callback_redeem_vouchers(
deps: DepsMut,
receiver: String,
redeem: VoucherRedemption,
) -> Result<Response, ContractError> {
let VoucherRedemption { class, token_ids } = redeem;
let nft_contract = CLASS_ID_TO_NFT_CONTRACT.load(deps.storage, class.id)?;
let receiver = deps.api.addr_validate(&receiver)?;
Ok(Response::default()
.add_attribute("method", "callback_redeem_vouchers")
.add_messages(
token_ids
.into_iter()
.map(|token_id| {
Ok(WasmMsg::Execute {
contract_addr: nft_contract.to_string(),
msg: to_binary(&cw721::Cw721ExecuteMsg::TransferNft {
recipient: receiver.to_string(),
token_id: token_id.into(),
})?,
funds: vec![],
})
})
.collect::<StdResult<Vec<WasmMsg>>>()?,
))
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::ClassId { contract } => {
to_binary(&query_class_id_for_nft_contract(deps, contract)?)
}
QueryMsg::NftContract { class_id } => {
to_binary(&query_nft_contract_for_class_id(deps, class_id)?)
}
QueryMsg::ClassMetadata { class_id } => to_binary(&query_class_metadata(deps, class_id)?),
QueryMsg::TokenMetadata { class_id, token_id } => {
to_binary(&query_token_metadata(deps, class_id, token_id)?)
}
QueryMsg::Owner { class_id, token_id } => {
to_binary(&query_owner(deps, class_id, token_id)?)
}
QueryMsg::Pauser {} => to_binary(&PO.query_pauser(deps.storage)?),
QueryMsg::Paused {} => to_binary(&PO.query_paused(deps.storage)?),
QueryMsg::Proxy {} => to_binary(&PROXY.load(deps.storage)?),
}
}
fn query_class_id_for_nft_contract(deps: Deps, contract: String) -> StdResult<Option<ClassId>> {
let contract = deps.api.addr_validate(&contract)?;
NFT_CONTRACT_TO_CLASS_ID.may_load(deps.storage, contract)
}
fn query_nft_contract_for_class_id(deps: Deps, class_id: String) -> StdResult<Option<Addr>> {
CLASS_ID_TO_NFT_CONTRACT.may_load(deps.storage, ClassId::new(class_id))
}
fn query_class_metadata(deps: Deps, class_id: String) -> StdResult<Option<Class>> {
CLASS_ID_TO_CLASS.may_load(deps.storage, ClassId::new(class_id))
}
fn query_token_metadata(
deps: Deps,
class_id: String,
token_id: String,
) -> StdResult<Option<Token>> {
let token_id = TokenId::new(token_id);
let class_id = ClassId::new(class_id);
let Some(token_metadata) = TOKEN_METADATA.may_load(
deps.storage,
(class_id.clone(), token_id.clone()),
)? else {
// Token metadata is set unconditionaly on mint. If we have no
// metadata entry, we have no entry for this token at all.
return Ok(None)
};
let Some(token_contract) = CLASS_ID_TO_NFT_CONTRACT.may_load(
deps.storage,
class_id
)? else {
debug_assert!(false, "token_metadata != None => token_contract != None");
return Ok(None)
};
let UniversalNftInfoResponse { token_uri, .. } = deps.querier.query_wasm_smart(
token_contract,
&cw721::Cw721QueryMsg::NftInfo {
token_id: token_id.clone().into(),
},
)?;
Ok(Some(Token {
id: token_id,
uri: token_uri,
data: token_metadata,
}))
}
fn query_owner(
deps: Deps,
class_id: String,
token_id: String,
) -> StdResult<cw721::OwnerOfResponse> {
let class_uri = CLASS_ID_TO_NFT_CONTRACT.load(deps.storage, ClassId::new(class_id))?;
let resp: cw721::OwnerOfResponse = deps.querier.query_wasm_smart(
class_uri,
&cw721::Cw721QueryMsg::OwnerOf {
token_id,
include_expired: None,
},
)?;
Ok(resp)
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> Result<Response, ContractError> {
match msg {
MigrateMsg::WithUpdate { pauser, proxy } => {
PROXY.save(
deps.storage,
&proxy
.as_ref()
.map(|h| deps.api.addr_validate(h))
.transpose()?,
)?;
PO.set_pauser(deps.storage, deps.api, pauser.as_deref())?;
Ok(Response::default().add_attribute("method", "migrate"))
}
}
}