Skip to main content

rootchain_std/
traits.rs

1use alloc::string::String;
2use rootchain_core::types::Address;
3
4pub trait RRC20 {
5    fn name(&self) -> &str;
6    fn symbol(&self) -> &str;
7    fn decimals(&self) -> u8;
8    fn total_supply(&self) -> u64;
9    fn balance_of(&self, account: Address) -> u64;
10    fn transfer(&mut self, to: Address, amount: u64) -> Result<(), crate::error::Error>;
11    fn allowance(&self, owner: Address, spender: Address) -> u64;
12    fn approve(&mut self, spender: Address, amount: u64) -> Result<(), crate::error::Error>;
13    fn transfer_from(
14        &mut self,
15        from: Address,
16        to: Address,
17        amount: u64,
18    ) -> Result<(), crate::error::Error>;
19}
20
21pub trait RRC721 {
22    fn name(&self) -> &str;
23    fn symbol(&self) -> &str;
24    fn balance_of(&self, owner: Address) -> u64;
25    fn owner_of(&self, token_id: u64) -> Address;
26    fn safe_transfer_from(
27        &mut self,
28        from: Address,
29        to: Address,
30        token_id: u64,
31    ) -> Result<(), crate::error::Error>;
32    fn approve(&mut self, to: Address, token_id: u64) -> Result<(), crate::error::Error>;
33    fn get_approved(&self, token_id: u64) -> Address;
34    fn token_uri(&self, token_id: u64) -> String;
35
36    /// On-chain metadata support for small attributes
37    fn get_attribute(&self, token_id: u64, key: &str) -> String;
38}
39
40pub trait Ownable {
41    fn owner(&self) -> Address {
42        let mut addr = [0u8; 32];
43        unsafe { crate::state_get(b"owner".as_ptr(), 5, addr.as_mut_ptr(), 32) };
44        Address(addr)
45    }
46
47    fn transfer_ownership(&mut self, new_owner: Address) -> Result<(), crate::error::Error> {
48        let current_owner = self.owner();
49        let mut caller = [0u8; 32];
50        unsafe { crate::get_caller(caller.as_mut_ptr()) };
51
52        if Address(caller) != current_owner {
53            return Err(crate::error::Error::NotOwner);
54        }
55
56        unsafe { crate::state_set(b"owner".as_ptr(), 5, new_owner.0.as_ptr(), 32) };
57        Ok(())
58    }
59}
60
61pub trait Contract {
62    fn init(&mut self) -> Result<(), crate::error::Error>;
63    fn apply(&mut self) -> Result<(), crate::error::Error>;
64}