11import { ValidationError } from '../../errors'
22import { PriceData } from '../common'
3+ import { isHex } from '../utils'
34
45import {
56 BaseTransaction ,
@@ -12,6 +13,8 @@ import {
1213
1314const PRICE_DATA_SERIES_MAX_LENGTH = 10
1415const SCALE_MAX = 10
16+ const MINIMUM_ASSET_PRICE_LENGTH = 1
17+ const MAXIMUM_ASSET_PRICE_LENGTH = 16
1518
1619/**
1720 * Creates a new Oracle ledger entry or updates the fields of an existing one, using the Oracle ID.
@@ -82,7 +85,7 @@ export function validateOracleSet(tx: Record<string, unknown>): void {
8285
8386 validateOptionalField ( tx , 'AssetClass' , isString )
8487
85- // eslint-disable-next-line max-lines-per-function -- necessary to validate many fields
88+ /* eslint-disable max-statements, max-lines-per-function -- necessary to validate many fields */
8689 validateRequiredField ( tx , 'PriceDataSeries' , ( value ) => {
8790 if ( ! Array . isArray ( value ) ) {
8891 throw new ValidationError ( 'OracleSet: PriceDataSeries must be an array' )
@@ -142,14 +145,32 @@ export function validateOracleSet(tx: Record<string, unknown>): void {
142145 )
143146 }
144147
145- if (
146- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- we are validating the type
147- 'AssetPrice' in priceData . PriceData &&
148- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- we are validating the type
149- ! isNumber ( priceData . PriceData . AssetPrice )
150- ) {
151- throw new ValidationError ( 'OracleSet: invalid field AssetPrice' )
148+ /* eslint-disable @typescript-eslint/no-unsafe-member-access, max-depth --
149+ we need to validate priceData.PriceData.AssetPrice value */
150+ if ( 'AssetPrice' in priceData . PriceData ) {
151+ if ( ! isNumber ( priceData . PriceData . AssetPrice ) ) {
152+ if ( typeof priceData . PriceData . AssetPrice !== 'string' ) {
153+ throw new ValidationError (
154+ 'OracleSet: Field AssetPrice must be a string or a number' ,
155+ )
156+ }
157+ if ( ! isHex ( priceData . PriceData . AssetPrice ) ) {
158+ throw new ValidationError (
159+ 'OracleSet: Field AssetPrice must be a valid hex string' ,
160+ )
161+ }
162+ if (
163+ priceData . PriceData . AssetPrice . length <
164+ MINIMUM_ASSET_PRICE_LENGTH ||
165+ priceData . PriceData . AssetPrice . length > MAXIMUM_ASSET_PRICE_LENGTH
166+ ) {
167+ throw new ValidationError (
168+ `OracleSet: Length of AssetPrice field must be between ${ MINIMUM_ASSET_PRICE_LENGTH } and ${ MAXIMUM_ASSET_PRICE_LENGTH } characters long` ,
169+ )
170+ }
171+ }
152172 }
173+ /* eslint-enable @typescript-eslint/no-unsafe-member-access, max-depth */
153174
154175 if (
155176 // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- we are validating the type
@@ -173,4 +194,5 @@ export function validateOracleSet(tx: Record<string, unknown>): void {
173194 }
174195 return true
175196 } )
197+ /* eslint-enable max-statements, max-lines-per-function */
176198}
0 commit comments