odra_modules/erc1155/mod.rs
1//! Erc1155 standard implementation.
2use odra::casper_types::{bytesrepr::Bytes, U256};
3use odra::prelude::*;
4
5pub mod erc1155_base;
6pub mod extensions;
7pub mod owned_erc1155;
8
9/// The ERC-1155 interface as defined in the standard.
10pub trait Erc1155 {
11 /// Returns the amount of tokens of token type `id` owned by `owner`.
12 fn balance_of(&self, owner: &Address, id: &U256) -> U256;
13 /// Batched version of [Erc1155::balance_of](Self::balance_of).
14 ///
15 /// The length of `owners` and `ids` must be the same.
16 fn balance_of_batch(&self, owners: Vec<Address>, ids: Vec<U256>) -> Vec<U256>;
17 /// Allows or denials the `operator` to transfer the caller’s tokens.
18 ///
19 /// Emits [crate::erc1155::events::ApprovalForAll].
20 fn set_approval_for_all(&mut self, operator: &Address, approved: bool);
21 /// Checks if the `operator` is approved to transfer `owner`'s tokens.
22 fn is_approved_for_all(&self, owner: &Address, operator: &Address) -> bool;
23 /// Transfers amount tokens of token type id from `from` to `to`.
24 ///
25 /// Emits [TransferSingle](crate::erc1155::events::TransferSingle).
26 ///
27 /// If `to` refers to a smart contract, it must implement [Erc1155Receiver::on_erc1155_received](crate::erc1155_receiver::Erc1155Receiver::on_erc1155_received).
28 fn safe_transfer_from(
29 &mut self,
30 from: &Address,
31 to: &Address,
32 id: &U256,
33 amount: &U256,
34 data: &Option<Bytes>
35 );
36 /// Batched version of [Erc1155::safe_transfer_from](Self::safe_transfer_from).
37 ///
38 /// Emits [TransferBatch](crate::erc1155::events::TransferBatch).
39 ///
40 /// If `to` refers to a smart contract, it must implement [Erc1155Receiver::on_erc1155_batch_received](crate::erc1155_receiver::Erc1155Receiver::on_erc1155_batch_received).
41 fn safe_batch_transfer_from(
42 &mut self,
43 from: &Address,
44 to: &Address,
45 ids: Vec<U256>,
46 amounts: Vec<U256>,
47 data: &Option<Bytes>
48 );
49}
50
51/// Erc1155-related Odra events.
52pub mod events {
53 use odra::casper_types::U256;
54 use odra::prelude::*;
55
56 /// Emitted when a single Erc1155 transfer is performed.
57 #[odra::event]
58 pub struct TransferSingle {
59 /// The operator that called the function.
60 pub operator: Option<Address>,
61 /// The address from which the tokens are transferred.
62 pub from: Option<Address>,
63 /// The address to which the tokens are transferred.
64 pub to: Option<Address>,
65 /// The token id.
66 pub id: U256,
67 /// The token amount.
68 pub value: U256
69 }
70
71 /// Emitted when a batched Erc1155 transfer is performed.
72 #[odra::event]
73 pub struct TransferBatch {
74 /// The operator that called the function.
75 pub operator: Option<Address>,
76 /// The address from which the tokens are transferred.
77 pub from: Option<Address>,
78 /// The address to which the tokens are transferred.
79 pub to: Option<Address>,
80 /// The token ids.
81 pub ids: Vec<U256>,
82 /// The token amounts.
83 pub values: Vec<U256>
84 }
85
86 /// Emitted when the `owner` approves or revokes the `operator`.
87 #[odra::event]
88 pub struct ApprovalForAll {
89 /// The owner of the tokens.
90 pub owner: Address,
91 /// The operator that is approved.
92 pub operator: Address,
93 /// The approval status.
94 pub approved: bool
95 }
96}
97
98/// Erc1155-related Odra errors.
99pub mod errors {
100 use odra::prelude::OdraError;
101
102 /// Possible errors in the context of Erc1155.
103 #[odra::odra_error]
104 pub enum Error {
105 /// Collections of addresses and token ids have different length.
106 AccountsAndIdsLengthMismatch = 30_000,
107 /// The owner cannot approve himself.
108 ApprovalForSelf = 30_001,
109 /// The operator is not allowed to perform the action.
110 NotAnOwnerOrApproved = 30_002,
111 /// Insufficient token amount to perform a transaction.
112 InsufficientBalance = 30_003,
113 /// Token transfer finished with an error.
114 TransferRejected = 30_004,
115 /// Collections of token ids and amounts have different length.
116 IdsAndAmountsLengthMismatch = 30_005
117 }
118}