forked from Vero-protocol/vero-core-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreasury.rs
More file actions
134 lines (111 loc) · 3.58 KB
/
Copy pathtreasury.rs
File metadata and controls
134 lines (111 loc) · 3.58 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
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
use crate::types::{ContractError, DataKey, WithdrawalRequest};
use soroban_sdk::{Address, Env};
// ~24 hours at Stellar's target of 5 seconds per ledger
pub const TIME_LOCK_LEDGERS: u32 = 17_280;
pub fn request_withdrawal(
env: &Env,
admin: &Address,
recipient: &Address,
amount: i128,
) -> Result<u64, ContractError> {
admin.require_auth();
let stored_admin: Address = env
.storage()
.instance()
.get(&DataKey::Admin)
.ok_or(ContractError::NotInitialized)?;
if admin != &stored_admin {
return Err(ContractError::NotAuthorized);
}
let counter: u64 = env
.storage()
.instance()
.get(&DataKey::WithdrawalCounter)
.unwrap_or(0u64);
let id = counter + 1;
env.storage().instance().set(&DataKey::WithdrawalCounter, &id);
let request = WithdrawalRequest {
id,
recipient: recipient.clone(),
amount,
requested_at_ledger: env.ledger().sequence(),
is_executed: false,
is_cancelled: false,
};
env.storage()
.instance()
.set(&DataKey::WithdrawalRequest(id), &request);
crate::events::emit_withdrawal_requested(env, id, recipient, amount);
Ok(id)
}
pub fn execute_withdrawal(env: &Env, request_id: u64) -> Result<(), ContractError> {
let mut request: WithdrawalRequest = env
.storage()
.instance()
.get(&DataKey::WithdrawalRequest(request_id))
.ok_or(ContractError::WithdrawalNotFound)?;
if request.is_cancelled {
return Err(ContractError::WithdrawalCancelled);
}
if request.is_executed {
return Err(ContractError::WithdrawalAlreadyExecuted);
}
let current_ledger = env.ledger().sequence();
if current_ledger < request.requested_at_ledger.saturating_add(TIME_LOCK_LEDGERS) {
return Err(ContractError::TimeLockActive);
}
let token: Address = env
.storage()
.instance()
.get(&DataKey::TokenAddress)
.ok_or(ContractError::NotInitialized)?;
let token_client = soroban_sdk::token::Client::new(env, &token);
token_client.transfer(
&env.current_contract_address(),
&request.recipient,
&request.amount,
);
request.is_executed = true;
env.storage()
.instance()
.set(&DataKey::WithdrawalRequest(request_id), &request);
crate::events::emit_withdrawal_executed(env, request_id, &request.recipient, request.amount);
Ok(())
}
pub fn cancel_withdrawal(
env: &Env,
admin: &Address,
request_id: u64,
) -> Result<(), ContractError> {
admin.require_auth();
let stored_admin: Address = env
.storage()
.instance()
.get(&DataKey::Admin)
.ok_or(ContractError::NotInitialized)?;
if admin != &stored_admin {
return Err(ContractError::NotAuthorized);
}
let mut request: WithdrawalRequest = env
.storage()
.instance()
.get(&DataKey::WithdrawalRequest(request_id))
.ok_or(ContractError::WithdrawalNotFound)?;
if request.is_executed {
return Err(ContractError::WithdrawalAlreadyExecuted);
}
if request.is_cancelled {
return Err(ContractError::WithdrawalCancelled);
}
request.is_cancelled = true;
env.storage()
.instance()
.set(&DataKey::WithdrawalRequest(request_id), &request);
crate::events::emit_withdrawal_cancelled(env, request_id);
Ok(())
}
pub fn get_withdrawal_request(env: &Env, request_id: u64) -> Option<WithdrawalRequest> {
env.storage()
.instance()
.get(&DataKey::WithdrawalRequest(request_id))
}