[contracts] Use Duration instead of float for clock offset
Co-authored-by: Eric Mastro <eric.mastro@gmail.com>
This commit is contained in:
parent
4e6d84850a
commit
58f0439f37
|
@ -10,7 +10,7 @@ type
|
||||||
OnChainClock* = ref object of Clock
|
OnChainClock* = ref object of Clock
|
||||||
provider: Provider
|
provider: Provider
|
||||||
subscription: Subscription
|
subscription: Subscription
|
||||||
offset: float
|
offset: times.Duration
|
||||||
started: bool
|
started: bool
|
||||||
|
|
||||||
proc new*(_: type OnChainClock, provider: Provider): OnChainClock =
|
proc new*(_: type OnChainClock, provider: Provider): OnChainClock =
|
||||||
|
@ -22,8 +22,8 @@ proc start*(clock: OnChainClock) {.async.} =
|
||||||
clock.started = true
|
clock.started = true
|
||||||
|
|
||||||
proc onBlock(blck: Block) {.async, upraises:[].} =
|
proc onBlock(blck: Block) {.async, upraises:[].} =
|
||||||
let blockTime = blck.timestamp.truncate(int64).float
|
let blockTime = initTime(blck.timestamp.truncate(int64), 0)
|
||||||
let computerTime = epochTime()
|
let computerTime = getTime()
|
||||||
clock.offset = blockTime - computerTime
|
clock.offset = blockTime - computerTime
|
||||||
|
|
||||||
if latestBlock =? (await clock.provider.getBlock(BlockTag.latest)):
|
if latestBlock =? (await clock.provider.getBlock(BlockTag.latest)):
|
||||||
|
@ -40,4 +40,4 @@ proc stop*(clock: OnChainClock) {.async.} =
|
||||||
|
|
||||||
method now*(clock: OnChainClock): SecondsSince1970 =
|
method now*(clock: OnChainClock): SecondsSince1970 =
|
||||||
doAssert clock.started, "clock should be started before calling now()"
|
doAssert clock.started, "clock should be started before calling now()"
|
||||||
int64(epochTime() + clock.offset)
|
toUnix(getTime() + clock.offset)
|
||||||
|
|
Loading…
Reference in New Issue