-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathconditions.test.ts
More file actions
522 lines (470 loc) · 19 KB
/
Copy pathconditions.test.ts
File metadata and controls
522 lines (470 loc) · 19 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
import { type Address, decodeAbiParameters, erc20Abi, parseUnits } from "viem"
import { describe, expect, test } from "vitest"
import { buildComposableCall } from "../../account/decorators/instructions/buildComposable"
import { ComposabilityVersion } from "../../constants"
import {
CONSTRAINT_TUPLE_ABI,
ConstraintType,
InputParamFetcherType,
greaterThanOrEqualTo,
greaterThanOrEqualToSigned,
inRange,
lessThanOrEqualTo,
lessThanOrEqualToSigned,
orConstraint,
runtimeERC20BalanceOf,
validateAndProcessConstraints
} from "./composabilityCalls"
import {
ConditionType,
createCondition,
createConditionInputParam
} from "./conditions"
describe("Conditional Execution - Unit Tests", () => {
describe("condition builders", () => {
const mockContract = "0x1234567890123456789012345678901234567890" as Address
test("createCondition should create a condition with GTE constraint", () => {
const cond = createCondition({
targetContract: mockContract,
functionAbi: erc20Abi,
functionName: "balanceOf",
args: [mockContract],
value: 1000n,
type: ConditionType.GTE,
description: "Min balance 1000"
})
expect(cond.targetContract).toBe(mockContract)
expect(cond.functionAbi).toBe(erc20Abi)
expect(cond.functionName).toBe("balanceOf")
expect(cond.args).toEqual([mockContract])
expect(cond.constraint).toEqual({
type: ConstraintType.GTE,
value: 1000n
})
expect(cond.description).toBe("Min balance 1000")
})
test("createCondition should create a condition with LTE constraint", () => {
const cond = createCondition({
targetContract: mockContract,
functionAbi: erc20Abi,
functionName: "balanceOf",
args: [mockContract],
value: 5000n,
type: ConditionType.LTE,
description: "Max balance 5000"
})
expect(cond.targetContract).toBe(mockContract)
expect(cond.functionName).toBe("balanceOf")
expect(cond.constraint).toEqual({
type: ConstraintType.LTE,
value: 5000n
})
expect(cond.description).toBe("Max balance 5000")
})
test("createCondition should create a condition with EQ constraint", () => {
const cond = createCondition({
targetContract: mockContract,
functionAbi: erc20Abi,
functionName: "totalSupply",
args: [],
value: 1000000n,
type: ConditionType.EQ,
description: "Total supply must equal 1000000"
})
expect(cond.targetContract).toBe(mockContract)
expect(cond.functionName).toBe("totalSupply")
expect(cond.constraint).toEqual({
type: ConstraintType.EQ,
value: 1000000n
})
expect(cond.description).toBe("Total supply must equal 1000000")
})
})
describe("createConditionInputParam", () => {
const mockContract = "0x1234567890123456789012345678901234567890" as Address
test("should create InputParam with STATIC_CALL fetcher type and the encoded function call", () => {
const conditionParams = {
targetContract: mockContract,
functionAbi: erc20Abi,
functionName: "balanceOf" as const,
args: [mockContract] as [Address],
value: 1000n
}
const conditionGt = createCondition({
...conditionParams,
type: ConditionType.GTE
})
const conditionLt = createCondition({
...conditionParams,
type: ConditionType.LTE
})
const conditionEq = createCondition({
...conditionParams,
type: ConditionType.EQ
})
const inputParamGt = createConditionInputParam(conditionGt)
const inputParamLt = createConditionInputParam(conditionLt)
const inputParamEq = createConditionInputParam(conditionEq)
expect(inputParamGt.fetcherType).toBe(InputParamFetcherType.STATIC_CALL)
expect(inputParamGt.paramData).toContain(
mockContract.slice(2).toLowerCase()
) // should be abi.encodePacked(address, bytes) particularly
expect(inputParamGt).toMatchInlineSnapshot(`
{
"constraints": [
{
"constraintType": 1,
"referenceData": "0x00000000000000000000000000000000000000000000000000000000000003e8",
},
],
"fetcherType": 1,
"paramData": "0x00000000000000000000000012345678901234567890123456789012345678900000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000002470a08231000000000000000000000000123456789012345678901234567890123456789000000000000000000000000000000000000000000000000000000000",
}
`)
expect(inputParamLt.fetcherType).toBe(InputParamFetcherType.STATIC_CALL)
expect(inputParamLt.paramData).toContain(
mockContract.slice(2).toLowerCase()
) // should be abi.encodePacked(address, bytes) particularly
expect(inputParamLt).toMatchInlineSnapshot(`
{
"constraints": [
{
"constraintType": 2,
"referenceData": "0x00000000000000000000000000000000000000000000000000000000000003e8",
},
],
"fetcherType": 1,
"paramData": "0x00000000000000000000000012345678901234567890123456789012345678900000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000002470a08231000000000000000000000000123456789012345678901234567890123456789000000000000000000000000000000000000000000000000000000000",
}
`)
expect(inputParamEq.fetcherType).toBe(InputParamFetcherType.STATIC_CALL)
expect(inputParamEq.paramData).toContain(
mockContract.slice(2).toLowerCase()
) // should be abi.encodePacked(address, bytes) particularly
expect(inputParamEq).toMatchInlineSnapshot(`
{
"constraints": [
{
"constraintType": 0,
"referenceData": "0x00000000000000000000000000000000000000000000000000000000000003e8",
},
],
"fetcherType": 1,
"paramData": "0x00000000000000000000000012345678901234567890123456789012345678900000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000002470a08231000000000000000000000000123456789012345678901234567890123456789000000000000000000000000000000000000000000000000000000000",
}
`)
// reference data should be the same
expect(inputParamGt.constraints[0].referenceData).toBe(
inputParamLt.constraints[0].referenceData
)
expect(inputParamGt.constraints[0].referenceData).toBe(
inputParamEq.constraints[0].referenceData
)
// paramData should be the same
expect(inputParamGt.paramData).toBe(inputParamLt.paramData)
expect(inputParamGt.paramData).toBe(inputParamEq.paramData)
})
})
describe("validateAndProcessConstraints — IN range constraint", () => {
test("inRange encodes lower and upper bounds as two bytes32 values", () => {
const result = validateAndProcessConstraints([inRange(10n, 100n)])
expect(result).toHaveLength(1)
expect(result[0].constraintType).toBe(ConstraintType.IN)
// referenceData must encode two bytes32 values (64 bytes = 128 hex chars + 0x prefix)
expect(result[0].referenceData.length).toBe(2 + 128)
})
test("inRange with equal lower and upper (exact value)", () => {
const result = validateAndProcessConstraints([inRange(42n, 42n)])
expect(result).toHaveLength(1)
expect(result[0].constraintType).toBe(ConstraintType.IN)
})
test("inRange with zero lower bound", () => {
const result = validateAndProcessConstraints([inRange(0n, 1000n)])
expect(result).toHaveLength(1)
expect(result[0].constraintType).toBe(ConstraintType.IN)
})
test("inRange referenceData decodes to correct bounds", () => {
const lower = 50n
const upper = 200n
const result = validateAndProcessConstraints([inRange(lower, upper)])
const [decodedLower, decodedUpper] = decodeAbiParameters(
[{ type: "bytes32" }, { type: "bytes32" }],
result[0].referenceData as `0x${string}`
)
expect(BigInt(decodedLower)).toBe(lower)
expect(BigInt(decodedUpper)).toBe(upper)
})
test("inRange throws for negative lower bound", () => {
expect(() => validateAndProcessConstraints([inRange(-1n, 100n)])).toThrow(
"lower must be non-negative"
)
})
test("inRange throws for negative upper bound", () => {
expect(() => validateAndProcessConstraints([inRange(0n, -1n)])).toThrow(
"upper must be non-negative"
)
})
test("inRange throws when lower > upper", () => {
expect(() => validateAndProcessConstraints([inRange(100n, 50n)])).toThrow(
"lower must be <= upper"
)
})
test("inRange throws for invalid lower bound type", () => {
expect(() =>
validateAndProcessConstraints([
inRange("not-hex" as unknown as bigint, 100n)
])
).toThrow("lower must be bigint, boolean, hex, or address")
})
test("inRange can be used inside OR constraint", () => {
const result = validateAndProcessConstraints([
orConstraint([inRange(1n, 50n), greaterThanOrEqualTo(100n)])
])
expect(result).toHaveLength(1)
expect(result[0].constraintType).toBe(ConstraintType.OR)
const [subs] = decodeAbiParameters(
[CONSTRAINT_TUPLE_ABI],
result[0].referenceData as `0x${string}`
)
expect(subs).toHaveLength(2)
expect(subs[0].constraintType).toBe(ConstraintType.IN)
expect(subs[1].constraintType).toBe(ConstraintType.GTE)
})
})
describe("validateAndProcessConstraints — signed integer and OR constraints", () => {
test("greaterThanOrEqualToSigned encodes a positive bigint correctly", () => {
const result = validateAndProcessConstraints([
greaterThanOrEqualToSigned(100n)
])
expect(result).toHaveLength(1)
expect(result[0].constraintType).toBe(ConstraintType.GTE_SIGNED)
expect(result[0].referenceData).toBeDefined()
})
test("greaterThanOrEqualToSigned encodes a negative bigint correctly", () => {
const result = validateAndProcessConstraints([
greaterThanOrEqualToSigned(-42n)
])
expect(result).toHaveLength(1)
expect(result[0].constraintType).toBe(ConstraintType.GTE_SIGNED)
// negative value must be encoded in two's-complement — referenceData must be non-zero
expect(result[0].referenceData).not.toBe(
"0x0000000000000000000000000000000000000000000000000000000000000000"
)
})
test("lessThanOrEqualToSigned encodes a negative bigint correctly", () => {
const result = validateAndProcessConstraints([
lessThanOrEqualToSigned(-1n)
])
expect(result).toHaveLength(1)
expect(result[0].constraintType).toBe(ConstraintType.LTE_SIGNED)
})
test("greaterThanOrEqualToSigned throws for non-bigint value", () => {
expect(() =>
validateAndProcessConstraints([
greaterThanOrEqualToSigned("0x1234" as unknown as bigint)
])
).toThrow("signed constraints require bigint")
})
test("orConstraint encodes sub-constraints into referenceData", () => {
const result = validateAndProcessConstraints([
orConstraint([greaterThanOrEqualTo(10n), lessThanOrEqualTo(100n)])
])
expect(result).toHaveLength(1)
expect(result[0].constraintType).toBe(ConstraintType.OR)
expect(result[0].referenceData.length).toBeGreaterThan(2)
})
test("orConstraint throws for nested OR (OR inside OR)", () => {
expect(() =>
validateAndProcessConstraints([
orConstraint([
orConstraint([greaterThanOrEqualTo(1n)]) as unknown as ReturnType<
typeof greaterThanOrEqualTo
>
])
])
).toThrow("Nested OR constraints are not supported")
})
test("orConstraint throws for empty sub-constraint array", () => {
expect(() => validateAndProcessConstraints([orConstraint([])])).toThrow(
"OR constraint must have at least one sub-constraint"
)
})
test("orConstraint with signed sub-constraints encodes GTE_SIGNED and LTE_SIGNED correctly", () => {
const result = validateAndProcessConstraints([
orConstraint([
greaterThanOrEqualToSigned(-1n),
lessThanOrEqualToSigned(parseUnits("100", 6))
])
])
expect(result).toHaveLength(1)
expect(result[0].constraintType).toBe(ConstraintType.OR)
// Decode the OR's referenceData to verify sub-constraint types and encoding
const [subs] = decodeAbiParameters(
[CONSTRAINT_TUPLE_ABI],
result[0].referenceData as `0x${string}`
)
expect(subs).toHaveLength(2)
expect(subs[0].constraintType).toBe(ConstraintType.GTE_SIGNED)
expect(subs[1].constraintType).toBe(ConstraintType.LTE_SIGNED)
// -1n must be two's-complement encoded (all 0xff), not zero-padded as unsigned
expect((subs[0].referenceData as string).toLowerCase()).toContain(
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
)
// 100 USDC (1e8) must be present; non-negative so it encodes as a small positive value
expect(subs[1].referenceData).not.toBe(
"0x0000000000000000000000000000000000000000000000000000000000000000"
)
})
test("ConditionType.GTE_SIGNED maps to the signed GTE helper", () => {
const mockContract =
"0x1234567890123456789012345678901234567890" as Address
const cond = createCondition({
targetContract: mockContract,
functionAbi: erc20Abi,
functionName: "balanceOf",
args: [mockContract],
value: -5n,
type: ConditionType.GTE_SIGNED,
description: "Must be >= -5 (signed)"
})
expect(cond.constraint).toEqual({
type: ConstraintType.GTE_SIGNED,
value: -5n
})
})
test("ConditionType.LTE_SIGNED maps to the signed LTE helper", () => {
const mockContract =
"0x1234567890123456789012345678901234567890" as Address
const cond = createCondition({
targetContract: mockContract,
functionAbi: erc20Abi,
functionName: "balanceOf",
args: [mockContract],
value: 0n,
type: ConditionType.LTE_SIGNED
})
expect(cond.constraint).toEqual({
type: ConstraintType.LTE_SIGNED,
value: 0n
})
})
test("unsigned GTE still rejects negative bigint", () => {
expect(() =>
validateAndProcessConstraints([greaterThanOrEqualTo(-1n)])
).toThrow("Invalid constraint value")
})
})
describe("buildComposableCall — version guard for OR / signed-integer constraints", () => {
const TOKEN = "0x036CbD53842c5426634e7929541eC2318f3dCF7e" as Address
const SPENDER = "0x1234567890123456789012345678901234567890" as Address
const buildParams = (
constraints: ReturnType<typeof greaterThanOrEqualTo>[]
) => ({
to: TOKEN,
functionName: "transferFrom",
abi: erc20Abi,
chainId: 1,
args: [
SPENDER,
SPENDER,
runtimeERC20BalanceOf({
targetAddress: SPENDER,
tokenAddress: TOKEN,
constraints
})
]
})
test("OR constraint throws for ComposabilityVersion.V1_0_0", async () => {
await expect(
buildComposableCall(
buildParams([orConstraint([greaterThanOrEqualTo(1n)])]),
{ composabilityVersion: ComposabilityVersion.V1_0_0 }
)
).rejects.toThrow("OR constraints require Composability v1.1.1")
})
test("OR constraint throws for ComposabilityVersion.V1_1_0", async () => {
await expect(
buildComposableCall(
buildParams([orConstraint([greaterThanOrEqualTo(1n)])]),
{ composabilityVersion: ComposabilityVersion.V1_1_0 }
)
).rejects.toThrow("OR constraints require Composability v1.1.1")
})
test("OR constraint succeeds for ComposabilityVersion.V1_1_1", async () => {
const result = await buildComposableCall(
buildParams([
orConstraint([greaterThanOrEqualTo(1n), lessThanOrEqualTo(100n)])
]),
{ composabilityVersion: ComposabilityVersion.V1_1_1 }
)
expect(result.length).toBeGreaterThan(0)
})
test("GTE_SIGNED constraint throws for ComposabilityVersion.V1_0_0", async () => {
await expect(
buildComposableCall(buildParams([greaterThanOrEqualToSigned(-10n)]), {
composabilityVersion: ComposabilityVersion.V1_0_0
})
).rejects.toThrow("signed integer")
})
test("GTE_SIGNED constraint throws for ComposabilityVersion.V1_1_0", async () => {
await expect(
buildComposableCall(buildParams([greaterThanOrEqualToSigned(-10n)]), {
composabilityVersion: ComposabilityVersion.V1_1_0
})
).rejects.toThrow("signed integer")
})
test("GTE_SIGNED constraint succeeds for ComposabilityVersion.V1_1_1", async () => {
const result = await buildComposableCall(
buildParams([greaterThanOrEqualToSigned(-10n)]),
{ composabilityVersion: ComposabilityVersion.V1_1_1 }
)
expect(result.length).toBeGreaterThan(0)
})
test("LTE_SIGNED constraint throws for ComposabilityVersion.V1_1_0", async () => {
await expect(
buildComposableCall(buildParams([lessThanOrEqualToSigned(0n)]), {
composabilityVersion: ComposabilityVersion.V1_1_0
})
).rejects.toThrow("signed integer")
})
test("LTE_SIGNED constraint succeeds for ComposabilityVersion.V1_1_1", async () => {
const result = await buildComposableCall(
buildParams([lessThanOrEqualToSigned(0n)]),
{ composabilityVersion: ComposabilityVersion.V1_1_1 }
)
expect(result.length).toBeGreaterThan(0)
})
test("OR with signed sub-constraints succeeds for ComposabilityVersion.V1_1_1", async () => {
const result = await buildComposableCall(
buildParams([
orConstraint([
greaterThanOrEqualToSigned(-1n),
lessThanOrEqualToSigned(parseUnits("100", 6))
])
]),
{ composabilityVersion: ComposabilityVersion.V1_1_1 }
)
expect(result.length).toBeGreaterThan(0)
})
test("OR with signed sub-constraints throws for ComposabilityVersion.V1_1_0", async () => {
await expect(
buildComposableCall(
buildParams([
orConstraint([
greaterThanOrEqualToSigned(-1n),
lessThanOrEqualToSigned(parseUnits("100", 6))
])
]),
{ composabilityVersion: ComposabilityVersion.V1_1_0 }
)
).rejects.toThrow("OR constraints require Composability v1.1.1")
})
test("plain GTE constraint (unsigned) still works for V1_1_0", async () => {
const result = await buildComposableCall(
buildParams([greaterThanOrEqualTo(1n)]),
{ composabilityVersion: ComposabilityVersion.V1_1_0 }
)
expect(result.length).toBeGreaterThan(0)
})
})
})