Skip to content

Commit 2cf7c45

Browse files
committed
fix(predict): infer type from schema
1 parent 6f62777 commit 2cf7c45

5 files changed

Lines changed: 21 additions & 25 deletions

File tree

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,70 @@
11
import { create, StructError } from '@metamask/superstruct';
2-
import { Hex } from './common';
2+
import { HexSchema } from './common';
33

44
describe('Hex schema', () => {
55
it('validates a string starting with 0x', () => {
66
const input = '0xabc123';
77

8-
const result = create(input, Hex);
8+
const result = create(input, HexSchema);
99

1010
expect(result).toBe('0xabc123');
1111
});
1212

1313
it('validates a minimal 0x string', () => {
1414
const input = '0x';
1515

16-
const result = create(input, Hex);
16+
const result = create(input, HexSchema);
1717

1818
expect(result).toBe('0x');
1919
});
2020

2121
it('validates a full-length Ethereum address', () => {
2222
const input = '0xe6a2026d58eaff3c7ad7ba9386fb143388002382';
2323

24-
const result = create(input, Hex);
24+
const result = create(input, HexSchema);
2525

2626
expect(result).toBe('0xe6a2026d58eaff3c7ad7ba9386fb143388002382');
2727
});
2828

2929
it('throws for a string without 0x prefix', () => {
3030
const input = 'abc123';
3131

32-
expect(() => create(input, Hex)).toThrow(StructError);
32+
expect(() => create(input, HexSchema)).toThrow(StructError);
3333
});
3434

3535
it('throws for an empty string', () => {
3636
const input = '';
3737

38-
expect(() => create(input, Hex)).toThrow(StructError);
38+
expect(() => create(input, HexSchema)).toThrow(StructError);
3939
});
4040

4141
it('throws for a number value', () => {
4242
const input = 123;
4343

44-
expect(() => create(input, Hex)).toThrow(StructError);
44+
expect(() => create(input, HexSchema)).toThrow(StructError);
4545
});
4646

4747
it('throws for null', () => {
4848
const input = null;
4949

50-
expect(() => create(input, Hex)).toThrow(StructError);
50+
expect(() => create(input, HexSchema)).toThrow(StructError);
5151
});
5252

5353
it('throws for undefined', () => {
5454
const input = undefined;
5555

56-
expect(() => create(input, Hex)).toThrow(StructError);
56+
expect(() => create(input, HexSchema)).toThrow(StructError);
5757
});
5858

5959
it('throws for a boolean value', () => {
6060
const input = true;
6161

62-
expect(() => create(input, Hex)).toThrow(StructError);
62+
expect(() => create(input, HexSchema)).toThrow(StructError);
6363
});
6464

6565
it('throws for an object', () => {
6666
const input = { value: '0x123' };
6767

68-
expect(() => create(input, Hex)).toThrow(StructError);
68+
expect(() => create(input, HexSchema)).toThrow(StructError);
6969
});
7070
});

app/components/UI/Predict/schemas/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { define } from '@metamask/superstruct';
22

3-
export const Hex = define<`0x${string}`>('Hex', (value) => {
3+
export const HexSchema = define<`0x${string}`>('Hex', (value) => {
44
if (typeof value !== 'string') {
55
return false;
66
}

app/components/UI/Predict/schemas/flags.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import {
66
object,
77
string,
88
} from '@metamask/superstruct';
9-
import { Hex } from './common';
9+
import { HexSchema } from './common';
1010
import { DEFAULT_FEE_COLLECTION_FLAG } from '../constants/flags';
1111

1212
export const PredictFeeCollectionSchema = defaulted(
1313
object({
1414
enabled: defaulted(boolean(), () => DEFAULT_FEE_COLLECTION_FLAG.enabled),
15-
collector: defaulted(Hex, () => DEFAULT_FEE_COLLECTION_FLAG.collector),
15+
collector: defaulted(
16+
HexSchema,
17+
() => DEFAULT_FEE_COLLECTION_FLAG.collector,
18+
),
1619
metamaskFee: defaulted(
1720
number(),
1821
() => DEFAULT_FEE_COLLECTION_FLAG.metamaskFee,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { Hex } from './common';
1+
export { HexSchema } from './common';
22
export { PredictFeeCollectionSchema } from './flags';

app/components/UI/Predict/types/flags.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
import type { Hex } from '@metamask/utils';
1+
import { Infer } from '@metamask/superstruct';
2+
import { PredictFeeCollectionSchema } from '../schemas';
23
import { VersionGatedFeatureFlag } from '../../../../util/remoteFeatureFlag';
34

4-
export interface PredictFeeCollection {
5-
enabled: boolean;
6-
collector: Hex;
7-
metamaskFee: number;
8-
providerFee: number;
9-
waiveList: string[];
10-
executors?: string[];
11-
permit2Enabled?: boolean;
12-
}
5+
export type PredictFeeCollection = Infer<typeof PredictFeeCollectionSchema>;
136

147
export interface PredictLiveSportsFlag {
158
enabled: boolean;

0 commit comments

Comments
 (0)