-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevm.ts
53 lines (44 loc) · 1.46 KB
/
evm.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
46
47
48
49
50
51
52
53
import axios, { AxiosResponse } from "axios"
import { HeightStrategy } from "."
import { logger } from ".."
import { localRPCWrapper, remoteRPCIterator } from "../common"
const {
// use `hmyv2_blockNumber` for Harmony
EVM_BLOCK_NUMBER_METHOD_NAME,
EVM_BLOCK_NUMBER_FIELD_PATH
} = process.env;
const method = EVM_BLOCK_NUMBER_METHOD_NAME || 'eth_blockNumber'
const fieldPath = EVM_BLOCK_NUMBER_FIELD_PATH || 'result'
/** Gets an object value from a period-delimited string path. eg. "result.healthy" */
const resolvePath = (response: AxiosResponse, path: string) =>
path.split('.').reduce((p, c) => (p && p[c]) ?? null, response);
const getHeightEVM = async (url: string) => {
const result = await axios({
method: "POST",
url,
data: {
jsonrpc: "2.0",
id: 1,
method,
params: [],
},
timeout: 5000
})
const { data } = result
const blockNumber = resolvePath(data, fieldPath)
logger.debug({ data, url, method }, 'response from EVM RPC')
if (typeof blockNumber === 'number') {
return blockNumber
} else {
return parseInt(blockNumber, 16)
}
}
const evm: HeightStrategy = {
name: 'evm',
getLocalHeight: () => localRPCWrapper(getHeightEVM),
getRemoteHeight: () => remoteRPCIterator(getHeightEVM),
init: async () => {
logger.info(`EVM height check strategy initiated with "${method}" RPC method`)
}
}
export default evm