-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpocket.ts
86 lines (73 loc) · 2.13 KB
/
pocket.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import axios from "axios"; // Optional if you need it for other REST calls
import { HeightStrategy } from ".";
import { logger } from "..";
import { localRPCWrapper, remoteRPCIterator } from "../common";
import { GraphQLClient, request } from 'graphql-request';
const getHeight = async (url: string) => {
const result = await axios({
method: "POST",
url: `${url}/v1/query/height`,
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
},
data: {},
timeout: 5000
})
const { data } = result
logger.debug({ data }, 'response from pocket RPC')
const { height } = data
if (typeof height === 'number') {
return height
} else {
return parseInt(height)
}
}
// PoktScan GraphQL-based height fetching logic
interface GetLatestBlockResponse {
GetLatestBlock: {
block: {
height: number;
time: string; // Or whatever the actual type of 'time' is
}
}
}
const getPoktscanHeight = async () => {
const query = `
{
GetLatestBlock {
block {
height
time
}
}
}
`;
const headers = {
'authorization': process.env.POKTSCAN_API_KEY
}
try {
const client = new GraphQLClient(process.env.POKTSCAN_ENDPOINT, { headers });
const data: GetLatestBlockResponse = await client.request(query);
return data.GetLatestBlock.block.height;
} catch (error) {
logger.error('Error fetching height from Poktscan:', error);
throw error;
}
}
const pocket: HeightStrategy = {
name: 'pocket',
getLocalHeight: () => localRPCWrapper(getHeight),
getRemoteHeight: () => {
if (process.env.USE_POKTSCAN === 'true') {
return getPoktscanHeight();
} else {
return remoteRPCIterator(getHeight);
}
},
init: async () => {
const strategy = process.env.USE_POKTSCAN === 'true' ? 'Poktscan' : 'REST';
logger.info(`Pocket height check strategy initiated (using ${strategy})`);
}
}
export default pocket