odra_modules/
cep95.rs

1#![allow(unused_variables, missing_docs)]
2
3use alloc::collections::BTreeMap;
4use odra::{
5    casper_types::{
6        bytesrepr::{Bytes, ToBytes},
7        U256
8    },
9    named_keys::{
10        base64_encoded_key_value_storage, compound_key_value_storage, single_value_storage
11    },
12    prelude::*,
13    ContractRef
14};
15
16/// Casper-compatible NFT interface
17pub trait CEP95Interface {
18    /// Returns a name of the NFT token/collection.
19    fn name(&self) -> String;
20
21    /// Returns a short symbol or abbreviation for the NFT token/collection.
22    fn symbol(&self) -> String;
23
24    /// Returns the number of NFTs owned by a given account or contract
25    ///
26    /// # Arguments
27    /// owner - The account to query.
28    ///
29    /// # Returns
30    /// The number of NFTs owned by the account.
31    fn balance_of(&self, owner: Address) -> U256;
32
33    /// Returns the owner of a specific NFT.
34    ///
35    /// # Arguments
36    /// token_id - The ID of the NFT.
37    ///
38    /// # Returns
39    /// The owner if it exists, else None.
40    fn owner_of(&self, token_id: U256) -> Option<Address>;
41
42    /// Performs a recipient check and transfers the ownership of an NFT.
43    ///
44    /// Reverts unless the contract caller is the current owner, an authorized
45    /// operator, or the approved spender for this NFT. Reverts if `from` is not
46    /// the current owner, or if `token_id` does not reference a valid NFT.
47    /// Once ownership is updated and a `Transfer` event is emitted, the function
48    /// checks whether `to` is a contract hash. If it is, the contract MUST call
49    /// `on_cep95_received` on `to` and revert the entire transfer if that call
50    /// is absent or returns any value other than `true`.
51    ///
52    /// # Arguments
53    /// from - The current owner of the NFT.
54    /// to - The new owner.
55    /// token_id - The NFT ID.
56    /// data - Optional payload to pass to a receiving contract.
57    fn safe_transfer_from(
58        &mut self,
59        from: Address,
60        to: Address,
61        token_id: U256,
62        data: Option<Bytes>
63    );
64
65    /// Transfers the ownership of an NFT without checking the recipient contract.
66    ///
67    /// # Arguments
68    /// from - The current owner of the NFT.
69    /// to - The new owner.
70    /// token_id - The NFT ID.
71    fn transfer_from(&mut self, from: Address, to: Address, token_id: U256);
72
73    /// Approves another account or contract to transfer a specific NFT.
74    ///
75    /// # Arguments
76    /// spender - The account or contract that will be granted approval.
77    /// token_id - The NFT ID.
78    fn approve(&mut self, spender: Address, token_id: U256);
79
80    /// Revokes approval for a specific NFT.
81    ///
82    /// # Arguments
83    /// token_id - The NFT ID to revoke approval for.
84    fn revoke_approval(&mut self, token_id: U256);
85
86    /// Gets the approved account or contract for a specific NFT.
87    ///
88    /// # Arguments
89    /// token_id - The ID of the NFT to check.
90    ///
91    /// # Returns
92    /// Option<Address> - The approved spender account if one exists, else None.
93    fn approved_for(&self, token_id: U256) -> Option<Address>;
94
95    /// Enables operator approval for all of the caller's NFTs.
96    ///
97    /// # Arguments
98    /// operator - The operator address to be approved.
99    fn approve_for_all(&mut self, operator: Address);
100
101    /// Revokes operator approval for all of the caller's NFTs.
102    ///
103    /// # Arguments
104    /// operator - The operator address to be revoked.
105    fn revoke_approval_for_all(&mut self, operator: Address);
106
107    /// Checks if an operator is approved to manage all NFTs of the owner.
108    ///
109    /// # Arguments
110    /// owner - The NFT owner's address.
111    /// operator - The operator to check.
112    ///
113    /// # Returns
114    /// True if the operator is approved for all NFTs, false otherwise.
115    fn is_approved_for_all(&self, owner: Address, operator: Address) -> bool;
116
117    /// Returns metadata for a given token ID.
118    ///
119    /// # Arguments
120    /// token_id - The ID of the NFT.
121    ///
122    /// # Returns
123    /// A vector of key-value pairs representing the metadata.
124    fn token_metadata(&self, token_id: U256) -> Vec<(String, String)>;
125}
126
127#[odra::module]
128/// Receiver interface
129struct CEP95Receiver;
130
131#[odra::module]
132impl CEP95Receiver {
133    /// Called after a `safe_transfer_from` completes its internal state update.
134    /// MUST return `true` to signal acceptance; returning `false` or reverting
135    /// causes the entire transfer to roll back.
136    ///
137    /// # Arguments
138    /// operator - The account (EOA or contract) that invoked `safe_transfer_from`.
139    /// from - The previous owner of `token_id`.
140    /// token_id - The NFT being transferred.
141    /// data - Opaque auxiliary data forwarded from the original call; may be `None` if no extra data was
142    /// supplied.
143    ///
144    /// # Returns
145    /// `true` to accept the NFT, anything else to reject.
146    #[allow(dead_code)]
147    pub fn on_cep95_received(
148        &mut self,
149        operator: &Address,
150        from: &Address,
151        token_id: &U256,
152        data: &Option<Bytes>
153    ) -> bool {
154        // This is a placeholder implementation. In a real contract, you would
155        // implement the logic to handle the received NFT here.
156        // For example, you might want to store the token ID and data in your contract's state.
157        // For now, we just return true to indicate acceptance.
158        true
159    }
160}
161
162const KEY_BALANCES: &str = "balances";
163const KEY_NAME: &str = "name";
164const KEY_SYMBOL: &str = "symbol";
165const KEY_APPROVED: &str = "approvals";
166const KEY_OPERATORS: &str = "operators";
167const KEY_METADATA: &str = "token_metadata";
168const KEY_OWNERS: &str = "owners";
169
170single_value_storage!(Cep95Name, String, KEY_NAME, Error::ValueNotSet);
171single_value_storage!(Cep95Symbol, String, KEY_SYMBOL, Error::ValueNotSet);
172base64_encoded_key_value_storage!(Cep95Balances, KEY_BALANCES, Address, U256);
173base64_encoded_key_value_storage!(Cep95Approvals, KEY_APPROVED, U256, Option<Address>);
174compound_key_value_storage!(Cep95Operators, KEY_OPERATORS, Address, bool);
175base64_encoded_key_value_storage!(Cep95Owners, KEY_OWNERS, U256, Option<Address>);
176base64_encoded_key_value_storage!(Cep95Metadata, KEY_METADATA, U256, BTreeMap<String, String>);
177
178/// Error enum for the CEP-95 contract.
179#[odra::odra_error]
180pub enum Error {
181    /// The value is not set.
182    ValueNotSet = 40_000,
183    /// The transfer failed.
184    TransferFailed = 40_001,
185    /// The token ID is invalid.
186    NotAnOwnerOrApproved = 40_002,
187    /// The approval is set to the current owner.
188    ApprovalToCurrentOwner = 40_003,
189    /// The caller is the same as the operator.
190    ApproveToCaller = 40_004,
191    /// The token ID is invalid.
192    InvalidTokenId = 40_005,
193    /// The token with the given ID already exists.
194    TokenAlreadyExists = 40_006
195}
196
197#[odra::event]
198/// Emitted when an NFT is minted
199pub struct Mint {
200    /// The address of the recipient.
201    pub to: Address,
202    /// The ID of the minted token.
203    pub token_id: U256
204}
205
206#[odra::event]
207/// Emitted when an NFT is burned
208pub struct Burn {
209    /// The address of the owner.
210    pub from: Address,
211    /// The ID of the burned token.
212    pub token_id: U256
213}
214
215#[odra::event]
216/// Emitted when an NFT is transferred
217pub struct Transfer {
218    /// The address of the previous owner.
219    pub from: Address,
220    /// The address of the new owner.
221    pub to: Address,
222    /// The ID of the transferred token.
223    pub token_id: U256
224}
225
226#[odra::event]
227/// Emitted when a specific NFT is approved to an account/contract
228pub struct Approval {
229    /// The address of the owner.
230    pub owner: Address,
231    /// The address of the approved spender.
232    pub spender: Address,
233    /// The ID of the approved token.
234    pub token_id: U256
235}
236
237#[odra::event]
238/// Emitted when a specific NFT approval is revoked from an account/contract
239pub struct RevokeApproval {
240    /// The address of the owner.
241    pub owner: Address,
242    /// The address of the revoked spender.
243    pub spender: Address,
244    /// The ID of the revoked token.
245    pub token_id: U256
246}
247
248#[odra::event]
249/// Emitted when an operator is approved for all NFTs of an owner
250pub struct ApprovalForAll {
251    /// The address of the owner.
252    pub owner: Address,
253    /// The address of the operator.
254    pub operator: Address
255}
256
257#[odra::event]
258/// Emitted when an operator approval is revoked for all NFTs of an owner
259pub struct RevokeApprovalForAll {
260    /// The address of the owner.
261    pub owner: Address,
262    /// The address of the operator.
263    pub operator: Address
264}
265
266#[odra::event]
267/// Emitted whenever on-chain metadata for a token is created or updated
268pub struct MetadataUpdate {
269    /// The ID of the token.
270    pub token_id: U256
271}
272
273#[odra::module(
274    events = [
275        Transfer,
276        Approval,
277        RevokeApproval,
278        ApprovalForAll,
279        RevokeApprovalForAll,
280        Mint,
281        Burn,
282        MetadataUpdate
283    ],
284    errors = Error
285)]
286/// A module representing a CEP-95 standard.
287pub struct Cep95 {
288    /// A submodule for the token name.
289    pub name: SubModule<Cep95Name>,
290    /// A submodule for the token symbol.
291    pub symbol: SubModule<Cep95Symbol>,
292    /// A submodule for the token balances mapping.
293    pub balances: SubModule<Cep95Balances>,
294    /// A submodule for the token owners mapping.
295    pub owners: SubModule<Cep95Owners>,
296    /// A submodule for the token approvals mapping.
297    pub approvals: SubModule<Cep95Approvals>,
298    /// A submodule for the token operators mapping.
299    pub operators: SubModule<Cep95Operators>,
300    /// A submodule for the token metadata mapping.
301    pub metadata: SubModule<Cep95Metadata>
302}
303
304#[odra::module]
305impl CEP95Interface for Cep95 {
306    fn name(&self) -> String {
307        self.name.get()
308    }
309
310    fn symbol(&self) -> String {
311        self.symbol.get()
312    }
313
314    fn balance_of(&self, owner: Address) -> U256 {
315        self.balances.get(&owner).unwrap_or_default()
316    }
317
318    fn owner_of(&self, token_id: U256) -> Option<Address> {
319        self.owners.get(&token_id).flatten()
320    }
321
322    fn safe_transfer_from(
323        &mut self,
324        from: Address,
325        to: Address,
326        token_id: U256,
327        data: Option<Bytes>
328    ) {
329        self.transfer_from(from, to, token_id);
330        if to.is_contract() {
331            let mut receiver = CEP95ReceiverContractRef::new(self.env(), to);
332            let caller = self.env().caller();
333            let result = receiver.on_cep95_received(&caller, &from, &token_id, &data);
334            if !result {
335                self.env().revert(Error::TransferFailed);
336            }
337        }
338    }
339
340    fn transfer_from(&mut self, from: Address, to: Address, token_id: U256) {
341        self.assert_exists(&token_id);
342
343        let caller = self.env().caller();
344        let owner = self
345            .owner_of(token_id)
346            .unwrap_or_revert_with(self, Error::ValueNotSet);
347        // Only the owner or an approved spender can transfer the token.
348        if (owner != from || owner != caller) && !self.is_approved_for_all(from, caller) {
349            if let Some(approved) = self.approved_for(token_id) {
350                if approved != caller {
351                    self.env().revert(Error::NotAnOwnerOrApproved);
352                }
353            } else {
354                self.env().revert(Error::NotAnOwnerOrApproved);
355            }
356        }
357
358        self.raw_transfer_from(from, to, token_id);
359    }
360
361    fn approve(&mut self, spender: Address, token_id: U256) {
362        let caller = self.env().caller();
363        self.set_approve(token_id, Some(spender));
364        self.env().emit_event(Approval {
365            owner: caller,
366            spender,
367            token_id
368        });
369    }
370
371    fn revoke_approval(&mut self, token_id: U256) {
372        let spender = self
373            .approved_for(token_id)
374            .unwrap_or_revert_with(self, Error::ValueNotSet);
375        self.set_approve(token_id, None);
376        self.env().emit_event(RevokeApproval {
377            owner: self.env().caller(),
378            spender,
379            token_id
380        });
381    }
382
383    fn approved_for(&self, token_id: U256) -> Option<Address> {
384        self.assert_exists(&token_id);
385        self.approvals.get(&token_id).flatten()
386    }
387
388    fn approve_for_all(&mut self, operator: Address) {
389        let caller = self.env().caller();
390        self.set_approval_for_all(caller, operator, true);
391        self.env().emit_event(ApprovalForAll {
392            owner: caller,
393            operator
394        });
395    }
396
397    fn revoke_approval_for_all(&mut self, operator: Address) {
398        let caller = self.env().caller();
399        self.set_approval_for_all(caller, operator, false);
400        self.env().emit_event(RevokeApprovalForAll {
401            owner: caller,
402            operator
403        });
404    }
405
406    fn is_approved_for_all(&self, owner: Address, operator: Address) -> bool {
407        self.operators.get_or_default(&owner, &operator)
408    }
409
410    fn token_metadata(&self, token_id: U256) -> Vec<(String, String)> {
411        self.assert_exists(&token_id);
412        self.metadata
413            .get(&token_id)
414            .unwrap_or_default()
415            .into_iter()
416            .collect()
417    }
418}
419
420impl Cep95 {
421    /// Initializes the module with a name and symbol.
422    pub fn init(&mut self, name: String, symbol: String) {
423        self.name.set(name);
424        self.symbol.set(symbol);
425    }
426
427    #[inline]
428    /// Asserts that the token ID exists.
429    /// Reverts with `Error::InvalidTokenId` if it does not.
430    pub fn assert_exists(&self, token_id: &U256) {
431        if !self.exists(token_id) {
432            self.env().revert(Error::InvalidTokenId);
433        }
434    }
435
436    /// Checks if the token ID exists.
437    #[inline]
438    pub fn exists(&self, token_id: &U256) -> bool {
439        self.owners.get(token_id).flatten().is_some()
440    }
441
442    /// Clears the approval for a specific token ID.
443    #[inline]
444    pub fn clear_approval(&mut self, token_id: &U256) {
445        if self.approvals.get(token_id).is_some() {
446            self.approvals.set(token_id, None);
447        }
448    }
449
450    /// Mints a new NFT and assigns it to the specified address.
451    pub fn mint(&mut self, to: Address, token_id: U256, metadata: Vec<(String, String)>) {
452        if self.exists(&token_id) {
453            self.env().revert(Error::TokenAlreadyExists);
454        }
455
456        self.balances.set(&to, self.balance_of(to) + 1);
457        self.owners.set(&token_id, Some(to));
458        self.set_metadata(token_id, metadata);
459
460        self.env().emit_event(Mint { to, token_id });
461    }
462
463    /// Burns an NFT, removing it from the owner's balance and the contract.
464    pub fn burn(&mut self, token_id: U256) {
465        self.assert_exists(&token_id);
466        let owner = self
467            .owner_of(token_id)
468            .unwrap_or_revert_with(self, Error::ValueNotSet);
469
470        self.clear_approval(&token_id);
471        self.balances.set(&owner, self.balance_of(owner) - 1);
472        self.owners.set(&token_id, None);
473        self.metadata.set(&token_id, Default::default());
474
475        self.env().emit_event(Burn {
476            from: owner,
477            token_id
478        });
479    }
480
481    /// Sets metadata for a specific token ID.
482    /// Replaces any existing metadata.
483    pub fn set_metadata(&mut self, token_id: U256, metadata: Vec<(String, String)>) {
484        self.raw_update_metadata(token_id, metadata, BTreeMap::new());
485    }
486
487    /// Updates metadata for a specific token ID.
488    /// If a key already exists, its value will be updated.
489    /// If a key does not exist, it will be added.
490    /// The remaining keys will be preserved.
491    pub fn update_metadata(&mut self, token_id: U256, metadata: Vec<(String, String)>) {
492        let current_metadata = self.metadata.get(&token_id).unwrap_or_default();
493        self.raw_update_metadata(token_id, metadata, current_metadata);
494    }
495
496    /// Transfers an NFT from one address to another without checking the recipient contract.
497    pub fn raw_transfer_from(&mut self, from: Address, to: Address, token_id: U256) {
498        self.clear_approval(&token_id);
499        self.balances.set(&from, self.balance_of(from) - 1);
500        self.balances.set(&to, self.balance_of(to) + 1);
501        self.owners.set(&token_id, Some(to));
502
503        self.env().emit_event(Transfer { from, to, token_id });
504    }
505
506    fn raw_update_metadata(
507        &mut self,
508        token_id: U256,
509        new_metadata: Vec<(String, String)>,
510        mut current_metadata: BTreeMap<String, String>
511    ) {
512        self.assert_exists(&token_id);
513        for (k, v) in new_metadata {
514            current_metadata.insert(k, v);
515        }
516        self.metadata.set(&token_id, current_metadata);
517        self.env().emit_event(MetadataUpdate { token_id });
518    }
519
520    #[inline]
521    fn set_approve(&mut self, token_id: U256, spender: Option<Address>) {
522        let owner = self
523            .owner_of(token_id)
524            .unwrap_or_revert_with(self, Error::ValueNotSet);
525        let caller = self.env().caller();
526
527        if Some(owner) == spender {
528            self.env().revert(Error::ApprovalToCurrentOwner);
529        }
530
531        if caller != owner && !self.is_approved_for_all(owner, caller) {
532            self.env().revert(Error::NotAnOwnerOrApproved);
533        }
534
535        self.approvals.set(&token_id, spender);
536    }
537
538    #[inline]
539    fn set_approval_for_all(&mut self, caller: Address, operator: Address, approved: bool) {
540        if caller == operator {
541            self.env().revert(Error::ApproveToCaller)
542        }
543
544        self.operators.set(&caller, &operator, approved);
545    }
546}
547
548mod utils {
549    #![allow(dead_code)]
550    use super::*;
551
552    #[odra::module]
553    pub(crate) struct BasicCep95 {
554        token: SubModule<Cep95>
555    }
556
557    #[odra::module]
558    impl BasicCep95 {
559        /// Initializes the contract with the given parameters.
560        pub fn init(&mut self, name: String, symbol: String) {
561            self.token.init(name, symbol);
562        }
563
564        delegate! {
565            to self.token {
566                fn name(&self) -> String;
567                fn symbol(&self) -> String;
568                fn balance_of(&self, owner: Address) -> U256;
569                fn owner_of(&self, token_id: U256) -> Option<Address>;
570                fn safe_transfer_from(&mut self, from: Address, to: Address, token_id: U256, data: Option<Bytes>);
571                fn transfer_from(&mut self, from: Address, to: Address, token_id: U256);
572                fn approve(&mut self, spender: Address, token_id: U256);
573                fn revoke_approval(&mut self, token_id: U256);
574                fn approved_for(&self, token_id: U256) -> Option<Address>;
575                fn approve_for_all(&mut self, operator: Address);
576                fn revoke_approval_for_all(&mut self, operator: Address);
577                fn is_approved_for_all(&self, owner: Address, operator: Address) -> bool;
578                fn token_metadata(&self, token_id: U256) -> Vec<(String, String)>;
579            }
580        }
581
582        pub fn mint(&mut self, to: Address, token_id: U256, metadata: Vec<(String, String)>) {
583            self.token.mint(to, token_id, metadata);
584        }
585
586        pub fn burn(&mut self, token_id: U256) {
587            self.token.burn(token_id);
588        }
589
590        pub fn set_metadata(&mut self, token_id: U256, metadata: Vec<(String, String)>) {
591            self.token.set_metadata(token_id, metadata);
592        }
593
594        pub fn update_metadata(&mut self, token_id: U256, metadata: Vec<(String, String)>) {
595            self.token.update_metadata(token_id, metadata);
596        }
597    }
598
599    #[odra::module]
600    pub(crate) struct NFTReceiver {
601        #[allow(clippy::type_complexity)]
602        last_call_data: Var<((Address, Address), (U256, Option<Bytes>))>
603    }
604
605    #[odra::module]
606    impl NFTReceiver {
607        #[allow(dead_code)]
608        pub fn on_cep95_received(
609            &mut self,
610            operator: Address,
611            from: Address,
612            token_id: U256,
613            data: Option<Bytes>
614        ) -> bool {
615            self.last_call_data
616                .set(((operator, from), (token_id, data.clone())));
617            true
618        }
619
620        #[allow(dead_code)]
621        pub fn last_call_result(&self) -> ((Address, Address), (U256, Option<Bytes>)) {
622            self.last_call_data.get().unwrap_or_revert(self)
623        }
624    }
625
626    #[odra::module]
627    pub(crate) struct RejectingNFTReceiver;
628
629    #[odra::module]
630    impl RejectingNFTReceiver {
631        #[allow(dead_code)]
632        pub fn on_cep95_received(
633            &mut self,
634            operator: Address,
635            from: Address,
636            token_id: U256,
637            data: Option<Bytes>
638        ) -> bool {
639            false
640        }
641    }
642
643    #[odra::module]
644    pub(crate) struct BasicContract;
645
646    #[odra::module]
647    impl BasicContract {}
648}
649
650#[cfg(test)]
651mod tests {
652    use super::*;
653    use crate::cep95::utils::*;
654    use odra::{
655        host::{Deployer, HostEnv, NoArgs},
656        Addressable, VmError
657    };
658    use odra_test;
659
660    fn setup() -> (HostEnv, BasicCep95HostRef) {
661        let env = odra_test::env();
662        let cep95 = BasicCep95::try_deploy(
663            &env,
664            BasicCep95InitArgs {
665                name: "TestToken".to_string(),
666                symbol: "TT".to_string()
667            }
668        )
669        .unwrap();
670        (env, cep95)
671    }
672
673    #[test]
674    fn test_deploy() {
675        let env = odra_test::env();
676        let cep95 = BasicCep95::try_deploy(
677            &env,
678            BasicCep95InitArgs {
679                name: "TestToken".to_string(),
680                symbol: "TT".to_string()
681            }
682        );
683        assert!(cep95.is_ok());
684    }
685
686    #[test]
687    fn test_name() {
688        let (env, cep95) = setup();
689        let name = cep95.name();
690        assert_eq!(name, "TestToken");
691    }
692
693    #[test]
694    fn test_symbol() {
695        let (env, cep95) = setup();
696        let symbol = cep95.symbol();
697        assert_eq!(symbol, "TT");
698    }
699
700    #[test]
701    fn test_mint() {
702        let (env, mut cep95) = setup();
703        let owner = env.caller();
704
705        let token_id = U256::from(1);
706        let metadata = vec![("key".to_string(), "value".to_string())];
707        cep95.mint(owner, token_id, metadata);
708
709        assert_eq!(cep95.balance_of(owner), U256::from(1));
710        assert_eq!(cep95.owner_of(token_id), Some(owner));
711        assert_eq!(
712            cep95.token_metadata(token_id),
713            vec![("key".to_string(), "value".to_string())]
714        );
715        assert!(env.emitted(cep95.address(), "Mint"));
716    }
717
718    #[test]
719    fn test_minting_existing_token() {
720        let (env, mut cep95) = setup();
721        let owner = env.caller();
722
723        let token_id = U256::from(1);
724        let metadata = vec![("key".to_string(), "value".to_string())];
725        cep95.mint(owner, token_id, metadata.clone());
726
727        let result = cep95.try_mint(owner, token_id, metadata);
728        assert_eq!(result, Err(Error::TokenAlreadyExists.into()));
729    }
730
731    #[test]
732    fn test_mint_many_tokens() {
733        let (env, mut cep95) = setup();
734        let owner = env.caller();
735
736        let token_id1 = U256::from(1);
737        let metadata1 = vec![("key1".to_string(), "value1".to_string())];
738        cep95.mint(owner, token_id1, metadata1);
739
740        let token_id2 = U256::from(2);
741        let metadata2 = vec![("key2".to_string(), "value2".to_string())];
742        cep95.mint(owner, token_id2, metadata2);
743
744        assert_eq!(cep95.balance_of(owner), U256::from(2));
745        assert_eq!(cep95.owner_of(token_id1), Some(owner));
746        assert_eq!(cep95.owner_of(token_id2), Some(owner));
747    }
748
749    #[test]
750    fn test_burn() {
751        let (env, mut cep95) = setup();
752        let owner = env.caller();
753
754        let token_id = U256::from(1);
755        let metadata = vec![("key".to_string(), "value".to_string())];
756        cep95.mint(owner, token_id, metadata);
757        cep95.burn(token_id);
758
759        assert_eq!(cep95.balance_of(owner), U256::from(0));
760        assert_eq!(cep95.owner_of(token_id), None);
761        assert_eq!(
762            cep95.try_token_metadata(token_id),
763            Err(Error::InvalidTokenId.into())
764        );
765        assert!(env.emitted(cep95.address(), "Burn"));
766    }
767
768    #[test]
769    fn test_burn_non_existing_token() {
770        let (env, mut cep95) = setup();
771        let owner = env.caller();
772
773        let token_id = U256::from(1);
774        let result = cep95.try_burn(token_id);
775        assert_eq!(result, Err(Error::InvalidTokenId.into()));
776    }
777
778    #[test]
779    fn test_safe_transfer_to_receiver() {
780        let (env, mut cep95) = setup();
781        let nft_receiver = NFTReceiver::deploy(&env, NoArgs);
782        let recipient = *nft_receiver.address();
783        let owner = env.caller();
784
785        let token_id = U256::from(1);
786        let metadata = vec![("key".to_string(), "value".to_string())];
787        cep95.mint(owner, token_id, metadata);
788
789        cep95.safe_transfer_from(owner, recipient, token_id, None);
790
791        assert_eq!(cep95.balance_of(owner), U256::from(0));
792        assert_eq!(cep95.balance_of(recipient), U256::from(1));
793        assert_eq!(
794            nft_receiver.last_call_result(),
795            ((owner, owner), (token_id, None))
796        );
797    }
798
799    #[test]
800    fn test_safe_transfer_to_non_receiver() {
801        let (env, mut cep95) = setup();
802        let contract = BasicContract::deploy(&env, NoArgs);
803        let recipient = *contract.address();
804        let owner = env.caller();
805
806        let token_id = U256::from(1);
807        let metadata = vec![("key".to_string(), "value".to_string())];
808        cep95.mint(owner, token_id, metadata);
809
810        let result = cep95.try_safe_transfer_from(owner, recipient, token_id, None);
811        assert_eq!(
812            result,
813            Err(OdraError::VmError(VmError::NoSuchMethod(
814                "on_cep95_received".to_string()
815            )))
816        );
817
818        assert_eq!(cep95.balance_of(owner), U256::from(1));
819        assert_eq!(cep95.balance_of(recipient), U256::from(0));
820    }
821
822    #[test]
823    fn test_safe_transfer_to_rejecting_receiver() {
824        let (env, mut cep95) = setup();
825
826        let owner = env.get_account(0);
827        let contract = RejectingNFTReceiver::deploy(&env, NoArgs);
828        let recipient = *contract.address();
829
830        let token_id = U256::from(1);
831        let metadata = vec![("key".to_string(), "value".to_string())];
832        cep95.mint(owner, token_id, metadata);
833
834        let result = cep95.try_safe_transfer_from(owner, recipient, token_id, None);
835        assert_eq!(result, Err(Error::TransferFailed.into()));
836
837        assert_eq!(cep95.balance_of(owner), U256::from(1));
838        assert_eq!(cep95.balance_of(recipient), U256::from(0));
839    }
840
841    #[test]
842    fn test_transfer() {
843        let (env, mut cep95) = setup();
844
845        let owner = env.get_account(0);
846        let recipient = env.get_account(10);
847
848        let token_id = U256::from(1);
849        let metadata = vec![("key".to_string(), "value".to_string())];
850        cep95.mint(owner, token_id, metadata);
851        cep95.transfer_from(owner, recipient, token_id);
852
853        assert_eq!(cep95.balance_of(owner), U256::from(0));
854        assert_eq!(cep95.balance_of(recipient), U256::from(1));
855        assert!(env.emitted(cep95.address(), "Transfer"));
856    }
857
858    #[test]
859    fn test_transfer_non_existing_token() {
860        let (env, mut cep95) = setup();
861
862        let owner = env.get_account(0);
863        let recipient = env.get_account(10);
864
865        let token_id = U256::from(1);
866        let metadata = vec![("key".to_string(), "value".to_string())];
867        cep95.mint(owner, token_id, metadata);
868
869        let result = cep95.try_transfer_from(owner, recipient, U256::from(2));
870        assert_eq!(result, Err(Error::InvalidTokenId.into()));
871    }
872
873    #[test]
874    fn test_transfer_from_non_owner() {
875        let (env, mut cep95) = setup();
876
877        let recipient = env.get_account(10);
878        let non_owner = env.get_account(11);
879        let owner = env.get_account(0);
880
881        let token_id = U256::from(1);
882        let metadata = vec![("key".to_string(), "value".to_string())];
883        cep95.mint(owner, token_id, metadata);
884
885        let result = cep95.try_transfer_from(non_owner, recipient, token_id);
886        assert_eq!(result, Err(Error::NotAnOwnerOrApproved.into()));
887        assert_eq!(cep95.balance_of(owner), U256::from(1));
888        assert_eq!(cep95.balance_of(recipient), U256::from(0));
889    }
890
891    #[test]
892    fn test_approve() {
893        let (env, mut cep95) = setup();
894        let owner = env.caller();
895
896        let token_id = U256::from(1);
897        let metadata = vec![("key".to_string(), "value".to_string())];
898        cep95.mint(owner, token_id, metadata);
899
900        let spender = env.get_account(10);
901        cep95.approve(spender, token_id);
902
903        assert_eq!(cep95.approved_for(token_id), Some(spender));
904        assert!(env.emitted_event(
905            &cep95,
906            &Approval {
907                owner,
908                spender,
909                token_id
910            }
911        ));
912    }
913
914    #[test]
915    fn test_approve_by_non_owner() {
916        let (env, mut cep95) = setup();
917        let owner = env.get_account(0);
918        let non_owner = env.get_account(11);
919
920        let token_id = U256::from(1);
921        let metadata = vec![("key".to_string(), "value".to_string())];
922        cep95.mint(owner, token_id, metadata);
923
924        let spender = env.get_account(10);
925        env.set_caller(non_owner);
926        let result = cep95.try_approve(spender, token_id);
927        assert_eq!(result, Err(Error::NotAnOwnerOrApproved.into()));
928    }
929
930    #[test]
931    fn test_transfer_by_approved() {
932        let (env, mut cep95) = setup();
933        let owner = env.get_account(0);
934        let recipient = env.get_account(10);
935
936        let token_id = U256::from(1);
937        let metadata = vec![("key".to_string(), "value".to_string())];
938        cep95.mint(owner, token_id, metadata);
939
940        let spender = env.get_account(11);
941        cep95.approve(spender, token_id);
942        env.set_caller(spender);
943        cep95.transfer_from(owner, recipient, token_id);
944
945        assert_eq!(cep95.balance_of(owner), U256::from(0));
946        assert_eq!(cep95.balance_of(recipient), U256::from(1));
947        assert!(env.emitted_event(
948            &cep95,
949            &Transfer {
950                from: owner,
951                to: recipient,
952                token_id
953            }
954        ));
955    }
956
957    #[test]
958    fn test_approve_for_all() {
959        let (env, mut cep95) = setup();
960        let owner = env.caller();
961        let operator = env.get_account(10);
962
963        cep95.approve_for_all(operator);
964
965        assert!(cep95.is_approved_for_all(owner, operator));
966        assert!(env.emitted(cep95.address(), "ApprovalForAll"));
967    }
968
969    #[test]
970    fn test_revoke_approval_for_all() {
971        let (env, mut cep95) = setup();
972        let owner = env.caller();
973        let operator = env.get_account(10);
974
975        cep95.approve_for_all(operator);
976        cep95.revoke_approval_for_all(operator);
977
978        assert!(!cep95.is_approved_for_all(owner, operator));
979        assert!(env.emitted(cep95.address(), "RevokeApprovalForAll"));
980    }
981
982    #[test]
983    fn test_approve_for_all_self() {
984        let (env, mut cep95) = setup();
985        let owner = env.caller();
986
987        let result = cep95.try_approve_for_all(owner);
988        assert_eq!(result, Err(Error::ApproveToCaller.into()));
989    }
990
991    #[test]
992    fn test_revoke_approval() {
993        let (env, mut cep95) = setup();
994        let owner = env.caller();
995
996        let token_id = U256::from(1);
997        let metadata = vec![("key".to_string(), "value".to_string())];
998        cep95.mint(owner, token_id, metadata);
999
1000        let spender = env.get_account(10);
1001        cep95.approve(spender, token_id);
1002        cep95.revoke_approval(token_id);
1003
1004        assert_eq!(cep95.approved_for(token_id), None);
1005        assert!(env.emitted(cep95.address(), "RevokeApproval"));
1006    }
1007
1008    #[test]
1009    fn revoke_non_existing_approval() {
1010        let (env, mut cep95) = setup();
1011        let owner = env.caller();
1012
1013        let token_id = U256::from(1);
1014        let metadata = vec![("key".to_string(), "value".to_string())];
1015        cep95.mint(owner, token_id, metadata);
1016
1017        let result = cep95.try_revoke_approval(token_id);
1018        assert_eq!(result, Err(Error::ValueNotSet.into()));
1019    }
1020
1021    #[test]
1022    fn test_revoke_approval_by_non_owner() {
1023        let (env, mut cep95) = setup();
1024        let owner = env.get_account(0);
1025        let non_owner = env.get_account(11);
1026
1027        let token_id = U256::from(1);
1028        let metadata = vec![("key".to_string(), "value".to_string())];
1029        cep95.mint(owner, token_id, metadata);
1030
1031        let spender = env.get_account(10);
1032        cep95.approve(spender, token_id);
1033        env.set_caller(non_owner);
1034        let result = cep95.try_revoke_approval(token_id);
1035        assert_eq!(result, Err(Error::NotAnOwnerOrApproved.into()));
1036    }
1037
1038    #[test]
1039    fn test_metadata() {
1040        let (env, mut cep95) = setup();
1041        let owner = env.caller();
1042
1043        let token_id = U256::from(1);
1044        let metadata = vec![("key".to_string(), "value".to_string())];
1045        cep95.mint(owner, token_id, metadata);
1046
1047        assert_eq!(
1048            cep95.token_metadata(token_id),
1049            vec![("key".to_string(), "value".to_string())]
1050        );
1051    }
1052
1053    #[test]
1054    fn test_transfer_by_operator() {
1055        let (env, mut cep95) = setup();
1056        let owner = env.caller();
1057        let recipient = env.get_account(10);
1058        let operator = env.get_account(11);
1059
1060        let token_id1 = U256::from(1);
1061        let metadata = vec![("key".to_string(), "value".to_string())];
1062        cep95.mint(owner, token_id1, metadata.clone());
1063
1064        cep95.approve_for_all(operator);
1065        let token_id2 = U256::from(2);
1066        cep95.mint(owner, token_id2, metadata);
1067
1068        env.set_caller(operator);
1069        cep95.transfer_from(owner, recipient, token_id1);
1070        cep95.transfer_from(owner, recipient, token_id2);
1071
1072        assert_eq!(cep95.balance_of(owner), U256::from(0));
1073        assert_eq!(cep95.balance_of(recipient), U256::from(2));
1074    }
1075
1076    #[test]
1077    fn test_update_metadata() {
1078        let (env, mut cep95) = setup();
1079        let owner = env.caller();
1080
1081        let token_id = U256::from(1);
1082        let metadata = vec![
1083            ("age".to_string(), "30".to_string()),
1084            ("name".to_string(), "Alice".to_string()),
1085        ];
1086        cep95.mint(owner, token_id, metadata);
1087
1088        let new_metadata = vec![("name".to_string(), "Bob".to_string())];
1089        cep95.update_metadata(token_id, new_metadata);
1090
1091        assert_eq!(
1092            cep95.token_metadata(token_id),
1093            vec![
1094                ("age".to_string(), "30".to_string()),
1095                ("name".to_string(), "Bob".to_string()),
1096            ]
1097        );
1098        assert!(env.emitted(cep95.address(), "MetadataUpdate"));
1099    }
1100
1101    #[test]
1102    fn test_set_metadata() {
1103        let (env, mut cep95) = setup();
1104        let owner = env.caller();
1105
1106        let token_id = U256::from(1);
1107        let metadata = vec![
1108            ("age".to_string(), "30".to_string()),
1109            ("name".to_string(), "Alice".to_string()),
1110        ];
1111        cep95.mint(owner, token_id, metadata);
1112
1113        let new_metadata = vec![("name".to_string(), "Bob".to_string())];
1114        cep95.set_metadata(token_id, new_metadata);
1115
1116        assert_eq!(
1117            cep95.token_metadata(token_id),
1118            vec![("name".to_string(), "Bob".to_string())]
1119        );
1120        assert!(env.emitted(cep95.address(), "MetadataUpdate"));
1121    }
1122}