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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use crate::blueprints::account::*;
use crate::blueprints::component::*;
use crate::blueprints::macros::*;
use crate::blueprints::resource::*;
use radix_common::data::manifest::model::*;
use radix_common::prelude::*;

define_type_marker!(Some(LOCKER_PACKAGE), AccountLocker);

pub const ACCOUNT_LOCKER_BLUEPRINT: &str = "AccountLocker";

//===========
// Functions
//===========

define_invocation! {
    blueprint_name: AccountLocker,
    function_name: instantiate,
    input: struct {
        owner_role: OwnerRole,
        storer_role: AccessRule,
        storer_updater_role: AccessRule,
        recoverer_role: AccessRule,
        recoverer_updater_role: AccessRule,
        address_reservation: Option<GlobalAddressReservation>
    },
    output: type Global<AccountLockerMarker>,
    manifest_input: struct {
        owner_role: OwnerRole,
        storer_role: AccessRule,
        storer_updater_role: AccessRule,
        recoverer_role: AccessRule,
        recoverer_updater_role: AccessRule,
        address_reservation: Option<ManifestAddressReservation>
    }
}

define_invocation! {
    blueprint_name: AccountLocker,
    function_name: instantiate_simple,
    input: struct {
        allow_recover: bool
    },
    output: type (Global<AccountLockerMarker>, Bucket),
    manifest_input: struct {
        allow_recover: bool
    }
}

//================
// Storer Methods
//================

define_invocation! {
    blueprint_name: AccountLocker,
    function_name: store,
    input: struct {
        claimant: Global<AccountMarker>,
        bucket: Bucket,
        try_direct_send: bool
    },
    output: type (),
    manifest_input: struct {
        claimant: ComponentAddress,
        bucket: ManifestBucket,
        try_direct_send: bool
    }
}

define_invocation! {
    blueprint_name: AccountLocker,
    function_name: airdrop,
    input: struct {
        claimants: IndexMap<Global<AccountMarker>, ResourceSpecifier>,
        bucket: Bucket,
        try_direct_send: bool
    },
    output: type Option<Bucket>,
    manifest_input: struct {
        claimants: IndexMap<ComponentAddress, ResourceSpecifier>,
        bucket: ManifestBucket,
        try_direct_send: bool
    }
}

//===================
// Recoverer Methods
//===================

define_invocation! {
    blueprint_name: AccountLocker,
    function_name: recover,
    input: struct {
        claimant: Global<AccountMarker>,
        resource_address: ResourceAddress,
        amount: Decimal
    },
    output: type Bucket,
    manifest_input: struct {
        claimant: ComponentAddress,
        resource_address: ResourceAddress,
        amount: Decimal
    }
}

define_invocation! {
    blueprint_name: AccountLocker,
    function_name: recover_non_fungibles,
    input: struct {
        claimant: Global<AccountMarker>,
        resource_address: ResourceAddress,
        ids: IndexSet<NonFungibleLocalId>
    },
    output: type Bucket,
    manifest_input: struct {
        claimant: ComponentAddress,
        resource_address: ResourceAddress,
        ids: IndexSet<NonFungibleLocalId>
    }
}

//=====================
// Public User Methods
//=====================

define_invocation! {
    blueprint_name: AccountLocker,
    function_name: claim,
    input: struct {
        claimant: Global<AccountMarker>,
        resource_address: ResourceAddress,
        amount: Decimal
    },
    output: type Bucket,
    manifest_input: struct {
        claimant: ComponentAddress,
        resource_address: ResourceAddress,
        amount: Decimal
    }
}

define_invocation! {
    blueprint_name: AccountLocker,
    function_name: claim_non_fungibles,
    input: struct {
        claimant: Global<AccountMarker>,
        resource_address: ResourceAddress,
        ids: IndexSet<NonFungibleLocalId>
    },
    output: type Bucket,
    manifest_input: struct {
        claimant: ComponentAddress,
        resource_address: ResourceAddress,
        ids: IndexSet<NonFungibleLocalId>
    }
}

//================
// Getter Methods
//================

define_invocation! {
    blueprint_name: AccountLocker,
    function_name: get_amount,
    input: struct {
        claimant: Global<AccountMarker>,
        resource_address: ResourceAddress,
    },
    output: type Decimal,
    manifest_input: struct {
        claimant: ComponentAddress,
        resource_address: ResourceAddress,
    }
}

define_invocation! {
    blueprint_name: AccountLocker,
    function_name: get_non_fungible_local_ids,
    input: struct {
        claimant: Global<AccountMarker>,
        resource_address: ResourceAddress,
        limit: u32
    },
    output: type IndexSet<NonFungibleLocalId>,
    manifest_input: struct {
        claimant: ComponentAddress,
        resource_address: ResourceAddress,
        limit: u32
    }
}

//==================
// Additional Types
//==================

#[derive(Clone, Debug, ScryptoSbor, ManifestSbor, PartialEq, Eq)]
pub enum ResourceSpecifier {
    Fungible(Decimal),
    NonFungible(IndexSet<NonFungibleLocalId>),
}

impl ResourceSpecifier {
    pub fn new_empty(resource_address: ResourceAddress) -> Self {
        if resource_address.is_fungible() {
            Self::Fungible(Default::default())
        } else {
            Self::NonFungible(Default::default())
        }
    }

    pub fn checked_add(&self, other: &Self) -> Option<Self> {
        match (self, other) {
            (ResourceSpecifier::Fungible(amount1), ResourceSpecifier::Fungible(amount2)) => amount1
                .checked_add(*amount2)
                .map(ResourceSpecifier::Fungible),
            (ResourceSpecifier::NonFungible(ids1), ResourceSpecifier::NonFungible(ids2)) => Some(
                ResourceSpecifier::NonFungible(ids1.clone().union(ids2).cloned().collect()),
            ),
            (ResourceSpecifier::Fungible(_), ResourceSpecifier::NonFungible(_))
            | (ResourceSpecifier::NonFungible(_), ResourceSpecifier::Fungible(_)) => None,
        }
    }

    pub fn checked_sub(&self, other: &Self) -> Option<Self> {
        match (self, other) {
            (ResourceSpecifier::Fungible(amount1), ResourceSpecifier::Fungible(amount2)) => {
                // Ensure that amount 2 is smaller than or equal to amount 1
                if amount2 <= amount1 {
                    amount1.checked_sub(*amount2).map(Self::Fungible)
                } else {
                    None
                }
            }
            (ResourceSpecifier::NonFungible(ids1), ResourceSpecifier::NonFungible(ids2)) => {
                // Ensure that ids2 is a subset of ids1
                if ids2.is_subset(ids1) {
                    Some(Self::NonFungible(
                        ids1.clone().difference(ids2).cloned().collect(),
                    ))
                } else {
                    None
                }
            }
            (ResourceSpecifier::Fungible(_), ResourceSpecifier::NonFungible(_))
            | (ResourceSpecifier::NonFungible(_), ResourceSpecifier::Fungible(_)) => None,
        }
    }
}