|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity 0.8.24; |
| 3 | + |
| 4 | +// NOTE: The required interfaces will soon be available in the Chainlink package for you to import. |
| 5 | + |
| 6 | +interface IBundleAggregatorProxy { |
| 7 | + /** |
| 8 | + * @notice Returns the latest bundle data |
| 9 | + * @return bundle The latest bundle as raw bytes |
| 10 | + */ |
| 11 | + function latestBundle() external view returns (bytes memory); |
| 12 | + |
| 13 | + /** |
| 14 | + * @notice Returns the timestamp of the latest bundle |
| 15 | + * @return timestamp The timestamp of the latest bundle |
| 16 | + */ |
| 17 | + function latestBundleTimestamp() external view returns (uint256); |
| 18 | + |
| 19 | + /** |
| 20 | + * @notice Returns the decimals for each field in the bundle |
| 21 | + * @return decimalsArray Array of decimals for each value in the bundle |
| 22 | + */ |
| 23 | + function bundleDecimals() external view returns (uint8[] memory); |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * @notice This struct defines the exact data structure of the MVR feed |
| 28 | + * @dev The order and types must match exactly what's defined in the feed |
| 29 | + */ |
| 30 | +struct Data { |
| 31 | + uint256 netAssetValue; |
| 32 | + uint256 assetsUnderManagement; |
| 33 | + uint256 outstandingShares; |
| 34 | + uint256 netIncomeExpenses; |
| 35 | + bool openToNewInvestors; |
| 36 | +} |
| 37 | + |
| 38 | +contract MVRDataConsumer { |
| 39 | + // Reference to the MVR feed proxy |
| 40 | + IBundleAggregatorProxy public s_proxy; |
| 41 | + |
| 42 | + // Maximum allowed staleness duration for the data |
| 43 | + // IMPORTANT: This should be configured based on the specific feed's heartbeat interval |
| 44 | + // Check the feed's documentation for the appropriate value instead of using this example value |
| 45 | + uint256 public immutable STALENESS_THRESHOLD; |
| 46 | + |
| 47 | + // Storage for scaled values (after dividing by decimals) |
| 48 | + uint256 public netAssetValue; |
| 49 | + uint256 public assetsUnderManagement; |
| 50 | + uint256 public outstandingShares; |
| 51 | + uint256 public netIncomeExpenses; |
| 52 | + bool public openToNewInvestors; |
| 53 | + |
| 54 | + // Storage for original onchain values (no decimal adjustments) |
| 55 | + uint256 public rawNetAssetValue; |
| 56 | + uint256 public rawAssetsUnderManagement; |
| 57 | + uint256 public rawOutstandingShares; |
| 58 | + uint256 public rawNetIncomeExpenses; |
| 59 | + |
| 60 | + // Keep track of decimals for each field in the struct. |
| 61 | + // Non-numeric fields (e.g., bool) typically return 0. |
| 62 | + uint8[] public decimals; |
| 63 | + |
| 64 | + // Error for stale data |
| 65 | + error StaleData( |
| 66 | + uint256 lastUpdateTimestamp, |
| 67 | + uint256 blockTimestamp, |
| 68 | + uint256 threshold |
| 69 | + ); |
| 70 | + |
| 71 | + // Error for insufficient decimals array |
| 72 | + error InsufficientDecimals(uint256 expected, uint256 actual); |
| 73 | + |
| 74 | + /** |
| 75 | + * @notice Constructor that sets the staleness threshold for the feed |
| 76 | + * @param _proxy The address of the MVR feed's proxy contract |
| 77 | + * @param _stalenessThreshold Maximum time (in seconds) since last update before data is considered stale |
| 78 | + * @dev The threshold should be based on the feed's heartbeat interval from documentation |
| 79 | + * For example, if a feed updates every 24 hours (86400s), you might set this to 86400s + some buffer |
| 80 | + */ |
| 81 | + constructor(IBundleAggregatorProxy _proxy, uint256 _stalenessThreshold) { |
| 82 | + s_proxy = _proxy; |
| 83 | + STALENESS_THRESHOLD = _stalenessThreshold; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @notice Stores the decimals array in your contract for repeated usage. |
| 88 | + * @dev Index mapping for this example: |
| 89 | + * 0 -> netAssetValue, |
| 90 | + * 1 -> assetsUnderManagement, |
| 91 | + * 2 -> outstandingShares, |
| 92 | + * 3 -> netIncomeExpenses, |
| 93 | + * 4 -> openToNewInvestors (likely returns 0). |
| 94 | + */ |
| 95 | + function storeDecimals() external { |
| 96 | + decimals = s_proxy.bundleDecimals(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @notice Returns the timestamp of the most recent MVR feed update. |
| 101 | + */ |
| 102 | + function getLatestBundleTimestamp() external view returns (uint256) { |
| 103 | + return s_proxy.latestBundleTimestamp(); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @notice Simple boolean check for data freshness (block explorer friendly) |
| 108 | + * @return true if data is fresh, false if stale |
| 109 | + */ |
| 110 | + function isDataFresh() public view returns (bool) { |
| 111 | + uint256 lastUpdateTime = s_proxy.latestBundleTimestamp(); |
| 112 | + return (block.timestamp - lastUpdateTime) <= STALENESS_THRESHOLD; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @notice Fetches and decodes the latest MVR feed data, then stores both the raw and scaled values. |
| 117 | + * @dev This process demonstrates the complete flow of consuming MVR feed data: |
| 118 | + * 1. Check data freshness |
| 119 | + * 2. Fetch the raw bytes |
| 120 | + * 3. Decode into the struct matching the feed's data structure |
| 121 | + * 4. Store raw values (preserving original precision) |
| 122 | + * 5. Apply decimal conversions to get the true numerical values |
| 123 | + */ |
| 124 | + function consumeData() external { |
| 125 | + // Check data freshness before proceeding |
| 126 | + if (!isDataFresh()) { |
| 127 | + uint256 lastUpdateTime = s_proxy.latestBundleTimestamp(); |
| 128 | + revert StaleData( |
| 129 | + lastUpdateTime, |
| 130 | + block.timestamp, |
| 131 | + STALENESS_THRESHOLD |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + // Ensure we have the decimals array - if not, fetch it |
| 136 | + if (decimals.length == 0) { |
| 137 | + decimals = s_proxy.bundleDecimals(); |
| 138 | + } |
| 139 | + |
| 140 | + // Verify we have enough decimal values for our struct fields |
| 141 | + if (decimals.length < 4) { |
| 142 | + revert InsufficientDecimals(4, decimals.length); |
| 143 | + } |
| 144 | + |
| 145 | + // 1. Retrieve the raw bytes from the MVR feed |
| 146 | + // This is the encoded form of all data fields packed together |
| 147 | + bytes memory b = s_proxy.latestBundle(); |
| 148 | + |
| 149 | + // 2. Decode the raw bytes into our known struct |
| 150 | + // The struct Data must match exactly what the feed encodes |
| 151 | + Data memory d = abi.decode(b, (Data)); |
| 152 | + |
| 153 | + // 3. Store the raw (original onchain) values |
| 154 | + // These preserve the full precision as reported by the feed |
| 155 | + rawNetAssetValue = d.netAssetValue; |
| 156 | + rawAssetsUnderManagement = d.assetsUnderManagement; |
| 157 | + rawOutstandingShares = d.outstandingShares; |
| 158 | + rawNetIncomeExpenses = d.netIncomeExpenses; |
| 159 | + openToNewInvestors = d.openToNewInvestors; // Boolean, no need for decimal adjustment |
| 160 | + |
| 161 | + // 4. Convert values by dividing by 10^decimals[i] |
| 162 | + // This removes the decimal scaling factor to get the human-readable representation |
| 163 | + // Note: This uses integer division which truncates decimal places |
| 164 | + // For example, if decimals[0] = 8 and rawNetAssetValue = 1850000000, |
| 165 | + // then netAssetValue = 18 (integer division, decimals are truncated) |
| 166 | + netAssetValue = d.netAssetValue / (10 ** decimals[0]); |
| 167 | + assetsUnderManagement = d.assetsUnderManagement / (10 ** decimals[1]); |
| 168 | + outstandingShares = d.outstandingShares / (10 ** decimals[2]); |
| 169 | + netIncomeExpenses = d.netIncomeExpenses / (10 ** decimals[3]); |
| 170 | + // Note: We don't need to apply decimals to boolean fields |
| 171 | + // The openToNewInvestors field typically has 0 decimals in the array |
| 172 | + } |
| 173 | +} |
0 commit comments