odra_modules/erc721/
mod.rs

1//! Erc721 standard implementation.
2use odra::casper_types::{bytesrepr::Bytes, U256};
3use odra::prelude::*;
4
5pub mod erc721_base;
6pub mod extensions;
7pub mod owned_erc721_with_metadata;
8
9/// The ERC-721 interface as defined in the standard.
10pub trait Erc721 {
11    /// Returns the amount of tokens owned by `owner`.
12    fn balance_of(&self, owner: &Address) -> U256;
13    /// Returns the `owner` of the `token_id` token.
14    ///
15    /// Reverts if token_id does not exist.
16    fn owner_of(&self, token_id: &U256) -> Address;
17    /// Safely transfers `token_id` token from `from` to `to`, checking the recipient contract uses
18    /// [Erc721Receiver](crate::erc721_receiver::Erc721Receiver).
19    ///
20    /// Emits a [Transfer](crate::erc721::events::Transfer) event.
21    fn safe_transfer_from(&mut self, from: &Address, to: &Address, token_id: &U256);
22    /// Safely transfers `token_id` token from `from` to `to`, checking the recipient contract uses
23    /// [Erc721Receiver](crate::erc721_receiver::Erc721Receiver), passes additional data.
24    ///
25    /// Emits a [Transfer](crate::erc721::events::Transfer) event.
26    fn safe_transfer_from_with_data(
27        &mut self,
28        from: &Address,
29        to: &Address,
30        token_id: &U256,
31        data: &Bytes
32    );
33    /// Transfers a specific NFT `tokenId` from one account `from` to another `to`.
34    fn transfer_from(&mut self, from: &Address, to: &Address, token_id: &U256);
35    /// Grants permission to `approved` to transfer `token_id` token. The approval is cleared when the token is transferred.
36    ///
37    /// Only a single account can be approved at a time, so approving None clears the previous approval.
38    fn approve(&mut self, approved: &Option<Address>, token_id: &U256);
39    /// Approves or removes operator for the caller. Operators can call `transfer_from` or `safe_transfer_from` for all
40    /// tokens owned by the caller.
41    ///
42    /// The operator cannot be the caller.
43    ///
44    /// Emits a [ApprovalForAll](crate::erc721::events::ApprovalForAll) event.
45    fn set_approval_for_all(&mut self, operator: &Address, approved: bool);
46    /// Returns the address approved for `token_id` token.
47    ///
48    /// Reverts if token_id does not exist.
49    fn get_approved(&self, token_id: &U256) -> Option<Address>;
50    /// Returns if the `operator` is allowed to manage all of the tokens of `owner`.
51    fn is_approved_for_all(&self, owner: &Address, operator: &Address) -> bool;
52}
53
54/// Erc721-related Odra events.
55pub mod events {
56    use odra::casper_event_standard;
57    use odra::casper_types::U256;
58    use odra::prelude::*;
59
60    /// Emitted when the `token_id` token is transferred (also minted or burned).
61    #[odra::event]
62    pub struct Transfer {
63        /// The address of the sender.
64        pub from: Option<Address>,
65        /// The address of the receiver.
66        pub to: Option<Address>,
67        /// The token id.
68        pub token_id: U256
69    }
70
71    /// Emitted when the `owner` approves `approved` to operate on the `token_id` token.
72    #[odra::event]
73    pub struct Approval {
74        /// The owner of the tokens.
75        pub owner: Address,
76        /// The operator that is approved.
77        pub approved: Option<Address>,
78        /// The token id.
79        pub token_id: U256
80    }
81
82    /// Emitted when the `owner` approves or revokes `operator`.
83    #[odra::event]
84    pub struct ApprovalForAll {
85        /// The owner of the tokens.
86        pub owner: Address,
87        /// The operator that is approved or revoked.
88        pub operator: Address,
89        /// The approval status.
90        pub approved: bool
91    }
92}
93
94/// Erc721-related Odra errors.
95pub mod errors {
96    use odra::prelude::OdraError;
97
98    /// Possible errors in the context of Erc721 token.
99    #[odra::odra_error]
100    pub enum Error {
101        /// Token is invalid in the given context or does not exist.
102        InvalidTokenId = 30_000,
103        /// Address in not eligible to operate on the token.
104        NotAnOwnerOrApproved = 30_001,
105        /// The owner cannot be approved.
106        ApprovalToCurrentOwner = 30_002,
107        /// The caller cannot approve self.
108        ApproveToCaller = 30_003,
109        /// Token transfer ends with an error
110        TransferFailed = 30_004
111    }
112}