From 70e23ca5c6b98a300352cc9482fca11e5916bcca Mon Sep 17 00:00:00 2001 From: Eric <5089238+emizzle@users.noreply.github.com> Date: Thu, 14 Dec 2023 16:56:49 +1100 Subject: [PATCH] add back clock offset for non-hardhat cases --- codex/contracts/clock.nim | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/codex/contracts/clock.nim b/codex/contracts/clock.nim index 26d2d263..d5f2f4ee 100644 --- a/codex/contracts/clock.nim +++ b/codex/contracts/clock.nim @@ -1,3 +1,4 @@ +import std/times import pkg/ethers import pkg/chronos import pkg/stint @@ -13,6 +14,7 @@ type OnChainClock* = ref object of Clock provider: Provider subscription: Subscription + offset: times.Duration started: bool newBlock: AsyncEvent lastBlockTime: UInt256 @@ -25,6 +27,9 @@ method start*(clock: OnChainClock) {.async.} = return proc onBlock(blck: Block) {.upraises:[].} = + let blockTime = initTime(blck.timestamp.truncate(int64), 0) + let computerTime = getTime() + clock.offset = blockTime - computerTime clock.lastBlockTime = blck.timestamp clock.newBlock.fire() @@ -57,9 +62,7 @@ method now*(clock: OnChainClock): SecondsSince1970 = else: doAssert clock.started, "clock should be started before calling now()" - trace "using cached block timestamp (newHeads) for clock.now", - timestamp = clock.lastBlockTime.truncate(int64) - return clock.lastBlockTime.truncate(int64) + return toUnix(getTime() + clock.offset) method waitUntil*(clock: OnChainClock, time: SecondsSince1970) {.async.} = while (let difference = time - clock.now(); difference > 0):