-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathlib.rs
More file actions
374 lines (323 loc) · 11.3 KB
/
Copy pathlib.rs
File metadata and controls
374 lines (323 loc) · 11.3 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
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
#![no_std]
mod events;
#[cfg(test)]
mod test;
#[cfg(test)]
mod upgrade_batch_test;
use bc_forge_admin as admin;
use soroban_sdk::{
contract, contractclient, contracterror, contractimpl, contracttype, Address, BytesN, Env,
String, Vec,
};
/// Minimal SEP-41 client so this crate does not link `bc-forge-token`'s WASM
/// exports (those collide with this contract's upgrade-governance entry points).
#[contractclient(name = "TokenClient")]
pub trait TokenInterface {
fn balance(id: Address) -> i128;
fn transfer(from: Address, to: Address, amount: i128);
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[contracttype]
pub struct Recipient {
pub to: Address,
pub amount: i128,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[contracttype]
pub enum InvoiceStatus {
Pending,
PartiallyReleased,
FullyReleased,
Failed,
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[contracttype]
pub struct FailedPayout {
pub invoice_id: u64,
pub recipient: Address,
pub amount: i128,
pub retry_count: u32,
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[contracttype]
pub struct Invoice {
pub invoice_id: u64,
pub total_amount: i128,
pub released_amount: i128,
pub status: InvoiceStatus,
pub recipients: Vec<Recipient>,
pub created_at: u32,
}
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[contracterror]
#[repr(u32)]
pub enum SplitError {
InvoiceNotFound = 1,
InvalidRecipient = 2,
InsufficientBalance = 3,
InvoiceAlreadyCompleted = 4,
FailedPayoutNotFound = 5,
}
#[contract]
pub struct SplitContract;
impl SplitContract {
fn ensure_invoice_exists(env: &Env, invoice_id: u64) -> Result<(), SplitError> {
let key = DataKey::Invoice(invoice_id);
if !env.storage().persistent().has(&key) {
Err(SplitError::InvoiceNotFound)
} else {
Ok(())
}
}
fn read_invoice(env: &Env, invoice_id: u64) -> Result<Invoice, SplitError> {
let key = DataKey::Invoice(invoice_id);
env.storage()
.persistent()
.get(&key)
.ok_or(SplitError::InvoiceNotFound)
}
fn write_invoice(env: &Env, invoice: &Invoice) {
let key = DataKey::Invoice(invoice.invoice_id);
env.storage().persistent().set(&key, invoice);
}
fn read_failed_payout(env: &Env, invoice_id: u64, recipient: &Address) -> Option<FailedPayout> {
let key = DataKey::FailedPayout(invoice_id, recipient.clone());
env.storage().persistent().get(&key)
}
fn write_failed_payout(env: &Env, failed_payout: &FailedPayout) {
let key = DataKey::FailedPayout(failed_payout.invoice_id, failed_payout.recipient.clone());
env.storage().persistent().set(&key, failed_payout);
}
fn remove_failed_payout(env: &Env, invoice_id: u64, recipient: &Address) {
let key = DataKey::FailedPayout(invoice_id, recipient.clone());
env.storage().persistent().remove(&key);
}
fn try_transfer(
env: &Env,
token_id: &Address,
from: &Address,
to: &Address,
amount: i128,
) -> bool {
let client = TokenClient::new(env, token_id);
let from_balance = client.balance(from);
if from_balance < amount || amount <= 0 {
return false;
}
client.transfer(from, to, &amount);
true
}
fn release_payment_inner(env: &Env, invoice_id: u64) -> Result<(), SplitError> {
let token_address = env
.storage()
.instance()
.get(&DataKey::Token)
.ok_or(SplitError::InsufficientBalance)?;
let mut invoice = Self::read_invoice(env, invoice_id)?;
if invoice.status != InvoiceStatus::Pending {
return Err(SplitError::InvoiceAlreadyCompleted);
}
let mut total_to_release = 0i128;
let mut failed_count = 0;
let mut invoice_updated = false;
let current_contract = env.current_contract_address();
for recipient in &invoice.recipients {
let failed_payout_opt = Self::read_failed_payout(env, invoice_id, &recipient.to);
if let Some(ref failed_payout) = failed_payout_opt {
if failed_payout.retry_count >= 3 {
continue;
}
}
let succeeded = Self::try_transfer(
env,
&token_address,
¤t_contract,
&recipient.to,
recipient.amount,
);
if succeeded {
total_to_release += recipient.amount;
invoice_updated = true;
} else {
failed_count += 1;
let retry_count = failed_payout_opt.map_or(1, |fp| fp.retry_count + 1);
let failed_payout = FailedPayout {
invoice_id,
recipient: recipient.to.clone(),
amount: recipient.amount,
retry_count,
};
Self::write_failed_payout(env, &failed_payout);
events::emit_payout_failed(env, invoice_id, &recipient.to);
}
}
if invoice_updated {
invoice.released_amount = total_to_release;
if failed_count == invoice.recipients.len() {
invoice.status = InvoiceStatus::Failed;
} else if total_to_release == invoice.total_amount {
invoice.status = InvoiceStatus::FullyReleased;
} else {
invoice.status = InvoiceStatus::PartiallyReleased;
}
Self::write_invoice(env, &invoice);
}
Ok(())
}
fn retry_failed_payout_inner(
env: &Env,
invoice_id: u64,
recipient: &Address,
) -> Result<(), SplitError> {
Self::ensure_invoice_exists(env, invoice_id)?;
let mut failed_payout = Self::read_failed_payout(env, invoice_id, recipient)
.ok_or(SplitError::FailedPayoutNotFound)?;
if failed_payout.retry_count >= 3 {
return Err(SplitError::InvoiceAlreadyCompleted);
}
let token_address = env
.storage()
.instance()
.get(&DataKey::Token)
.ok_or(SplitError::InsufficientBalance)?;
let current_contract = env.current_contract_address();
let succeeded = Self::try_transfer(
env,
&token_address,
¤t_contract,
recipient,
failed_payout.amount,
);
if succeeded {
Self::remove_failed_payout(env, invoice_id, recipient);
events::emit_payout_succeeded(env, invoice_id, recipient, failed_payout.amount);
Ok(())
} else {
failed_payout.retry_count += 1;
Self::write_failed_payout(env, &failed_payout);
events::emit_payout_failed(env, invoice_id, recipient);
Err(SplitError::InsufficientBalance)
}
}
}
#[contractimpl]
impl SplitContract {
pub fn create_invoice(
env: Env,
admin_address: Address,
invoice_id: u64,
total_amount: i128,
recipients: Vec<Recipient>,
token_address: Address,
) -> Result<(), SplitError> {
admin::require_super_admin(&env, &admin_address);
if total_amount <= 0 {
return Err(SplitError::InvalidRecipient);
}
let mut recipient_amounts: i128 = 0;
for recipient in &recipients {
if recipient.amount <= 0 {
return Err(SplitError::InvalidRecipient);
}
recipient_amounts = match recipient_amounts.checked_add(recipient.amount) {
Some(total) => total,
None => return Err(SplitError::InvalidRecipient),
};
}
if recipient_amounts != total_amount {
return Err(SplitError::InvalidRecipient);
}
let invoice = Invoice {
invoice_id,
total_amount,
released_amount: 0,
status: InvoiceStatus::Pending,
recipients,
created_at: env.ledger().sequence(),
};
env.storage()
.instance()
.set(&DataKey::Token, &token_address);
admin::set_admin(&env, &admin_address);
Self::write_invoice(&env, &invoice);
events::emit_invoice_created(&env, invoice_id, total_amount);
Ok(())
}
pub fn release_payment(
env: Env,
invoice_id: u64,
admin_address: Address,
) -> Result<(), SplitError> {
admin::require_super_admin(&env, &admin_address);
Self::release_payment_inner(&env, invoice_id)
}
pub fn retry_failed_payout(
env: Env,
invoice_id: u64,
recipient: Address,
admin_address: Address,
) -> Result<(), SplitError> {
admin::require_super_admin(&env, &admin_address);
Self::retry_failed_payout_inner(&env, invoice_id, &recipient)
}
pub fn get_invoice(env: Env, invoice_id: u64) -> Result<Invoice, SplitError> {
Self::read_invoice(&env, invoice_id)
}
pub fn get_failed_payout(
env: Env,
invoice_id: u64,
recipient: Address,
) -> Result<FailedPayout, SplitError> {
let failed_payout_opt = Self::read_failed_payout(&env, invoice_id, &recipient);
failed_payout_opt.ok_or(SplitError::FailedPayoutNotFound)
}
/// Upgrades the split contract WASM (SuperAdmin path).
///
/// @notice Replaces the current contract executable with `new_wasm_hash`.
/// @dev Mirrors the token contract's SuperAdmin-gated upgrade entry point.
pub fn upgrade(
env: Env,
upgrader: Address,
new_wasm_hash: BytesN<32>,
) -> Result<(), SplitError> {
admin::require_super_admin(&env, &upgrader);
events::emit_upgraded(&env, &upgrader, &new_wasm_hash);
env.deployer().update_current_contract_wasm(new_wasm_hash);
Ok(())
}
/// Configures the multi-sig admin pool used to gate WASM upgrades.
pub fn set_admin_pool(env: Env, pool: Vec<Address>, threshold: u32) {
admin::set_admin_pool(&env, pool, threshold);
}
/// Creates a multi-sig governance proposal for an upgrade (or other action).
pub fn create_proposal(env: Env, creator: Address, description: String) -> u64 {
admin::create_proposal(&env, creator, description)
}
/// Approves a multi-sig governance proposal.
pub fn approve_proposal(env: Env, admin_address: Address, proposal_id: u64) {
admin::approve_proposal(&env, admin_address, proposal_id);
}
/// Returns whether a governance proposal has met its approval quorum.
pub fn is_proposal_ready(env: Env, proposal_id: u64) -> bool {
admin::is_proposal_ready(&env, proposal_id)
}
/// Executes a quorum-approved WASM upgrade on this split contract.
///
/// @notice Applies `wasm_hash` after the referenced proposal meets quorum.
/// @dev Delegates to [`admin::execute_upgrade`].
pub fn execute_upgrade(
env: Env,
executor: Address,
proposal_id: u64,
wasm_hash: BytesN<32>,
) -> Result<(), admin::AdminError> {
admin::execute_upgrade(&env, executor, proposal_id, wasm_hash)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[contracttype]
enum DataKey {
Invoice(u64),
Token,
FailedPayout(u64, Address),
}