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
//! Erc721 metadata.

use crate::erc721::extensions::erc721_metadata::errors::Error;
use odra::{prelude::string::String, UnwrapOrRevert, Variable};

/// The ERC721 Metadata interface as defined in the standard.
pub trait Erc721Metadata {
    /// Returns the token collection name.
    fn name(&self) -> String;
    /// Returns the token collection symbol.
    fn symbol(&self) -> String;
    /// Returns the base URI for the token collection.
    fn base_uri(&self) -> String;
}

#[odra::module]
pub struct Erc721MetadataExtension {
    name: Variable<String>,
    symbol: Variable<String>,
    base_uri: Variable<String>
}

impl Erc721Metadata for Erc721MetadataExtension {
    fn name(&self) -> String {
        self.name.get().unwrap_or_revert_with(Error::NameNotSet)
    }

    fn symbol(&self) -> String {
        self.symbol.get().unwrap_or_revert_with(Error::SymbolNotSet)
    }

    fn base_uri(&self) -> String {
        self.base_uri
            .get()
            .unwrap_or_revert_with(Error::BaseUriNotSet)
    }
}

impl Erc721MetadataExtension {
    pub fn init(&mut self, name: String, symbol: String, base_uri: String) {
        self.name.set(name);
        self.symbol.set(symbol);
        self.base_uri.set(base_uri);
    }
}

pub mod errors {
    use odra::execution_error;

    execution_error! {
        pub enum Error {
            NameNotSet => 31_000,
            SymbolNotSet => 31_001,
            BaseUriNotSet => 31_002,
        }
    }
}