1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//! Erc1155 standard implementation.
use odra::types::{Address, Bytes, U256};

pub mod erc1155_base;
pub mod extensions;
pub mod owned_erc1155;

/// The ERC-1155 interface as defined in the standard.
pub trait Erc1155 {
    /// Returns the amount of tokens of token type `id` owned by `owner`.
    fn balance_of(&self, owner: &Address, id: &U256) -> U256;
    ///  Batched version of [Erc1155::balance_of](Self::balance_of).
    ///
    /// The length of `owners` and `ids` must be the same.
    fn balance_of_batch(&self, owners: &[Address], ids: &[U256]) -> Vec<U256>;
    /// Allows or denials the `operator` to transfer the caller’s tokens.
    ///
    /// Emits [crate::erc1155::events::ApprovalForAll].
    fn set_approval_for_all(&mut self, operator: &Address, approved: bool);
    /// Checks if the `operator` is approved to transfer `owner`'s tokens.
    fn is_approved_for_all(&self, owner: &Address, operator: &Address) -> bool;
    /// Transfers amount tokens of token type id from `from` to `to`.
    ///
    /// Emits [TransferSingle](crate::erc1155::events::TransferSingle).
    ///
    /// If `to` refers to a smart contract, it must implement [Erc1155Receiver::on_erc1155_received](crate::erc1155_receiver::Erc1155Receiver::on_erc1155_received).
    fn safe_transfer_from(
        &mut self,
        from: &Address,
        to: &Address,
        id: &U256,
        amount: &U256,
        data: &Option<Bytes>
    );
    /// Batched version of [Erc1155::safe_transfer_from](Self::safe_transfer_from).
    ///
    /// Emits [TransferBatch](crate::erc1155::events::TransferBatch).
    ///
    /// If `to` refers to a smart contract, it must implement [Erc1155Receiver::on_erc1155_batch_received](crate::erc1155_receiver::Erc1155Receiver::on_erc1155_batch_received).
    fn safe_batch_transfer_from(
        &mut self,
        from: &Address,
        to: &Address,
        ids: &[U256],
        amounts: &[U256],
        data: &Option<Bytes>
    );
}

/// Erc1155-related Odra events.
pub mod events {
    use odra::types::{Address, U256};

    /// Emitted when a single Erc1155 transfer is performed.
    #[derive(odra::Event, PartialEq, Eq, Debug, Clone)]
    pub struct TransferSingle {
        pub operator: Option<Address>,
        pub from: Option<Address>,
        pub to: Option<Address>,
        pub id: U256,
        pub value: U256
    }

    /// Emitted when a batched Erc1155 transfer is performed.
    #[derive(odra::Event, PartialEq, Eq, Debug, Clone)]
    pub struct TransferBatch {
        pub operator: Option<Address>,
        pub from: Option<Address>,
        pub to: Option<Address>,
        pub ids: Vec<U256>,
        pub values: Vec<U256>
    }

    /// Emitted when the `owner` approves or revokes the `operator`.
    #[derive(odra::Event, PartialEq, Eq, Debug, Clone)]
    pub struct ApprovalForAll {
        pub owner: Address,
        pub operator: Address,
        pub approved: bool
    }
}

/// Erc1155-related Odra errors.
pub mod errors {
    use odra::execution_error;

    execution_error! {
        /// Possible errors in the context of Erc1155.
        pub enum Error {
            /// Collections of addresses and token ids have different length.
            AccountsAndIdsLengthMismatch => 30_000,
            /// The owner cannot approve himself.
            ApprovalForSelf => 30_001,
            /// The operator is not allowed to perform the action.
            NotAnOwnerOrApproved => 30_002,
            /// Insufficient token amount to perform a transaction.
            InsufficientBalance => 30_003,
            /// Token transfer finished with an error.
            TransferRejected => 30_004,
            /// Collections of token ids and amounts have different length.
            IdsAndAmountsLengthMismatch => 30_005,
        }
    }
}