forked from definedfi/sdk
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathupdateConfig.ts
More file actions
56 lines (46 loc) · 1.31 KB
/
updateConfig.ts
File metadata and controls
56 lines (46 loc) · 1.31 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
50
51
52
53
54
55
56
import { Codex } from "@codex-data/sdk";
// This example demonstrates how to dynamically update the SDK configuration,
// particularly the WebSocket authentication headers. It starts a subscription
// to token events, then after 5 seconds creates a new API token and updates
// the SDK's WebSocket headers to use the new token before starting another subscription.
const sdk = new Codex(process.env.CODEX_API_KEY || "", {
apiUrl: process.env.CODEX_API_URL,
});
const subscribe = () => {
sdk.subscriptions.onTokenEventsCreated(
{
input: {
tokenAddress: "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c",
networkId: 56,
},
},
{
next(value) {
console.log("Token event: ", value);
},
complete() {
console.log("Token event subscription completed");
},
error(error) {
console.error("Token event subscription error: ", error);
},
},
);
};
subscribe();
setTimeout(async () => {
console.log("Updating config...");
const { createApiTokens } = await sdk.mutations.createApiTokens({
input: {
count: 1,
},
});
const token = createApiTokens[0].token;
console.log("Updating config with new token", token);
sdk.updateConfig({
wsHeaders: {
Authorization: `Bearer ${token}`,
},
});
subscribe();
}, 5000);