nim-dagger/tests/contracts/testClock.nim
markspanbroek d1658d7b77
Fix on-chain clock (#732)
* market: use `pending` blocktag when querying onchain state

* clock: use wall clock in integration tests

reason: we'll need to wait for the next period in
integration tests, and we can't do that if the
time doesn't advance

* clock: remove unused field

* integration: use pending block time to get current time

* clock: fix on-chain clock for hardhat

Only use 'latest' block for updates
Only update the first time you see a block

* integration: do not start tests with a very outdated block

* integration: allow for longer expiry period
2024-03-11 16:57:20 +00:00

47 lines
1.4 KiB
Nim

import std/times
import pkg/chronos
import codex/contracts/clock
import codex/utils/json
import ../ethertest
ethersuite "On-Chain Clock":
var clock: OnChainClock
setup:
clock = OnChainClock.new(ethProvider)
await clock.start()
teardown:
await clock.stop()
test "returns the current time of the EVM":
let latestBlock = (!await ethProvider.getBlock(BlockTag.latest))
let timestamp = latestBlock.timestamp.truncate(int64)
check clock.now() == timestamp
test "updates time with timestamp of new blocks":
let future = (getTime() + 42.years).toUnix
discard await ethProvider.send("evm_setNextBlockTimestamp", @[%future])
discard await ethProvider.send("evm_mine")
check eventually clock.now() == future
test "can wait until a certain time is reached by the chain":
let future = clock.now() + 42 # seconds
let waiting = clock.waitUntil(future)
discard await ethProvider.send("evm_setNextBlockTimestamp", @[%future])
discard await ethProvider.send("evm_mine")
check await waiting.withTimeout(chronos.milliseconds(100))
test "can wait until a certain time is reached by the wall-clock":
let future = clock.now() + 1 # seconds
let waiting = clock.waitUntil(future)
check await waiting.withTimeout(chronos.seconds(2))
test "handles starting multiple times":
await clock.start()
await clock.start()
test "handles stopping multiple times":
await clock.stop()
await clock.stop()