forked from Liquifact/Liquifact-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.rs
More file actions
226 lines (208 loc) · 6.99 KB
/
Copy pathtests.rs
File metadata and controls
226 lines (208 loc) · 6.99 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
#![allow(
unused_imports,
unused_variables,
dead_code,
unused_comparisons,
unused_doc_comments,
unused_macros,
unused_assignments,
clippy::needless_borrow,
clippy::len_zero,
clippy::explicit_counter_loop,
clippy::empty_line_after_doc_comments,
clippy::empty_line_after_outer_attr,
clippy::absurd_extreme_comparisons,
clippy::needless_range_loop,
clippy::mutable_key_type,
clippy::unusual_byte_groupings
)]
#[allow(unused_imports)]
use super::{
AttestationDigestAppended, AttestationDigestRevoked, AttestationDigestUnrevoked,
CollateralRecordedEvt, ContractUpgraded, DataKey, DeprecatedTransferAdminUsed, EscrowError,
EscrowFunded, EscrowInitialized, EscrowUnfunded, FundingCancelled, FundingTargetUpdated,
InvestorRefundedEvt, LiquifactEscrow, LiquifactEscrowClient, MaturityMaxHorizonUpdated,
MaxUniqueInvestorsCapLowered, PrimaryAttestationBound, RegistryRefRebound, TreasuryDustSwept,
YieldTier, MAX_ATTESTATION_APPEND_BATCH, MAX_ATTESTATION_APPEND_ENTRIES, MAX_DUST_SWEEP_AMOUNT,
MAX_FUND_BATCH, SCHEMA_VERSION,
};
use soroban_sdk::{
symbol_short,
testutils::{Address as _, Events, Ledger as _},
token::{StellarAssetClient, TokenClient},
Address, Env, Error, Event, InvokeError, String, Val, Vec as SorobanVec,
};
use std::fmt::Debug;
pub use soroban_sdk::Symbol;
pub(crate) fn assert_contract_error<T, E>(
result: Result<Result<T, E>, Result<Error, InvokeError>>,
expected: EscrowError,
) where
T: Debug,
E: Debug,
{
let expected_code = expected as u32;
match result {
Err(Ok(error)) => {
assert_eq!(error, Error::from_contract_error(expected_code));
}
Err(Err(InvokeError::Contract(code))) => {
assert_eq!(code, expected_code);
}
other => panic!("expected ContractError({expected_code}), got {other:?}"),
}
}
// Focused test tree for escrow behavior. Shared helpers live here so feature
// modules stay assertion-focused and each test still owns a fresh Env.
mod admin;
mod attestations;
mod auth_matrix;
mod cap_validation;
mod collateral_config_view;
mod collateral_limit_setter;
#[rustfmt::skip]
mod coverage;
mod external_calls;
mod external_calls_mocked;
mod funding;
mod init;
mod integration;
mod integration_status_guards;
mod legal_hold;
mod migration_errors;
mod paginated_views;
mod pause;
mod properties;
mod reconciliation_lifecycle;
mod settlement;
mod settlement_limit;
/// Registers a new escrow contract instance and returns its contract id.
pub fn deploy_id(env: &Env) -> Address {
env.register(LiquifactEscrow, ())
}
pub fn deploy(env: &Env) -> LiquifactEscrowClient<'_> {
let id = deploy_id(env);
LiquifactEscrowClient::new(env, &id)
}
#[allow(dead_code)]
pub fn deploy_with_id(env: &Env) -> (Address, LiquifactEscrowClient<'_>) {
let id = deploy_id(env);
let client = LiquifactEscrowClient::new(env, &id);
(id, client)
}
pub fn setup(env: &Env) -> (LiquifactEscrowClient<'_>, Address, Address) {
let mut ledger_info = env.ledger().get();
ledger_info.timestamp = 0;
ledger_info.sequence_number = 100;
env.ledger().set(ledger_info);
env.mock_all_auths();
let client = deploy(env);
let admin = Address::generate(env);
let sme = Address::generate(env);
(client, admin, sme)
}
pub fn free_addresses(env: &Env) -> (Address, Address) {
(Address::generate(env), Address::generate(env))
}
pub struct StellarTestToken<'a> {
/// Contract id for the standard Stellar asset token.
pub id: Address,
/// SEP-41 interface (the same interface the escrow uses in `external_calls`).
pub token: TokenClient<'a>,
/// Test-only admin client used for minting balances into accounts/contracts.
pub stellar: StellarAssetClient<'a>,
}
/// Install a **standard** Stellar asset token contract (Soroban StellarAsset contract v2).
///
/// This is intentionally used for tests that require "well-behaved" SEP-41 semantics:
/// - No fee-on-transfer / rebasing / callback side-effects.
/// - `balance` deltas match transfer amounts (as asserted by `external_calls` wrappers).
///
/// **Out of scope:** non-standard/malicious token economics; see `escrow/src/external_calls.rs`
/// and `docs/ESCROW_TOKEN_INTEGRATION_CHECKLIST.md`.
pub fn install_stellar_asset_token<'a>(env: &'a Env) -> StellarTestToken<'a> {
let sac = env.register_stellar_asset_contract_v2(Address::generate(env));
let id = sac.address();
StellarTestToken {
id: id.clone(),
token: TokenClient::new(env, &id),
stellar: StellarAssetClient::new(env, &id),
}
}
#[allow(dead_code)]
pub fn default_init(client: &LiquifactEscrowClient<'_>, env: &Env, admin: &Address, sme: &Address) {
let (token, treasury) = free_addresses(env);
client.init(
admin,
&soroban_sdk::String::from_str(env, "INV001"),
sme,
&100_000_000_000i128,
&800i64,
&0u64,
&token,
&None,
&treasury,
&None,
&None,
&None,
&None,
&None,
&None, // No funding deadline,
&None,
&None,
&None::<i64>,
);
}
#[allow(dead_code)]
pub const TARGET: i128 = 100_000_000_000i128;
/// Create a **new** escrow contract backed by a real Stellar asset contract (SAC),
/// initialise it with a funded target, fund it to exactly `target`, and mint `target`
/// tokens into the escrow contract address so that `withdraw()` can actually transfer
/// them.
///
/// Returns `(client, escrow_id, sme, token_client)`. The caller must have called
/// `env.mock_all_auths()` (or equivalent) before invoking this helper.
#[allow(dead_code)]
pub fn init_and_fund_with_real_token<'a>(
env: &'a Env,
target: i128,
invoice_id: &str,
) -> (LiquifactEscrowClient<'a>, Address, Address) {
let sac = env.register_stellar_asset_contract_v2(Address::generate(env));
let token_id = sac.address();
let sac_admin = StellarAssetClient::new(env, &token_id);
let escrow_id = env.register(LiquifactEscrow, ());
let client = LiquifactEscrowClient::new(env, &escrow_id);
let admin = Address::generate(env);
let sme = Address::generate(env);
let treasury = Address::generate(env);
client.init(
&admin,
&soroban_sdk::String::from_str(env, invoice_id),
&sme,
&target,
&800i64,
&0u64,
&token_id,
&None,
&treasury,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
let investor = Address::generate(env);
// The investor must actually hold the principal so the pre-transfer balance
// guard in `fund` passes and tokens really move into the escrow.
sac_admin.mint(&investor, &target);
client.fund(&investor, &target);
// Mint the coupon headroom into the escrow (on top of the principal already
// transferred in by `fund`) so withdraw() can transfer principal + yield.
sac_admin.mint(&escrow_id, &target);
(client, escrow_id, sme)
}