-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy patherc20PeriodTransferBuilder.ts
More file actions
49 lines (43 loc) · 1.59 KB
/
Copy patherc20PeriodTransferBuilder.ts
File metadata and controls
49 lines (43 loc) · 1.59 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
import { createERC20TokenPeriodTransferTerms } from '@metamask/delegation-core';
import type { Address } from 'viem';
import type { Caveat, DeleGatorEnvironment } from '../types';
export const erc20PeriodTransfer = 'erc20PeriodTransfer';
/**
* Builds a caveat struct for ERC20PeriodTransferEnforcer.
* This enforcer validates that ERC20 token transfers do not exceed a specified amount
* within a given time period. The transferable amount resets at the beginning of each period,
* and any unused tokens are forfeited once the period ends.
*
* @param environment - The DeleGator environment.
* @param tokenAddress - The address of the ERC20 token contract.
* @param periodAmount - The maximum amount of tokens that can be transferred per period.
* @param periodDuration - The duration of each period in seconds.
* @param startDate - The timestamp when the first period begins.
* @returns The Caveat.
* @throws Error if the token address is invalid or if any of the numeric parameters are invalid.
*/
export const erc20PeriodTransferBuilder = (
environment: DeleGatorEnvironment,
tokenAddress: Address,
periodAmount: bigint,
periodDuration: number,
startDate: number,
): Caveat => {
const terms = createERC20TokenPeriodTransferTerms({
tokenAddress,
periodAmount,
periodDuration,
startDate,
});
const {
caveatEnforcers: { ERC20PeriodTransferEnforcer },
} = environment;
if (!ERC20PeriodTransferEnforcer) {
throw new Error('ERC20PeriodTransferEnforcer not found in environment');
}
return {
enforcer: ERC20PeriodTransferEnforcer,
terms,
args: '0x',
};
};