-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.ts
45 lines (39 loc) · 1.69 KB
/
time.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { BigNumber, BigNumberish, providers } from "ethers";
import { ethers } from "hardhat";
export const ONE_MINUTE = 60;
export const ONE_HOUR = ONE_MINUTE * 60;
export const ONE_DAY = ONE_HOUR * 24;
/**
* @param seconds How many seconds from now the timestamp should be set to.
* @param shouldAdvanceBlock True if a block should be mined, effecting the next read call.
* False to use this value as the timestamp for the next write call.
* @returns The new timestamp.
*/
export async function increaseTime(seconds: BigNumberish, shouldAdvanceBlock = true): Promise<number> {
const secondsNumber = BigNumber.from(seconds).toNumber();
const now = await getBlockTime();
const targetTime = now + secondsNumber;
await increaseTimeTo(targetTime, shouldAdvanceBlock);
return targetTime;
}
export async function increaseTimeTo(timestamp: BigNumberish, shouldAdvanceBlock = true): Promise<void> {
const timestampNumber = BigNumber.from(timestamp).toNumber();
const provider: providers.JsonRpcProvider = ethers.provider;
await provider.send("evm_setNextBlockTimestamp", [timestampNumber]);
if (shouldAdvanceBlock) {
await advanceBlock();
}
}
export async function increaseTimeToNextHour(): Promise<void> {
const time = await getBlockTime();
const timeToMoveTo = Math.ceil((time + 1) / ONE_HOUR) * ONE_HOUR;
await increaseTimeTo(timeToMoveTo);
}
export async function getBlockTime(block: string | number = "latest"): Promise<number> {
const provider: providers.JsonRpcProvider = ethers.provider;
return (await provider.getBlock(block)).timestamp;
}
export async function advanceBlock() {
const provider: providers.JsonRpcProvider = ethers.provider;
await provider.send("evm_mine", []);
}