@@ -13,34 +13,33 @@ export type FeesFormContext = {
1313 minimalFeeLimit ?: string ;
1414} ;
1515
16- const FeeLevelLabels : Array < FeeLevelLabel > = [ 'economy' , 'normal' , 'high' , 'custom' ] ;
16+ const FeeLevelLabels : Array < FeeLevelLabel > = [ 'economy' , 'low' , ' 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,40 @@ 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 ( ) . test (
99+ 'custom-max-fee-per-gas-higher-than-priority' ,
100+ 'Max fee must be higher than or equal to priority fee.' ,
101+ function ( value ) {
102+ if ( ! value ) return true ;
103+
104+ const { customMaxPriorityFeePerGas } = this . parent ;
105+
106+ if ( ! customMaxPriorityFeePerGas ) return true ;
107+
108+ return Number ( value ) > Number ( customMaxPriorityFeePerGas ) ;
109+ } ,
110+ ) ,
111+ customMaxPriorityFeePerGas : yup
112+ . string ( )
113+ . test ( 'too-many-decimals' , 'Too many decimals.' , createFeeDecimalsTest )
114+ . test (
115+ 'custom-priority-fee-per-gas' ,
116+ 'Priority fee higher than max fee or below minimum.' ,
117+ function ( value ) {
118+ if ( ! value ) return true ;
119+ const { networkFeeInfo } = this . options . context ! ;
120+
121+ if ( ! networkFeeInfo ) return false ;
122+ const { minPriorityFee } = networkFeeInfo ;
123+
124+ if ( Number ( value ) < minPriorityFee ) return false ;
125+
126+ const { customMaxFeePerGas } = this . parent ;
127+
128+ return ! ( customMaxFeePerGas && Number ( value ) > Number ( customMaxFeePerGas ) ) ;
129+ } ,
130+ ) ,
97131} ) ;
98132
99133export type FeesFormValues = yup . InferType < typeof feesFormValidationSchema > ;
0 commit comments