Skip to content

Commit 5be332a

Browse files
committed
Update @metamask/delegation-core encoders and decoders periodDuration as either number of bigint
- erc20tokenPeriodTransfer - nativeTokenPeriodTransfer - multiTokenPeriod - also adds missing tests for erc20TokenPeriodTransfer
1 parent 1098598 commit 5be332a

7 files changed

Lines changed: 403 additions & 16 deletions

File tree

packages/delegation-core/src/caveats/erc20TokenPeriodTransfer.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ import type { Hex } from '../types';
3030
*/
3131
export type ERC20TokenPeriodTransferTerms<
3232
TBytesLike extends BytesLike = BytesLike,
33+
TDuration extends number | bigint = number,
3334
> = {
3435
/** The address of the ERC20 token. */
3536
tokenAddress: TBytesLike;
3637
/** The maximum amount that can be transferred within each period. */
3738
periodAmount: bigint;
3839
/** The duration of each period in seconds. */
39-
periodDuration: number;
40+
periodDuration: TDuration;
4041
/** Unix timestamp when the first period begins. */
4142
startDate: number;
4243
};
@@ -52,11 +53,11 @@ export type ERC20TokenPeriodTransferTerms<
5253
* @throws Error if any of the numeric parameters are invalid.
5354
*/
5455
export function createERC20TokenPeriodTransferTerms(
55-
terms: ERC20TokenPeriodTransferTerms,
56+
terms: ERC20TokenPeriodTransferTerms<BytesLike, number | bigint>,
5657
encodingOptions?: EncodingOptions<'hex'>,
5758
): Hex;
5859
export function createERC20TokenPeriodTransferTerms(
59-
terms: ERC20TokenPeriodTransferTerms,
60+
terms: ERC20TokenPeriodTransferTerms<BytesLike, number | bigint>,
6061
encodingOptions: EncodingOptions<'bytes'>,
6162
): Uint8Array;
6263
/**
@@ -69,7 +70,7 @@ export function createERC20TokenPeriodTransferTerms(
6970
* @throws Error if any of the numeric parameters are invalid.
7071
*/
7172
export function createERC20TokenPeriodTransferTerms(
72-
terms: ERC20TokenPeriodTransferTerms,
73+
terms: ERC20TokenPeriodTransferTerms<BytesLike, number | bigint>,
7374
encodingOptions: EncodingOptions<ResultValue> = defaultOptions,
7475
): Hex | Uint8Array {
7576
const { tokenAddress, periodAmount, periodDuration, startDate } = terms;

packages/delegation-core/src/caveats/multiTokenPeriod.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,24 @@ import type { Hex } from '../types';
3131
/**
3232
* Configuration for a single token in MultiTokenPeriod terms.
3333
*/
34-
export type TokenPeriodConfig<TBytesLike extends BytesLike = BytesLike> = {
34+
export type TokenPeriodConfig<
35+
TBytesLike extends BytesLike = BytesLike,
36+
TDuration extends number | bigint = number,
37+
> = {
3538
token: TBytesLike;
3639
periodAmount: bigint;
37-
periodDuration: number;
40+
periodDuration: TDuration;
3841
startDate: number;
3942
};
4043

4144
/**
4245
* Terms for configuring a MultiTokenPeriod caveat.
4346
*/
44-
export type MultiTokenPeriodTerms<TBytesLike extends BytesLike = BytesLike> = {
45-
tokenConfigs: TokenPeriodConfig<TBytesLike>[];
47+
export type MultiTokenPeriodTerms<
48+
TBytesLike extends BytesLike = BytesLike,
49+
TDuration extends number | bigint = number,
50+
> = {
51+
tokenConfigs: TokenPeriodConfig<TBytesLike, TDuration>[];
4652
};
4753

4854
/**
@@ -54,11 +60,11 @@ export type MultiTokenPeriodTerms<TBytesLike extends BytesLike = BytesLike> = {
5460
* @throws Error if the tokenConfigs array is empty or contains invalid parameters.
5561
*/
5662
export function createMultiTokenPeriodTerms(
57-
terms: MultiTokenPeriodTerms,
63+
terms: MultiTokenPeriodTerms<BytesLike, number | bigint>,
5864
encodingOptions?: EncodingOptions<'hex'>,
5965
): Hex;
6066
export function createMultiTokenPeriodTerms(
61-
terms: MultiTokenPeriodTerms,
67+
terms: MultiTokenPeriodTerms<BytesLike, number | bigint>,
6268
encodingOptions: EncodingOptions<'bytes'>,
6369
): Uint8Array;
6470
/**
@@ -70,7 +76,7 @@ export function createMultiTokenPeriodTerms(
7076
* @throws Error if the tokenConfigs array is empty or contains invalid parameters.
7177
*/
7278
export function createMultiTokenPeriodTerms(
73-
terms: MultiTokenPeriodTerms,
79+
terms: MultiTokenPeriodTerms<BytesLike, number | bigint>,
7480
encodingOptions: EncodingOptions<ResultValue> = defaultOptions,
7581
): Hex | Uint8Array {
7682
const { tokenConfigs } = terms;

packages/delegation-core/src/caveats/nativeTokenPeriodTransfer.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ import type { Hex } from '../types';
2626
/**
2727
* Terms for configuring a periodic transfer allowance of native tokens.
2828
*/
29-
export type NativeTokenPeriodTransferTerms = {
29+
export type NativeTokenPeriodTransferTerms<
30+
TDuration extends number | bigint = number,
31+
> = {
3032
/** The maximum amount that can be transferred within each period (in wei). */
3133
periodAmount: bigint;
3234
/** The duration of each period in seconds. */
33-
periodDuration: number;
35+
periodDuration: TDuration;
3436
/** Unix timestamp when the first period begins. */
3537
startDate: number;
3638
};
@@ -46,11 +48,11 @@ export type NativeTokenPeriodTransferTerms = {
4648
* @throws Error if any of the numeric parameters are invalid.
4749
*/
4850
export function createNativeTokenPeriodTransferTerms(
49-
terms: NativeTokenPeriodTransferTerms,
51+
terms: NativeTokenPeriodTransferTerms<number | bigint>,
5052
encodingOptions?: EncodingOptions<'hex'>,
5153
): Hex;
5254
export function createNativeTokenPeriodTransferTerms(
53-
terms: NativeTokenPeriodTransferTerms,
55+
terms: NativeTokenPeriodTransferTerms<number | bigint>,
5456
encodingOptions: EncodingOptions<'bytes'>,
5557
): Uint8Array;
5658
/**
@@ -63,7 +65,7 @@ export function createNativeTokenPeriodTransferTerms(
6365
* @throws Error if any of the numeric parameters are invalid.
6466
*/
6567
export function createNativeTokenPeriodTransferTerms(
66-
terms: NativeTokenPeriodTransferTerms,
68+
terms: NativeTokenPeriodTransferTerms<number | bigint>,
6769
encodingOptions: EncodingOptions<ResultValue> = defaultOptions,
6870
): Hex | Uint8Array {
6971
const { periodAmount, periodDuration, startDate } = terms;

packages/delegation-core/test/caveats/decoders.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,20 @@ describe('Terms Decoders', () => {
319319
const decoded = decodeNativeTokenPeriodTransferTerms(encoded);
320320
expect(decoded).toEqual(original);
321321
});
322+
323+
it('decodes bigint periodDuration input as number', () => {
324+
const encoded = createNativeTokenPeriodTransferTerms({
325+
periodAmount: 1000000000000000000n,
326+
periodDuration: 86400n,
327+
startDate: 1640995200,
328+
});
329+
const decoded = decodeNativeTokenPeriodTransferTerms(encoded);
330+
expect(decoded).toEqual({
331+
periodAmount: 1000000000000000000n,
332+
periodDuration: 86400,
333+
startDate: 1640995200,
334+
});
335+
});
322336
});
323337

324338
describe('decodeNativeTokenStreamingTerms', () => {
@@ -385,6 +399,24 @@ describe('Terms Decoders', () => {
385399
expect(decoded).toEqual(original);
386400
});
387401

402+
it('decodes bigint periodDuration input as number', () => {
403+
const encoded = createERC20TokenPeriodTransferTerms({
404+
tokenAddress:
405+
'0x1234567890123456789012345678901234567890' as `0x${string}`,
406+
periodAmount: 1000000000000000000n,
407+
periodDuration: 86400n,
408+
startDate: 1640995200,
409+
});
410+
const decoded = decodeERC20TokenPeriodTransferTerms(encoded);
411+
expect(decoded).toEqual({
412+
tokenAddress:
413+
'0x1234567890123456789012345678901234567890' as `0x${string}`,
414+
periodAmount: 1000000000000000000n,
415+
periodDuration: 86400,
416+
startDate: 1640995200,
417+
});
418+
});
419+
388420
it('throws when encoded terms are not exactly 116 bytes', () => {
389421
expect(() =>
390422
decodeERC20TokenPeriodTransferTerms(`0x${'00'.repeat(115)}`),
@@ -525,6 +557,46 @@ describe('Terms Decoders', () => {
525557
const decoded = decodeMultiTokenPeriodTerms(encoded);
526558
expect(decoded).toEqual(original);
527559
});
560+
561+
it('decodes bigint periodDuration input as number', () => {
562+
const encoded = createMultiTokenPeriodTerms({
563+
tokenConfigs: [
564+
{
565+
token:
566+
'0x1234567890123456789012345678901234567890' as `0x${string}`,
567+
periodAmount: 1000000000000000000n,
568+
periodDuration: 86400n,
569+
startDate: 1640995200,
570+
},
571+
{
572+
token:
573+
'0xabcdefabcdefabcdefabcdefabcdefabcdefabcd' as `0x${string}`,
574+
periodAmount: 2000000000000000000n,
575+
periodDuration: 172800n,
576+
startDate: 1640995200,
577+
},
578+
],
579+
});
580+
const decoded = decodeMultiTokenPeriodTerms(encoded);
581+
expect(decoded).toEqual({
582+
tokenConfigs: [
583+
{
584+
token:
585+
'0x1234567890123456789012345678901234567890' as `0x${string}`,
586+
periodAmount: 1000000000000000000n,
587+
periodDuration: 86400,
588+
startDate: 1640995200,
589+
},
590+
{
591+
token:
592+
'0xabcdefabcdefabcdefabcdefabcdefabcdefabcd' as `0x${string}`,
593+
periodAmount: 2000000000000000000n,
594+
periodDuration: 172800,
595+
startDate: 1640995200,
596+
},
597+
],
598+
});
599+
});
528600
});
529601

530602
describe('decodeSpecificActionERC20TransferBatchTerms', () => {

0 commit comments

Comments
 (0)