@@ -15,32 +15,31 @@ export type FeesFormContext = {
1515
1616const 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
99135export type FeesFormValues = yup . InferType < typeof feesFormValidationSchema > ;
0 commit comments