forked from MyFanss/MyFans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
86 lines (70 loc) · 2.17 KB
/
Copy pathlib.rs
File metadata and controls
86 lines (70 loc) · 2.17 KB
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
#![no_std]
use soroban_sdk::{
contract, contracterror, contractimpl, contracttype, panic_with_error, Address, Env, Symbol,
};
#[contracttype]
enum DataKey {
Admin,
Earnings(Address),
}
#[contracterror]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Error {
AlreadyInitialized = 1,
}
#[contract]
pub struct Earnings;
#[contractimpl]
impl Earnings {
pub fn init(env: Env, admin: Address) {
if env.storage().instance().has(&DataKey::Admin) {
panic_with_error!(&env, Error::AlreadyInitialized);
}
admin.require_auth();
env.storage().instance().set(&DataKey::Admin, &admin);
}
pub fn admin(env: Env) -> Address {
env.storage().instance().get(&DataKey::Admin).unwrap()
}
pub fn record(env: Env, creator: Address, amount: i128) {
let admin = Self::admin(env.clone());
admin.require_auth();
let current: i128 = env
.storage()
.instance()
.get(&DataKey::Earnings(creator.clone()))
.unwrap_or(0);
env.storage()
.instance()
.set(&DataKey::Earnings(creator), &(current + amount));
}
pub fn get_earnings(env: Env, creator: Address) -> i128 {
env.storage()
.instance()
.get(&DataKey::Earnings(creator))
.unwrap_or(0)
}
/// Withdraw `amount` from `creator`'s recorded earnings.
///
/// - Creator must authorize.
/// - Panics with "insufficient balance" if amount > recorded earnings.
/// - Emits `withdraw` event: topics `(symbol, creator)`, data `amount`.
pub fn withdraw(env: Env, creator: Address, amount: i128) {
creator.require_auth();
let current: i128 = env
.storage()
.instance()
.get(&DataKey::Earnings(creator.clone()))
.unwrap_or(0);
if amount > current {
panic!("insufficient balance");
}
env.storage()
.instance()
.set(&DataKey::Earnings(creator.clone()), &(current - amount));
env.events()
.publish((Symbol::new(&env, "withdraw"), creator), amount);
}
}
#[cfg(test)]
mod test;