-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathdeployedBuilder.test.ts
More file actions
104 lines (84 loc) · 3.35 KB
/
Copy pathdeployedBuilder.test.ts
File metadata and controls
104 lines (84 loc) · 3.35 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
import { concat, pad, size, type Address, type Hex } from 'viem';
import { expect, describe, it } from 'vitest';
import { deployedBuilder } from '../../src/caveatBuilder/deployedBuilder';
import type { DeleGatorEnvironment } from '../../src/types';
import { randomAddress, randomBytes } from '../utils';
describe('deployedBuilder()', () => {
const EXPECTED_TERMS_LENGTH_EXCLUDING_BYTECODE = 52; // 20 bytes for contractAddress + 32 bytes for salt
const environment = {
caveatEnforcers: { DeployedEnforcer: randomAddress() },
} as any as DeleGatorEnvironment;
const buildWithDetails = (
contractAddress: Address,
salt: Hex,
bytecode: Hex,
) => {
return deployedBuilder(environment, contractAddress, salt, bytecode);
};
const validContractAddress = randomAddress();
const validSalt = randomBytes(32);
const validBytecode = randomBytes(256);
describe('validation', () => {
it("should fail with contractAddress that isn't a valid address", () => {
const invalidContractAddress = 'invalid-address' as Address;
expect(() =>
buildWithDetails(invalidContractAddress, validSalt, validBytecode),
).to.throw('Invalid contractAddress: must be a valid Ethereum address');
});
it('should fail with contractAddress that is hex, but invalid length', () => {
const invalidContractAddress = randomBytes(10);
expect(() =>
buildWithDetails(invalidContractAddress, validSalt, validBytecode),
).to.throw('Invalid contractAddress: must be a valid Ethereum address');
});
it("should allow valid contractAddress, that's not checksummed", () => {
// we uppercase here, because lowercase is considered valid by `isAddress`
const nonChecksummedAddress: Address = `0x${randomAddress()
.slice(2)
.toUpperCase()}`;
expect(() =>
buildWithDetails(nonChecksummedAddress, validSalt, validBytecode),
).to.not.throw();
});
it('should fail with invalid salt', () => {
const invalidSalt = 'invalid-salt' as Hex;
expect(() =>
buildWithDetails(validContractAddress, invalidSalt, validBytecode),
).to.throw('Invalid salt: must be a valid hexadecimal string');
});
it('should fail with invalid bytecode', () => {
const invalidBytecode = 'invalid-bytecode' as Hex;
expect(() =>
buildWithDetails(validContractAddress, validSalt, invalidBytecode),
).to.throw('Invalid bytecode: must be a valid hexadecimal string');
});
});
describe('builds a caveat', () => {
it('should build a caveat with valid inputs', () => {
const caveat = buildWithDetails(
validContractAddress,
validSalt,
validBytecode,
);
const terms = concat([
validContractAddress,
pad(validSalt, { size: 32 }),
validBytecode,
]);
expect(caveat).to.deep.equal({
enforcer: environment.caveatEnforcers.DeployedEnforcer,
terms,
args: '0x',
});
});
});
it('should create a caveat with terms length matching number of targets', () => {
const contractAddress = randomAddress();
const salt = randomBytes(32);
const bytecode = randomBytes(256);
const caveat = buildWithDetails(contractAddress, salt, bytecode);
expect(size(caveat.terms)).to.equal(
EXPECTED_TERMS_LENGTH_EXCLUDING_BYTECODE + size(bytecode),
);
});
});