2022-05-16 13:38:52 +00:00
|
|
|
import std/times
|
|
|
|
import pkg/ethers
|
2024-12-03 11:16:24 +00:00
|
|
|
import pkg/questionable
|
2022-05-16 13:38:52 +00:00
|
|
|
import pkg/chronos
|
|
|
|
import pkg/stint
|
2022-05-17 09:50:52 +00:00
|
|
|
import ../clock
|
2023-12-19 21:06:24 +00:00
|
|
|
import ../conf
|
2022-05-16 13:38:52 +00:00
|
|
|
|
2022-05-17 14:42:03 +00:00
|
|
|
export clock
|
|
|
|
|
2023-12-19 21:06:24 +00:00
|
|
|
logScope:
|
|
|
|
topics = "contracts clock"
|
|
|
|
|
2022-05-16 13:38:52 +00:00
|
|
|
type
|
2022-05-17 09:50:52 +00:00
|
|
|
OnChainClock* = ref object of Clock
|
2022-05-16 13:38:52 +00:00
|
|
|
provider: Provider
|
|
|
|
subscription: Subscription
|
2022-09-29 09:18:40 +00:00
|
|
|
offset: times.Duration
|
2024-03-11 16:57:20 +00:00
|
|
|
blockNumber: UInt256
|
2022-05-16 13:38:52 +00:00
|
|
|
started: bool
|
2023-07-13 09:19:45 +00:00
|
|
|
newBlock: AsyncEvent
|
2022-05-16 13:38:52 +00:00
|
|
|
|
2022-05-17 09:50:52 +00:00
|
|
|
proc new*(_: type OnChainClock, provider: Provider): OnChainClock =
|
2023-07-13 09:19:45 +00:00
|
|
|
OnChainClock(provider: provider, newBlock: newAsyncEvent())
|
2022-05-16 13:38:52 +00:00
|
|
|
|
2024-03-11 16:57:20 +00:00
|
|
|
proc update(clock: OnChainClock, blck: Block) =
|
|
|
|
if number =? blck.number and number > clock.blockNumber:
|
2022-09-29 09:18:40 +00:00
|
|
|
let blockTime = initTime(blck.timestamp.truncate(int64), 0)
|
|
|
|
let computerTime = getTime()
|
2022-05-16 13:38:52 +00:00
|
|
|
clock.offset = blockTime - computerTime
|
2024-03-11 16:57:20 +00:00
|
|
|
clock.blockNumber = number
|
|
|
|
trace "updated clock", blockTime=blck.timestamp, blockNumber=number, offset=clock.offset
|
2023-07-13 09:19:45 +00:00
|
|
|
clock.newBlock.fire()
|
2022-05-16 13:38:52 +00:00
|
|
|
|
2024-03-11 16:57:20 +00:00
|
|
|
proc update(clock: OnChainClock) {.async.} =
|
|
|
|
try:
|
|
|
|
if latest =? (await clock.provider.getBlock(BlockTag.latest)):
|
|
|
|
clock.update(latest)
|
2024-05-23 15:29:30 +00:00
|
|
|
except CancelledError as error:
|
|
|
|
raise error
|
2024-03-11 16:57:20 +00:00
|
|
|
except CatchableError as error:
|
|
|
|
debug "error updating clock: ", error=error.msg
|
|
|
|
discard
|
|
|
|
|
|
|
|
method start*(clock: OnChainClock) {.async.} =
|
|
|
|
if clock.started:
|
|
|
|
return
|
|
|
|
|
2024-12-03 11:16:24 +00:00
|
|
|
proc onBlock(blckResult: ?!Block) =
|
|
|
|
if eventError =? blckResult.errorOption:
|
|
|
|
error "There was an error in block subscription", msg=eventError.msg
|
|
|
|
return
|
|
|
|
|
2024-03-11 16:57:20 +00:00
|
|
|
# ignore block parameter; hardhat may call this with pending blocks
|
|
|
|
asyncSpawn clock.update()
|
|
|
|
|
|
|
|
await clock.update()
|
2022-05-16 13:38:52 +00:00
|
|
|
|
|
|
|
clock.subscription = await clock.provider.subscribe(onBlock)
|
2023-12-19 21:06:24 +00:00
|
|
|
clock.started = true
|
2022-05-16 13:38:52 +00:00
|
|
|
|
2023-11-22 10:09:12 +00:00
|
|
|
method stop*(clock: OnChainClock) {.async.} =
|
2022-05-16 13:38:52 +00:00
|
|
|
if not clock.started:
|
|
|
|
return
|
|
|
|
|
|
|
|
await clock.subscription.unsubscribe()
|
2023-12-19 21:06:24 +00:00
|
|
|
clock.started = false
|
2022-05-16 13:38:52 +00:00
|
|
|
|
2024-02-16 22:12:16 +00:00
|
|
|
method now*(clock: OnChainClock): SecondsSince1970 =
|
2024-03-11 16:57:20 +00:00
|
|
|
doAssert clock.started, "clock should be started before calling now()"
|
|
|
|
return toUnix(getTime() + clock.offset)
|
2023-07-13 09:19:45 +00:00
|
|
|
|
|
|
|
method waitUntil*(clock: OnChainClock, time: SecondsSince1970) {.async.} =
|
2024-02-16 22:12:16 +00:00
|
|
|
while (let difference = time - clock.now(); difference > 0):
|
2023-07-13 09:19:45 +00:00
|
|
|
clock.newBlock.clear()
|
|
|
|
discard await clock.newBlock.wait().withTimeout(chronos.seconds(difference))
|