forked from aave-dao/aave-v3-origin
-
Notifications
You must be signed in to change notification settings - Fork 7
feat: scaled price adaptor #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
66f0d6a
feat: scaled price adaptor
DhairyaSethi c3aec05
chore: rename, negative constructor test
DhairyaSethi a3de678
chore: price feed decimal
DhairyaSethi b91bff7
chore: nits on natspec, mv scale scaleFactor
DhairyaSethi 481aba9
fix: fuzz bounds, reorder docs
DhairyaSethi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
src/contracts/extensions/price-adapters/ScaledPriceAdapter.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
| pragma solidity ^0.8.10; | ||
|
|
||
| import {AggregatorInterface} from '../../dependencies/chainlink/AggregatorInterface.sol'; | ||
| import {IScaledPriceAdapter} from '../../interfaces/IScaledPriceAdapter.sol'; | ||
|
|
||
| /** | ||
| * @title ScaledPriceAdapter | ||
| * @author Aave Labs | ||
| * @dev Price Adapter for Chainlink price feeds to scale price to standard USD unit of 8 decimals. | ||
| */ | ||
| contract ScaledPriceAdapter is IScaledPriceAdapter { | ||
| AggregatorInterface internal immutable _SOURCE; | ||
|
|
||
| uint8 internal constant _BASE_DECIMALS = 8; | ||
| bool internal immutable _SCALE_UP; | ||
| uint256 internal immutable _SCALE_FACTOR; | ||
|
|
||
| constructor(address source_) { | ||
| _SOURCE = AggregatorInterface(source_); | ||
| uint8 sourceDecimals = _SOURCE.decimals(); | ||
| _SCALE_UP = sourceDecimals < _BASE_DECIMALS; | ||
| _SCALE_FACTOR = | ||
| 10 ** (_SCALE_UP ? _BASE_DECIMALS - sourceDecimals : sourceDecimals - _BASE_DECIMALS); | ||
| } | ||
|
|
||
| /// @inheritdoc IScaledPriceAdapter | ||
| function latestAnswer() external view returns (int256) { | ||
| return | ||
| _SCALE_UP | ||
| ? _SOURCE.latestAnswer() * int256(_SCALE_FACTOR) | ||
| : _SOURCE.latestAnswer() / int256(_SCALE_FACTOR); | ||
| } | ||
|
|
||
| /// @inheritdoc IScaledPriceAdapter | ||
| function description() external view returns (string memory) { | ||
| return string.concat(_SOURCE.description(), ' (USD Scaled)'); | ||
| } | ||
|
|
||
| /// @inheritdoc IScaledPriceAdapter | ||
| function decimals() external pure returns (uint8) { | ||
| return _BASE_DECIMALS; | ||
| } | ||
|
|
||
| /// @inheritdoc IScaledPriceAdapter | ||
| function scale() external view returns (bool, uint256) { | ||
| return (_SCALE_UP, _SCALE_FACTOR); | ||
| } | ||
|
|
||
| /// @inheritdoc IScaledPriceAdapter | ||
| function source() external view returns (address) { | ||
| return address(_SOURCE); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
| pragma solidity ^0.8.10; | ||
|
|
||
| interface IScaledPriceAdapter { | ||
| /** | ||
| * @dev Direction and units used to scale latestAnswer to base decimals of 8. | ||
| * @dev Loses price precision by log10(scaleFactor) when scaling down. | ||
| * @return scaleUp Whether to scale up or down. | ||
| * @return scaleFactor The units to scale by. | ||
| */ | ||
| function scale() external view returns (bool scaleUp, uint256 scaleFactor); | ||
|
|
||
| /** | ||
| * @dev The decimals of price adapter. | ||
| */ | ||
| function decimals() external view returns (uint8); | ||
|
|
||
| /** | ||
| * @dev Underlying chainlink price source. | ||
| */ | ||
| function source() external view returns (address); | ||
|
|
||
| /** | ||
| * @dev Description of price adapter. | ||
| */ | ||
| function description() external view returns (string memory); | ||
|
|
||
| /** | ||
| * @dev Scaled `latestAnswer` from chainlink price feed. | ||
| * @dev Loses price precision by log10(scaleFactor) when scaling down. | ||
| */ | ||
| function latestAnswer() external view returns (int256); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/contracts/mocks/oracle/CLAggregators/MockAggregatorMetadata.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.10; | ||
|
|
||
| import {MockAggregator} from './MockAggregator.sol'; | ||
|
|
||
| contract MockAggregatorMetadata is MockAggregator { | ||
| uint8 internal immutable _DECIMALS; | ||
|
|
||
| constructor(int256 initialAnswer_, uint8 decimals_) MockAggregator(initialAnswer_) { | ||
| _DECIMALS = decimals_; | ||
| } | ||
|
|
||
| function decimals() external view override returns (uint8) { | ||
| return _DECIMALS; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
| pragma solidity ^0.8.0; | ||
|
|
||
| import {MockAggregatorMetadata} from '../../../src/contracts/mocks/oracle/CLAggregators/MockAggregatorMetadata.sol'; | ||
| import {ScaledPriceAdapter} from '../../../src/contracts/extensions/price-adapters/ScaledPriceAdapter.sol'; | ||
| import {TestnetProcedures} from '../../utils/TestnetProcedures.sol'; | ||
|
|
||
| contract ScaledPriceAdapterTests is TestnetProcedures { | ||
| function test_adapter_less_than_base() public { | ||
| test_fuzz_adapter({sourceDecimals: 2, price: 1e2}); | ||
| test_fuzz_adapter({sourceDecimals: 6, price: 32.323e6}); | ||
| } | ||
|
|
||
| function test_adapter_greater_than_base() public { | ||
| test_fuzz_adapter({sourceDecimals: 12, price: 1e12}); | ||
| } | ||
|
|
||
| function test_adapter_equal_to_base() public { | ||
| test_fuzz_adapter({sourceDecimals: 8, price: 1e8}); | ||
| } | ||
|
|
||
| function test_fuzz_adapter(uint256 sourceDecimals, int256 price) public { | ||
| sourceDecimals = bound(sourceDecimals, 1, 36); | ||
| price = bound(price, 1, int256(10 ** sourceDecimals)); | ||
| address source = address(new MockAggregatorMetadata(price, uint8(sourceDecimals))); | ||
| ScaledPriceAdapter adapter = new ScaledPriceAdapter(source); | ||
|
|
||
| (bool scaleUp, uint256 scale) = adapter.scale(); | ||
| assertEq(adapter.decimals(), 8); | ||
| assertEq(scaleUp, adapter.decimals() > sourceDecimals); | ||
| assertEq( | ||
| scale, | ||
| 10 ** (scaleUp ? adapter.decimals() - sourceDecimals : sourceDecimals - adapter.decimals()) | ||
| ); | ||
| assertEq(adapter.latestAnswer(), scaleUp ? price * int256(scale) : price / int256(scale)); | ||
| assertEq(adapter.source(), source); | ||
| } | ||
|
|
||
| function test_adapter_invalid_source_feed() public { | ||
| vm.expectRevert(); | ||
| new ScaledPriceAdapter(makeAddr('invalid')); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is wrong no? because price will only go up to 1 USD then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah yeah, good call; was originally written differently & got overlooked. the min is incorrect as well, the adaptor should forward 0 price as well
thinking of setting 1e10 source feed units