-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathexactExecutionBatchBuilder.test.ts
More file actions
122 lines (110 loc) · 3.29 KB
/
Copy pathexactExecutionBatchBuilder.test.ts
File metadata and controls
122 lines (110 loc) · 3.29 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
import { encodeAbiParameters } from 'viem';
import type { Address, Hex } from 'viem';
import { expect, describe, it } from 'vitest';
import { exactExecutionBatchBuilder } from '../../src/caveatBuilder/exactExecutionBatchBuilder';
import type { DeleGatorEnvironment } from '../../src/types';
import { randomAddress } from '../utils';
describe('exactExecutionBatchBuilder()', () => {
const environment = {
caveatEnforcers: { ExactExecutionBatchEnforcer: randomAddress() },
} as any as DeleGatorEnvironment;
const buildWithParams = (
executions: {
target: Address;
value: bigint;
callData: Hex;
}[],
) => {
const config = { executions };
return exactExecutionBatchBuilder(environment, config);
};
describe('validation', () => {
it('should fail with an empty executions array', () => {
expect(() => buildWithParams([])).to.throw(
'Invalid executions: array cannot be empty',
);
});
it('should fail with an invalid target address', () => {
const invalidAddress = 'invalid-address' as any;
expect(() =>
buildWithParams([
{
target: invalidAddress,
value: 0n,
callData: '0x',
},
]),
).to.throw('Invalid target: must be a valid address');
});
it('should fail with a negative value', () => {
expect(() =>
buildWithParams([
{
target: randomAddress(),
value: -1n,
callData: '0x',
},
]),
).to.throw('Invalid value: must be a non-negative number');
});
it('should fail with invalid callData format', () => {
expect(() =>
buildWithParams([
{
target: randomAddress(),
value: 0n,
callData: 'invalid' as any,
},
]),
).to.throw('Invalid calldata: must be a hex string starting with 0x');
});
it('should allow valid addresses that are not checksummed', () => {
const nonChecksummedAddress = randomAddress().toLowerCase() as Address;
expect(() =>
buildWithParams([
{
target: nonChecksummedAddress,
value: 0n,
callData: '0x',
},
]),
).to.not.throw();
});
});
describe('builds a caveat', () => {
it('should build a caveat with valid parameters', () => {
const executions = [
{
target: randomAddress(),
value: 1000000000000000000n, // 1 ETH
callData: '0x12345678' as const,
},
{
target: randomAddress(),
value: 0n,
callData: '0xabcdef' as const,
},
];
const caveat = buildWithParams(executions);
expect(caveat.enforcer).to.equal(
environment.caveatEnforcers.ExactExecutionBatchEnforcer,
);
expect(caveat.args).to.equal('0x');
// Verify terms encoding
const expectedTerms = encodeAbiParameters(
[
{
type: 'tuple[]',
components: [
{ type: 'address', name: 'target' },
{ type: 'uint256', name: 'value' },
{ type: 'bytes', name: 'callData' },
],
},
],
[executions],
);
expect(caveat.terms).to.equal(expectedTerms);
});
});
});