forked from SwiftChainn/SwiftChain-SmartContract
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
281 lines (256 loc) · 8.62 KB
/
Copy pathlib.rs
File metadata and controls
281 lines (256 loc) · 8.62 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
#![no_std]
use soroban_sdk::{contract, contractimpl, contracttype, contracterror, Env, Symbol, Address, panic_with_error};
use shared_types::DeliveryStatus;
#[contracttype]
#[derive(Clone)]
enum DataKey {
Admin,
PlatformFeeBps,
Amount,
}
#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[repr(u32)]
pub enum EscrowError {
InvalidState = 1,
}
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FeeUpdated {
pub old_fee: u32,
pub new_fee: u32,
}
#[contract]
pub struct EscrowContract;
#[contractimpl]
impl EscrowContract {
/// Initialize the escrow with an admin and amount
pub fn init(env: Env, admin: Address, amount: i128) {
if env.storage().instance().has(&DataKey::Admin) {
panic!("Already initialized");
}
env.storage().instance().set(&DataKey::Admin, &admin);
env.storage().instance().set(&DataKey::Amount, &amount);
env.storage().instance().set(&DataKey::PlatformFeeBps, &0u32);
}
/// Update the platform fee in basis points (max 1000 = 10%)
pub fn update_platform_fee(env: Env, admin: Address, new_fee_bps: u32) {
// 1. Verify against stored admin
let stored_admin: Address = env.storage().instance().get(&DataKey::Admin).expect("Not initialized");
if admin != stored_admin {
panic!("Unauthorized");
}
// 2. Require authentication
admin.require_auth();
// 3. Validate fee <= 1000 bps
if new_fee_bps > 1000 {
panic_with_error!(&env, EscrowError::InvalidState);
}
// 4. Update storage and emit event
let old_fee: u32 = env.storage().instance().get(&DataKey::PlatformFeeBps).unwrap_or(0);
env.storage().instance().set(&DataKey::PlatformFeeBps, &new_fee_bps);
env.events().publish(
(Symbol::new(&env, "FeeUpdated"),),
FeeUpdated { old_fee, new_fee: new_fee_bps }
);
}
/// Get current platform fee in basis points
pub fn get_platform_fee(env: Env) -> u32 {
env.storage().instance().get(&DataKey::PlatformFeeBps).unwrap_or(0)
}
/// Retrieve the delivery status for the escrow
pub fn get_status(_env: Env) -> DeliveryStatus {
DeliveryStatus::Created
}
pub fn get_admin(env: Env) -> Address {
env.storage()
.instance()
.get(&Symbol::new(&env, "admin"))
.unwrap()
}
pub fn get_amount(env: Env) -> i128 {
env.storage()
.persistent()
.get(&Symbol::new(&env, "amount"))
.unwrap()
}
pub fn propose_admin(env: Env, current_admin: Address, new_admin: Address) {
current_admin.require_auth();
let stored_admin: Address = env
.storage()
.instance()
.get(&Symbol::new(&env, "admin"))
.unwrap();
if stored_admin != current_admin {
panic!("caller is not the admin");
}
env.storage()
.instance()
.set(&Symbol::new(&env, "pending_admin"), &new_admin);
env.storage().instance().extend_ttl(
constants::ESCROW_TTL_THRESHOLD,
constants::ESCROW_TTL_EXTEND_TO,
);
}
pub fn accept_admin(env: Env, new_admin: Address) {
new_admin.require_auth();
let pending: Address = env
.storage()
.instance()
.get(&Symbol::new(&env, "pending_admin"))
.unwrap();
if pending != new_admin {
panic!("caller is not the pending admin");
}
let old_admin: Address = env
.storage()
.instance()
.get(&Symbol::new(&env, "admin"))
.unwrap();
env.storage()
.instance()
.set(&Symbol::new(&env, "admin"), &new_admin);
env.storage()
.instance()
.remove(&Symbol::new(&env, "pending_admin"));
env.storage().instance().extend_ttl(
constants::ESCROW_TTL_THRESHOLD,
constants::ESCROW_TTL_EXTEND_TO,
);
env.events().publish(
(Symbol::new(&env, "AdminTransferred"),),
(old_admin, new_admin),
);
}
// ── Escrow lifecycle ──────────────────────────────────────────────────────
pub fn create_escrow(
env: Env,
sender: Address,
driver: Address,
delivery_id: u64,
token: Address,
amount: i128,
) {
sender.require_auth();
if env
.storage()
.persistent()
.has(&DataKey::Escrow(delivery_id))
{
panic!("escrow already exists for this delivery_id");
}
token::Client::new(&env, &token).transfer(
&sender,
&env.current_contract_address(),
&amount,
);
save_escrow(
&env,
delivery_id,
&EscrowRecord {
sender: sender.clone(),
driver,
token,
amount,
status: EscrowStatus::Pending,
},
);
env.events().publish(
(events::escrow_funded(), delivery_id),
(sender, amount),
);
}
pub fn release_escrow(env: Env, caller: Address, delivery_id: u64) {
caller.require_auth();
require_admin(&env, &caller);
let mut record = load_escrow(&env, delivery_id);
if record.status != EscrowStatus::Pending {
panic!("escrow is not in pending state");
}
token::Client::new(&env, &record.token).transfer(
&env.current_contract_address(),
&record.driver,
&record.amount,
);
record.status = EscrowStatus::Released;
save_escrow(&env, delivery_id, &record);
env.events().publish(
(events::escrow_released(), delivery_id),
(record.driver, record.amount, 0i128),
);
}
pub fn refund_escrow(env: Env, caller: Address, delivery_id: u64) {
caller.require_auth();
require_admin(&env, &caller);
let mut record = load_escrow(&env, delivery_id);
if record.status != EscrowStatus::Pending {
panic!("escrow is not in pending state");
}
token::Client::new(&env, &record.token).transfer(
&env.current_contract_address(),
&record.sender,
&record.amount,
);
record.status = EscrowStatus::Refunded;
save_escrow(&env, delivery_id, &record);
env.events().publish(
(events::escrow_refunded(), delivery_id),
(record.sender, record.amount),
);
}
pub fn raise_dispute(env: Env, caller: Address, delivery_id: u64) {
caller.require_auth();
let mut record = load_escrow(&env, delivery_id);
if caller != record.sender {
panic!("only the escrow sender can raise a dispute");
}
if record.status != EscrowStatus::Pending {
panic!("escrow is not in pending state");
}
record.status = EscrowStatus::Disputed;
save_escrow(&env, delivery_id, &record);
let timestamp = env.ledger().timestamp();
env.events().publish(
(events::delivery_disputed(), delivery_id),
(caller, timestamp),
);
}
pub fn resolve_dispute(
env: Env,
caller: Address,
delivery_id: u64,
release_to_driver: bool,
) {
caller.require_auth();
require_admin(&env, &caller);
let mut record = load_escrow(&env, delivery_id);
if record.status != EscrowStatus::Disputed {
panic!("escrow is not in disputed state");
}
if release_to_driver {
token::Client::new(&env, &record.token).transfer(
&env.current_contract_address(),
&record.driver,
&record.amount,
);
record.status = EscrowStatus::Released;
} else {
token::Client::new(&env, &record.token).transfer(
&env.current_contract_address(),
&record.sender,
&record.amount,
);
record.status = EscrowStatus::Refunded;
}
save_escrow(&env, delivery_id, &record);
env.events().publish(
(events::dispute_resolved(), delivery_id),
(release_to_driver, caller),
);
}
pub fn get_escrow_record(env: Env, delivery_id: u64) -> EscrowRecord {
load_escrow(&env, delivery_id)
}
}
#[cfg(test)]
mod test;