From 77b162c121ef48fd8090dfd319440d2c1adff069 Mon Sep 17 00:00:00 2001 From: Reinis Martinsons Date: Mon, 20 May 2024 16:06:08 +0000 Subject: [PATCH] fix: pass data struct in internal call Signed-off-by: Reinis Martinsons --- .../BoundedUnionSourceAdapter.sol | 22 +++++++------------ .../BoundedUnionSource.SelectBoundedPrice.sol | 10 ++++++++- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/adapters/source-adapters/BoundedUnionSourceAdapter.sol b/src/adapters/source-adapters/BoundedUnionSourceAdapter.sol index 6ca6a56..113ed43 100644 --- a/src/adapters/source-adapters/BoundedUnionSourceAdapter.sol +++ b/src/adapters/source-adapters/BoundedUnionSourceAdapter.sol @@ -61,9 +61,7 @@ abstract contract BoundedUnionSourceAdapter is returns (int256 answer, uint256 timestamp) { AllSourceData memory data = _getAllLatestSourceData(); - return _selectBoundedPrice( - data.clAnswer, data.clTimestamp, data.crAnswer, data.crTimestamp, data.pyAnswer, data.pyTimestamp - ); + return _selectBoundedPrice(data); } /** @@ -106,9 +104,7 @@ abstract contract BoundedUnionSourceAdapter is { // In the happy path there have been no source updates since requested time, so we can return the latest data. AllSourceData memory data = _getAllLatestSourceData(); - (int256 boundedAnswer, uint256 boundedTimestamp) = _selectBoundedPrice( - data.clAnswer, data.clTimestamp, data.crAnswer, data.crTimestamp, data.pyAnswer, data.pyTimestamp - ); + (int256 boundedAnswer, uint256 boundedTimestamp) = _selectBoundedPrice(data); if (boundedTimestamp <= timestamp) return (boundedAnswer, boundedTimestamp, 1); // Chainlink has price history, so use tryLatestDataAt to pull the most recent price that satisfies the timestamp constraint. @@ -125,9 +121,7 @@ abstract contract BoundedUnionSourceAdapter is ); // Update bounded data with constrained source data. - (boundedAnswer, boundedTimestamp) = _selectBoundedPrice( - data.clAnswer, data.clTimestamp, data.crAnswer, data.crTimestamp, data.pyAnswer, data.pyTimestamp - ); + (boundedAnswer, boundedTimestamp) = _selectBoundedPrice(data); // Return bounded data unless there is a newer snapshotted data that still satisfies time constraint. if (boundedTimestamp >= snapshot.timestamp || snapshot.timestamp > timestamp) { @@ -147,14 +141,14 @@ abstract contract BoundedUnionSourceAdapter is } // Selects the appropriate price from the three sources based on the bounding tolerance and logic. - function _selectBoundedPrice(int256 cl, uint256 clT, int256 cr, uint256 crT, int256 py, uint256 pyT) - internal - view - returns (int256, uint256) - { + function _selectBoundedPrice(AllSourceData memory data) internal view returns (int256, uint256) { int256 newestVal = 0; uint256 newestT = 0; + // Unpack the data to short named variables for better code readability below. + (int256 cl, uint256 clT, int256 cr, uint256 crT, int256 py, uint256 pyT) = + (data.clAnswer, data.clTimestamp, data.crAnswer, data.crTimestamp, data.pyAnswer, data.pyTimestamp); + // For each price, check if it is within tolerance of the other two. If so, check if it is the newest. if (pyT > newestT && (_withinTolerance(py, cr) || _withinTolerance(py, cl))) (newestVal, newestT) = (py, pyT); if (crT > newestT && (_withinTolerance(cr, py) || _withinTolerance(cr, cl))) (newestVal, newestT) = (cr, crT); diff --git a/test/unit/adapters/BoundedUnionSource.SelectBoundedPrice.sol b/test/unit/adapters/BoundedUnionSource.SelectBoundedPrice.sol index 521c941..9941d23 100644 --- a/test/unit/adapters/BoundedUnionSource.SelectBoundedPrice.sol +++ b/test/unit/adapters/BoundedUnionSource.SelectBoundedPrice.sol @@ -24,7 +24,15 @@ contract TestBoundedUnionSource is BoundedUnionSourceAdapter { view returns (int256, uint256) { - return _selectBoundedPrice(cl, clT, cr, crT, py, pyT); + AllSourceData memory data = AllSourceData({ + clAnswer: cl, + clTimestamp: clT, + crAnswer: cr, + crTimestamp: crT, + pyAnswer: py, + pyTimestamp: pyT + }); + return _selectBoundedPrice(data); } function withinTolerance(int256 a, int256 b) public view returns (bool) {