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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use crate::engine::scrypto_env::ScryptoVmV1Api;
use crate::modules::ModuleHandle;
use crate::runtime::*;
use crate::*;
use radix_engine_common::types::RoyaltyAmount;
use radix_engine_interface::api::node_modules::royalty::{
    ComponentClaimRoyaltiesInput, ComponentRoyaltyCreateInput, ComponentRoyaltyLockInput,
    ComponentRoyaltySetInput, COMPONENT_ROYALTY_BLUEPRINT, COMPONENT_ROYALTY_CLAIMER_ROLE,
    COMPONENT_ROYALTY_CLAIMER_UPDATER_ROLE, COMPONENT_ROYALTY_CLAIM_ROYALTIES_IDENT,
    COMPONENT_ROYALTY_CREATE_IDENT, COMPONENT_ROYALTY_LOCKER_ROLE,
    COMPONENT_ROYALTY_LOCKER_UPDATER_ROLE, COMPONENT_ROYALTY_LOCK_ROYALTY_IDENT,
    COMPONENT_ROYALTY_SETTER_ROLE, COMPONENT_ROYALTY_SETTER_UPDATER_ROLE,
    COMPONENT_ROYALTY_SET_ROYALTY_IDENT,
};
use radix_engine_interface::api::AttachedModuleId;
use radix_engine_interface::blueprints::resource::Bucket;
use radix_engine_interface::constants::ROYALTY_MODULE_PACKAGE;
use radix_engine_interface::data::scrypto::{scrypto_decode, scrypto_encode};
use radix_engine_interface::types::ComponentRoyaltyConfig;
use sbor::rust::string::ToString;
use sbor::rust::vec;
use sbor::rust::vec::Vec;
use scrypto::modules::Attachable;

pub trait HasComponentRoyalties {
    fn set_royalty<M: ToString>(&self, method: M, amount: RoyaltyAmount);
    fn lock_royalty<M: ToString>(&self, method: M);
    fn claim_component_royalties(&self) -> Bucket;
}

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct Royalty(pub ModuleHandle);

impl Attachable for Royalty {
    const MODULE_ID: AttachedModuleId = AttachedModuleId::Royalty;

    fn new(handle: ModuleHandle) -> Self {
        Royalty(handle)
    }

    fn handle(&self) -> &ModuleHandle {
        &self.0
    }
}

impl Default for Royalty {
    fn default() -> Self {
        Royalty::new(ComponentRoyaltyConfig::default())
    }
}

impl Royalty {
    pub fn new(royalty_config: ComponentRoyaltyConfig) -> Self {
        let rtn = ScryptoVmV1Api::blueprint_call(
            ROYALTY_MODULE_PACKAGE,
            COMPONENT_ROYALTY_BLUEPRINT,
            COMPONENT_ROYALTY_CREATE_IDENT,
            scrypto_encode(&ComponentRoyaltyCreateInput { royalty_config }).unwrap(),
        );

        let royalty: Own = scrypto_decode(&rtn).unwrap();
        Self(ModuleHandle::Own(royalty))
    }

    pub fn set_royalty<M: ToString>(&self, method: M, amount: RoyaltyAmount) {
        self.call_ignore_rtn(
            COMPONENT_ROYALTY_SET_ROYALTY_IDENT,
            &ComponentRoyaltySetInput {
                method: method.to_string(),
                amount,
            },
        );
    }

    pub fn lock_royalty<M: ToString>(&self, method: M) {
        self.call_ignore_rtn(
            COMPONENT_ROYALTY_LOCK_ROYALTY_IDENT,
            &ComponentRoyaltyLockInput {
                method: method.to_string(),
            },
        );
    }

    pub fn claim_royalties(&self) -> Bucket {
        self.call(
            COMPONENT_ROYALTY_CLAIM_ROYALTIES_IDENT,
            &ComponentClaimRoyaltiesInput {},
        )
    }
}

pub struct RoyaltyRoles<T> {
    pub royalty_setter: T,
    pub royalty_setter_updater: T,
    pub royalty_locker: T,
    pub royalty_locker_updater: T,
    pub royalty_claimer: T,
    pub royalty_claimer_updater: T,
}

impl<T> RoyaltyRoles<T> {
    pub fn list(self) -> Vec<(&'static str, T)> {
        vec![
            (COMPONENT_ROYALTY_SETTER_ROLE, self.royalty_setter),
            (
                COMPONENT_ROYALTY_SETTER_UPDATER_ROLE,
                self.royalty_setter_updater,
            ),
            (COMPONENT_ROYALTY_LOCKER_ROLE, self.royalty_locker),
            (
                COMPONENT_ROYALTY_LOCKER_UPDATER_ROLE,
                self.royalty_locker_updater,
            ),
            (COMPONENT_ROYALTY_CLAIMER_ROLE, self.royalty_claimer),
            (
                COMPONENT_ROYALTY_CLAIMER_UPDATER_ROLE,
                self.royalty_claimer_updater,
            ),
        ]
    }
}