-
Notifications
You must be signed in to change notification settings - Fork 3
/
erc721_token.rs
667 lines (549 loc) · 20.8 KB
/
erc721_token.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//! A pluggable Odra module implementing Erc721 token with metadata and ownership.
use crate::access::Ownable;
use crate::erc721::erc721_base::Erc721Base;
use crate::erc721::events::Transfer;
use crate::erc721::extensions::erc721_metadata::{Erc721Metadata, Erc721MetadataExtension};
use crate::erc721::owned_erc721_with_metadata::OwnedErc721WithMetadata;
use crate::erc721::Erc721;
use crate::erc721_token::errors::Error;
use odra::prelude::*;
use odra::{
casper_types::{bytesrepr::Bytes, U256},
Address, SubModule
};
/// The ERC721 token implementation.
///
/// It uses the [ERC721](Erc721Base) base implementation, the [ERC721 metadata](Erc721MetadataExtension) extension
/// and the [Ownable] module.
#[odra::module(events = [Transfer])]
pub struct Erc721Token {
core: SubModule<Erc721Base>,
metadata: SubModule<Erc721MetadataExtension>,
ownable: SubModule<Ownable>
}
#[odra::module]
impl OwnedErc721WithMetadata for Erc721Token {
fn init(&mut self, name: String, symbol: String, base_uri: String) {
self.metadata.init(name, symbol, base_uri);
self.ownable.init();
}
fn name(&self) -> String {
self.metadata.name()
}
fn symbol(&self) -> String {
self.metadata.symbol()
}
fn base_uri(&self) -> String {
self.metadata.base_uri()
}
fn balance_of(&self, owner: &Address) -> U256 {
self.core.balance_of(owner)
}
fn owner_of(&self, token_id: &U256) -> Address {
self.core.owner_of(token_id)
}
fn safe_transfer_from(&mut self, from: &Address, to: &Address, token_id: &U256) {
self.core.safe_transfer_from(from, to, token_id);
}
fn safe_transfer_from_with_data(
&mut self,
from: &Address,
to: &Address,
token_id: &U256,
data: &Bytes
) {
self.core
.safe_transfer_from_with_data(from, to, token_id, data);
}
fn transfer_from(&mut self, from: &Address, to: &Address, token_id: &U256) {
self.core.transfer_from(from, to, token_id);
}
fn approve(&mut self, approved: &Option<Address>, token_id: &U256) {
self.core.approve(approved, token_id);
}
fn set_approval_for_all(&mut self, operator: &Address, approved: bool) {
self.core.set_approval_for_all(operator, approved);
}
fn get_approved(&self, token_id: &U256) -> Option<Address> {
self.core.get_approved(token_id)
}
fn is_approved_for_all(&self, owner: &Address, operator: &Address) -> bool {
self.core.is_approved_for_all(owner, operator)
}
fn renounce_ownership(&mut self) {
self.ownable.renounce_ownership();
}
fn transfer_ownership(&mut self, new_owner: &Address) {
self.ownable.transfer_ownership(new_owner);
}
fn owner(&self) -> Address {
self.ownable.get_owner()
}
fn mint(&mut self, to: &Address, token_id: &U256) {
self.ownable.assert_owner(&self.env().caller());
if self.core.exists(token_id) {
self.env().revert(Error::TokenAlreadyExists)
}
self.core.balances.add(to, U256::from(1));
self.core.owners.set(token_id, Some(*to));
}
fn burn(&mut self, token_id: &U256) {
self.core.assert_exists(token_id);
self.ownable.assert_owner(&self.env().caller());
let owner = self.core.owner_of(token_id);
let balance = self.core.balance_of(&owner);
self.core.balances.set(&owner, balance - U256::from(1));
self.core.owners.set(token_id, None);
self.core.clear_approval(token_id);
self.env().emit_event(Transfer {
from: Some(owner),
to: None,
token_id: *token_id
});
}
}
/// Erc721 errors.
pub mod errors {
use odra::OdraError;
/// Erc721 errors.
#[derive(OdraError)]
pub enum Error {
/// Token with a given id already exists.
TokenAlreadyExists = 35_000
}
}
#[cfg(test)]
mod tests {
use super::{Erc721TokenHostRef, Erc721TokenInitArgs};
use crate::access::errors::Error as AccessError;
use crate::erc20::{Erc20HostRef, Erc20InitArgs};
use crate::erc721::errors::Error::{InvalidTokenId, NotAnOwnerOrApproved};
use crate::erc721::owned_erc721_with_metadata::OwnedErc721WithMetadata;
use crate::erc721_receiver::events::Received;
use crate::erc721_receiver::Erc721ReceiverHostRef;
use crate::erc721_token::errors::Error::TokenAlreadyExists;
use odra::host::{Deployer, HostEnv, HostRef, NoArgs};
use odra::prelude::*;
use odra::{casper_types::U256, Address, OdraError, VmError};
const NAME: &str = "PlascoinNFT";
const SYMBOL: &str = "PLSNFT";
const BASE_URI: &str = "https://plascoin.org/";
struct TokenEnv {
env: HostEnv,
token: Erc721TokenHostRef,
alice: Address,
bob: Address,
carol: Address
}
fn setup() -> TokenEnv {
let env = odra_test::env();
TokenEnv {
env: env.clone(),
token: Erc721TokenHostRef::deploy(
&env,
Erc721TokenInitArgs {
name: NAME.to_string(),
symbol: SYMBOL.to_string(),
base_uri: BASE_URI.to_string()
}
),
alice: env.get_account(1),
bob: env.get_account(2),
carol: env.get_account(3)
}
}
#[test]
fn mints_nft() {
// When deploy a contract with the initial supply.
let mut erc721_env = setup();
// And mint a token to Alice.
erc721_env.token.mint(&erc721_env.alice, &U256::from(1));
// Then Alice has a balance of 1 and Bob has a balance of 0.
assert_eq!(
erc721_env.token.balance_of(&erc721_env.alice),
U256::from(1)
);
assert_eq!(erc721_env.token.balance_of(&erc721_env.bob), U256::from(0));
// And the owner of the token is Alice.
assert_eq!(erc721_env.token.owner_of(&U256::from(1)), erc721_env.alice);
}
#[test]
fn balance_of() {
// When deploy a contract with the initial supply.
let mut erc721_env = setup();
// And mint a token to Alice.
erc721_env.token.mint(&erc721_env.alice, &U256::from(1));
// Then Alice has a balance of 1 and Bob has a balance of 0.
assert_eq!(
erc721_env.token.balance_of(&erc721_env.alice),
U256::from(1)
);
// When we mint another token to Alice.
erc721_env.token.mint(&erc721_env.alice, &U256::from(2));
// Then Alice has a balance of 2.
assert_eq!(
erc721_env.token.balance_of(&erc721_env.alice),
U256::from(2)
);
}
#[test]
fn minting_same_id() {
// When deploy a contract with the initial supply.
let mut erc721_env = setup();
// And mint a token to Alice.
erc721_env.token.mint(&erc721_env.alice, &U256::from(1));
// Then minting the same token again throws an error.
let err = erc721_env
.token
.try_mint(&erc721_env.alice, &U256::from(1))
.unwrap_err();
assert_eq!(err, TokenAlreadyExists.into());
}
#[test]
fn finding_owner() {
// When deploy a contract with the initial supply.
let mut erc721_env = setup();
// And mint a token to Alice.
erc721_env.token.mint(&erc721_env.alice, &U256::from(1));
// Then the owner of the token is Alice.
assert_eq!(erc721_env.token.owner_of(&U256::from(1)), erc721_env.alice);
}
#[test]
fn finding_owner_of_non_existing_token() {
// When deploy a contract with the initial supply.
let erc721_env = setup();
// Then the owner of the token is Alice.
assert_eq!(
erc721_env.token.try_owner_of(&U256::from(1)).unwrap_err(),
InvalidTokenId.into()
);
}
#[test]
fn approve() {
// When deploy a contract with the initial supply.
let mut erc721_env = setup();
// And mint a token to Alice.
erc721_env.token.mint(&erc721_env.alice, &U256::from(1));
// And approve Bob to transfer the token.
erc721_env.env.set_caller(erc721_env.alice);
erc721_env
.token
.approve(&Some(erc721_env.bob), &U256::from(1));
// Then Bob is approved to transfer the token.
assert_eq!(
erc721_env.token.get_approved(&U256::from(1)),
Some(erc721_env.bob)
);
}
#[test]
fn cancel_approve() {
// When deploy a contract with the initial supply.
let mut erc721_env = setup();
// And mint a token to Alice.
erc721_env.token.mint(&erc721_env.alice, &U256::from(1));
// And approve Bob to transfer the token.
erc721_env.env.set_caller(erc721_env.alice);
erc721_env
.token
.approve(&Some(erc721_env.bob), &U256::from(1));
// And cancel the approval.
erc721_env.env.set_caller(erc721_env.alice);
erc721_env.token.approve(&None, &U256::from(1));
// Then Bob is not approved to transfer the token.
assert_eq!(erc721_env.token.get_approved(&U256::from(1)), None);
}
#[test]
fn approve_non_existing_token() {
// When deploy a contract with the initial supply.
let mut erc721_env = setup();
// Then approving a non existing token throws an error.
assert_eq!(
erc721_env
.token
.try_approve(&Some(erc721_env.bob), &U256::from(1))
.unwrap_err(),
InvalidTokenId.into()
);
}
#[test]
fn approve_not_owned_token() {
// When deploy a contract with the initial supply.
let mut erc721_env = setup();
// And mint a token to Alice.
erc721_env.token.mint(&erc721_env.alice, &U256::from(1));
// Then approving a token that is not owned by the caller throws an error.
assert_eq!(
erc721_env
.token
.try_approve(&Some(erc721_env.bob), &U256::from(1))
.unwrap_err(),
NotAnOwnerOrApproved.into()
);
}
#[test]
fn set_an_operator() {
// When deploy a contract with the initial supply.
let mut erc721_env = setup();
// And mint a token to Alice.
erc721_env.token.mint(&erc721_env.alice, &U256::from(1));
// And set Bob as an operator.
erc721_env.env.set_caller(erc721_env.alice);
erc721_env.token.set_approval_for_all(&erc721_env.bob, true);
// Then Bob is an operator.
assert!(erc721_env
.token
.is_approved_for_all(&erc721_env.alice, &erc721_env.bob));
}
#[test]
fn cancel_an_operator() {
// When deploy a contract with the initial supply.
let mut erc721_env = setup();
// And mint a token to Alice.
erc721_env.token.mint(&erc721_env.alice, &U256::from(1));
// And set Bob as an operator.
erc721_env.env.set_caller(erc721_env.alice);
erc721_env.token.set_approval_for_all(&erc721_env.bob, true);
// And cancel Bob as an operator.
erc721_env.env.set_caller(erc721_env.alice);
erc721_env
.token
.set_approval_for_all(&erc721_env.bob, false);
// Then Bob is not an operator.
assert!(!erc721_env
.token
.is_approved_for_all(&erc721_env.alice, &erc721_env.bob));
}
#[test]
fn transfer_nft() {
// When deploy a contract with the initial supply.
let mut erc721_env = setup();
// And mint a token to Alice.
erc721_env.token.mint(&erc721_env.alice, &U256::from(1));
// And transfer the token to Bob.
erc721_env.env.set_caller(erc721_env.alice);
erc721_env
.token
.transfer_from(&erc721_env.alice, &erc721_env.bob, &U256::from(1));
// Then the owner of the token is Bob.
assert_eq!(erc721_env.token.owner_of(&U256::from(1)), erc721_env.bob);
}
#[test]
fn transfer_approved() {
// When deploy a contract with the initial supply.
let mut erc721_env = setup();
// And mint a token to Alice.
erc721_env.token.mint(&erc721_env.alice, &U256::from(1));
// And approve Bob to transfer the token.
erc721_env.env.set_caller(erc721_env.alice);
erc721_env
.token
.approve(&Some(erc721_env.bob), &U256::from(1));
// And transfer the token to Carol.
erc721_env.env.set_caller(erc721_env.bob);
erc721_env
.token
.transfer_from(&erc721_env.alice, &erc721_env.carol, &U256::from(1));
// Then the owner of the token is Carol.
assert_eq!(erc721_env.token.owner_of(&U256::from(1)), erc721_env.carol);
}
#[test]
fn transfer_operator() {
// When deploy a contract with the initial supply.
let mut erc721_env = setup();
// And mint a token to Alice.
erc721_env.token.mint(&erc721_env.alice, &U256::from(1));
// And set Bob as an operator.
erc721_env.env.set_caller(erc721_env.alice);
erc721_env.token.set_approval_for_all(&erc721_env.bob, true);
// And transfer the token to Carol.
erc721_env.env.set_caller(erc721_env.bob);
erc721_env
.token
.transfer_from(&erc721_env.alice, &erc721_env.carol, &U256::from(1));
// Then the owner of the token is Carol.
assert_eq!(erc721_env.token.owner_of(&U256::from(1)), erc721_env.carol);
}
#[test]
fn transfer_not_owned() {
// When deploy a contract with the initial supply.
let mut erc721_env = setup();
// And mint a token to Alice.
erc721_env.token.mint(&erc721_env.alice, &U256::from(1));
// Then transferring a token that is not owned by the caller throws an error.
erc721_env.env.set_caller(erc721_env.bob);
let err = erc721_env
.token
.try_transfer_from(&erc721_env.alice, &erc721_env.carol, &U256::from(1))
.unwrap_err();
assert_eq!(err, NotAnOwnerOrApproved.into());
}
#[test]
fn transferring_invalid_nft() {
// When deploy a contract with the initial supply.
let mut erc721_env = setup();
// Then transferring a token that does not exist throws an error.
let err = erc721_env
.token
.try_transfer_from(&erc721_env.alice, &erc721_env.carol, &U256::from(1))
.unwrap_err();
assert_eq!(err, InvalidTokenId.into());
}
#[test]
fn safe_transfer() {
// When deploy a contract with the initial supply.
let mut erc721_env = setup();
assert!(erc721_env.token.address().is_contract());
assert!(!erc721_env.alice.is_contract());
// And mint a token to Alice.
erc721_env.token.mint(&erc721_env.alice, &U256::from(1));
// And safe transfer the token to Bob.
erc721_env.env.set_caller(erc721_env.alice);
erc721_env
.token
.safe_transfer_from(&erc721_env.alice, &erc721_env.bob, &U256::from(1));
// Then the owner of the token is Bob.
assert_eq!(erc721_env.token.owner_of(&U256::from(1)), erc721_env.bob);
}
#[test]
fn safe_transfer_to_contract_which_does_not_support_nft() {
// When deploy a contract with the initial supply
let mut erc721_env = setup();
// And another contract which does not support nfts
let erc20 = Erc20HostRef::deploy(
&erc721_env.env,
Erc20InitArgs {
name: "PLASCOIN".to_string(),
symbol: "PLS".to_string(),
decimals: 10,
initial_supply: None
}
);
// And mint a token to Alice.
erc721_env.token.mint(&erc721_env.alice, &U256::from(1));
// Then safe transfer the token to the contract which does not support nfts throws an error.
erc721_env.env.set_caller(erc721_env.alice);
assert_eq!(
Err(OdraError::VmError(VmError::NoSuchMethod(
"on_erc721_received".to_string()
))),
erc721_env.token.try_safe_transfer_from(
&erc721_env.alice,
erc20.address(),
&U256::from(1)
)
);
}
#[test]
fn safe_transfer_to_contract_which_supports_nft() {
// When deploy a contract with the initial supply
let mut erc721_env = setup();
// And another contract which does not support nfts
let receiver = Erc721ReceiverHostRef::deploy(&erc721_env.env, NoArgs);
// And mint a token to Alice.
erc721_env.token.mint(&erc721_env.alice, &U256::from(1));
// And transfer the token to the contract which does support nfts
erc721_env.env.set_caller(erc721_env.alice);
erc721_env
.token
.safe_transfer_from(&erc721_env.alice, receiver.address(), &U256::from(1));
// Then the owner of the token is the contract
assert_eq!(
erc721_env.token.owner_of(&U256::from(1)),
*receiver.address()
);
// And the receiver contract is aware of the transfer
erc721_env.env.emitted_event(
receiver.address(),
&Received {
operator: Some(erc721_env.alice),
from: Some(erc721_env.alice),
token_id: U256::from(1),
data: None
}
);
}
#[test]
fn safe_transfer_to_contract_with_data() {
// When deploy a contract with the initial supply
let mut erc721_env = setup();
// And another contract which does not support nfts
let receiver = Erc721ReceiverHostRef::deploy(&erc721_env.env, NoArgs);
// And mint a token to Alice.
erc721_env.token.mint(&erc721_env.alice, &U256::from(1));
// And transfer the token to the contract which does support nfts
erc721_env.env.set_caller(erc721_env.alice);
erc721_env.token.safe_transfer_from_with_data(
&erc721_env.alice,
receiver.address(),
&U256::from(1),
&b"data".to_vec().into()
);
// Then the owner of the token is the contract
assert_eq!(
erc721_env.token.owner_of(&U256::from(1)),
receiver.address().clone()
);
// And the receiver contract is aware of the transfer
erc721_env.env.emitted_event(
receiver.address(),
&Received {
operator: Some(erc721_env.alice),
from: Some(erc721_env.alice),
token_id: U256::from(1),
data: Some(b"data".to_vec().into())
}
);
}
#[test]
fn burn() {
// When deploy a contract with the initial supply.
let mut erc721_env = setup();
// And mint a token to Alice.
erc721_env.token.mint(&erc721_env.alice, &U256::from(1));
// And burn the token.
erc721_env.token.burn(&U256::from(1));
// Then the owner of throws an error.
let err = erc721_env.token.try_owner_of(&U256::from(1)).unwrap_err();
assert_eq!(err, InvalidTokenId.into());
}
#[test]
fn burn_error() {
// When deploy a contract with the initial supply.
let mut erc721_env = setup();
// And mint a token to Alice.
erc721_env.token.mint(&erc721_env.alice, &U256::from(1));
// Then burn the token as Alice errors out
erc721_env.env.set_caller(erc721_env.alice);
let err = erc721_env.token.try_burn(&U256::from(1)).unwrap_err();
assert_eq!(err, AccessError::CallerNotTheOwner.into());
}
#[test]
fn burn_non_existing_nft() {
// When deploy a contract with the initial supply.
let mut erc721_env = setup();
// Then burning a token that does not exist throws an error.
let err = erc721_env.token.try_burn(&U256::from(1)).unwrap_err();
assert_eq!(err, InvalidTokenId.into());
}
#[test]
fn metadata() {
// When deploy a contract with the initial supply.
let erc721 = setup();
// Then the contract has the metadata set.
assert_eq!(erc721.token.symbol(), SYMBOL.to_string());
assert_eq!(erc721.token.name(), NAME.to_string());
assert_eq!(erc721.token.base_uri(), BASE_URI.to_string());
}
#[test]
fn minting_by_not_an_owner() {
// When deploy a contract with the initial supply.
let mut erc721_env = setup();
// Then minting a token by not an owner throws an error.
erc721_env.env.set_caller(erc721_env.bob);
let err = erc721_env
.token
.try_mint(&erc721_env.alice, &U256::from(1))
.unwrap_err();
assert_eq!(err, AccessError::CallerNotTheOwner.into());
}
}