versatus_rust/eip721.rs
1//! [eip-721](https://eips.ethereum.org/EIPS/eip-721)
2//!
3//! The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in this document are to be interpreted as described in RFC 2119.
4//!
5//! Every ERC-721 compliant contract must implement the ERC721 and ERC165 interfaces (subject to “caveats” below):
6
7use crate::{eip165::ERC165, versatus_rust::Address};
8use anyhow::Result;
9use ethnum::U256;
10use serde_derive::{Deserialize, Serialize};
11
12/// @title ERC-721 Non-Fungible Token Standard
13/// @dev See https://eips.ethereum.org/EIPS/eip-721
14/// Note: the ERC-165 identifier for this interface is 0x80ac58cd.
15pub trait ERC721: ERC165 {
16 /// @notice Count all NFTs assigned to an owner
17 /// @dev NFTs assigned to the zero address are considered invalid, and this
18 /// function throws for queries about the zero address.
19 /// @param _owner An address for whom to query the balance
20 /// @return The number of NFTs owned by `_owner`, possibly zero
21 fn balance_of(&self, owner: Address) -> Result<U256>;
22
23 /// @notice Find the owner of an NFT
24 /// @dev NFTs assigned to zero address are considered invalid, and queries
25 /// about them do throw.
26 /// @param _tokenId The identifier for an NFT
27 /// @return The address of the owner of the NFT
28 fn owner_of(&self, token_id: U256) -> Result<Address>;
29
30 /// @notice Transfers the ownership of an NFT from one address to another address
31 /// @dev Throws unless `msg.sender` is the current owner, an authorized
32 /// operator, or the approved address for this NFT. Throws if `_from` is
33 /// not the current owner. Throws if `_to` is the zero address. Throws if
34 /// `_tokenId` is not a valid NFT. When transfer is complete, this function
35 /// checks if `_to` is a smart contract (code size > 0). If so, it calls
36 /// `onERC721Received` on `_to` and throws if the return value is not
37 /// `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`.
38 /// @param _from The current owner of the NFT
39 /// @param _to The new owner
40 /// @param _tokenId The NFT to transfer
41 /// @param data Additional data with no specified format, sent in call to `_to`
42 fn safe_transfer_with_data(
43 &self,
44 from: Address,
45 to: Address,
46 token_id: U256,
47 data: &[u8],
48 ) -> Result<()>;
49
50 /// @notice Transfers the ownership of an NFT from one address to another address
51 /// @dev This works identically to the other function with an extra data parameter,
52 /// except this function just sets data to "".
53 /// @param _from The current owner of the NFT
54 /// @param _to The new owner
55 /// @param _tokenId The NFT to transfer
56 fn safe_transfer_from(&self, from: Address, to: Address, token_id: U256) -> Result<()> {
57 self.safe_transfer_with_data(from, to, token_id, &[])
58 }
59
60 /// @notice Transfer ownership of an NFT -- THE CALLER IS RESPONSIBLE
61 /// TO CONFIRM THAT `_to` IS CAPABLE OF RECEIVING NFTS OR ELSE
62 /// THEY MAY BE PERMANENTLY LOST
63 /// @dev Throws unless `msg.sender` is the current owner, an authorized
64 /// operator, or the approved address for this NFT. Throws if `_from` is
65 /// not the current owner. Throws if `_to` is the zero address. Throws if
66 /// `_tokenId` is not a valid NFT.
67 /// @param _from The current owner of the NFT
68 /// @param _to The new owner
69 /// @param _tokenId The NFT to transfer
70 fn transfer_from(&self, from: Address, to: Address, token_id: U256) -> Result<()>;
71
72 /// @notice Change or reaffirm the approved address for an NFT
73 /// @dev The zero address indicates there is no approved address.
74 /// Throws unless `msg.sender` is the current NFT owner, or an authorized
75 /// operator of the current owner.
76 /// @param _approved The new approved NFT controller
77 /// @param _tokenId The NFT to approve
78 fn approve(&self, approved: Address, token_id: U256) -> Result<()>;
79
80 /// @notice Enable or disable approval for a third party ("operator") to manage
81 /// all of `msg.sender`'s assets
82 /// @dev Emits the ApprovalForAll event. The contract MUST allow
83 /// multiple operators per owner.
84 /// @param _operator Address to add to the set of authorized operators
85 /// @param _approved True if the operator is approved, false to revoke approval
86 fn set_approval_for_all(&self, operator: Address, approved: bool) -> Result<()>;
87
88 /// @notice Get the approved address for a single NFT
89 /// @dev Throws if `_tokenId` is not a valid NFT.
90 /// @param _tokenId The NFT to find the approved address for
91 /// @return The approved address for this NFT, or the zero address if there is none
92 fn get_approved(&self, token_id: U256) -> Result<Address>;
93
94 /// @notice Query if an address is an authorized operator for another address
95 /// @param _owner The address that owns the NFTs
96 /// @param _operator The address that acts on behalf of the owner
97 /// @return True if `_operator` is an approved operator for `_owner`, false otherwise
98 fn is_approved_for_all(&self, owner: Address, operator: Address) -> Result<bool>;
99}
100
101#[derive(Debug, Serialize, Deserialize)]
102#[serde(rename_all = "camelCase")]
103pub enum Erc721Event {
104 /// @dev This emits when ownership of any NFT changes by any mechanism.
105 /// This event emits when NFTs are created (`from` == 0) and destroyed
106 /// (`to` == 0). Exception: during contract creation, any number of NFTs
107 /// may be created and assigned without emitting Transfer. At the time of
108 /// any transfer, the approved address for that NFT (if any) is reset to none.
109 Transfer {
110 from: Address,
111 to: Address,
112 token_id: U256,
113 },
114 /// @dev This emits when the approved address for an NFT is changed or
115 /// reaffirmed. The zero address indicates there is no approved address.
116 /// When a Transfer event emits, this also indicates that the approved
117 /// address for that NFT (if any) is reset to none.
118 Approval {
119 owner: Address,
120 approved: Address,
121 token_id: U256,
122 },
123 /// @dev This emits when an operator is enabled or disabled for an owner.
124 /// The operator can manage all NFTs of the owner.
125 ApprovalForAll {
126 owner: Address,
127 operator: Address,
128 approved: bool,
129 },
130}
131
132/// A wallet/broker/auction application MUST implement the wallet interface if it will accept safe transfers.
133///
134/// @dev Note: the ERC-165 identifier for this interface is 0x150b7a02.
135pub trait ERC721TokenReceiver {
136 /// @notice Handle the receipt of an NFT
137 /// @dev The ERC721 smart contract calls this function on the recipient
138 /// after a `transfer`. This function MAY throw to revert and reject the
139 /// transfer. Return of other than the magic value MUST result in the
140 /// transaction being reverted.
141 /// Note: the contract address is always the message sender.
142 /// @param _operator The address which called `safeTransferFrom` function
143 /// @param _from The address which previously owned the token
144 /// @param _tokenId The NFT identifier which is being transferred
145 /// @param _data Additional data with no specified format
146 /// @return `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`
147 /// unless throwing
148 fn on_erc721_received(
149 &self,
150 operator: Address,
151 from: Address,
152 token_id: U256,
153 data: &[u8],
154 ) -> Result<&[u8; 4]>;
155}
156
157/// The metadata extension is OPTIONAL for ERC-721 smart contracts. This allows
158/// your smart contract to be interrogated for its name and for details about the
159/// assets which your NFTs represent.
160///
161/// @title ERC-721 Non-Fungible Token Standard, optional metadata extension
162/// @dev See https://eips.ethereum.org/EIPS/eip-721
163/// Note: the ERC-165 identifier for this interface is 0x5b5e139f.
164pub trait ERC721Metadata: ERC721 {
165 /// @notice A descriptive name for a collection of NFTs in this contract
166 fn name(&self) -> Result<String>;
167
168 /// @notice An abbreviated name for NFTs in this contract
169 fn symbol(&self) -> Result<String>;
170
171 /// @notice A distinct Uniform Resource Identifier (URI) for a given asset.
172 /// @dev Throws if `_tokenId` is not a valid NFT. URIs are defined in RFC
173 /// 3986. The URI may point to a JSON file that conforms to the "ERC721
174 /// Metadata JSON Schema".
175 fn token_uri(&self, token_id: U256) -> Result<String>;
176}