-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathTimeUtils.ts
More file actions
49 lines (45 loc) · 1.69 KB
/
TimeUtils.ts
File metadata and controls
49 lines (45 loc) · 1.69 KB
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
46
47
48
49
import { UMA_FIRST_EMP_BLOCK } from "./Constants";
import dotenv from "dotenv";
import type Web3 from "web3";
dotenv.config();
/**
* @notice Return average block-time for a period.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export async function averageBlockTimeSeconds(chainId?: number): Promise<number> {
// TODO: Call an external API to get this data. Currently this value is a hard-coded estimate
// based on the data from https://etherscan.io/chart/blocktime. ~13.5 seconds has been the average
// since April 2016, although this value seems to spike periodically for a relatively short period of time.
const defaultBlockTimeSeconds = 12;
if (!defaultBlockTimeSeconds) {
throw "Missing default block time value";
}
// - Optimism- Bedrock produces blocks every 2 seconds
// https://community.optimism.io/docs/developers/bedrock/how-is-bedrock-different/#block-production
// - Polygon source: https://polygonscan.com/chart/blocktime
switch (chainId) {
case 10:
return 2;
case 42161:
return 0.5;
case 288:
return 150;
case 137:
return 2.5;
case 1:
return defaultBlockTimeSeconds;
default:
return defaultBlockTimeSeconds;
}
}
// Sets fromBlock to the value of an environment variable if one is set. This can be set to 0 to make tests work with Ganache, or any other value needed for a production script or bot.
export async function getFromBlock(web3: Web3): Promise<number> {
const networkType = await web3.eth.net.getNetworkType();
if (process.env.FROM_BLOCK) {
return Number(process.env.FROM_BLOCK);
} else if (networkType === "main") {
return UMA_FIRST_EMP_BLOCK;
} else {
return 0;
}
}