Skip to content

Commit 1091aad

Browse files
committed
add wrappers
1 parent 01e3fc6 commit 1091aad

File tree

3 files changed

+136
-3
lines changed

3 files changed

+136
-3
lines changed

src/feeds/PriceFeedNoStale.sol

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.20;
3+
4+
import "src/interfaces/IChainlinkFeed.sol";
5+
6+
contract PriceFeedNoStale {
7+
IChainlinkFeed public immutable feed;
8+
string public description;
9+
10+
constructor(
11+
address _feed
12+
) {
13+
feed = IChainlinkFeed(_feed);
14+
require(feed.decimals() == 18, "Wrong Decimals");
15+
description = feed.description();
16+
}
17+
18+
/**
19+
* @notice Retrieves the latest round data for the asset token price feed
20+
* @return roundId Will return 0
21+
* @return usdPrice The latest asset price in USD with 18 decimals
22+
* @return startedAt Will return 0
23+
* @return updatedAt Will return block.timestamp
24+
* @return answeredInRound Will return 0
25+
*/
26+
function latestRoundData()
27+
public
28+
view
29+
returns (
30+
uint80 roundId,
31+
int256 usdPrice,
32+
uint256 startedAt,
33+
uint256 updatedAt,
34+
uint80 answeredInRound
35+
)
36+
{
37+
return (0, feed.latestAnswer(), 0, block.timestamp, 0 );
38+
}
39+
40+
/**
41+
* @notice Returns the latest price only
42+
* @dev Unlike chainlink oracles, the latestAnswer will always be the same as in the latestRoundData
43+
* @return int256 Returns the last finalized price of the chainlink oracle
44+
*/
45+
function latestAnswer() external view returns (int256) {
46+
return feed.latestAnswer();
47+
}
48+
49+
function decimals() external pure returns (uint8) {
50+
return 18;
51+
}
52+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.20;
3+
4+
import "src/interfaces/IChainlinkFeed.sol";
5+
6+
contract PriceFeedNoStaleBasic {
7+
IChainlinkFeed public immutable vaultFeed;
8+
IChainlinkFeed public immutable assetToUsd;
9+
string public description;
10+
11+
constructor(
12+
address _vaultFeed,
13+
address _assetToUsd
14+
) {
15+
vaultFeed = IChainlinkFeed(_vaultFeed);
16+
assetToUsd = IChainlinkFeed(_assetToUsd);
17+
require(vaultFeed.decimals() == 18 && assetToUsd.decimals() == 18);
18+
description = string(abi.encodePacked(vaultFeed.description(), " * (", assetToUsd.description(),")"));
19+
}
20+
21+
/**
22+
* @notice Retrieves the latest round data for the asset token price feed
23+
* @return roundId Will return 0
24+
* @return usdPrice The latest asset price in USD with 18 decimals
25+
* @return startedAt Will return 0
26+
* @return updatedAt Will return block.timestamp
27+
* @return answeredInRound Will return 0
28+
*/
29+
function latestRoundData()
30+
public
31+
view
32+
returns (
33+
uint80 roundId,
34+
int256 usdPrice,
35+
uint256 startedAt,
36+
uint256 updatedAt,
37+
uint80 answeredInRound
38+
)
39+
{
40+
uint256 vaultPrice = uint(vaultFeed.latestAnswer());
41+
uint256 assetToUsdPrice = uint(assetToUsd.latestAnswer());
42+
usdPrice = int(vaultPrice * assetToUsdPrice / 1e18);
43+
return (0, usdPrice, 0, block.timestamp, 0 );
44+
}
45+
46+
/**
47+
* @notice Returns the latest price only
48+
* @dev Unlike chainlink oracles, the latestAnswer will always be the same as in the latestRoundData
49+
* @return int256 Returns the last finalized price of the chainlink oracle
50+
*/
51+
function latestAnswer() external view returns (int256) {
52+
(, int price, , ,) = latestRoundData();
53+
return price;
54+
}
55+
56+
function decimals() external pure returns (uint8) {
57+
return 18;
58+
}
59+
}

test/feedForkTests/WOETH.t.sol

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ import {ERC4626Feed, IERC4626} from "src/feeds/ERC4626Feed.sol";
66
import {ChainlinkBridgeAssetFeed} from "src/feeds/ChainlinkBridgeAssetFeed.sol";
77
import {ChainlinkBridgeAssetBase} from "test/feedForkTests/ChainlinkBridgeAssetBase.t.sol";
88
import {ChainlinkBasePriceFeed} from "src/feeds/ChainlinkBasePriceFeed.sol";
9-
9+
import {PriceFeedNoStale} from "src/feeds/PriceFeedNoStale.sol";
10+
import {PriceFeedNoStaleBasic} from "src/feeds/PriceFeedNoStaleBasic.sol";
1011
import "forge-std/console2.sol";
1112

1213

13-
contract WOETHFeed is ChainlinkBridgeAssetBase {
14+
contract WOETHFeedTest is ChainlinkBridgeAssetBase {
1415
ERC4626Feed vaultFeed;
1516
ChainlinkBasePriceFeed ethWrapper;
1617
ChainlinkBasePriceFeed oEthToEthWrapper;
17-
18+
PriceFeedNoStale feedNoStale;
19+
PriceFeedNoStaleBasic feedNoStaleBasic;
20+
1821
address oEthToEth = 0x703118C4CbccCBF2AB31913e0f8075fbbb15f563;
1922
address wOeth = 0xDcEe70654261AF21C44c093C300eD3Bb97b78192;
2023
address ethToUsd = 0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419;
@@ -26,6 +29,9 @@ contract WOETHFeed is ChainlinkBridgeAssetBase {
2629
vaultFeed = new ERC4626Feed(wOeth, address(oEthToEthWrapper));
2730
ethWrapper = new ChainlinkBasePriceFeed(address(this),ethToUsd, address(0), 3600);
2831
init(address(vaultFeed), address(ethWrapper), true);
32+
feedNoStale = new PriceFeedNoStale(address(feed));
33+
feedNoStaleBasic = new PriceFeedNoStaleBasic(address(vaultFeed),address(ethWrapper));
34+
2935
}
3036

3137
function test_woEth() public {
@@ -37,4 +43,20 @@ contract WOETHFeed is ChainlinkBridgeAssetBase {
3743
assertEq(woEthToUsdPrice, uint(feed.latestAnswer()));
3844
console2.log(uint(feed.latestAnswer()));
3945
}
46+
47+
function test_feedNoStale() public {
48+
assertEq(feed.latestAnswer(), feedNoStale.latestAnswer());
49+
(,int price, , uint updateAt,) = feedNoStale.latestRoundData();
50+
assertEq(updateAt, block.timestamp);
51+
assertEq(price, feed.latestAnswer());
52+
console2.log(feedNoStaleBasic.description());
53+
}
54+
55+
function test_feedNoStaleBasic() public {
56+
assertEq(feed.latestAnswer(), feedNoStaleBasic.latestAnswer());
57+
(,int price, , uint updateAt,) = feedNoStaleBasic.latestRoundData();
58+
assertEq(updateAt, block.timestamp);
59+
assertEq(price, feed.latestAnswer());
60+
console2.log(feedNoStaleBasic.description());
61+
}
4062
}

0 commit comments

Comments
 (0)