-
Notifications
You must be signed in to change notification settings - Fork 11
/
balance-monitor-cli.ts
84 lines (72 loc) · 2.01 KB
/
balance-monitor-cli.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
// Live monitoring of specified account.
// Run: yarn exec ts-node examples/balance-monitor-cli.ts <ACCOUNT ID> [<ASTROGRAPH URL>]
import { InMemoryCache } from "apollo-cache-inmemory";
import ApolloClient from "apollo-client";
import { WebSocketLink } from "apollo-link-ws";
import { gql } from "apollo-server";
import { SubscriptionClient } from "subscriptions-transport-ws";
import ws from "ws";
import { MutationType } from "../src/model";
import { ACCOUNT_ID, GRAPHQL_ENDPOINT } from "./args";
const client = new SubscriptionClient(GRAPHQL_ENDPOINT, { reconnect: true }, ws);
const link = new WebSocketLink(client);
const cache = new InMemoryCache();
const apolloClient = new ApolloClient({ link, cache });
console.log("Connecting to GraphQL...");
console.log("Account ID:", ACCOUNT_ID);
const SUBSCRIPTION = gql`
subscription balance($args: EventInput!) {
balance(args: $args) {
account {
id
}
mutationType
asset {
id
code
issuer { id }
}
values {
asset {
id
code
issuer { id }
}
balance
limit
authorized
}
}
}
`;
client.onError(error => console.log("An error occured!", error.target));
apolloClient
.subscribe({
fetchPolicy: "network-only",
query: SUBSCRIPTION,
variables: {
args: {
idEq: ACCOUNT_ID
}
}
})
.subscribe({
next(data: any) {
const payload = data.data.balance;
if (payload.mutationType === MutationType.Remove) {
console.log(`Trustline for ${payload.asset.code}-${payload.asset.issuer.id} is removed`);
return;
}
const values = payload.values;
console.log(
"New balance for",
values.asset.id,
"is now",
values.balance,
"with limit",
values.limit,
values.authorized ? "and authorization" : "without authorization"
);
}
});
// setTimeout(() => { console.log("Finished.", eventCount, "events received."); }, 10000);