|
| 1 | +import { |
| 2 | + defaultOptions, |
| 3 | + prepareResult, |
| 4 | + type EncodingOptions, |
| 5 | + type ResultValue, |
| 6 | +} from '../returns'; |
| 7 | +import type { Hex } from '../types'; |
| 8 | +import { toHexString } from '../utils'; |
| 9 | +import { type BytesLike, isHexString, bytesToHex } from '@metamask/utils'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Terms for configuring a periodic transfer allowance of ERC20 tokens. |
| 13 | + */ |
| 14 | +export type ERC20TokenPeriodTransferTerms = { |
| 15 | + /** The address of the ERC20 token. */ |
| 16 | + tokenAddress: BytesLike; |
| 17 | + /** The maximum amount that can be transferred within each period. */ |
| 18 | + periodAmount: bigint; |
| 19 | + /** The duration of each period in seconds. */ |
| 20 | + periodDuration: number; |
| 21 | + /** Unix timestamp when the first period begins. */ |
| 22 | + startDate: number; |
| 23 | +}; |
| 24 | + |
| 25 | +/** |
| 26 | + * Creates terms for an ERC20TokenPeriodTransfer caveat that validates that ERC20 token transfers |
| 27 | + * do not exceed a specified amount within a given time period. The transferable amount resets at the |
| 28 | + * beginning of each period, and any unused tokens are forfeited once the period ends. |
| 29 | + * |
| 30 | + * @param terms - The terms for the ERC20TokenPeriodTransfer caveat. |
| 31 | + * @param encodingOptions - The encoding options for the result. |
| 32 | + * @returns The terms as a 128-byte hex string (32 bytes for each parameter). |
| 33 | + * @throws Error if any of the numeric parameters are invalid. |
| 34 | + */ |
| 35 | +export function createERC20TokenPeriodTransferTerms( |
| 36 | + terms: ERC20TokenPeriodTransferTerms, |
| 37 | + encodingOptions?: EncodingOptions<'hex'>, |
| 38 | +): Hex; |
| 39 | +export function createERC20TokenPeriodTransferTerms( |
| 40 | + terms: ERC20TokenPeriodTransferTerms, |
| 41 | + encodingOptions: EncodingOptions<'bytes'>, |
| 42 | +): Uint8Array; |
| 43 | +/** |
| 44 | + * Creates terms for an ERC20TokenPeriodTransfer caveat that validates that ERC20 token transfers |
| 45 | + * do not exceed a specified amount within a given time period. |
| 46 | + * |
| 47 | + * @param terms - The terms for the ERC20TokenPeriodTransfer caveat. |
| 48 | + * @param encodingOptions - The encoding options for the result. |
| 49 | + * @returns The terms as a 128-byte hex string (32 bytes for each parameter). |
| 50 | + * @throws Error if any of the numeric parameters are invalid. |
| 51 | + */ |
| 52 | +export function createERC20TokenPeriodTransferTerms( |
| 53 | + terms: ERC20TokenPeriodTransferTerms, |
| 54 | + encodingOptions: EncodingOptions<ResultValue> = defaultOptions, |
| 55 | +): Hex | Uint8Array { |
| 56 | + const { tokenAddress, periodAmount, periodDuration, startDate } = terms; |
| 57 | + |
| 58 | + if (!tokenAddress) { |
| 59 | + throw new Error('Invalid tokenAddress: must be a valid address'); |
| 60 | + } |
| 61 | + |
| 62 | + let prefixedTokenAddressHex: string; |
| 63 | + |
| 64 | + if (typeof tokenAddress === 'string') { |
| 65 | + if (!isHexString(tokenAddress) || tokenAddress.length !== 42) { |
| 66 | + throw new Error('Invalid tokenAddress: must be a valid address'); |
| 67 | + } |
| 68 | + prefixedTokenAddressHex = tokenAddress; |
| 69 | + } else { |
| 70 | + if (tokenAddress.length !== 20) { |
| 71 | + throw new Error('Invalid tokenAddress: must be a valid address'); |
| 72 | + } |
| 73 | + prefixedTokenAddressHex = bytesToHex(tokenAddress); |
| 74 | + } |
| 75 | + |
| 76 | + if (periodAmount <= 0n) { |
| 77 | + throw new Error('Invalid periodAmount: must be a positive number'); |
| 78 | + } |
| 79 | + |
| 80 | + if (periodDuration <= 0) { |
| 81 | + throw new Error('Invalid periodDuration: must be a positive number'); |
| 82 | + } |
| 83 | + |
| 84 | + if (startDate <= 0) { |
| 85 | + throw new Error('Invalid startDate: must be a positive number'); |
| 86 | + } |
| 87 | + |
| 88 | + const periodAmountHex = toHexString({ value: periodAmount, size: 32 }); |
| 89 | + const periodDurationHex = toHexString({ value: periodDuration, size: 32 }); |
| 90 | + const startDateHex = toHexString({ value: startDate, size: 32 }); |
| 91 | + |
| 92 | + const hexValue = `${prefixedTokenAddressHex}${periodAmountHex}${periodDurationHex}${startDateHex}`; |
| 93 | + |
| 94 | + return prepareResult(hexValue, encodingOptions); |
| 95 | +} |
0 commit comments