From e0a8ad101b9bceb414de56274cba4c330dbacf05 Mon Sep 17 00:00:00 2001 From: Reinis Martinsons Date: Wed, 15 May 2024 09:57:28 +0000 Subject: [PATCH] fix: remove duplicate decimal scaling in uniswap anchored view source Signed-off-by: Reinis Martinsons --- .../UniswapAnchoredViewSourceAdapter.sol | 2 +- .../UniswapAnchoredViewSourceAdapter.sol | 26 ++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/adapters/source-adapters/UniswapAnchoredViewSourceAdapter.sol b/src/adapters/source-adapters/UniswapAnchoredViewSourceAdapter.sol index b33ec2b..39c4849 100644 --- a/src/adapters/source-adapters/UniswapAnchoredViewSourceAdapter.sol +++ b/src/adapters/source-adapters/UniswapAnchoredViewSourceAdapter.sol @@ -74,6 +74,6 @@ abstract contract UniswapAnchoredViewSourceAdapter is SnapshotSource { */ function tryLatestDataAt(uint256 timestamp, uint256 maxTraversal) public view override returns (int256, uint256) { Snapshot memory snapshot = _tryLatestDataAt(timestamp, maxTraversal); - return (DecimalLib.convertDecimals(snapshot.answer, SOURCE_DECIMALS, 18), snapshot.timestamp); + return (snapshot.answer, snapshot.timestamp); } } diff --git a/test/fork/adapters/UniswapAnchoredViewSourceAdapter.sol b/test/fork/adapters/UniswapAnchoredViewSourceAdapter.sol index e1bf827..596a575 100644 --- a/test/fork/adapters/UniswapAnchoredViewSourceAdapter.sol +++ b/test/fork/adapters/UniswapAnchoredViewSourceAdapter.sol @@ -47,7 +47,7 @@ contract UniswapAnchoredViewSourceAdapterTest is CommonTest { assertTrue(latestAggregatorTimestamp == latestSourceTimestamp); } - function testCorrectlyStandardizesOutputs() public { + function testCorrectlyStandardizesLatestOutputs() public { // Repeat the same test as above, but with cWBTC where underlying has 8 decimals. address cWBTC = 0xccF4429DB6322D5C611ee964527D42E5d685DD6a; sourceAdapter = new TestedSourceAdapter(uniswapAnchoredView, cWBTC); @@ -64,6 +64,30 @@ contract UniswapAnchoredViewSourceAdapterTest is CommonTest { assertTrue(latestAggregatorTimestamp == latestSourceTimestamp); } + function testCorrectlyStandardizesLatestAtOutputs() public { + // Repeat the same test for cWBTC as above, but for tryLatestDataAt. + uint256 targetTime = block.timestamp; + + address cWBTC = 0xccF4429DB6322D5C611ee964527D42E5d685DD6a; + sourceAdapter = new TestedSourceAdapter(uniswapAnchoredView, cWBTC); + aggregator = IAccessControlledAggregatorV3(address(sourceAdapter.aggregator())); + + // Fork ~24 hours (7200 blocks on mainnet) forward with persistent source adapter. + vm.makePersistent(address(sourceAdapter)); + vm.createSelectFork("mainnet", targetBlock + 7200); + _whitelistOnAggregator(); + + // UniswapAnchoredView does not support historical lookups so this should still return latest data without snapshotting. + uint256 latestUniswapAnchoredViewAnswer = uniswapAnchoredView.getUnderlyingPrice(cWBTC); + uint256 latestAggregatorTimestamp = aggregator.latestTimestamp(); + (int256 lookBackPrice, uint256 lookBackTimestamp) = sourceAdapter.tryLatestDataAt(targetTime, 100); + + // WBTC has 8 decimals, so source price feed is scaled at (36 - 8) = 28 decimals. + uint256 standardizedAnswer = latestUniswapAnchoredViewAnswer / 10 ** (28 - 18); + assertTrue(int256(standardizedAnswer) == lookBackPrice); + assertTrue(latestAggregatorTimestamp == lookBackTimestamp); + } + function testReturnsLatestSourceDataNoSnapshot() public { uint256 targetTime = block.timestamp;