Skip to content

Commit aa72312

Browse files
committed
feat: refactored constraints readme
1 parent 9f8cff7 commit aa72312

1 file changed

Lines changed: 21 additions & 33 deletions

File tree

README.md

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ batch.add([
8484
usdc.check({
8585
functionName: 'balanceOf',
8686
args: [scaAddress],
87-
constraints: [{ gte: amount }],
87+
constraint: { gte: amount },
8888
}),
8989

9090
// Action: transfer 50 USDC to the recipient
@@ -97,7 +97,7 @@ batch.add([
9797
usdc.check({
9898
functionName: 'balanceOf',
9999
args: [recipient],
100-
constraints: [{ gte: amount }],
100+
constraint: { gte: amount },
101101
}),
102102
]);
103103
```
@@ -223,51 +223,39 @@ The following constraint operators are available:
223223
| `{ lteSigned: value }` | signed (`int256`) | The resolved value (as `int256`) must be ≤ `value` |
224224
| `{ or: [...] }` || Passes if **any one** of the listed child constraints passes |
225225

226-
Constraints can be combined. All top-level constraints must pass. Children inside `or` must be standard or signed constraints — nested `or` is not supported.
226+
Each `check` or `runtimeValue` call accepts one `constraint`. To require multiple conditions simultaneously, use multiple separate calls. Children inside `or` must be standard or signed constraints — nested `or` is not supported.
227227

228228
#### Constraints on a check call
229229

230230
`check` reads a view function and asserts its return value. If the assertion fails, the entire batch reverts before any writes happen.
231231

232232
```ts
233-
// Assert USDC balance is between 10 and 1000 USDC (range check)
234-
usdc.check({
235-
functionName: 'balanceOf',
236-
args: [scaAddress],
237-
constraints: [
238-
{ gte: parseUnits('10', 6) },
239-
{ lte: parseUnits('1000', 6) },
240-
],
241-
})
233+
// Assert USDC balance is between 10 and 1000 USDC (range check — two separate calls)
234+
usdc.check({ functionName: 'balanceOf', args: [scaAddress], constraint: { gte: parseUnits('10', 6) } })
235+
usdc.check({ functionName: 'balanceOf', args: [scaAddress], constraint: { lte: parseUnits('1000', 6) } })
242236
```
243237

244238
```ts
245239
// Assert the pool has been fully swept — balance must be exactly zero
246240
usdc.check({
247241
functionName: 'balanceOf',
248242
args: [poolAddress],
249-
constraints: [{ eq: 0n }],
243+
constraint: { eq: 0n },
250244
})
251245
```
252246

253247
```ts
254-
// Signed range check — useful when a value may be negative (e.g. a signed price delta)
255-
oracle.check({
256-
functionName: 'priceDelta',
257-
args: [],
258-
constraints: [
259-
{ gteSigned: -500n }, // delta must be >= -500 (int256 comparison)
260-
{ lteSigned: 500n }, // delta must be <= +500
261-
],
262-
})
248+
// Signed constraint — useful when a value may be negative (e.g. a signed price delta)
249+
oracle.check({ functionName: 'priceDelta', args: [], constraint: { gteSigned: -500n } })
250+
oracle.check({ functionName: 'priceDelta', args: [], constraint: { lteSigned: 500n } })
263251
```
264252

265253
```ts
266254
// OR check — balance must be exactly 0 OR at least 10 USDC
267255
usdc.check({
268256
functionName: 'balanceOf',
269257
args: [scaAddress],
270-
constraints: [{ or: [{ eq: 0n }, { gte: parseUnits('10', 6) }] }],
258+
constraint: { or: [{ eq: 0n }, { gte: parseUnits('10', 6) }] },
271259
})
272260
```
273261

@@ -290,7 +278,7 @@ batch.add([
290278
args: [
291279
USDC,
292280
// Inject live USDC balance — but only if it is at least minExpected
293-
usdc.runtimeBalance({ constraints: [{ gte: minExpected }] }),
281+
usdc.runtimeBalance({ constraint: { gte: minExpected } }),
294282
],
295283
value: parseEther('0.01'),
296284
}),
@@ -381,7 +369,7 @@ batch.add([
381369
usdc.check({
382370
functionName: 'balanceOf',
383371
args: [scaAddress],
384-
constraints: [{ gte: amount }],
372+
constraint: { gte: amount },
385373
}),
386374
usdc.write({ functionName: 'transfer', args: ['0xRecipientAddress', amount] }),
387375
]);
@@ -453,7 +441,7 @@ batch.add([
453441
usdc.check({
454442
functionName: 'balanceOf',
455443
args: [scaAddress],
456-
constraints: [{ gte: amount }],
444+
constraint: { gte: amount },
457445
}),
458446

459447
// 2. Transfer exactly 10 USDC to the recipient
@@ -466,7 +454,7 @@ batch.add([
466454
usdc.check({
467455
functionName: 'balanceOf',
468456
args: [recipient],
469-
constraints: [{ gte: amount }],
457+
constraint: { gte: amount },
470458
}),
471459
]);
472460

@@ -527,7 +515,7 @@ batch.add([
527515
// 2. Assert the captured value on-chain
528516
await storage.check({
529517
storageKey,
530-
constraints: [{ eq: 10n }],
518+
constraint: { eq: 10n },
531519
}),
532520

533521
// 3. Transfer the captured amount to the recipient
@@ -552,9 +540,9 @@ batch.add([
552540
capture: { type: 'execResult', storageKey },
553541
}),
554542

555-
await storage.check({ storageKey, slotIndex: 0, constraints: [{ eq: 10n }] }),
556-
await storage.check({ storageKey, slotIndex: 1, constraints: [{ eq: 21n }] }),
557-
await storage.check({ storageKey, slotIndex: 2, constraints: [{ eq: 1n }] }),
543+
await storage.check({ storageKey, slotIndex: 0, constraint: { eq: 10n } }),
544+
await storage.check({ storageKey, slotIndex: 1, constraint: { eq: 21n } }),
545+
await storage.check({ storageKey, slotIndex: 2, constraint: { eq: 1n } }),
558546
]);
559547
```
560548

@@ -585,7 +573,7 @@ batch.add([
585573
// Assert the captured static call result on-chain
586574
await storage.check({
587575
storageKey,
588-
constraints: [{ eq: 12n }],
576+
constraint: { eq: 12n },
589577
}),
590578
]);
591579
```
@@ -620,7 +608,7 @@ batch.add([
620608
// 2. On-chain assertion: the slot must equal the value we just wrote
621609
await storage.check({
622610
storageKey,
623-
constraints: [{ eq: amount }],
611+
constraint: { eq: amount },
624612
}),
625613

626614
// 3. Transfer — the amount is resolved from storage at execution time

0 commit comments

Comments
 (0)