|
| 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 | + |
| 10 | +// Upper bound for timestamps (January 1, 10000 CE) |
| 11 | +const TIMESTAMP_UPPER_BOUND_SECONDS = 253402300799; |
| 12 | + |
| 13 | +/** |
| 14 | + * Terms for configuring a linear streaming allowance of native tokens. |
| 15 | + */ |
| 16 | +export type NativeTokenStreamingTerms = { |
| 17 | + /** The initial amount available immediately (in wei). */ |
| 18 | + initialAmount: bigint; |
| 19 | + /** The maximum total amount that can be transferred (in wei). */ |
| 20 | + maxAmount: bigint; |
| 21 | + /** The rate at which allowance increases per second (in wei). */ |
| 22 | + amountPerSecond: bigint; |
| 23 | + /** Unix timestamp when streaming begins. */ |
| 24 | + startTime: number; |
| 25 | +}; |
| 26 | + |
| 27 | +/** |
| 28 | + * Creates terms for the NativeTokenStreaming caveat, configuring a linear |
| 29 | + * streaming allowance of native tokens. |
| 30 | + * |
| 31 | + * @param terms - The terms for the NativeTokenStreaming caveat. |
| 32 | + * @param encodingOptions - The encoding options for the result. |
| 33 | + * @returns Hex-encoded terms for the caveat (128 bytes). |
| 34 | + * @throws Error if initialAmount is negative. |
| 35 | + * @throws Error if maxAmount is not positive. |
| 36 | + * @throws Error if maxAmount is less than initialAmount. |
| 37 | + * @throws Error if amountPerSecond is not positive. |
| 38 | + * @throws Error if startTime is not positive. |
| 39 | + * @throws Error if startTime exceeds upper bound. |
| 40 | + */ |
| 41 | +export function createNativeTokenStreamingTerms( |
| 42 | + terms: NativeTokenStreamingTerms, |
| 43 | + encodingOptions?: EncodingOptions<'hex'>, |
| 44 | +): Hex; |
| 45 | +export function createNativeTokenStreamingTerms( |
| 46 | + terms: NativeTokenStreamingTerms, |
| 47 | + encodingOptions: EncodingOptions<'bytes'>, |
| 48 | +): Uint8Array; |
| 49 | +/** |
| 50 | + * Creates terms for the NativeTokenStreaming caveat, configuring a linear |
| 51 | + * streaming allowance of native tokens. |
| 52 | + * |
| 53 | + * @param terms - The terms for the NativeTokenStreaming caveat. |
| 54 | + * @param encodingOptions - The encoding options for the result. |
| 55 | + * @returns The terms as a 128-byte hex string. |
| 56 | + * @throws Error if any of the numeric parameters are invalid. |
| 57 | + */ |
| 58 | +export function createNativeTokenStreamingTerms( |
| 59 | + terms: NativeTokenStreamingTerms, |
| 60 | + encodingOptions: EncodingOptions<ResultValue> = defaultOptions, |
| 61 | +): Hex | Uint8Array { |
| 62 | + const { initialAmount, maxAmount, amountPerSecond, startTime } = terms; |
| 63 | + |
| 64 | + if (initialAmount < 0n) { |
| 65 | + throw new Error('Invalid initialAmount: must be greater than zero'); |
| 66 | + } |
| 67 | + |
| 68 | + if (maxAmount <= 0n) { |
| 69 | + throw new Error('Invalid maxAmount: must be a positive number'); |
| 70 | + } |
| 71 | + |
| 72 | + if (maxAmount < initialAmount) { |
| 73 | + throw new Error('Invalid maxAmount: must be greater than initialAmount'); |
| 74 | + } |
| 75 | + |
| 76 | + if (amountPerSecond <= 0n) { |
| 77 | + throw new Error('Invalid amountPerSecond: must be a positive number'); |
| 78 | + } |
| 79 | + |
| 80 | + if (startTime <= 0) { |
| 81 | + throw new Error('Invalid startTime: must be a positive number'); |
| 82 | + } |
| 83 | + |
| 84 | + if (startTime > TIMESTAMP_UPPER_BOUND_SECONDS) { |
| 85 | + throw new Error( |
| 86 | + 'Invalid startTime: must be less than or equal to 253402300799', |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + const initialAmountHex = toHexString({ value: initialAmount, size: 32 }); |
| 91 | + const maxAmountHex = toHexString({ value: maxAmount, size: 32 }); |
| 92 | + const amountPerSecondHex = toHexString({ value: amountPerSecond, size: 32 }); |
| 93 | + const startTimeHex = toHexString({ value: startTime, size: 32 }); |
| 94 | + |
| 95 | + const hexValue = `0x${initialAmountHex}${maxAmountHex}${amountPerSecondHex}${startTimeHex}`; |
| 96 | + |
| 97 | + return prepareResult(hexValue, encodingOptions); |
| 98 | +} |
0 commit comments