forked from benelabs/crucible
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.rs
More file actions
192 lines (158 loc) · 5.12 KB
/
Copy pathtest.rs
File metadata and controls
192 lines (158 loc) · 5.12 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
#![cfg(test)]
extern crate std;
use crucible::prelude::*;
use crucible::{assert_emitted, assert_reverts};
use soroban_sdk::{symbol_short, Address};
use crate::{Escrow, EscrowClient, EscrowStatus};
const AMOUNT: i128 = 1_000_000; // 1 USDC (6 decimals)
const BASE_TIME: u64 = 1_000_000;
const LOCK_DURATION: u64 = 86_400; // 1 day in seconds
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
struct Ctx {
pub env: MockEnv,
pub id: Address,
pub depositor: AccountHandle,
pub recipient: AccountHandle,
pub arbiter: AccountHandle,
pub token: MockToken,
}
impl Ctx {
fn setup() -> Self {
let env = MockEnv::builder()
.at_timestamp(BASE_TIME)
.with_contract::<Escrow>()
.with_account("depositor", Stroops::xlm(100))
.with_account("recipient", Stroops::xlm(10))
.with_account("arbiter", Stroops::xlm(10))
.build();
let id = env.contract_id::<Escrow>();
let depositor = env.account("depositor");
let recipient = env.account("recipient");
let arbiter = env.account("arbiter");
let token = MockToken::new(&env, "USDC", 6);
token.mint(&depositor, AMOUNT * 2); // fund depositor generously
Ctx {
env,
id,
depositor,
recipient,
arbiter,
token,
}
}
fn client(&self) -> EscrowClient<'_> {
EscrowClient::new(self.env.inner(), &self.id)
}
/// Convenience: create a live escrow with unlock_time = BASE_TIME + LOCK_DURATION.
fn create_escrow(&self) {
self.env.mock_all_auths();
self.client().create(
&self.depositor,
&self.recipient,
&self.arbiter,
&self.token.address(),
&AMOUNT,
&(BASE_TIME + LOCK_DURATION),
);
}
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[test]
fn test_create_transfers_tokens_to_contract() {
let ctx = Ctx::setup();
ctx.create_escrow();
// Contract should hold the escrowed amount.
assert_eq!(ctx.token.balance(&ctx.id), AMOUNT);
// Depositor balance reduced.
assert_eq!(ctx.token.balance(&ctx.depositor), AMOUNT); // started with AMOUNT*2
}
#[test]
fn test_claim_after_timeout() {
let ctx = Ctx::setup();
ctx.create_escrow();
// Advance past the time lock.
ctx.env.advance_time(Duration::seconds(LOCK_DURATION + 1));
ctx.env.mock_all_auths();
ctx.client().claim();
assert_eq!(ctx.token.balance(&ctx.recipient), AMOUNT);
assert_eq!(ctx.token.balance(&ctx.id), 0);
assert_eq!(ctx.client().get_state().status, EscrowStatus::Claimed);
}
#[test]
fn test_claim_before_timeout_reverts() {
let ctx = Ctx::setup();
ctx.create_escrow();
// Do NOT advance time — time lock still active.
ctx.env.mock_all_auths();
assert_reverts!(ctx.client().claim(), "time lock");
}
#[test]
fn test_arbiter_approve_allows_early_claim() {
let ctx = Ctx::setup();
ctx.create_escrow();
// Arbiter approves without waiting for the time lock.
ctx.env.mock_all_auths();
ctx.client().approve(&ctx.arbiter);
// Recipient claims immediately.
ctx.client().claim();
assert_eq!(ctx.token.balance(&ctx.recipient), AMOUNT);
assert_eq!(ctx.client().get_state().status, EscrowStatus::Claimed);
}
#[test]
fn test_only_arbiter_can_approve() {
let ctx = Ctx::setup();
ctx.create_escrow();
ctx.env.mock_all_auths();
// recipient tries to act as arbiter — logic check, not gated by auth
assert_reverts!(
ctx.client().approve(&ctx.recipient),
"only the arbiter can approve"
);
}
#[test]
fn test_refund_after_timeout() {
let ctx = Ctx::setup();
ctx.create_escrow();
// Advance past the time lock.
ctx.env.advance_time(Duration::seconds(LOCK_DURATION + 1));
ctx.env.mock_all_auths();
ctx.client().refund();
// Depositor gets their tokens back.
assert_eq!(ctx.token.balance(&ctx.depositor), AMOUNT * 2);
assert_eq!(ctx.client().get_state().status, EscrowStatus::Refunded);
}
#[test]
fn test_refund_before_timeout_reverts() {
let ctx = Ctx::setup();
ctx.create_escrow();
ctx.env.mock_all_auths();
assert_reverts!(ctx.client().refund(), "time lock");
}
#[test]
fn test_double_claim_reverts() {
let ctx = Ctx::setup();
ctx.create_escrow();
ctx.env.advance_time(Duration::seconds(LOCK_DURATION + 1));
ctx.env.mock_all_auths();
ctx.client().claim();
// Second claim should revert.
assert_reverts!(ctx.client().claim(), "already settled");
}
#[test]
fn test_create_emits_event() {
let ctx = Ctx::setup();
ctx.env.mock_all_auths();
ctx.client().create(
&ctx.depositor,
&ctx.recipient,
&ctx.arbiter,
&ctx.token.address(),
&AMOUNT,
&(BASE_TIME + LOCK_DURATION),
);
assert_emitted!(ctx.env, ctx.id, (symbol_short!("created"),), AMOUNT);
}