@sparkysolutions/agentrail 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/.github/ISSUE_TEMPLATE/drips_wave_task.md +33 -0
  2. package/.github/workflows/auto-merge.yml +119 -0
  3. package/.github/workflows/ci.yml +35 -0
  4. package/BENCHMARKS.md +67 -0
  5. package/CONTRIBUTING.md +40 -0
  6. package/DRIPS_WAVE.md +81 -0
  7. package/FUNDING.json +7 -0
  8. package/ISSUES_PIPELINE.json +106 -0
  9. package/LICENSE +21 -0
  10. package/README.md +145 -2
  11. package/RUNBOOK.md +95 -0
  12. package/SCF_ACTIVATION_PROPOSAL.md +137 -0
  13. package/assets/sorobanagents_avatar.jpg +0 -0
  14. package/assets/sorobanagents_avatar.png +0 -0
  15. package/assets/sorobanagents_avatar_elite.png +0 -0
  16. package/contracts/agentrail/Cargo.lock +1895 -0
  17. package/contracts/agentrail/Cargo.toml +25 -0
  18. package/contracts/agentrail/src/lib.rs +296 -0
  19. package/contracts/agentrail/src/test.rs +442 -0
  20. package/contracts/agentrail/test_snapshots/test/test_circuit_breaker_pause_and_unpause.1.json +195 -0
  21. package/contracts/agentrail/test_snapshots/test/test_double_initialize_fails.1.json +123 -0
  22. package/contracts/agentrail/test_snapshots/test/test_execute_spend_deducts_and_accumulates.1.json +347 -0
  23. package/contracts/agentrail/test_snapshots/test/test_get_allowance_returns_none_for_unregistered.1.json +61 -0
  24. package/contracts/agentrail/test_snapshots/test/test_get_version_matches_cargo_toml.1.json +61 -0
  25. package/contracts/agentrail/test_snapshots/test/test_grant_allowance_negative_amount_fails.1.json +123 -0
  26. package/contracts/agentrail/test_snapshots/test/test_grant_allowance_success.1.json +250 -0
  27. package/contracts/agentrail/test_snapshots/test/test_grant_allowance_zero_amount_fails.1.json +123 -0
  28. package/contracts/agentrail/test_snapshots/test/test_initialize_and_protocol_fee.1.json +154 -0
  29. package/contracts/agentrail/test_snapshots/test/test_initialize_success.1.json +124 -0
  30. package/contracts/agentrail/test_snapshots/test/test_initialize_with_excessive_fee_fails.1.json +61 -0
  31. package/contracts/agentrail/test_snapshots/test/test_max_protocol_fee_spend_boundary.1.json +298 -0
  32. package/contracts/agentrail/test_snapshots/test/test_multi_agent_allowance_isolation.1.json +426 -0
  33. package/contracts/agentrail/test_snapshots/test/test_multi_owner_isolation.1.json +426 -0
  34. package/contracts/agentrail/test_snapshots/test/test_regrant_allowance_updates_limits.1.json +346 -0
  35. package/contracts/agentrail/test_snapshots/test/test_revoke_allowance_freezes_spending.1.json +292 -0
  36. package/contracts/agentrail/test_snapshots/test/test_set_protocol_fee_by_admin.1.json +165 -0
  37. package/contracts/agentrail/test_snapshots/test/test_spend_after_expiry_fails.1.json +250 -0
  38. package/contracts/agentrail/test_snapshots/test/test_spend_exact_allowance_boundary.1.json +298 -0
  39. package/contracts/agentrail/test_snapshots/test/test_spend_exceeding_allowance_fails.1.json +250 -0
  40. package/contracts/agentrail/test_snapshots/test/test_spend_fails_when_circuit_breaker_tripped.1.json +285 -0
  41. package/contracts/agentrail/test_snapshots/test/test_spend_negative_amount_fails.1.json +250 -0
  42. package/contracts/agentrail/test_snapshots/test/test_spend_non_existent_allowance_fails.1.json +123 -0
  43. package/contracts/agentrail/test_snapshots/test/test_spend_on_revoked_allowance_fails.1.json +292 -0
  44. package/contracts/agentrail/test_snapshots/test/test_spend_with_protocol_fee.1.json +286 -0
  45. package/contracts/agentrail/test_snapshots/test/test_spend_zero_amount_fails.1.json +250 -0
  46. package/contracts/agentrail/test_snapshots/test/test_unpause_restores_spend_operations.1.json +370 -0
  47. package/contracts/agentrail/test_snapshots/test/test_zero_protocol_fee_spend.1.json +298 -0
  48. package/deployment.testnet.json +15 -0
  49. package/dist/cli/sandbox.d.ts +41 -0
  50. package/dist/cli/sandbox.js +82 -0
  51. package/dist/index.d.ts +10 -0
  52. package/dist/index.js +26 -0
  53. package/dist/lib/micro-stream.d.ts +21 -0
  54. package/dist/lib/micro-stream.js +41 -0
  55. package/dist/lib/proof-of-task.d.ts +12 -0
  56. package/dist/lib/proof-of-task.js +32 -0
  57. package/dist/lib/scoped-allowance.d.ts +11 -0
  58. package/dist/lib/scoped-allowance.js +95 -0
  59. package/dist/lib/ttl-keeper.d.ts +10 -0
  60. package/dist/lib/ttl-keeper.js +36 -0
  61. package/dist/sdk/agent-client.d.ts +26 -0
  62. package/dist/sdk/agent-client.js +53 -0
  63. package/dist/types.d.ts +72 -0
  64. package/dist/types.js +5 -0
  65. package/docs/assets/favicon.png +0 -0
  66. package/docs/assets/logo.png +0 -0
  67. package/docs/index.html +840 -0
  68. package/package.json +38 -4
  69. package/scripts/demo-agent.ts +52 -0
  70. package/scripts/deploy-testnet.ts +228 -0
  71. package/scripts/security-audit.mjs +159 -0
  72. package/src/cli/sandbox.ts +127 -0
  73. package/src/index.ts +11 -0
  74. package/src/lib/micro-stream.ts +52 -0
  75. package/src/lib/proof-of-task.ts +39 -0
  76. package/src/lib/scoped-allowance.ts +112 -0
  77. package/src/lib/ttl-keeper.ts +44 -0
  78. package/src/sdk/agent-client.ts +66 -0
  79. package/src/types.ts +78 -0
  80. package/tests/agent-client.test.ts +62 -0
  81. package/tests/cli.test.ts +68 -0
  82. package/tests/micro-stream.test.ts +38 -0
  83. package/tests/proof-of-task.test.ts +78 -0
  84. package/tests/scoped-allowance.test.ts +135 -0
  85. package/tests/security-audit.test.ts +76 -0
  86. package/tests/ttl-keeper.test.ts +36 -0
  87. package/tsconfig.json +16 -0
  88. package/vitest.config.ts +8 -0
