@@ -217,7 +217,7 @@ mod erc721 {
217
217
218
218
let owner = token_owner. get ( id) . ok_or ( Error :: TokenNotFound ) ?;
219
219
if owner != caller {
220
- return Err ( Error :: NotOwner )
220
+ return Err ( Error :: NotOwner ) ;
221
221
} ;
222
222
223
223
let count = owned_tokens_count
@@ -244,14 +244,12 @@ mod erc721 {
244
244
id : TokenId ,
245
245
) -> Result < ( ) , Error > {
246
246
let caller = self . env ( ) . caller ( ) ;
247
- if !self . exists ( id) {
248
- return Err ( Error :: TokenNotFound )
247
+ let owner = self . owner_of ( id) . ok_or ( Error :: TokenNotFound ) ?;
248
+ if !self . approved_or_owner ( caller, id, owner) {
249
+ return Err ( Error :: NotApproved ) ;
249
250
} ;
250
- if !self . approved_or_owner ( Some ( caller) , id) {
251
- return Err ( Error :: NotApproved )
252
- } ;
253
- if self . token_owner . get ( id) != Some ( * from) {
254
- return Err ( Error :: NotOwner )
251
+ if owner != * from {
252
+ return Err ( Error :: NotOwner ) ;
255
253
} ;
256
254
self . clear_approval ( id) ;
257
255
self . remove_token_from ( from, id) ?;
@@ -277,7 +275,7 @@ mod erc721 {
277
275
} = self ;
278
276
279
277
if !token_owner. contains ( id) {
280
- return Err ( Error :: TokenNotFound )
278
+ return Err ( Error :: TokenNotFound ) ;
281
279
}
282
280
283
281
let count = owned_tokens_count
@@ -299,11 +297,11 @@ mod erc721 {
299
297
} = self ;
300
298
301
299
if token_owner. contains ( id) {
302
- return Err ( Error :: TokenExists )
300
+ return Err ( Error :: TokenExists ) ;
303
301
}
304
302
305
303
if * to == AccountId :: from ( [ 0x0 ; 32 ] ) {
306
- return Err ( Error :: NotAllowed )
304
+ return Err ( Error :: NotAllowed ) ;
307
305
} ;
308
306
309
307
let count = owned_tokens_count
@@ -325,7 +323,7 @@ mod erc721 {
325
323
) -> Result < ( ) , Error > {
326
324
let caller = self . env ( ) . caller ( ) ;
327
325
if to == caller {
328
- return Err ( Error :: NotAllowed )
326
+ return Err ( Error :: NotAllowed ) ;
329
327
}
330
328
self . env ( ) . emit_event ( ApprovalForAll {
331
329
owner : caller,
@@ -346,19 +344,17 @@ mod erc721 {
346
344
/// the message's sender.
347
345
fn approve_for ( & mut self , to : & AccountId , id : TokenId ) -> Result < ( ) , Error > {
348
346
let caller = self . env ( ) . caller ( ) ;
349
- let owner = self . owner_of ( id) ;
350
- if !( owner == Some ( caller)
351
- || self . approved_for_all ( owner. expect ( "Error with AccountId" ) , caller) )
352
- {
353
- return Err ( Error :: NotAllowed )
347
+ let owner = self . owner_of ( id) . ok_or ( Error :: TokenNotFound ) ?;
348
+ if !( owner == caller || self . approved_for_all ( owner, caller) ) {
349
+ return Err ( Error :: NotAllowed ) ;
354
350
} ;
355
351
356
352
if * to == AccountId :: from ( [ 0x0 ; 32 ] ) {
357
- return Err ( Error :: NotAllowed )
353
+ return Err ( Error :: NotAllowed ) ;
358
354
} ;
359
355
360
356
if self . token_approvals . contains ( id) {
361
- return Err ( Error :: CannotInsert )
357
+ return Err ( Error :: CannotInsert ) ;
362
358
} else {
363
359
self . token_approvals . insert ( id, to) ;
364
360
}
@@ -389,20 +385,16 @@ mod erc721 {
389
385
390
386
/// Returns true if the `AccountId` `from` is the owner of token `id`
391
387
/// or it has been approved on behalf of the token `id` owner.
392
- fn approved_or_owner ( & self , from : Option < AccountId > , id : TokenId ) -> bool {
393
- let owner = self . owner_of ( id) ;
394
- from != Some ( AccountId :: from ( [ 0x0 ; 32 ] ) )
388
+ fn approved_or_owner (
389
+ & self ,
390
+ from : AccountId ,
391
+ id : TokenId ,
392
+ owner : AccountId ,
393
+ ) -> bool {
394
+ from != AccountId :: from ( [ 0x0 ; 32 ] )
395
395
&& ( from == owner
396
- || from == self . token_approvals . get ( id)
397
- || self . approved_for_all (
398
- owner. expect ( "Error with AccountId" ) ,
399
- from. expect ( "Error with AccountId" ) ,
400
- ) )
401
- }
402
-
403
- /// Returns true if token `id` exists or false if it does not.
404
- fn exists ( & self , id : TokenId ) -> bool {
405
- self . token_owner . contains ( id)
396
+ || self . token_approvals . get ( id) == Some ( from)
397
+ || self . approved_for_all ( owner, from) )
406
398
}
407
399
}
408
400
@@ -563,6 +555,16 @@ mod erc721 {
563
555
assert ! ( !erc721. is_approved_for_all( accounts. alice, accounts. bob) ) ;
564
556
}
565
557
558
+ #[ ink:: test]
559
+ fn approve_nonexistent_token_should_fail ( ) {
560
+ let accounts =
561
+ ink:: env:: test:: default_accounts :: < ink:: env:: DefaultEnvironment > ( ) ;
562
+ // Create a new contract instance.
563
+ let mut erc721 = Erc721 :: new ( ) ;
564
+ // Approve transfer of nonexistent token id 1
565
+ assert_eq ! ( erc721. approve( accounts. bob, 1 ) , Err ( Error :: TokenNotFound ) ) ;
566
+ }
567
+
566
568
#[ ink:: test]
567
569
fn not_approved_transfer_should_fail ( ) {
568
570
let accounts =
0 commit comments