Skip to content

Commit

Permalink
feat: add input check for OracleSet params AssetPrice and Scale (
Browse files Browse the repository at this point in the history
…#2699)

* add input check for AssetPrice and Scale being either both present or excluded
  • Loading branch information
khancode authored May 23, 2024
1 parent 23adb49 commit b27bbb4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/xrpl/src/models/transactions/oracleSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ export function validateOracleSet(tx: Record<string, unknown>): void {
)
}

// Either AssetPrice and Scale are both present or both excluded
if (
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- we are validating the type
(priceData.PriceData.AssetPrice == null) !==
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- we are validating the type
(priceData.PriceData.Scale == null)
) {
throw new ValidationError(
'OracleSet: PriceDataSeries must have both `AssetPrice` and `Scale` if any are present',
)
}

if (
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- we are validating the type
'AssetPrice' in priceData.PriceData &&
Expand Down
16 changes: 16 additions & 0 deletions packages/xrpl/test/models/oracleSet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ describe('OracleSet', function () {
assert.throws(() => validate(tx), ValidationError, errorMessage)
})

it(`throws w/ missing AssetPrice with Scale present of PriceDataSeries`, function () {
delete tx.PriceDataSeries[0].PriceData.AssetPrice
const errorMessage =
'OracleSet: PriceDataSeries must have both `AssetPrice` and `Scale` if any are present'
assert.throws(() => validateOracleSet(tx), ValidationError, errorMessage)
assert.throws(() => validate(tx), ValidationError, errorMessage)
})

it(`throws w/ missing Scale with AssetPrice present of PriceDataSeries`, function () {
delete tx.PriceDataSeries[0].PriceData.Scale
const errorMessage =
'OracleSet: PriceDataSeries must have both `AssetPrice` and `Scale` if any are present'
assert.throws(() => validateOracleSet(tx), ValidationError, errorMessage)
assert.throws(() => validate(tx), ValidationError, errorMessage)
})

it(`throws w/ invalid AssetPrice of PriceDataSeries`, function () {
tx.PriceDataSeries[0].PriceData.AssetPrice = '1234'
const errorMessage = 'OracleSet: invalid field AssetPrice'
Expand Down

0 comments on commit b27bbb4

Please sign in to comment.