@@ -0,0 +1,442 @@
1
+ #![cfg(test)]
2
+
3
+ use super::*;
4
+ use soroban_sdk::{
5
+ testutils::{Address as _, Ledger},
6
+ Address, Env, String,
7
+ };
8
+
9
+ fn setup_test(
10
+ env: &Env,
11
+ ) -> (
12
+ AgentRailContractClient<'static>,
13
+ Address, // admin
14
+ Address, // token
15
+ Address, // treasury
16
+ Address, // owner
17
+ Address, // agent
18
+ Address, // destination
19
+ ) {
20
+ let contract_id = env.register(AgentRailContract, ());
21
+ let client = AgentRailContractClient::new(env, &contract_id);
22
+
23
+ let admin = Address::generate(env);
24
+ let token = Address::generate(env);
25
+ let treasury = Address::generate(env);
26
+ let owner = Address::generate(env);
27
+ let agent = Address::generate(env);
28
+ let destination = Address::generate(env);
29
+
30
+ (client, admin, token, treasury, owner, agent, destination)
31
+ }
32
+
33
+ // ─── 1. Version & Metadata Tests ───────────────────────────────────────────
34
+
35
+ #[test]
36
+ fn test_get_version_matches_cargo_toml() {
37
+ let env = Env::default();
38
+ let (client, _, _, _, _, _, _) = setup_test(&env);
39
+ let version = client.get_version();
40
+ let expected = String::from_str(&env, env!("CARGO_PKG_VERSION"));
41
+ assert_eq!(version, expected);
42
+ }
43
+
44
+ #[test]
45
+ fn test_contract_version_constant() {
46
+ assert_eq!(CONTRACT_VERSION, env!("CARGO_PKG_VERSION"));
47
+ assert!(!CONTRACT_VERSION.is_empty());
48
+ }
49
+
50
+ // ─── 2. Critical: Initialization & Fee Invariants ─────────────────────────
51
+
52
+ #[test]
53
+ fn test_initialize_success() {
54
+ let env = Env::default();
55
+ let (client, admin, token, treasury, _, _, _) = setup_test(&env);
56
+
57
+ client.initialize(&admin, &token, &treasury, &20);
58
+ let (fee_treasury, fee_bps) = client.get_protocol_fee();
59
+ assert_eq!(fee_treasury, treasury);
60
+ assert_eq!(fee_bps, 20);
61
+ assert_eq!(client.is_paused(), false);
62
+ }
63
+
64
+ #[test]
65
+ #[should_panic(expected = "Error(Contract, #1)")]
66
+ fn test_double_initialize_fails() {
67
+ let env = Env::default();
68
+ let (client, admin, token, treasury, _, _, _) = setup_test(&env);
69
+
70
+ client.initialize(&admin, &token, &treasury, &20);
71
+ // Second initialization must fail with AlreadyInitialized (#1)
72
+ client.initialize(&admin, &token, &treasury, &20);
73
+ }
74
+
75
+ #[test]
76
+ #[should_panic(expected = "Error(Contract, #9)")]
77
+ fn test_initialize_with_excessive_fee_fails() {
78
+ let env = Env::default();
79
+ let (client, admin, token, treasury, _, _, _) = setup_test(&env);
80
+ // Over MAX_FEE_BPS (500) must fail with InvalidFeeBps (#9)
81
+ client.initialize(&admin, &token, &treasury, &501);
82
+ }
83
+
84
+ #[test]
85
+ fn test_set_protocol_fee_by_admin() {
86
+ let env = Env::default();
87
+ env.mock_all_auths();
88
+ let (client, admin, token, treasury, _, _, _) = setup_test(&env);
89
+
90
+ client.initialize(&admin, &token, &treasury, &20);
91
+ let new_treasury = Address::generate(&env);
92
+ client.set_protocol_fee(&new_treasury, &100);
93
+
94
+ let (updated_treasury, updated_bps) = client.get_protocol_fee();
95
+ assert_eq!(updated_treasury, new_treasury);
96
+ assert_eq!(updated_bps, 100);
97
+ }
98
+
99
+ // ─── 3. Critical: Emergency Circuit Breaker (Pause/Unpause) ────────────────
100
+
101
+ #[test]
102
+ fn test_circuit_breaker_pause_and_unpause() {
103
+ let env = Env::default();
104
+ env.mock_all_auths();
105
+ let (client, admin, token, treasury, _, _, _) = setup_test(&env);
106
+
107
+ client.initialize(&admin, &token, &treasury, &20);
108
+ assert_eq!(client.is_paused(), false);
109
+
110
+ // Admin pauses contract
111
+ client.pause();
112
+ assert_eq!(client.is_paused(), true);
113
+
114
+ // Admin unpauses contract
115
+ client.unpause();
116
+ assert_eq!(client.is_paused(), false);
117
+ }
118
+
119
+ #[test]
120
+ #[should_panic(expected = "Error(Contract, #10)")]
121
+ fn test_spend_fails_when_circuit_breaker_tripped() {
122
+ let env = Env::default();
123
+ env.mock_all_auths();
124
+ let (client, admin, token, treasury, owner, agent, destination) = setup_test(&env);
125
+
126
+ client.initialize(&admin, &token, &treasury, &20);
127
+ client.grant_allowance(&owner, &agent, &10_000_000, &100_000);
128
+
129
+ // Emergency pause triggered!
130
+ client.pause();
131
+
132
+ // Spend must immediately fail with ContractPaused (#10)
133
+ client.execute_spend(&owner, &agent, &destination, &1_000_000);
134
+ }
135
+
136
+ // ─── 4. High: Scoped Allowance & Spend Verification ───────────────────────
137
+
138
+ #[test]
139
+ fn test_grant_allowance_success() {
140
+ let env = Env::default();
141
+ env.mock_all_auths();
142
+ let (client, admin, token, treasury, owner, agent, _) = setup_test(&env);
143
+
144
+ client.initialize(&admin, &token, &treasury, &20);
145
+ client.grant_allowance(&owner, &agent, &50_000_000, &10_000);
146
+
147
+ let allowance = client.get_allowance(&owner, &agent).unwrap();
148
+ assert_eq!(allowance.owner, owner);
149
+ assert_eq!(allowance.agent, agent);
150
+ assert_eq!(allowance.max_stroops, 50_000_000);
151
+ assert_eq!(allowance.spent_stroops, 0);
152
+ assert_eq!(allowance.valid_until, 10_000);
153
+ assert_eq!(allowance.active, true);
154
+ }
155
+
156
+ #[test]
157
+ #[should_panic(expected = "Error(Contract, #8)")]
158
+ fn test_grant_allowance_zero_amount_fails() {
159
+ let env = Env::default();
160
+ env.mock_all_auths();
161
+ let (client, admin, token, treasury, owner, agent, _) = setup_test(&env);
162
+
163
+ client.initialize(&admin, &token, &treasury, &20);
164
+ client.grant_allowance(&owner, &agent, &0, &10_000);
165
+ }
166
+
167
+ #[test]
168
+ #[should_panic(expected = "Error(Contract, #8)")]
169
+ fn test_grant_allowance_negative_amount_fails() {
170
+ let env = Env::default();
171
+ env.mock_all_auths();
172
+ let (client, admin, token, treasury, owner, agent, _) = setup_test(&env);
173
+
174
+ client.initialize(&admin, &token, &treasury, &20);
175
+ client.grant_allowance(&owner, &agent, &-500, &10_000);
176
+ }
177
+
178
+ #[test]
179
+ fn test_execute_spend_deducts_and_accumulates() {
180
+ let env = Env::default();
181
+ env.mock_all_auths();
182
+ let (client, admin, token, treasury, owner, agent, destination) = setup_test(&env);
183
+
184
+ client.initialize(&admin, &token, &treasury, &20);
185
+ client.grant_allowance(&owner, &agent, &20_000_000, &100_000);
186
+
187
+ // Spend 1: 5 USDC
188
+ client.execute_spend(&owner, &agent, &destination, &5_000_000);
189
+ let a1 = client.get_allowance(&owner, &agent).unwrap();
190
+ assert_eq!(a1.spent_stroops, 5_000_000);
191
+
192
+ // Spend 2: 10 USDC
193
+ client.execute_spend(&owner, &agent, &destination, &10_000_000);
194
+ let a2 = client.get_allowance(&owner, &agent).unwrap();
195
+ assert_eq!(a2.spent_stroops, 15_000_000);
196
+ }
197
+
198
+ #[test]
199
+ fn test_spend_exact_allowance_boundary() {
200
+ let env = Env::default();
201
+ env.mock_all_auths();
202
+ let (client, admin, token, treasury, owner, agent, destination) = setup_test(&env);
203
+
204
+ client.initialize(&admin, &token, &treasury, &20);
205
+ client.grant_allowance(&owner, &agent, &10_000_000, &100_000);
206
+
207
+ // Spend exactly the full limit
208
+ client.execute_spend(&owner, &agent, &destination, &10_000_000);
209
+ let a = client.get_allowance(&owner, &agent).unwrap();
210
+ assert_eq!(a.spent_stroops, 10_000_000);
211
+ }
212
+
213
+ #[test]
214
+ #[should_panic(expected = "Error(Contract, #6)")]
215
+ fn test_spend_exceeding_allowance_fails() {
216
+ let env = Env::default();
217
+ env.mock_all_auths();
218
+ let (client, admin, token, treasury, owner, agent, destination) = setup_test(&env);
219
+
220
+ client.initialize(&admin, &token, &treasury, &20);
221
+ client.grant_allowance(&owner, &agent, &5_000_000, &100_000);
222
+
223
+ // Attempting to spend 5.000001 USDC fails with AllowanceExceeded (#6)
224
+ client.execute_spend(&owner, &agent, &destination, &5_000_001);
225
+ }
226
+
227
+ #[test]
228
+ #[should_panic(expected = "Error(Contract, #5)")]
229
+ fn test_spend_after_expiry_fails() {
230
+ let env = Env::default();
231
+ env.mock_all_auths();
232
+ let (client, admin, token, treasury, owner, agent, destination) = setup_test(&env);
233
+
234
+ client.initialize(&admin, &token, &treasury, &20);
235
+ client.grant_allowance(&owner, &agent, &10_000_000, &500);
236
+
237
+ // Fast-forward ledger timestamp past expiry
238
+ env.ledger().set_timestamp(501);
239
+
240
+ client.execute_spend(&owner, &agent, &destination, &1_000_000);
241
+ }
242
+
243
+ #[test]
244
+ #[should_panic(expected = "Error(Contract, #4)")]
245
+ fn test_spend_non_existent_allowance_fails() {
246
+ let env = Env::default();
247
+ env.mock_all_auths();
248
+ let (client, admin, token, treasury, owner, agent, destination) = setup_test(&env);
249
+
250
+ client.initialize(&admin, &token, &treasury, &20);
251
+ client.execute_spend(&owner, &agent, &destination, &1_000_000);
252
+ }
253
+
254
+ // ─── 5. Critical: Revocation & Emergency Freeze ───────────────────────────
255
+
256
+ #[test]
257
+ fn test_revoke_allowance_freezes_spending() {
258
+ let env = Env::default();
259
+ env.mock_all_auths();
260
+ let (client, admin, token, treasury, owner, agent, _) = setup_test(&env);
261
+
262
+ client.initialize(&admin, &token, &treasury, &20);
263
+ client.grant_allowance(&owner, &agent, &10_000_000, &100_000);
264
+
265
+ // Owner revokes allowance
266
+ client.revoke_allowance(&owner, &agent);
267
+
268
+ let a = client.get_allowance(&owner, &agent).unwrap();
269
+ assert_eq!(a.active, false);
270
+ }
271
+
272
+ #[test]
273
+ #[should_panic(expected = "Error(Contract, #7)")]
274
+ fn test_spend_on_revoked_allowance_fails() {
275
+ let env = Env::default();
276
+ env.mock_all_auths();
277
+ let (client, admin, token, treasury, owner, agent, destination) = setup_test(&env);
278
+
279
+ client.initialize(&admin, &token, &treasury, &20);
280
+ client.grant_allowance(&owner, &agent, &10_000_000, &100_000);
281
+
282
+ client.revoke_allowance(&owner, &agent);
283
+
284
+ // Spend must fail with AllowanceInactive (#7)
285
+ client.execute_spend(&owner, &agent, &destination, &1_000_000);
286
+ }
287
+
288
+ // ─── 6. High: Multi-Agent Isolation & Concurrency ─────────────────────────
289
+
290
+ #[test]
291
+ fn test_multi_agent_allowance_isolation() {
292
+ let env = Env::default();
293
+ env.mock_all_auths();
294
+ let (client, admin, token, treasury, owner, agent1, destination) = setup_test(&env);
295
+ let agent2 = Address::generate(&env);
296
+
297
+ client.initialize(&admin, &token, &treasury, &20);
298
+
299
+ // Grant 10 USDC to Agent 1, and 5 USDC to Agent 2
300
+ client.grant_allowance(&owner, &agent1, &10_000_000, &100_000);
301
+ client.grant_allowance(&owner, &agent2, &5_000_000, &100_000);
302
+
303
+ // Agent 1 spends 6 USDC
304
+ client.execute_spend(&owner, &agent1, &destination, &6_000_000);
305
+
306
+ // Verify Agent 2 allowance balance is completely unaffected
307
+ let a1 = client.get_allowance(&owner, &agent1).unwrap();
308
+ let a2 = client.get_allowance(&owner, &agent2).unwrap();
309
+ assert_eq!(a1.spent_stroops, 6_000_000);
310
+ assert_eq!(a2.spent_stroops, 0);
311
+ }
312
+
313
+ #[test]
314
+ fn test_multi_owner_isolation() {
315
+ let env = Env::default();
316
+ env.mock_all_auths();
317
+ let (client, admin, token, treasury, owner1, agent, destination) = setup_test(&env);
318
+ let owner2 = Address::generate(&env);
319
+
320
+ client.initialize(&admin, &token, &treasury, &20);
321
+
322
+ // Owner 1 grants 10 USDC to agent, Owner 2 grants 20 USDC to agent
323
+ client.grant_allowance(&owner1, &agent, &10_000_000, &100_000);
324
+ client.grant_allowance(&owner2, &agent, &20_000_000, &100_000);
325
+
326
+ // Agent spends 3 USDC from Owner 1
327
+ client.execute_spend(&owner1, &agent, &destination, &3_000_000);
328
+
329
+ let a1 = client.get_allowance(&owner1, &agent).unwrap();
330
+ let a2 = client.get_allowance(&owner2, &agent).unwrap();
331
+ assert_eq!(a1.spent_stroops, 3_000_000);
332
+ assert_eq!(a2.spent_stroops, 0);
333
+ }
334
+
335
+ // ─── 7. Critical: Boundary Amounts & Fee Invariants ──────────────────────
336
+
337
+ #[test]
338
+ #[should_panic(expected = "Error(Contract, #8)")]
339
+ fn test_spend_zero_amount_fails() {
340
+ let env = Env::default();
341
+ env.mock_all_auths();
342
+ let (client, admin, token, treasury, owner, agent, destination) = setup_test(&env);
343
+
344
+ client.initialize(&admin, &token, &treasury, &20);
345
+ client.grant_allowance(&owner, &agent, &10_000_000, &100_000);
346
+
347
+ // 0 amount spend must fail with InvalidAmount (#8)
348
+ client.execute_spend(&owner, &agent, &destination, &0);
349
+ }
350
+
351
+ #[test]
352
+ #[should_panic(expected = "Error(Contract, #8)")]
353
+ fn test_spend_negative_amount_fails() {
354
+ let env = Env::default();
355
+ env.mock_all_auths();
356
+ let (client, admin, token, treasury, owner, agent, destination) = setup_test(&env);
357
+
358
+ client.initialize(&admin, &token, &treasury, &20);
359
+ client.grant_allowance(&owner, &agent, &10_000_000, &100_000);
360
+
361
+ // Negative amount spend must fail with InvalidAmount (#8)
362
+ client.execute_spend(&owner, &agent, &destination, &-10_000);
363
+ }
364
+
365
+ #[test]
366
+ fn test_regrant_allowance_updates_limits() {
367
+ let env = Env::default();
368
+ env.mock_all_auths();
369
+ let (client, admin, token, treasury, owner, agent, destination) = setup_test(&env);
370
+
371
+ client.initialize(&admin, &token, &treasury, &20);
372
+ client.grant_allowance(&owner, &agent, &10_000_000, &100_000);
373
+ client.execute_spend(&owner, &agent, &destination, &5_000_000);
374
+
375
+ // Owner increases allowance to 50 USDC with new expiration
376
+ client.grant_allowance(&owner, &agent, &50_000_000, &200_000);
377
+
378
+ let updated = client.get_allowance(&owner, &agent).unwrap();
379
+ assert_eq!(updated.max_stroops, 50_000_000);
380
+ assert_eq!(updated.spent_stroops, 0);
381
+ assert_eq!(updated.valid_until, 200_000);
382
+ }
383
+
384
+ #[test]
385
+ fn test_zero_protocol_fee_spend() {
386
+ let env = Env::default();
387
+ env.mock_all_auths();
388
+ let (client, admin, token, treasury, owner, agent, destination) = setup_test(&env);
389
+
390
+ // Initialized with 0 bps fee
391
+ client.initialize(&admin, &token, &treasury, &0);
392
+ client.grant_allowance(&owner, &agent, &10_000_000, &100_000);
393
+
394
+ client.execute_spend(&owner, &agent, &destination, &5_000_000);
395
+ let a = client.get_allowance(&owner, &agent).unwrap();
396
+ assert_eq!(a.spent_stroops, 5_000_000);
397
+ }
398
+
399
+ #[test]
400
+ fn test_max_protocol_fee_spend_boundary() {
401
+ let env = Env::default();
402
+ env.mock_all_auths();
403
+ let (client, admin, token, treasury, owner, agent, destination) = setup_test(&env);
404
+
405
+ // Initialized with maximum 500 bps (5.00%) fee
406
+ client.initialize(&admin, &token, &treasury, &MAX_FEE_BPS);
407
+ client.grant_allowance(&owner, &agent, &100_000_000, &100_000);
408
+
409
+ client.execute_spend(&owner, &agent, &destination, &10_000_000);
410
+ let a = client.get_allowance(&owner, &agent).unwrap();
411
+ assert_eq!(a.spent_stroops, 10_000_000);
412
+ }
413
+
414
+ #[test]
415
+ fn test_unpause_restores_spend_operations() {
416
+ let env = Env::default();
417
+ env.mock_all_auths();
418
+ let (client, admin, token, treasury, owner, agent, destination) = setup_test(&env);
419
+
420
+ client.initialize(&admin, &token, &treasury, &20);
421
+ client.grant_allowance(&owner, &agent, &10_000_000, &100_000);
422
+
423
+ // Trip circuit breaker
424
+ client.pause();
425
+ assert_eq!(client.is_paused(), true);
426
+
427
+ // Untrip circuit breaker
428
+ client.unpause();
429
+ assert_eq!(client.is_paused(), false);
430
+
431
+ // Spend succeeds after unpause
432
+ client.execute_spend(&owner, &agent, &destination, &2_000_000);
433
+ let a = client.get_allowance(&owner, &agent).unwrap();
434
+ assert_eq!(a.spent_stroops, 2_000_000);
435
+ }
436
+
437
+ #[test]
438
+ fn test_get_allowance_returns_none_for_unregistered() {
439
+ let env = Env::default();
440
+ let (client, _, _, _, owner, agent, _) = setup_test(&env);
441
+ assert!(client.get_allowance(&owner, &agent).is_none());
442
+ }
@@ -0,0 +1,195 @@
1
+ {
2
+ "generators": {
3
+ "address": 7,
4
+ "nonce": 0,
5
+ "mux_id": 0
6
+ },
7
+ "auth": [
8
+ [],
9
+ [],
10
+ [],
11
+ [
12
+ [
13
+ "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4",
14
+ {
15
+ "function": {
16
+ "contract_fn": {
17
+ "contract_address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
18
+ "function_name": "pause",
19
+ "args": []
20
+ }
21
+ },
22
+ "sub_invocations": []
23
+ }
24
+ ]
25
+ ],
26
+ [],
27
+ [
28
+ [
29
+ "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4",
30
+ {
31
+ "function": {
32
+ "contract_fn": {
33
+ "contract_address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
34
+ "function_name": "unpause",
35
+ "args": []
36
+ }
37
+ },
38
+ "sub_invocations": []
39
+ }
40
+ ]
41
+ ],
42
+ []
43
+ ],
44
+ "ledger": {
45
+ "protocol_version": 27,
46
+ "sequence_number": 0,
47
+ "timestamp": 0,
48
+ "network_id": "0000000000000000000000000000000000000000000000000000000000000000",
49
+ "base_reserve": 0,
50
+ "min_persistent_entry_ttl": 4096,
51
+ "min_temp_entry_ttl": 16,
52
+ "max_entry_ttl": 6312000,
53
+ "ledger_entries": [
54
+ {
55
+ "entry": {
56
+ "last_modified_ledger_seq": 0,
57
+ "data": {
58
+ "contract_data": {
59
+ "ext": "v0",
60
+ "contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
61
+ "key": "ledger_key_contract_instance",
62
+ "durability": "persistent",
63
+ "val": {
64
+ "contract_instance": {
65
+ "executable": {
66
+ "wasm": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
67
+ },
68
+ "storage": [
69
+ {
70
+ "key": {
71
+ "vec": [
72
+ {
73
+ "symbol": "Admin"
74
+ }
75
+ ]
76
+ },
77
+ "val": {
78
+ "address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4"
79
+ }
80
+ },
81
+ {
82
+ "key": {
83
+ "vec": [
84
+ {
85
+ "symbol": "Paused"
86
+ }
87
+ ]
88
+ },
89
+ "val": {
90
+ "bool": false
91
+ }
92
+ },
93
+ {
94
+ "key": {
95
+ "vec": [
96
+ {
97
+ "symbol": "ProtocolFeeBps"
98
+ }
99
+ ]
100
+ },
101
+ "val": {
102
+ "u32": 20
103
+ }
104
+ },
105
+ {
106
+ "key": {
107
+ "vec": [
108
+ {
109
+ "symbol": "Token"
110
+ }
111
+ ]
112
+ },
113
+ "val": {
114
+ "address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHK3M"
115
+ }
116
+ },
117
+ {
118
+ "key": {
119
+ "vec": [
120
+ {
121
+ "symbol": "Treasury"
122
+ }
123
+ ]
124
+ },
125
+ "val": {
126
+ "address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAITA4"
127
+ }
128
+ }
129
+ ]
130
+ }
131
+ }
132
+ }
133
+ },
134
+ "ext": "v0"
135
+ },
136
+ "live_until": 4095
137
+ },
138
+ {
139
+ "entry": {
140
+ "last_modified_ledger_seq": 0,
141
+ "data": {
142
+ "contract_data": {
143
+ "ext": "v0",
144
+ "contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4",
145
+ "key": {
146
+ "ledger_key_nonce": {
147
+ "nonce": "801925984706572462"
148
+ }
149
+ },
150
+ "durability": "temporary",
151
+ "val": "void"
152
+ }
153
+ },
154
+ "ext": "v0"
155
+ },
156
+ "live_until": 6311999
157
+ },
158
+ {
159
+ "entry": {
160
+ "last_modified_ledger_seq": 0,
161
+ "data": {
162
+ "contract_data": {
163
+ "ext": "v0",
164
+ "contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4",
165
+ "key": {
166
+ "ledger_key_nonce": {
167
+ "nonce": "5541220902715666415"
168
+ }
169
+ },
170
+ "durability": "temporary",
171
+ "val": "void"
172
+ }
173
+ },
174
+ "ext": "v0"
175
+ },
176
+ "live_until": 6311999
177
+ },
178
+ {
179
+ "entry": {
180
+ "last_modified_ledger_seq": 0,
181
+ "data": {
182
+ "contract_code": {
183
+ "ext": "v0",
184
+ "hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
185
+ "code": ""
186
+ }
187
+ },
188
+ "ext": "v0"
189
+ },
190
+ "live_until": 4095
191
+ }
192
+ ]
193
+ },
194
+ "events": []
195
+ }