-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonitoringService.ts
169 lines (158 loc) · 5.61 KB
/
MonitoringService.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import {
ILogUtils,
ILogUtilsType,
ITimeUtilsType,
ITimeUtils,
} from "@snickerdoodlelabs/common-utils";
import {
IMasterIndexer,
IMasterIndexerType,
} from "@snickerdoodlelabs/indexers";
import {
AccountIndexingError,
AjaxError,
DataWalletBackupID,
DiscordError,
EIndexerMethod,
InvalidParametersError,
isAccountValidForChain,
MethodSupportError,
PersistenceError,
SiteVisit,
TwitterError,
UnixTimestamp,
} from "@snickerdoodlelabs/objects";
import { inject, injectable } from "inversify";
import { okAsync, ResultAsync } from "neverthrow";
import { ResultUtils } from "neverthrow-result-utils";
import {
IDiscordService,
IDiscordServiceType,
IMonitoringService,
ITwitterService,
ITwitterServiceType,
} from "@core/interfaces/business/index.js";
import {
IBrowsingDataRepository,
IBrowsingDataRepositoryType,
IDataWalletPersistence,
IDataWalletPersistenceType,
ILinkedAccountRepository,
ILinkedAccountRepositoryType,
INftRepository,
INftRepositoryType,
ITransactionHistoryRepository,
ITransactionHistoryRepositoryType,
} from "@core/interfaces/data/index.js";
import {
IConfigProvider,
IConfigProviderType,
IContextProvider,
IContextProviderType,
} from "@core/interfaces/utilities/index.js";
@injectable()
export class MonitoringService implements IMonitoringService {
public constructor(
@inject(IMasterIndexerType)
protected masterIndexer: IMasterIndexer,
@inject(IContextProviderType) protected contextProvider: IContextProvider,
@inject(IConfigProviderType) protected configProvider: IConfigProvider,
@inject(ILogUtilsType) protected logUtils: ILogUtils,
@inject(IDataWalletPersistenceType)
protected persistence: IDataWalletPersistence,
@inject(ILinkedAccountRepositoryType)
protected accountRepo: ILinkedAccountRepository,
@inject(ITransactionHistoryRepositoryType)
protected transactionRepo: ITransactionHistoryRepository,
@inject(IBrowsingDataRepositoryType)
protected browsingDataRepo: IBrowsingDataRepository,
@inject(IDiscordServiceType)
protected discordService: IDiscordService,
@inject(ITwitterServiceType)
protected twitterService: ITwitterService,
@inject(INftRepositoryType)
protected nftRepository: INftRepository,
@inject(ITimeUtilsType) protected timeUtils: ITimeUtils,
) {}
public pollTransactions(): ResultAsync<
void,
PersistenceError | AccountIndexingError | AjaxError
> {
// Grab the linked accounts and the config
return ResultUtils.combine([
this.accountRepo.getAccounts(),
// Only get the supported chains for transactions!
this.masterIndexer.getSupportedChains(EIndexerMethod.Transactions),
])
.andThen(([linkedAccounts, supportedChains]) => {
// Loop over all the linked accounts in the data wallet, and get the last transaction for each supported chain
// config.chainInformation is the list of supported chains,
return ResultUtils.combine(
linkedAccounts.map((linkedAccount) => {
return ResultUtils.combine(
supportedChains.map((chain) => {
if (!isAccountValidForChain(chain, linkedAccount)) {
return okAsync([]);
}
return this.transactionRepo
.getLatestTransactionForAccount(
chain,
linkedAccount.sourceAccountAddress,
)
.andThen((tx) => {
let startTime = UnixTimestamp(0);
if (tx != null && tx.timestamp != null) {
startTime = tx.timestamp;
}
if (startTime == 0) {
this.logUtils.debug(
`For chain ${chain}, we are either cold-starting the transaction history for ${linkedAccount.sourceAccountAddress} or there are actually no transactions for this account yet. Fetching all transactions for this account.`,
);
}
return this.masterIndexer
.getLatestTransactions(
linkedAccount.sourceAccountAddress,
startTime,
chain,
)
.orElse((e) => {
this.logUtils.error(
`In pollTransactions(), received an error fetching transactions on chain ${chain} for account address ${linkedAccount.sourceAccountAddress}.`,
e,
);
return okAsync([]);
});
});
}),
);
}),
);
})
.andThen((transactionsArr) => {
const transactions = transactionsArr.flat(2);
return this.transactionRepo.addTransactions(transactions);
});
}
public pollNfts(): ResultAsync<void, unknown> {
return this.nftRepository
.getNfts(this.timeUtils.getUnixNow())
.map(() => {});
}
public siteVisited(
siteVisit: SiteVisit,
): ResultAsync<void, PersistenceError> {
return this.browsingDataRepo.addSiteVisits([siteVisit]);
}
public pollBackups(): ResultAsync<void, PersistenceError> {
return this.persistence.pollBackups();
}
public pollDiscord(): ResultAsync<void, PersistenceError | DiscordError> {
return this.discordService.poll();
}
public pollTwitter(): ResultAsync<void, PersistenceError | TwitterError> {
return this.twitterService.poll();
}
public postBackups(): ResultAsync<DataWalletBackupID[], PersistenceError> {
return this.persistence.postBackups();
}
}