This repository has been archived by the owner on May 19, 2023. It is now read-only.
/
definitions.ts
395 lines (305 loc) · 12.6 KB
/
definitions.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import type { Application as ExpressFeathers } from '@feathersjs/express'
import type { ServiceAddons } from '@feathersjs/feathers'
import * as Parser from '@oclif/parser'
import type { Eth } from 'web3-eth'
import type { Web3Events, EventsEmitterCreationOptions, NewBlockEmitterOptions } from '@rsksmart/web3-events'
import type { Observable } from 'rxjs'
import Libp2p from 'libp2p'
import type { Options as Libp2pOptions } from 'libp2p'
import type { AvgBillingPriceService, AgreementService, OfferService, StakeService as StorageStakeService, AvailableCapacityService } from './services/storage/services'
import { ProviderService, NotifierStakeService, PlansService as NotifierPlansService, SubscriptionsService as NotifierSubscriptionsService } from './services/notifier/services'
import type { RatesService } from './rates'
import type { RnsBaseService } from './services/rns'
import type { ReorgEmitterService, NewBlockEmitterService, ConfirmatorService } from './blockchain/services'
import * as storageEvents from '@rsksmart/rif-marketplace-storage/types/web3-v1-contracts/StorageManager'
import * as stakingEvents from '@rsksmart/rif-marketplace-storage/types/web3-v1-contracts/Staking'
import * as notifierStakingEvents from '@rsksmart/rif-marketplace-notifier/types/web3-v1-contracts/Staking'
import * as notifierEvents from '@rsksmart/rif-marketplace-notifier/types/web3-v1-contracts/NotifierManager'
import { NotificationService } from './notification'
import { CommsService } from './communication/service'
export type EmitFn = (...args: any[]) => void
export enum SupportedServices {
STORAGE = 'storage',
RNS = 'rns',
NOTIFIER = 'notifier'
}
export type SupportedTokens = 'rif' | 'rbtc'
export const ZERO_ADDRESS = '0x'.padEnd(42, '0')
export const ZERO_BYTES_32 = '0x'.padEnd(64, '0')
export function isSupportedServices (value: any): value is SupportedServices {
return Object.values(SupportedServices).includes(value)
}
export enum ServiceAddresses {
NOTIFICATION = '/notification',
COMMS = '/comms',
RNS_DOMAINS = '/rns/v0/domains',
RNS_SOLD = '/rns/v0/sold',
RNS_OFFERS = '/rns/v0/offers',
STORAGE_OFFERS = '/storage/v0/offers',
AVG_BILLING_PRICE = '/storage/v0/avgBillingPrice',
STORAGE_AGREEMENTS = '/storage/v0/agreements',
STORAGE_STAKES = '/storage/v0/stakes',
STORAGE_AVAILABLE_CAPACITY = '/storage/v0/availableCapacity',
NOTIFIER_STAKES = '/notifier/v0/stakes',
NOTIFIER_PROVIDERS = '/notifier/v0/providers',
NOTIFIER_OFFERS = '/notifier/v0/offers',
NOTIFIER_SUBSCRIPTIONS = '/notifier/v0/subscriptions',
XR = '/rates/v0/',
CONFIRMATIONS = '/confirmations',
NEW_BLOCK_EMITTER = '/new-block',
REORG_EMITTER = '/reorg'
}
// A mapping of service names to types. Will be extended in service files.
interface ServiceTypes {
[ServiceAddresses.STORAGE_OFFERS]: OfferService & ServiceAddons<any>
[ServiceAddresses.AVG_BILLING_PRICE]: AvgBillingPriceService & ServiceAddons<any>
[ServiceAddresses.STORAGE_AGREEMENTS]: AgreementService & ServiceAddons<any>
[ServiceAddresses.STORAGE_STAKES]: StorageStakeService & ServiceAddons<any>
[ServiceAddresses.STORAGE_AVAILABLE_CAPACITY]: AvailableCapacityService & ServiceAddons<any>
[ServiceAddresses.NOTIFIER_PROVIDERS]: ProviderService & ServiceAddons<any>
[ServiceAddresses.NOTIFIER_OFFERS]: NotifierPlansService & ServiceAddons<any>
[ServiceAddresses.NOTIFIER_SUBSCRIPTIONS]: NotifierSubscriptionsService & ServiceAddons<any>
[ServiceAddresses.NOTIFIER_STAKES]: NotifierStakeService & ServiceAddons<any>
[ServiceAddresses.XR]: RatesService & ServiceAddons<any>
[ServiceAddresses.RNS_DOMAINS]: RnsBaseService & ServiceAddons<any>
[ServiceAddresses.RNS_SOLD]: RnsBaseService & ServiceAddons<any>
[ServiceAddresses.RNS_OFFERS]: RnsBaseService & ServiceAddons<any>
[ServiceAddresses.CONFIRMATIONS]: ConfirmatorService & ServiceAddons<any>
[ServiceAddresses.NOTIFICATION]: NotificationService & ServiceAddons<any>
[ServiceAddresses.COMMS]: CommsService & ServiceAddons<any>
[ServiceAddresses.NEW_BLOCK_EMITTER]: NewBlockEmitterService & ServiceAddons<any>
[ServiceAddresses.REORG_EMITTER]: ReorgEmitterService & ServiceAddons<any>
}
// The application instance type that will be used everywhere else
export type Application = ExpressFeathers<ServiceTypes>;
export interface CachedService {
precache (eth: Eth, web3events: Web3Events): Observable<string>
purge (): Promise<void>
initialize (app: Application): Promise<{ stop: () => void }>
}
export interface BlockchainServiceOptions {
// Address of deployed contract
contractAddress?: string
// Specify behavior of EventsEmitter, that retrieves events from blockchain and pass them onwards for further processing.
eventsEmitter?: EventsEmitterCreationOptions
}
export interface DbBackUpConfig {
blocks: number
// Place where the backups will be put. If relative than it is relative to `dataDir`. Absolute path is honored.
path: string
}
export enum CommsStrategy {
Libp2p = 'libp2p',
API = 'api'
}
/// //////////////////////////////////////////////////////////////////////////////////////////////////
// RATES
export enum RatesProvider {
coingecko = 'coingecko'
}
export function isRatesProvider (value: any): value is RatesProvider {
return Object.values(RatesProvider).includes(value)
}
export type ToSymbols = 'usd' | 'eur' | 'btc' | 'ars' | 'cny' | 'krw' | 'jpy'
export const SUPPORTED_TO_SYMBOLS: ToSymbols[] = ['usd', 'eur', 'btc', 'ars', 'cny', 'krw', 'jpy']
export type FromSymbols = 'rbtc' | 'rif'
export const SUPPORTED_FROM_SYMBOLS: FromSymbols[] = ['rbtc', 'rif']
export type FetchedRates = Record<FromSymbols, Record<ToSymbols, number>>
export interface Config {
host?: string
port?: number
// Place where the DB file will be put. If relative than it is relative to `dataDir`. Absolute path is honored.
db?: string
// DB backup config
dbBackUp?: DbBackUpConfig
// Place where all the persistent data should be stored
// Recommended is absolute path, if relative then it will be based on root of the project (not CWD)
dataDir?: string
log?: {
level?: string
filter?: string
path?: string
}
comms?: {
strategy?: CommsStrategy
libp2p?: Libp2pOptions
countOfMessagesPersistedPerAgreement?: number
}
notification?: {
countOfNotificationPersistedPerAgreement?: number
retriesCount?: number
retryInterval?: number
}
blockchain?: {
networkId?: number
// Address to where web3js should connect to. Should be WS endpoint.
provider?: string
// Specifies behaviour of the default New Block emitter that will be used across all the EventsEmitters
newBlockEmitter?: NewBlockEmitterOptions
}
rates?: {
// Which provider to use for fetching the rates
provider?: RatesProvider
// Refresh period in seconds, be aware of rate-limiting of each Provider
refresh?: number
// Tokens that should be fetched
fromSymbols?: string[]
// Fiats that should be used as conversions for fromSymbols
toSymbols?: string[]
}
// Settings for Storage service related function
notifier?: {
// Sets if Storage service should be enabled
enabled?: boolean
// Toggle to run precache upon start
requirePrecache?: boolean
// Supported tokens and their addresses
tokens?: {
[key: string]: SupportedTokens
}
// Staking contract options
staking?: BlockchainServiceOptions
// Storage Manager contract options
notifierManager?: BlockchainServiceOptions
}
// Settings for Storage service related function
storage?: {
// Sets if Storage service should be enabled
enabled?: boolean
// Toggle to run precache upon start
requirePrecache?: boolean
// Supported tokens and their addresses
tokens?: {
[key: string]: SupportedTokens
}
// Staking contract options
staking?: BlockchainServiceOptions
// Storage Manager contract options
storageManager?: BlockchainServiceOptions
}
// Settings for RNS service related function
rns?: {
// Sets if RNS service should be enabled
enabled?: boolean
// Toggle to run precache upon start
requirePrecache?: boolean
// Auction registrar contract's options
registrar?: BlockchainServiceOptions
// FIFSAddrRegistrar contract's options
fifsAddrRegistrar?: BlockchainServiceOptions
// Owner contract's options
owner?: BlockchainServiceOptions
// Reverse contract's options
reverse?: BlockchainServiceOptions
// Placement contract's options
placement?: BlockchainServiceOptions
}
}
interface Args {[name: string]: any}
type Options<T> = T extends Parser.Input<infer R>
? Parser.Output<R, Args>
: any
export type Flags<T> = Options<T>['flags']
/**
* Basic logger interface used around the application.
*/
export interface Logger {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
critical (message: string | Error | Record<any, unknown>, ...meta: any[]): never
// eslint-disable-next-line @typescript-eslint/no-explicit-any
error (message: string | Error | Record<any, unknown>, ...meta: any[]): void
// eslint-disable-next-line @typescript-eslint/no-explicit-any
warn (message: string | Record<any, unknown>, ...meta: any[]): void
// eslint-disable-next-line @typescript-eslint/no-explicit-any
info (message: string | Record<any, unknown>, ...meta: any[]): void
// eslint-disable-next-line @typescript-eslint/no-explicit-any
verbose (message: string | Record<any, unknown>, ...meta: any[]): void
// eslint-disable-next-line @typescript-eslint/no-explicit-any
debug (message: string | Record<any, unknown>, ...meta: any[]): void
extend?: (name: string) => Logger
}
/**
* Interface for more complex handling of events.
*/
export interface Handler<E, T> {
events: string[]
process: (event: E, services: T, deps: { eth?: Eth, libp2p?: Libp2p }) => Promise<void>
}
/// //////////////////////////////////////////////////////////////////////////////////////////////////
// STORAGE
export type StorageAgreementEvents = storageEvents.AgreementFundsDeposited
| storageEvents.AgreementFundsPayout
| storageEvents.AgreementFundsWithdrawn
| storageEvents.AgreementStopped
| storageEvents.NewAgreement
export type StorageOfferEvents = storageEvents.BillingPlanSet
| storageEvents.MessageEmitted
| storageEvents.TotalCapacitySet
export type StakeEvents = stakingEvents.Staked | stakingEvents.Unstaked
export type StorageEvents = StorageOfferEvents | StorageAgreementEvents | StakeEvents
/// //////////////////////////////////////////////////////////////////////////////////////////////////
// NOTIFIER
export type NotificationManagerEvents = notifierEvents.ProviderRegistered
| notifierEvents.SubscriptionCreated
| notifierEvents.FundsWithdrawn
export type NotifierStakeEvents = notifierStakingEvents.Staked | notifierStakingEvents.Unstaked
export type NotifierEvents = NotificationManagerEvents | NotifierStakeEvents
/****************************************************************************************
* Communications
*/
export enum MessageCodesEnum {
I_AGREEMENT_NEW = 'I_AGR_NEW', // PROVIDER
I_AGREEMENT_EXPIRED = 'I_AGR_EXP', // BOTH
I_HASH_PINNED = 'I_HASH_STOP', // CONSUMER
E_AGREEMENT_SIZE_LIMIT_EXCEEDED = 'E_AGR_SIZE_OVERFLOW', // CONSUMER
//
I_HASH_START = 'I_HASH_START',
E_GENERAL = 'E_GEN',
W_HASH_RETRY = 'W_HASH_RETRY',
I_AGREEMENT_STOPPED = 'I_AGR_STOP',
I_GENERAL = 'I_GEN',
I_MULTIADDR_ANNOUNCEMENT = 'I_ADDR_ANNOUNCE',
I_RESEND_LATEST_MESSAGES = 'I_RESEND',
W_GENERAL = 'W_GEN',
E_HASH_NOT_FOUND = 'E_HASH_404',
}
interface BasePayload {
agreementReference: string
}
export interface RetryPayload extends BasePayload {
error: string
retryNumber: number
totalRetries: number
}
export interface HashInfoPayload extends BasePayload {
hash: string
}
export type AgreementInfoPayload = BasePayload
export interface AgreementSizeExceededPayload extends BasePayload {
hash: string
size: number
expectedSize: number
}
// Incoming messages
export interface MultiaddrAnnouncementPayload {
agreementReference: string
peerId: string
}
export interface ResendMessagesPayload {
requestId: string
agreementReference: string
code?: string
}
export interface CommsMessage<Payload> {
timestamp: number
version: number
code: string
payload: Payload
}
export type CommsPayloads = ResendMessagesPayload | MultiaddrAnnouncementPayload | AgreementSizeExceededPayload | AgreementInfoPayload | HashInfoPayload | RetryPayload
export type MessageHandler = (message: CommsMessage<CommsPayloads>) => Promise<void>
// NOTIFICATION
export enum NotificationType {
STORAGE = 'storage'
}
export type NotificationPayload = CommsPayloads & { code: string, timestamp: number }