Skip to content

Commit

Permalink
fix: account for traveling into future on testnets
Browse files Browse the repository at this point in the history
  • Loading branch information
doomsower committed Jul 18, 2024
1 parent a0d32eb commit 3f47fc5
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/services/RedstoneServiceV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,25 @@ export class RedstoneServiceV3 {
this.liquidationPreviewUpdates = this.liquidationPreviewUpdates.bind(this);

if (this.config.optimistic) {
const block = await this.client.pub.getBlock({
blockNumber: this.client.anvilForkBlock,
});
if (!block) {
throw new Error(`cannot get latest block`);
}
// https://github.com/redstone-finance/redstone-oracles-monorepo/blob/c7569a8eb7da1d3ad6209dfcf59c7ca508ea947b/packages/sdk/src/request-data-packages.ts#L82
// we round the timestamp to full minutes for being compatible with
// oracle-nodes, which usually work with rounded 10s and 60s intervals
this.#optimisticTimestamp =
60_000 * Math.floor(new Date().getTime() / 60_000);
//
// Also, when forking anvil->anvil (when running on testnets) block.timestamp can be in future because min ts for block is 1 seconds,
// and scripts can take dozens of blocks (hundreds for faucet). So we take min value;
const now = new Date().getTime();
const anvilTs = 10 * Math.floor(Number(block.timestamp) / 10) * 1000;
const fromNowTs = 60_000 * Math.floor(now / 60_000 - 60_000);
this.#optimisticTimestamp = Math.min(anvilTs, fromNowTs);
const delta = Math.ceil((now - this.#optimisticTimestamp) / 1000);
this.log.info(
`will use optimistic timestamp: ${this.#optimisticTimestamp}`,
`will use optimistic timestamp: ${this.#optimisticTimestamp} (delta: ${delta}s)`,
);
}
}
Expand Down

0 comments on commit 3f47fc5

Please sign in to comment.