Skip to content

Commit

Permalink
check for min and max amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
gretzke committed Oct 8, 2024
1 parent 9f5c04f commit 71ee4a8
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# AggregatedChainlinkOracle
[Git Source](https://github.com/cryptexfinance/tcapv2.0/blob/6fb291c7e6c372c076c9cd314a2348fadd32af09/src/oracle/AggregatedChainlinkOracle.sol)
[Git Source](https://github.com/cryptexfinance/tcapv2.0/blob/9f5c04f0c486dce359a7781c94ffd3096596f1da/src/oracle/AggregatedChainlinkOracle.sol)

**Inherits:**
[BaseOracleUSD](/src/oracle/BaseOracleUSD.sol/abstract.BaseOracleUSD.md)
Expand All @@ -15,6 +15,20 @@ AggregatorV3Interface public immutable feed;
```


### MIN_AMOUNT

```solidity
int256 private immutable MIN_AMOUNT;
```


### MAX_AMOUNT

```solidity
int256 private immutable MAX_AMOUNT;
```


### feedDecimals

```solidity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# AaveV3Pocket
[Git Source](https://github.com/cryptexfinance/tcapv2.0/blob/300e3dc5cffa328fb9714b67c38745c3400cb13b/src/pockets/AaveV3Pocket.sol)
[Git Source](https://github.com/cryptexfinance/tcapv2.0/blob/9f5c04f0c486dce359a7781c94ffd3096596f1da/src/pockets/AaveV3Pocket.sol)

**Inherits:**
[BasePocket](/src/pockets/BasePocket.sol/abstract.BasePocket.md), [IAaveV3Pocket](/src/interface/pockets/IAaveV3Pocket.sol/interface.IAaveV3Pocket.md)
Expand Down Expand Up @@ -28,7 +28,7 @@ constructor(address vault_, address underlyingToken_, address aavePool)


```solidity
function initialize() public override initializer;
function initialize() public initializer;
```

### _onDeposit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# BasePocket
[Git Source](https://github.com/cryptexfinance/tcapv2.0/blob/300e3dc5cffa328fb9714b67c38745c3400cb13b/src/pockets/BasePocket.sol)
[Git Source](https://github.com/cryptexfinance/tcapv2.0/blob/9f5c04f0c486dce359a7781c94ffd3096596f1da/src/pockets/BasePocket.sol)

**Inherits:**
[IPocket](/src/interface/pockets/IPocket.sol/interface.IPocket.md), Initializable
Expand Down Expand Up @@ -46,13 +46,6 @@ IERC20 public immutable OVERLYING_TOKEN;
constructor(address vault_, address underlyingToken_, address overlyingToken_);
```

### initialize


```solidity
function initialize() public virtual initializer;
```

### _getBasePocketStorage


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# DefaultPocket
[Git Source](https://github.com/cryptexfinance/tcapv2.0/blob/300e3dc5cffa328fb9714b67c38745c3400cb13b/src/pockets/DefaultPocket.sol)
[Git Source](https://github.com/cryptexfinance/tcapv2.0/blob/9f5c04f0c486dce359a7781c94ffd3096596f1da/src/pockets/DefaultPocket.sol)

**Inherits:**
[BasePocket](/src/pockets/BasePocket.sol/abstract.BasePocket.md)
Expand All @@ -21,7 +21,7 @@ constructor(address vault_, address underlyingToken_) BasePocket(vault_, underly


```solidity
function initialize() public override initializer;
function initialize() public initializer;
```

### _onDeposit
Expand Down
1 change: 1 addition & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ optimizer = true
optimizer_runs = 999999
via_ir = false
solc = "0.8.26"
evm_version = "cancun"
verbosity = 2
ffi = true
fuzz.runs = 10000
Expand Down
21 changes: 20 additions & 1 deletion src/oracle/AggregatedChainlinkOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {AggregatorV3Interface} from "@chainlink/interfaces/feeds/AggregatorV3Int
/// @dev all oracles are priced in USD with 18 decimals
contract AggregatedChainlinkOracle is BaseOracleUSD {
AggregatorV3Interface public immutable feed;
int256 private immutable MIN_AMOUNT;
int256 private immutable MAX_AMOUNT;

uint256 public immutable feedDecimals;
uint256 public immutable stalenessDelay;

Expand All @@ -16,6 +19,20 @@ contract AggregatedChainlinkOracle is BaseOracleUSD {
feed = AggregatorV3Interface(feed_);
feedDecimals = feed.decimals();
stalenessDelay = stalenessDelay_;
// get min and max answer from the aggregator, set the min amount to the reported min amount, set the max amount to the reported max amount in order to revert if chainlink reports exactly the min or max amount, which means the min or max amount are likely exceeded
(bool success, bytes memory data) = address(feed_).call(abi.encodeWithSignature("aggregator()"));
assert(success);
address aggregator = abi.decode(data, (address));
(success, data) = address(aggregator).call(abi.encodeWithSignature("minAnswer()"));
assert(success);
int192 minAmount = abi.decode(data, (int192));
assert(minAmount > 0);
MIN_AMOUNT = int256(minAmount);
(success, data) = address(aggregator).call(abi.encodeWithSignature("maxAnswer()"));
assert(success);
int192 maxAmount = abi.decode(data, (int192));
assert(maxAmount > 2 && maxAmount > minAmount);
MAX_AMOUNT = int256(maxAmount);
}

function latestPrice(bool checkStaleness) public view virtual override returns (uint256) {
Expand All @@ -27,7 +44,9 @@ contract AggregatedChainlinkOracle is BaseOracleUSD {
revert StaleOracle();
}
}
assert(answer > 0);
// revert if the answer is MIN_AMOUNT or less
// revert if the answer is MAX_AMOUNT or more
assert(answer > MIN_AMOUNT && answer < MAX_AMOUNT);
// @audit feed decimals cannot exceed 18
uint256 adjustedDecimalsAnswer = uint256(answer) * 10 ** (18 - feedDecimals);
return adjustedDecimalsAnswer;
Expand Down
2 changes: 1 addition & 1 deletion src/pockets/AaveV3Pocket.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ contract AaveV3Pocket is BasePocket, IAaveV3Pocket {
require(address(OVERLYING_TOKEN) != address(0));
}

function initialize() public override initializer {
function initialize() public initializer {
UNDERLYING_TOKEN.approve(address(POOL), type(uint256).max);
}

Expand Down
2 changes: 0 additions & 2 deletions src/pockets/BasePocket.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ abstract contract BasePocket is IPocket, Initializable {
_disableInitializers();
}

function initialize() public virtual initializer {}

function _getBasePocketStorage() private pure returns (BasePocketStorage storage $) {
assembly {
$.slot := BasePocketStorageLocation
Expand Down
2 changes: 1 addition & 1 deletion src/pockets/DefaultPocket.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ contract DefaultPocket is BasePocket {

constructor(address vault_, address underlyingToken_) BasePocket(vault_, underlyingToken_, underlyingToken_) {}

function initialize() public override initializer {}
function initialize() public initializer {}

function _onDeposit(uint256 amountUnderlying) internal pure override returns (uint256 amountOverlying) {
amountOverlying = amountUnderlying;
Expand Down
12 changes: 12 additions & 0 deletions test/mock/MockFeed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ contract MockFeed is AggregatorV3Interface {
uint256 internal _answer;
uint256 priceMultiplier = 10_000;
uint8 internal _feedDecimals = 8;
address public aggregator;

constructor(uint256 price) {
_answer = price;
aggregator = address(new Aggregator());
}

function setPrice(uint256 price) external {
Expand Down Expand Up @@ -42,3 +44,13 @@ contract MockFeed is AggregatorV3Interface {
return (0, int256(_answer * priceMultiplier / DENOMINATOR), 0, block.timestamp, 0);
}
}

contract Aggregator {
function minAnswer() external pure returns (int192) {
return 1;
}

function maxAnswer() external pure returns (int192) {
return type(int192).max;
}
}

0 comments on commit 71ee4a8

Please sign in to comment.