-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathmultiTokenPeriodBuilder.test.ts
More file actions
127 lines (110 loc) · 4.21 KB
/
Copy pathmultiTokenPeriodBuilder.test.ts
File metadata and controls
127 lines (110 loc) · 4.21 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
import type { Hex } from 'viem';
import { concat, pad, size, slice, toHex } from 'viem';
import { expect, describe, it } from 'vitest';
import type { TokenPeriodConfig } from '../../src/caveatBuilder/multiTokenPeriodBuilder';
import {
multiTokenPeriodBuilder,
multiTokenPeriod,
} from '../../src/caveatBuilder/multiTokenPeriodBuilder';
import type { DeleGatorEnvironment } from '../../src/types';
import { randomAddress } from '../utils';
const LENGTH_PER_CONFIG = 116;
describe('multiTokenPeriodBuilder', () => {
const mockEnvironment: DeleGatorEnvironment = {
caveatEnforcers: {
MultiTokenPeriodEnforcer: randomAddress(),
},
} as any as DeleGatorEnvironment;
const mockConfig: TokenPeriodConfig = {
token: randomAddress(),
periodAmount: 1000n,
periodDuration: 86400, // 1 day in seconds
startDate: 1672531200, // Jan 1, 2023 00:00:00 UTC
};
const secondMockConfig: TokenPeriodConfig = {
token: randomAddress(),
periodAmount: 2000n,
periodDuration: 3600, // 1 hour in seconds
startDate: 1672531200, // Jan 1, 2023 00:00:00 UTC
};
it('should have the correct name constant', () => {
expect(multiTokenPeriod).to.equal('multiTokenPeriod');
});
it('should encode a single token configuration correctly', () => {
const result = multiTokenPeriodBuilder(mockEnvironment, {
tokenConfigs: [mockConfig],
});
expect(size(result.terms)).to.equal(LENGTH_PER_CONFIG);
expect(slice(result.terms, 0, 20)).to.equal(
pad(mockConfig.token, { size: 20 }),
);
expect(result.enforcer).to.equal(
mockEnvironment.caveatEnforcers.MultiTokenPeriodEnforcer,
);
expect(result.args).to.equal('0x');
});
it('should encode multiple token correctly', () => {
const result = multiTokenPeriodBuilder(mockEnvironment as any, {
tokenConfigs: [mockConfig, secondMockConfig],
});
expect(result.enforcer).to.equal(
mockEnvironment.caveatEnforcers.MultiTokenPeriodEnforcer,
);
expect(result.args).to.equal('0x');
expect(result.terms).to.equal(
concat([
pad(mockConfig.token, { size: 20 }),
toHex(mockConfig.periodAmount, { size: 32 }),
toHex(mockConfig.periodDuration, { size: 32 }),
toHex(mockConfig.startDate, { size: 32 }),
pad(secondMockConfig.token, { size: 20 }),
toHex(secondMockConfig.periodAmount, { size: 32 }),
toHex(secondMockConfig.periodDuration, { size: 32 }),
toHex(secondMockConfig.startDate, { size: 32 }),
]),
);
});
it('should throw error for empty configs array', () => {
expect(() =>
multiTokenPeriodBuilder(mockEnvironment as any, { tokenConfigs: [] }),
).to.throw('MultiTokenPeriodBuilder: tokenConfigs array cannot be empty');
});
it('should encode numeric values correctly', () => {
const result = multiTokenPeriodBuilder(mockEnvironment as any, {
tokenConfigs: [mockConfig],
});
expect(slice(result.terms, 20, 20 + 32)).to.equal(
toHex(mockConfig.periodAmount, { size: 32 }),
);
expect(slice(result.terms, 20 + 32, 20 + 32 + 32)).to.equal(
toHex(mockConfig.periodDuration, { size: 32 }),
);
});
it('should validate terms length is multiple of 116 bytes', () => {
const result = multiTokenPeriodBuilder(mockEnvironment as any, {
tokenConfigs: [mockConfig, secondMockConfig],
});
expect(size(result.terms)).to.equal(LENGTH_PER_CONFIG * 2);
});
it('should throw error for invalid token address', () => {
expect(() =>
multiTokenPeriodBuilder(mockEnvironment as any, {
tokenConfigs: [{ ...mockConfig, token: 'invalid' as Hex }],
}),
).to.throw('Invalid token address: invalid');
});
it('should throw error for invalid period amount', () => {
expect(() =>
multiTokenPeriodBuilder(mockEnvironment as any, {
tokenConfigs: [{ ...mockConfig, periodAmount: 0n }],
}),
).to.throw('Invalid period amount: must be greater than 0');
});
it('should throw error for invalid period duration', () => {
expect(() =>
multiTokenPeriodBuilder(mockEnvironment as any, {
tokenConfigs: [{ ...mockConfig, periodDuration: 0 }],
}),
).to.throw('Invalid period duration: must be greater than 0');
});
});