Skip to content

Commit e32b080

Browse files
committed
feat(suite-native): create eip1559 custom fee validations
1 parent 9277539 commit e32b080

File tree

2 files changed

+61
-25
lines changed

2 files changed

+61
-25
lines changed

suite-native/transaction-management/src/components/fees/CustomFee/CustomFeeInputs.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const CustomFeeInputs = ({ symbol }: CustomFeeInputsProps) => {
7676
keyboardType="number-pad"
7777
onChangeText={handleFieldChangeValue(
7878
maxFeePerGasFieldName,
79-
integerTransformer,
79+
cryptoAmountTransformer,
8080
)}
8181
/>
8282
<TextInputField
@@ -89,7 +89,7 @@ export const CustomFeeInputs = ({ symbol }: CustomFeeInputsProps) => {
8989
keyboardType="number-pad"
9090
onChangeText={handleFieldChangeValue(
9191
maxPriorityFeePerGasFieldName,
92-
integerTransformer,
92+
cryptoAmountTransformer,
9393
)}
9494
/>
9595
</>

suite-native/transaction-management/src/feesFormSchema.ts

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,31 @@ export type FeesFormContext = {
1515

1616
const FeeLevelLabels: Array<FeeLevelLabel> = ['economy', 'normal', 'high', 'custom'];
1717

18-
export const feesFormValidationSchema = yup.object({
19-
feeLevel: yup.string().oneOf(FeeLevelLabels).required('Fee level is required'),
20-
customFeePerUnit: yup
21-
.string()
22-
.required()
23-
.test(
24-
'too-many-decimals',
25-
'Too many decimals.',
26-
(value, { options: { context } }: yup.TestContext<FeesFormContext>) => {
27-
if (!value) return true;
18+
const createFeeDecimalsTest = (
19+
value: string | undefined,
20+
{ options: { context } }: yup.TestContext<FeesFormContext>,
21+
) => {
22+
if (!value) return true;
2823

29-
const { networkFeeInfo, symbol } = context!;
24+
const { networkFeeInfo, symbol } = context!;
3025

31-
if (!symbol) return false;
26+
if (!symbol) return false;
3227

33-
const { networkType } = getNetwork(symbol);
28+
const { networkType } = getNetwork(symbol);
3429

35-
if (!networkFeeInfo || !networkType) return false;
30+
if (!networkFeeInfo || !networkType) return false;
3631

37-
const maxDecimals = getFeeDecimals({ symbol });
32+
const maxDecimals = getFeeDecimals({ symbol });
3833

39-
if (maxDecimals === null) return true;
34+
if (maxDecimals === null) return true;
4035

41-
return isDecimalsValid(value, maxDecimals);
42-
},
43-
)
36+
return isDecimalsValid(value, maxDecimals);
37+
};
38+
39+
const createFeeValidation = () =>
40+
yup
41+
.string()
42+
.test('too-many-decimals', 'Too many decimals.', createFeeDecimalsTest)
4443
.test(
4544
'fee-too-low',
4645
'Fee is too low.',
@@ -69,7 +68,11 @@ export const feesFormValidationSchema = yup.object({
6968

7069
return feeBig.lte(maxFee);
7170
},
72-
),
71+
);
72+
73+
export const feesFormValidationSchema = yup.object({
74+
feeLevel: yup.string().oneOf(FeeLevelLabels).required('Fee level is required'),
75+
customFeePerUnit: createFeeValidation().required(),
7376
customFeeLimit: yup
7477
.string()
7578
.test(
@@ -91,9 +94,42 @@ export const feesFormValidationSchema = yup.object({
9194
return feeBig.gte(minimalFeeLimit);
9295
},
9396
),
94-
// FIXME: add validations
95-
customMaxFeePerGas: yup.string(),
96-
customMaxPriorityFeePerGas: yup.string(),
97+
// EIP1559 only validations
98+
customMaxFeePerGas: createFeeValidation()
99+
.test(
100+
'custom-max-fee-per-gas-higher-than-priority',
101+
'Max fee must be higher than or equal to priority fee.',
102+
function (value) {
103+
if (!value) return true;
104+
105+
const { customMaxPriorityFeePerGas } = this.parent;
106+
107+
if (!customMaxPriorityFeePerGas) return true;
108+
109+
return Number(value) > Number(customMaxPriorityFeePerGas);
110+
},
111+
)
112+
.required(),
113+
customMaxPriorityFeePerGas: yup
114+
.string()
115+
.test('too-many-decimals', 'Too many decimals.', createFeeDecimalsTest)
116+
.test(
117+
'custom-priority-fee-per-gas',
118+
'Priority fee higher than max fee or below minimum.',
119+
function (value) {
120+
if (!value) return true;
121+
const { networkFeeInfo } = this.options.context!;
122+
123+
if (!networkFeeInfo) return false;
124+
const { minPriorityFee } = networkFeeInfo;
125+
126+
if (Number(value) < minPriorityFee) return false;
127+
128+
const { customMaxFeePerGas } = this.parent;
129+
130+
return !(customMaxFeePerGas && Number(value) > Number(customMaxFeePerGas));
131+
},
132+
),
97133
});
98134

99135
export type FeesFormValues = yup.InferType<typeof feesFormValidationSchema>;

0 commit comments

Comments
 (0)