-
Notifications
You must be signed in to change notification settings - Fork 2
/
birthday_bot_completed.move
391 lines (317 loc) · 14.6 KB
/
birthday_bot_completed.move
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
module overmind::birthday_bot {
//==============================================================================================
// Dependencies
//==============================================================================================
use std::table;
use std::signer;
use std::vector;
use std::table::Table;
use aptos_framework::event;
use aptos_framework::account;
use aptos_framework::timestamp;
use aptos_framework::simple_map;
use aptos_framework::coin::{Self, Coin};
use aptos_framework::aptos_coin::AptosCoin;
#[test_only]
use aptos_framework::aptos_coin;
//==============================================================================================
// Constants - DO NOT MODIFY
//==============================================================================================
const SEED: vector<u8> = b"birthday bot";
//==============================================================================================
// Error codes - DO NOT MODIFY
//==============================================================================================
const EDistributionStoreDoesNotExist: u64 = 0;
const EBirthdayGiftDoesNotExist: u64 = 2;
const EBirthdayTimestampHasNotPassed: u64 = 3;
const EInsufficientAptBalance: u64 = 4;
const EGiftAmountZero: u64 = 5;
//==============================================================================================
// Module Structs - DO NOT MODIFY
//==============================================================================================
struct BirthdayGift has store {
birthday_coin: Coin<AptosCoin>,
birthday_timestamp_seconds: u64
}
struct DistributionStore has key {
gifter: address,
birthday_gifts: simple_map::SimpleMap<address, BirthdayGift>
}
struct ReverseGiftLookUp has key {
look_up: Table<address, vector<address>>
}
struct ModuleEvents has key {
birthday_gift_added_events: event::EventHandle<BirthdayGiftAddedEvent>,
birthday_gift_removed_events: event::EventHandle<BirthdayGiftRemovedEvent>,
birthday_gift_claimed_events: event::EventHandle<BirthdayGiftClaimedEvent>
}
//==============================================================================================
// Event structs - DO NOT MODIFY
//==============================================================================================
struct BirthdayGiftAddedEvent has store, drop {
gifter: address,
recipient: address,
gift_amount_apt: u64,
birthday_timestamp_seconds: u64,
event_creation_timestamp_seconds: u64
}
struct BirthdayGiftRemovedEvent has store, drop {
gifter: address,
recipient: address,
gift_amount_apt: u64,
birthday_timestamp_seconds: u64,
event_creation_timestamp_seconds: u64
}
struct BirthdayGiftClaimedEvent has store, drop {
gifter: address,
recipient: address,
gift_amount_apt: u64,
birthday_timestamp_seconds: u64,
event_creation_timestamp_seconds: u64
}
//==============================================================================================
// Functions
//==============================================================================================
fun init_module(admin: &signer) {
let (resource_account_signer, _signer_cap) = account::create_resource_account(admin, SEED);
coin::register<AptosCoin>(&resource_account_signer);
move_to<ReverseGiftLookUp>(
&resource_account_signer,
ReverseGiftLookUp {
look_up: table::new()
}
);
move_to<ModuleEvents>(
&resource_account_signer,
ModuleEvents {
birthday_gift_added_events: account::new_event_handle(&resource_account_signer),
birthday_gift_removed_events: account::new_event_handle(&resource_account_signer),
birthday_gift_claimed_events: account::new_event_handle(&resource_account_signer)
}
);
}
public entry fun add_birthday_gift(
gifter: &signer,
recipient: address,
gift_amount_apt: u64,
birthday_timestamp_seconds: u64
) acquires DistributionStore, ReverseGiftLookUp, ModuleEvents {
let gifter_address = signer::address_of(gifter);
if (!exists<DistributionStore>(gifter_address))
{
move_to(gifter, DistributionStore {
gifter: gifter_address,
birthday_gifts: simple_map::new(),
});
};
assert_sufficient_apt_balance(gifter_address, gift_amount_apt);
let distribution_store = borrow_global_mut<DistributionStore>(gifter_address);
if (simple_map::contains_key(&mut distribution_store.birthday_gifts, &recipient))
{
let birthday_coin = coin::withdraw<AptosCoin>(gifter, gift_amount_apt);
let birthday_gift =
simple_map::borrow_mut(&mut distribution_store.birthday_gifts, &recipient);
coin::merge(&mut birthday_gift.birthday_coin, birthday_coin);
* &mut birthday_gift.birthday_timestamp_seconds = birthday_timestamp_seconds;
} else
{
assert_gift_amount_above_zero(gift_amount_apt);
let birthday_coin = coin::withdraw<AptosCoin>(gifter, gift_amount_apt);
let new_birthday_gift = BirthdayGift {
birthday_coin,
birthday_timestamp_seconds
};
simple_map::add(&mut distribution_store.birthday_gifts, recipient, new_birthday_gift);
let reverse_gift_look_up = borrow_global_mut<ReverseGiftLookUp>(get_resource_address());
if (table::contains(&reverse_gift_look_up.look_up, recipient))
{
let gifters = table::borrow_mut(&mut reverse_gift_look_up.look_up, recipient);
vector::push_back(gifters, gifter_address);
} else
{
let gifters = vector::empty();
vector::push_back(&mut gifters, gifter_address);
table::add(&mut reverse_gift_look_up.look_up, recipient, gifters);
};
};
let module_events = borrow_global_mut<ModuleEvents>(get_resource_address());
event::emit_event(
&mut module_events.birthday_gift_added_events,
BirthdayGiftAddedEvent {
gifter: gifter_address,
recipient,
gift_amount_apt,
birthday_timestamp_seconds,
event_creation_timestamp_seconds: timestamp::now_seconds()
}
);
}
public entry fun remove_birthday_gift(
gifter: &signer,
recipient: address,
) acquires DistributionStore, ReverseGiftLookUp, ModuleEvents {
let gifter_address = signer::address_of(gifter);
assert_distribution_store_exists(gifter_address);
let distribution_store = borrow_global_mut<DistributionStore>(gifter_address);
if (simple_map::contains_key(&mut distribution_store.birthday_gifts, &recipient)) {
let (
_,
BirthdayGift {
birthday_coin,
birthday_timestamp_seconds
}
) = simple_map::remove(&mut distribution_store.birthday_gifts, &recipient);
let gift_amount_apt = coin::value<AptosCoin>(&birthday_coin);
coin::deposit<AptosCoin>(gifter_address, birthday_coin);
let reverse_gift_look_up = borrow_global_mut<ReverseGiftLookUp>(get_resource_address());
let gifters = table::borrow_mut(&mut reverse_gift_look_up.look_up, recipient);
let (_, gifter_index) = vector::index_of(gifters, &gifter_address);
vector::remove(gifters, gifter_index);
let module_events = borrow_global_mut<ModuleEvents>(get_resource_address());
event::emit_event(
&mut module_events.birthday_gift_removed_events,
BirthdayGiftRemovedEvent {
gifter: gifter_address,
recipient,
gift_amount_apt,
birthday_timestamp_seconds,
event_creation_timestamp_seconds: timestamp::now_seconds()
}
);
};
}
public entry fun claim_birthday_gift(
recipient: &signer,
gifter: address
) acquires DistributionStore, ReverseGiftLookUp, ModuleEvents {
let recipient_address = signer::address_of(recipient);
assert_distribution_store_exists(gifter);
assert_birthday_gift_exists(gifter, recipient_address);
assert_birthday_timestamp_seconds_has_passed(gifter, recipient_address);
let distribution_store = borrow_global_mut<DistributionStore>(gifter);
let (
_,
BirthdayGift {
birthday_coin,
birthday_timestamp_seconds
}
) = simple_map::remove(&mut distribution_store.birthday_gifts, &recipient_address);
let reverse_gift_look_up = borrow_global_mut<ReverseGiftLookUp>(get_resource_address());
let gifters = table::borrow_mut(&mut reverse_gift_look_up.look_up, signer::address_of(recipient));
let (_, gifter_index) = vector::index_of(gifters, &gifter);
vector::remove(gifters, gifter_index);
let gift_amount_apt = coin::value<AptosCoin>(&birthday_coin);
coin::deposit<AptosCoin>(recipient_address, birthday_coin);
let module_events = borrow_global_mut<ModuleEvents>(get_resource_address());
event::emit_event(
&mut module_events.birthday_gift_claimed_events,
BirthdayGiftClaimedEvent {
recipient: recipient_address,
gifter,
gift_amount_apt,
birthday_timestamp_seconds,
event_creation_timestamp_seconds: timestamp::now_seconds()
}
);
}
#[view]
public fun view_gifters_gifts(gifter: address): (vector<address>, vector<u64>, vector<u64>)
acquires DistributionStore {
if (!exists<DistributionStore>(gifter)) {
return (vector::empty(), vector::empty(), vector::empty())
};
let distribution_store = borrow_global<DistributionStore>(gifter);
let birthday_gifts = &distribution_store.birthday_gifts;
let recipients = simple_map::keys(birthday_gifts);
let birthday_coin_amounts = vector::empty();
let birthday_timestamps_seconds = vector::empty();
let number_gifts = simple_map::length(birthday_gifts);
let i = 0;
while (i < number_gifts) {
let recipient = vector::borrow(&recipients, i);
let birthday_gift = simple_map::borrow(birthday_gifts, recipient);
vector::push_back(
&mut birthday_coin_amounts,
coin::value<AptosCoin>(&birthday_gift.birthday_coin)
);
vector::push_back(
&mut birthday_timestamps_seconds,
birthday_gift.birthday_timestamp_seconds
);
i = i + 1;
};
(recipients, birthday_coin_amounts, birthday_timestamps_seconds)
}
#[view]
public fun view_recipients_gifts(recipient: address): (vector<address>, vector<u64>, vector<u64>) acquires ReverseGiftLookUp, DistributionStore {
let reverse_gift_look_up = borrow_global<ReverseGiftLookUp>(get_resource_address());
if (!table::contains(&reverse_gift_look_up.look_up, recipient)) {
return (vector::empty(), vector::empty(), vector::empty())
};
let gifters = table::borrow(&reverse_gift_look_up.look_up, recipient);
let number_gifts = vector::length(gifters);
let gift_froms = vector::empty();
let gift_amounts = vector::empty();
let gift_timestamps_seconds = vector::empty();
let i = 0;
while (i < number_gifts) {
let gifter = *vector::borrow(gifters, i);
let distribution_store = borrow_global<DistributionStore>(gifter);
let birthday_gift = simple_map::borrow(&distribution_store.birthday_gifts, &recipient);
vector::push_back(
&mut gift_froms,
gifter
);
vector::push_back(
&mut gift_amounts,
coin::value<AptosCoin>(&birthday_gift.birthday_coin)
);
vector::push_back(
&mut gift_timestamps_seconds,
birthday_gift.birthday_timestamp_seconds
);
i = i + 1;
};
(gift_froms, gift_amounts, gift_timestamps_seconds)
}
//==============================================================================================
// Helper functions
//==============================================================================================
fun get_resource_address(): address {
account::create_resource_address(&@overmind, SEED)
}
//==============================================================================================
// Assert functions
//==============================================================================================
inline fun assert_distribution_store_exists(account_address: address) {
assert!(
exists<DistributionStore>(account_address),
EDistributionStoreDoesNotExist
);
}
inline fun assert_birthday_gift_exists(distribution_address: address,address: address)
acquires DistributionStore {
let distribution_store = borrow_global_mut<DistributionStore>(distribution_address);
assert!(
simple_map::contains_key(&mut distribution_store.birthday_gifts, &address),
EBirthdayGiftDoesNotExist
);
}
inline fun assert_birthday_timestamp_seconds_has_passed(
distribution_address: address,
recipient: address
) acquires DistributionStore {
let distribution_store = borrow_global_mut<DistributionStore>(distribution_address);
let birthday_gift = simple_map::borrow(&distribution_store.birthday_gifts, &recipient);
assert!(
timestamp::now_seconds() > birthday_gift.birthday_timestamp_seconds,
EBirthdayTimestampHasNotPassed
);
}
inline fun assert_sufficient_apt_balance(account_address: address, amount: u64) {
assert!(coin::balance<AptosCoin>(account_address) >= amount,EInsufficientAptBalance);
}
inline fun assert_gift_amount_above_zero(amount: u64,) {
assert!(amount > 0, EGiftAmountZero);
}
}