|
3 | 3 | pragma solidity ^0.8.0; |
4 | 4 |
|
5 | 5 | import "@openzeppelin/contracts/utils/math/SafeCast.sol"; |
| 6 | +import "@openzeppelin/contracts/utils/math/SignedMath.sol"; |
| 7 | +import "@openzeppelin/contracts/utils/math/Math.sol"; |
6 | 8 | import "@pythnetwork/pyth-sdk-solidity/IPyth.sol"; |
7 | 9 | import "./IScheduler.sol"; |
8 | 10 | import "./SchedulerState.sol"; |
@@ -168,34 +170,143 @@ abstract contract Scheduler is IScheduler, SchedulerState { |
168 | 170 | revert InsufficientBalance(); |
169 | 171 | } |
170 | 172 |
|
171 | | - // Parse price feed updates with the same timestamp for all feeds |
172 | | - uint64 publishTime = SafeCast.toUint64(block.timestamp); |
| 173 | + // Parse price feed updates with an expected timestamp range of [-10s, now] |
| 174 | + // We will validate the trigger conditions and timestamps ourselves |
| 175 | + // using the returned PriceFeeds. |
| 176 | + uint64 maxPublishTime = SafeCast.toUint64(block.timestamp); |
| 177 | + uint64 minPublishTime = maxPublishTime - 10 seconds; |
173 | 178 | PythStructs.PriceFeed[] memory priceFeeds = pyth.parsePriceFeedUpdates{ |
174 | 179 | value: pythFee |
175 | | - }(updateData, priceIds, publishTime, publishTime); |
| 180 | + }(updateData, priceIds, minPublishTime, maxPublishTime); |
176 | 181 |
|
177 | 182 | // Verify all price feeds have the same timestamp |
178 | | - uint64 timestamp = SafeCast.toUint64(priceFeeds[0].price.publishTime); |
| 183 | + uint256 timestamp = priceFeeds[0].price.publishTime; |
179 | 184 | for (uint8 i = 1; i < priceFeeds.length; i++) { |
180 | | - if ( |
181 | | - SafeCast.toUint64(priceFeeds[i].price.publishTime) != timestamp |
182 | | - ) { |
| 185 | + if (priceFeeds[i].price.publishTime != timestamp) { |
183 | 186 | revert PriceTimestampMismatch(); |
184 | 187 | } |
185 | 188 | } |
186 | 189 |
|
| 190 | + // Verify that update conditions are met, and that the timestamp |
| 191 | + // is more recent than latest stored update's. Reverts if not. |
| 192 | + _validateShouldUpdatePrices(subscriptionId, params, status, priceFeeds); |
| 193 | + |
| 194 | + // Store the price updates, update status, and emit event |
| 195 | + _storePriceUpdatesAndStatus( |
| 196 | + subscriptionId, |
| 197 | + status, |
| 198 | + priceFeeds, |
| 199 | + pythFee |
| 200 | + ); |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * @notice Stores the price updates, updates subscription status, and emits event. |
| 205 | + */ |
| 206 | + function _storePriceUpdatesAndStatus( |
| 207 | + uint256 subscriptionId, |
| 208 | + SubscriptionStatus storage status, |
| 209 | + PythStructs.PriceFeed[] memory priceFeeds, |
| 210 | + uint256 pythFee |
| 211 | + ) internal { |
187 | 212 | // Store the price updates |
188 | 213 | for (uint8 i = 0; i < priceFeeds.length; i++) { |
189 | | - _state.priceUpdates[subscriptionId][priceIds[i]] = priceFeeds[i]; |
| 214 | + _state.priceUpdates[subscriptionId][priceFeeds[i].id] = priceFeeds[ |
| 215 | + i |
| 216 | + ]; |
190 | 217 | } |
191 | | - |
192 | | - // Update subscription status |
193 | | - status.priceLastUpdatedAt = timestamp; |
| 218 | + status.priceLastUpdatedAt = priceFeeds[0].price.publishTime; |
194 | 219 | status.balanceInWei -= pythFee; |
195 | 220 | status.totalUpdates += 1; |
196 | 221 | status.totalSpent += pythFee; |
197 | 222 |
|
198 | | - emit PricesUpdated(subscriptionId, timestamp); |
| 223 | + emit PricesUpdated(subscriptionId, priceFeeds[0].price.publishTime); |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * @notice Validates whether the update trigger criteria is met for a subscription. Reverts if not met. |
| 228 | + * @dev This function assumes that all updates in priceFeeds have the same timestamp. The caller is expected to enforce this invariant. |
| 229 | + * @param subscriptionId The ID of the subscription (needed for reading previous prices). |
| 230 | + * @param params The subscription's parameters struct. |
| 231 | + * @param status The subscription's status struct. |
| 232 | + * @param priceFeeds The array of price feeds to validate. |
| 233 | + */ |
| 234 | + function _validateShouldUpdatePrices( |
| 235 | + uint256 subscriptionId, |
| 236 | + SubscriptionParams storage params, |
| 237 | + SubscriptionStatus storage status, |
| 238 | + PythStructs.PriceFeed[] memory priceFeeds |
| 239 | + ) internal view returns (bool) { |
| 240 | + // SECURITY NOTE: this check assumes that all updates in priceFeeds have the same timestamp. |
| 241 | + // The caller is expected to enforce this invariant. |
| 242 | + uint256 updateTimestamp = priceFeeds[0].price.publishTime; |
| 243 | + |
| 244 | + // Reject updates if they're older than the latest stored ones |
| 245 | + if ( |
| 246 | + status.priceLastUpdatedAt > 0 && |
| 247 | + updateTimestamp <= status.priceLastUpdatedAt |
| 248 | + ) { |
| 249 | + revert TimestampOlderThanLastUpdate( |
| 250 | + updateTimestamp, |
| 251 | + status.priceLastUpdatedAt |
| 252 | + ); |
| 253 | + } |
| 254 | + |
| 255 | + // If updateOnHeartbeat is enabled and the heartbeat interval has passed, trigger update |
| 256 | + if (params.updateCriteria.updateOnHeartbeat) { |
| 257 | + uint256 lastUpdateTime = status.priceLastUpdatedAt; |
| 258 | + |
| 259 | + if ( |
| 260 | + lastUpdateTime == 0 || |
| 261 | + updateTimestamp >= |
| 262 | + lastUpdateTime + params.updateCriteria.heartbeatSeconds |
| 263 | + ) { |
| 264 | + return true; |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + // If updateOnDeviation is enabled, check if any price has deviated enough |
| 269 | + if (params.updateCriteria.updateOnDeviation) { |
| 270 | + for (uint8 i = 0; i < priceFeeds.length; i++) { |
| 271 | + // Get the previous price feed for this price ID using subscriptionId |
| 272 | + PythStructs.PriceFeed storage previousFeed = _state |
| 273 | + .priceUpdates[subscriptionId][priceFeeds[i].id]; |
| 274 | + |
| 275 | + // If there's no previous price, this is the first update |
| 276 | + if (previousFeed.id == bytes32(0)) { |
| 277 | + return true; |
| 278 | + } |
| 279 | + |
| 280 | + // Calculate the deviation percentage |
| 281 | + int64 currentPrice = priceFeeds[i].price.price; |
| 282 | + int64 previousPrice = previousFeed.price.price; |
| 283 | + |
| 284 | + // Skip if either price is zero to avoid division by zero |
| 285 | + if (previousPrice == 0 || currentPrice == 0) { |
| 286 | + continue; |
| 287 | + } |
| 288 | + |
| 289 | + // Calculate absolute deviation basis points (scaled by 1e4) |
| 290 | + uint256 numerator = SignedMath.abs( |
| 291 | + currentPrice - previousPrice |
| 292 | + ); |
| 293 | + uint256 denominator = SignedMath.abs(previousPrice); |
| 294 | + uint256 deviationBps = Math.mulDiv( |
| 295 | + numerator, |
| 296 | + 10_000, |
| 297 | + denominator |
| 298 | + ); |
| 299 | + |
| 300 | + // If deviation exceeds threshold, trigger update |
| 301 | + if ( |
| 302 | + deviationBps >= params.updateCriteria.deviationThresholdBps |
| 303 | + ) { |
| 304 | + return true; |
| 305 | + } |
| 306 | + } |
| 307 | + } |
| 308 | + |
| 309 | + revert UpdateConditionsNotMet(); |
199 | 310 | } |
200 | 311 |
|
201 | 312 | function getLatestPrices( |
|
0 commit comments