Skip to content

Commit c928510

Browse files
authored
Update timestamp calculation (#151)
* update timestamp calculation * re-add calculateNextExecutionTimestamp (used by FYV)
1 parent 2953e7d commit c928510

1 file changed

Lines changed: 18 additions & 30 deletions

File tree

cadence/contracts/interfaces/DeFiActions.cdc

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,19 +1095,23 @@ access(all) contract DeFiActions {
10951095
/// @return String?: The error message, or nil if the next execution was scheduled
10961096
///
10971097
access(Schedule) fun scheduleNextRebalance(whileExecuting: UInt64?): String? {
1098-
// get the next execution timestamp
1099-
var timestamp = self.calculateNextExecutionTimestampAsConfigured()
11001098
// perform pre-flight checks before estimating the transaction fees
1101-
var errorMessage: String? = nil
11021099
if self._recurringConfig == nil {
1103-
errorMessage = "MISSING_RECURRING_CONFIG"
1104-
} else if timestamp == nil {
1105-
errorMessage = "NEXT_EXECUTION_TIMESTAMP_UNAVAILABLE"
1100+
return "MISSING_RECURRING_CONFIG"
11061101
} else if self._selfCap?.check() != true {
1107-
errorMessage = "INVALID_SELF_CAPABILITY"
1102+
return "INVALID_SELF_CAPABILITY"
1103+
}
1104+
let config = self._recurringConfig!
1105+
// get the next execution timestamp
1106+
var timestamp = self.calculateNextExecutionTimestampAsConfigured()!
1107+
// fallback in event there was an issue with assigning the last rebalance timestamp or last rebalance was
1108+
// executed long ago - ensure timestamp is in the future
1109+
let nextPossibleTimestamp = getCurrentBlock().timestamp.saturatingAdd(1.0)
1110+
if timestamp < nextPossibleTimestamp {
1111+
timestamp = nextPossibleTimestamp
11081112
}
1109-
if errorMessage != nil {
1110-
return errorMessage
1113+
if timestamp == UFix64.max {
1114+
return "INTERVAL_OVERFLOW"
11111115
}
11121116

11131117
// check for other scheduled transactions within the desired interval
@@ -1118,28 +1122,16 @@ access(all) contract DeFiActions {
11181122
let scheduledTxn = self.borrowScheduledTransaction(id: id)!
11191123
if scheduledTxn.status() == FlowTransactionScheduler.Status.Scheduled {
11201124
// found another scheduled transaction within the configured interval
1121-
if scheduledTxn.timestamp <= timestamp! {
1125+
if scheduledTxn.timestamp <= timestamp {
11221126
return nil
11231127
}
11241128
}
11251129
}
11261130

1127-
// fallback in event there was an issue with assigning the last rebalance timestamp or last rebalance was
1128-
// executed long ago
1129-
let config = self._recurringConfig!
1130-
let now = getCurrentBlock().timestamp
1131-
if timestamp! < now {
1132-
// protect overflow & update timestamp value
1133-
if UInt64(UFix64.max) - UInt64(now) < UInt64(config.interval) {
1134-
return "INTERVAL_OVERFLOW"
1135-
}
1136-
timestamp = now + UFix64(config.interval)
1137-
}
1138-
11391131
// estimate the transaction fees
11401132
let estimate = FlowTransactionScheduler.estimate(
11411133
data: config.forceRebalance,
1142-
timestamp: timestamp!,
1134+
timestamp: timestamp,
11431135
priority: config.priority,
11441136
executionEffort: config.executionEffort
11451137
)
@@ -1169,7 +1161,7 @@ access(all) contract DeFiActions {
11691161
let txn <- FlowTransactionScheduler.schedule(
11701162
handlerCap: self._selfCap!,
11711163
data: { "force": config.forceRebalance },
1172-
timestamp: timestamp!,
1164+
timestamp: timestamp,
11731165
priority: config.priority,
11741166
executionEffort: config.executionEffort,
11751167
fees: <-fees
@@ -1199,17 +1191,13 @@ access(all) contract DeFiActions {
11991191
return &self._scheduledTransactions[id]
12001192
}
12011193
/// Calculates the next execution timestamp for a recurring rebalance if the AutoBalancer is configured as such.
1202-
/// Returns nil if either unconfigured for recurring rebalancing or the interval is greater than the maximum
1203-
/// possible timestamp.
1194+
/// Returns nil if unconfigured for recurring rebalancing.
12041195
///
12051196
/// @return UFix64?: The next execution timestamp, or nil if a recurring rebalance is not configured
12061197
///
12071198
access(all) view fun calculateNextExecutionTimestampAsConfigured(): UFix64? {
12081199
if let config = self._recurringConfig {
1209-
// protect overflow
1210-
return (UInt64(UFix64.max) - UInt64(self._lastRebalanceTimestamp)) >= UInt64(config.interval)
1211-
? self._lastRebalanceTimestamp + UFix64(config.interval)
1212-
: nil
1200+
return self._lastRebalanceTimestamp.saturatingAdd(UFix64(config.interval))
12131201
}
12141202
return nil
12151203
}

0 commit comments

Comments
 (0)