Skip to content

Commit

Permalink
fix: pass data struct in internal call
Browse files Browse the repository at this point in the history
Signed-off-by: Reinis Martinsons <reinis@umaproject.org>
  • Loading branch information
Reinis-FRP committed May 20, 2024
1 parent 89bad8b commit 77b162c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
22 changes: 8 additions & 14 deletions src/adapters/source-adapters/BoundedUnionSourceAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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.
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
10 changes: 9 additions & 1 deletion test/unit/adapters/BoundedUnionSource.SelectBoundedPrice.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 77b162c

Please sign in to comment.