-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy patherc20TransferAmountBuilder.test.ts
More file actions
90 lines (74 loc) · 2.99 KB
/
Copy patherc20TransferAmountBuilder.test.ts
File metadata and controls
90 lines (74 loc) · 2.99 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
import { concat, size, toHex } from 'viem';
import type { Address } from 'viem';
import { expect, describe, it } from 'vitest';
import { erc20TransferAmountBuilder } from '../../src/caveatBuilder/erc20TransferAmountBuilder';
import type { DeleGatorEnvironment } from '../../src/types';
import { randomAddress } from '../utils';
describe('erc20TransferAmountBuilder()', () => {
const EXPECTED_TERMS_LENGTH = 52; // 20 bytes for token address + 32 bytes for max amount
const environment = {
caveatEnforcers: { ERC20TransferAmountEnforcer: randomAddress() },
} as any as DeleGatorEnvironment;
const buildWithParams = (tokenAddress: Address, maxAmount: bigint) => {
return erc20TransferAmountBuilder(environment, tokenAddress, maxAmount);
};
describe('validation', () => {
it('should fail with an invalid token address', () => {
const invalidAddress = 'invalid-address' as Address;
expect(() => buildWithParams(invalidAddress, 1000n)).to.throw(
'Invalid tokenAddress: must be a valid address',
);
});
it('should fail with a non-positive max amount', () => {
const validAddress = randomAddress();
expect(() => buildWithParams(validAddress, 0n)).to.throw(
'Invalid maxAmount: must be a positive number',
);
expect(() => buildWithParams(validAddress, -1n)).to.throw(
'Invalid maxAmount: must be a positive number',
);
});
it('should allow a valid address that is not checksummed', () => {
const nonChecksummedAddress = randomAddress().toLowerCase() as Address;
expect(() =>
buildWithParams(nonChecksummedAddress, 1000n),
).to.not.throw();
});
});
describe('builds a caveat', () => {
it('should build a caveat with valid parameters', () => {
const tokenAddress = randomAddress();
const maxAmount = 1000000n;
const caveat = buildWithParams(tokenAddress, maxAmount);
const expectedTerms = concat([
tokenAddress,
toHex(maxAmount, { size: 32 }),
]);
expect(caveat).to.deep.equal({
enforcer: environment.caveatEnforcers.ERC20TransferAmountEnforcer,
terms: expectedTerms,
args: '0x',
});
});
it('should correctly handle large max amounts', () => {
const tokenAddress = randomAddress();
const maxAmount = 2n ** 256n - 1n; // Maximum possible value for uint256
const caveat = buildWithParams(tokenAddress, maxAmount);
const expectedTerms = concat([
tokenAddress,
toHex(maxAmount, { size: 32 }),
]);
expect(caveat).to.deep.equal({
enforcer: environment.caveatEnforcers.ERC20TransferAmountEnforcer,
terms: expectedTerms,
args: '0x',
});
});
});
it('should create a caveat with terms length matching number of targets', () => {
const tokenAddress = randomAddress();
const maxAmount = 1000n;
const caveat = buildWithParams(tokenAddress, maxAmount);
expect(size(caveat.terms)).to.equal(EXPECTED_TERMS_LENGTH);
});
});