-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
88 lines (80 loc) · 2.17 KB
/
index.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
87
88
import { Exchange, Network } from "@zetamarkets/sdk";
import { PublicKey, Connection, ConfirmOptions } from "@solana/web3.js";
import { EventType } from "@zetamarkets/sdk/dist/events";
import {
collectSurfaceData,
collectPricingData,
} from "./greeks-update-processing";
import { alert } from "./utils/telegram";
import {
collectZetaGroupMarketMetadata,
subscribeZetaGroupChanges,
} from "./market-metadata-processing";
import { DEBUG_MODE } from "./utils/constants";
const callback = (eventType: EventType, data: any) => {
switch (eventType) {
case EventType.GREEKS:
collectSurfaceData();
collectPricingData();
}
};
const opts: ConfirmOptions = {
skipPreflight: false,
preflightCommitment: "finalized",
commitment: "finalized",
};
export const connection = new Connection(process.env.RPC_URL, opts.commitment);
const network =
process.env!.NETWORK === "mainnet"
? Network.MAINNET
: process.env!.NETWORK === "devnet"
? Network.DEVNET
: Network.LOCALNET;
export const reloadExchange = async () => {
try {
await Exchange.close();
const newConnection = new Connection(process.env.RPC_URL, "finalized");
await Exchange.load(
new PublicKey(process.env.PROGRAM_ID),
network,
newConnection,
opts,
undefined,
undefined,
callback
);
subscribeZetaGroupChanges();
} catch (e) {
alert("Failed to reload exchange", true);
reloadExchange();
}
};
const main = async () => {
if (DEBUG_MODE) {
console.log("Running in debug mode, no data will be pushed to AWS");
}
alert("Loading exchange...", false);
await Exchange.load(
new PublicKey(process.env.PROGRAM_ID),
network,
connection,
opts,
undefined,
undefined,
callback
);
alert("Loaded exchange.", false);
collectZetaGroupMarketMetadata();
subscribeZetaGroupChanges();
setInterval(async () => {
await reloadExchange();
}, 60 * 60 * 1000); // Refresh once every hour
setInterval(async () => {
try {
await Exchange.updateExchangeState();
} catch (e) {
alert(`Failed to update exchange state: ${e}`, true);
}
}, 60_000);
};
main().catch(console.error.bind(console));