/
RaidenClient.ts
619 lines (547 loc) · 20.2 KB
/
RaidenClient.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
import assert from 'assert';
import http from 'http';
import { SwapClientType, SwapRole, SwapState } from '../constants/enums';
import { CurrencyInstance } from '../db/types';
import Logger from '../Logger';
import swapErrors from '../swaps/errors';
import SwapClient, { ChannelBalance, ClientStatus, PaymentState, WalletBalance, TradingLimits } from '../swaps/SwapClient';
import { SwapDeal } from '../swaps/types';
import { UnitConverter } from '../utils/UnitConverter';
import errors, { errorCodes } from './errors';
import { Channel, OpenChannelPayload, PaymentEvent, RaidenChannelCount, RaidenClientConfig,
RaidenInfo, RaidenVersion, TokenPaymentRequest, TokenPaymentResponse } from './types';
import { parseResponseBody } from '../utils/utils';
type RaidenErrorResponse = { errors: string };
type PendingTransfer = {
initiator: string;
locked_amount: string;
payment_identifier: string;
role: string;
target: string;
token_address: string;
transferred_amount: string;
};
/**
* A class representing a client to interact with raiden.
*/
class RaidenClient extends SwapClient {
public readonly type = SwapClientType.Raiden;
public readonly finalLock = 100;
public address?: string;
/** A map of currency symbols to token addresses. */
public tokenAddresses = new Map<string, string>();
private port: number;
private host: string;
private unitConverter: UnitConverter;
private totalOutboundAmounts = new Map<string, number>();
private maxChannelOutboundAmounts = new Map<string, number>();
private maxChannelInboundAmounts = new Map<string, number>();
private directChannelChecks: boolean;
/**
* Creates a raiden client.
*/
constructor(
{ config, logger, unitConverter, currencyInstances, directChannelChecks = false }:
{
config: RaidenClientConfig,
logger: Logger,
unitConverter: UnitConverter,
currencyInstances: CurrencyInstance[],
directChannelChecks?: boolean,
},
) {
super(logger, config.disable);
const { host, port } = config;
this.setTokenAddresses(currencyInstances);
this.port = port;
this.host = host;
this.unitConverter = unitConverter;
this.directChannelChecks = directChannelChecks;
}
public get minutesPerBlock() {
return 0.25; // 15 seconds per block target
}
public get label() {
return 'Raiden';
}
/**
* Derives an integer identifier using the first 4 bytes of a provided payment hash in hex.
* @param rHash a payment hash in hex
*/
private static getIdentifier(rHash: string) {
return parseInt(rHash.substr(0, 8), 16);
}
public initSpecific = async () => {};
/**
* Associate raiden with currencies that have a token address
*/
private setTokenAddresses = (currencyInstances: CurrencyInstance[]) => {
currencyInstances.forEach((currency) => {
if (currency.swapClient === SwapClientType.Raiden && currency.tokenAddress) {
this.tokenAddresses.set(currency.id, currency.tokenAddress);
}
});
}
public totalOutboundAmount = (currency: string): number => {
return this.totalOutboundAmounts.get(currency) || 0;
}
public maxChannelOutboundAmount = (currency: string): number => {
return this.maxChannelOutboundAmounts.get(currency) || 0;
}
public maxChannelInboundAmount = (currency: string): number => {
return this.maxChannelInboundAmounts.get(currency) || 0;
}
protected updateCapacity = async () => {
try {
const channelBalancePromises = [];
for (const [currency] of this.tokenAddresses) {
channelBalancePromises.push(this.channelBalance(currency));
}
await Promise.all(channelBalancePromises);
} catch (e) {
this.logger.error('failed to update total outbound capacity', e);
}
}
protected verifyConnection = async () => {
if (!this.isOperational()) {
throw(errors.RAIDEN_IS_DISABLED);
}
this.logger.info(`trying to verify connection to raiden with uri: ${this.host}:${this.port}`);
try {
const address = await this.getAddress();
/** The new raiden address value if different from the one we had previously. */
let newAddress: string | undefined;
if (this.address !== address) {
newAddress = address;
this.address = newAddress;
this.logger.debug(`address is ${newAddress}`);
}
await this.setConnected(newAddress);
} catch (err) {
this.logger.error(
`could not verify connection to raiden at ${this.host}:${this.port}, retrying in ${RaidenClient.RECONNECT_INTERVAL} ms`,
err,
);
this.disconnect();
}
}
public sendSmallestAmount = async (rHash: string, destination: string, currency: string) => {
const tokenAddress = this.tokenAddresses.get(currency);
if (!tokenAddress) {
throw(errors.TOKEN_ADDRESS_NOT_FOUND);
}
const tokenPaymentResponse = await this.tokenPayment({
token_address: tokenAddress,
target_address: destination,
amount: 1,
secret_hash: rHash,
});
return this.sanitizeTokenPaymentResponse(tokenPaymentResponse);
}
public sendPayment = async (deal: SwapDeal): Promise<string> => {
assert(deal.state === SwapState.Active);
assert(deal.destination);
let amount: number;
let tokenAddress: string;
let lock_timeout: number | undefined;
if (deal.role === SwapRole.Maker) {
// we are the maker paying the taker
amount = deal.takerUnits;
tokenAddress = this.tokenAddresses.get(deal.takerCurrency)!;
} else {
// we are the taker paying the maker
amount = deal.makerUnits;
tokenAddress = this.tokenAddresses.get(deal.makerCurrency)!;
lock_timeout = deal.makerCltvDelta!;
}
if (!tokenAddress) {
throw(errors.TOKEN_ADDRESS_NOT_FOUND);
}
try {
const tokenPaymentResponse = await this.tokenPayment({
amount,
lock_timeout,
token_address: tokenAddress,
target_address: deal.destination,
secret_hash: deal.rHash,
});
return this.sanitizeTokenPaymentResponse(tokenPaymentResponse);
} catch (err) {
switch (err.code) {
case 'ECONNRESET':
case errorCodes.UNEXPECTED:
case errorCodes.TIMEOUT:
case errorCodes.SERVER_ERROR:
case errorCodes.INVALID_TOKEN_PAYMENT_RESPONSE:
throw swapErrors.UNKNOWN_PAYMENT_ERROR(err.message);
default:
throw swapErrors.FINAL_PAYMENT_ERROR(err.message);
}
}
}
private sanitizeTokenPaymentResponse = (response: TokenPaymentResponse) => {
if (response.secret) {
// remove '0x'
return response.secret.slice(2);
} else {
throw errors.INVALID_TOKEN_PAYMENT_RESPONSE;
}
}
public addInvoice = async () => {
// not implemented, raiden does not use invoices
}
public settleInvoice = async () => {
// not implemented, raiden does not use invoices
}
public removeInvoice = async () => {
// not implemented, raiden does not use invoices
}
public lookupPayment = async (rHash: string, currency?: string, destination?: string) => {
const identifier = RaidenClient.getIdentifier(rHash);
// first check if the payment is pending
const pendingTransfers = await this.getPendingTransfers(currency, destination);
for (const pendingTransfer of pendingTransfers) {
if (identifier === Number(pendingTransfer.payment_identifier)) {
return { state: PaymentState.Pending };
}
}
// if the payment isn't pending, check if it has succeeded or failed
const paymentEvents = await this.getPaymentEvents(currency, destination);
for (const paymentEvent of paymentEvents) {
if (paymentEvent.identifier === identifier) {
const success = paymentEvent.event === 'EventPaymentSentSuccess';
if (success) {
const preimage = paymentEvent.secret;
return { preimage, state: PaymentState.Succeeded };
} else {
return { state: PaymentState.Failed };
}
}
}
// if there is no pending payment or event found, we assume that the payment was never attempted by raiden
return { state: PaymentState.Failed };
}
private getPendingTransfers = async (currency?: string, destination?: string) => {
let endpoint = 'pending_transfers';
if (currency) {
const tokenAddress = this.tokenAddresses.get(currency);
endpoint += `/${tokenAddress}`;
if (destination) {
endpoint += `/${destination}`;
}
}
const res = await this.sendRequest(endpoint, 'GET');
return parseResponseBody<PendingTransfer[]>(res);
}
private getPaymentEvents = async (currency?: string, destination?: string) => {
let endpoint = 'payments';
if (currency) {
const tokenAddress = this.tokenAddresses.get(currency);
endpoint += `/${tokenAddress}`;
if (destination) {
endpoint += `/${destination}`;
}
}
const res = await this.sendRequest(endpoint, 'GET');
return parseResponseBody<PaymentEvent[]>(res);
}
public getRoute = async (units: number, destination: string, currency: string) => {
// a query routes call is not currently provided by raiden
/** A placeholder route value that assumes a fixed lock time of 100 Raiden's blocks. */
const placeholderRoute = {
getTotalTimeLock: () => 101,
};
if (this.directChannelChecks) {
// temporary check for a direct channel in raiden
const tokenAddress = this.tokenAddresses.get(currency);
const channels = await this.getChannels(tokenAddress);
for (const channel of channels) {
if (channel.partner_address && channel.partner_address === destination) {
const balance = channel.balance;
if (balance >= units) {
this.logger.debug(`found a direct channel for ${currency} to ${destination} with ${balance} balance`);
return placeholderRoute;
} else {
this.logger.warn(`direct channel found for ${currency} to ${destination} with balance of ${balance} is insufficient for ${units})`);
return undefined; // we have a direct channel but it doesn't have enough balance, return no route
}
}
}
this.logger.warn(`no direct channel found for ${currency} to ${destination}`);
return undefined; // no direct channels, return no route
} else {
return placeholderRoute;
}
}
public canRouteToNode = async (destination: string, currency: string) => {
const route = await this.getRoute(1, destination, currency);
return route !== undefined;
}
public getHeight = async () => {
return 1; // raiden's API does not tell us the height
}
public getRaidenInfo = async (): Promise<RaidenInfo> => {
let channels: RaidenChannelCount | undefined;
let address: string | undefined;
let version: string | undefined;
let status = 'Ready';
const chain = Object.keys(this.tokenAddresses).find(key => this.tokenAddresses.get(key) === this.address) || 'raiden';
if (this.isDisabled()) {
status = errors.RAIDEN_IS_DISABLED.message;
} else {
try {
version = (await this.getVersion()).version;
const raidenChannels = await this.getChannels();
channels = {
active: raidenChannels.filter(c => c.state === 'opened').length,
settled: raidenChannels.filter(c => c.state === 'settled').length,
closed: raidenChannels.filter(c => c.state === 'closed').length,
};
address = this.address;
if (channels.active <= 0) {
status = errors.RAIDEN_HAS_NO_ACTIVE_CHANNELS().message;
}
} catch (err) {
status = err.message;
}
}
return {
chain,
status,
channels,
address,
version,
};
}
/**
* Sends a request to the Raiden REST API.
* @param endpoint the URL endpoint
* @param method an HTTP request method
* @param payload the request payload
*/
private sendRequest = (endpoint: string, method: string, payload?: object): Promise<http.IncomingMessage> => {
return new Promise((resolve, reject) => {
const options: http.RequestOptions = {
method,
hostname: this.host,
port: this.port,
path: `/api/v1/${endpoint}`,
};
let payloadStr: string | undefined;
if (payload) {
payloadStr = JSON.stringify(payload);
options.headers = {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(payloadStr),
};
}
this.logger.trace(`sending request to ${endpoint}: ${payloadStr}`);
const req = http.request(options, async (res) => {
switch (res.statusCode) {
case 200:
case 201:
case 204:
resolve(res);
break;
case 402:
reject(errors.INSUFFICIENT_BALANCE);
break;
case 408:
reject(errors.TIMEOUT);
break;
case 409:
const body = await parseResponseBody<RaidenErrorResponse>(res);
reject(errors.INVALID(body.errors));
break;
case 500:
this.logger.error(`raiden server error ${res.statusCode}: ${res.statusMessage}`);
reject(errors.SERVER_ERROR);
break;
default:
this.logger.error(`unexpected raiden status ${res.statusCode}: ${res.statusMessage}`);
reject(errors.UNEXPECTED);
break;
}
});
req.on('error', (err: any) => {
if (err.code === 'ECONNREFUSED') {
this.disconnect();
}
this.logger.error(err);
reject(err);
});
if (payloadStr) {
req.write(payloadStr);
}
req.end();
});
}
/**
* Gets the raiden version.
*/
public getVersion = async (): Promise<RaidenVersion> => {
const endpoint = 'version';
const res = await this.sendRequest(endpoint, 'GET');
return parseResponseBody<RaidenVersion>(res);
}
/**
* Gets info about a given raiden payment channel.
* @param token_address the token address for the network to which the channel belongs
* @param channel_address the address of the channel to query
*/
public getChannel = async (token_address: string, channel_address: string): Promise<Channel> => {
const endpoint = `channels/${token_address}/${channel_address}`;
const res = await this.sendRequest(endpoint, 'GET');
return parseResponseBody<Channel>(res);
}
/**
* Gets info about all non-settled channels.
* @param token_address an optional parameter to specify channels belonging to the specified token network
*/
public getChannels = async (token_address?: string): Promise<Channel[]> => {
const endpoint = token_address ? `channels/${token_address}` : 'channels';
const res = await this.sendRequest(endpoint, 'GET');
return parseResponseBody<[Channel]>(res);
}
public channelBalance = async (currency?: string): Promise<ChannelBalance> => {
if (!currency) {
return { balance: 0, pendingOpenBalance: 0, inactiveBalance: 0 };
}
const channels = await this.getChannels(this.tokenAddresses.get(currency));
const units = channels.filter(channel => channel.state === 'opened')
.map(channel => channel.balance)
.reduce((sum, acc) => sum + acc, 0);
const balance = this.unitConverter.unitsToAmount({
currency,
units,
});
if (this.totalOutboundAmounts.get(currency) !== balance) {
this.totalOutboundAmounts.set(currency, balance);
this.logger.debug(`new total outbound capacity for ${currency}: ${balance}`);
}
return { balance, pendingOpenBalance: 0, inactiveBalance: 0 };
}
public tradingLimits = async (currency?: string): Promise<TradingLimits> => {
if (!currency) {
return { maxSell: 0, maxBuy: 0 };
}
const channels = await this.getChannels(this.tokenAddresses.get(currency));
let maxOutboundUnits = 0;
let maxInboundUnits = 0;
channels.forEach((channel) => {
if (channel.state !== 'open') {
return;
}
const outboundUnits = channel.balance;
if (maxOutboundUnits < outboundUnits) {
maxOutboundUnits = outboundUnits;
}
const inboundUnits = channel.total_deposit - channel.balance;
if (maxInboundUnits < inboundUnits) {
maxInboundUnits = inboundUnits;
}
});
const maxOutboundAmount = this.unitConverter.unitsToAmount({ currency, units: maxOutboundUnits });
const maxInboundAmount = this.unitConverter.unitsToAmount({ currency, units: maxInboundUnits });
if (this.maxChannelOutboundAmounts.get(currency) !== maxOutboundAmount) {
this.maxChannelOutboundAmounts.set(currency, maxOutboundAmount);
this.logger.debug(`new channel outbound capacity for ${currency}: ${maxOutboundAmount}`);
}
if (this.maxChannelInboundAmounts.get(currency) !== maxInboundAmount) {
this.maxChannelInboundAmounts.set(currency, maxInboundAmount);
this.logger.debug(`new channel outbound capacity for ${currency}: ${maxInboundAmount}`);
}
return {
maxSell: this.maxChannelOutboundAmounts.get(currency)!,
maxBuy: this.maxChannelInboundAmounts.get(currency)!,
};
}
/**
* Returns the balances available in wallet for a specified currency.
*/
public walletBalance = async (_currency?: string): Promise<WalletBalance> => {
return { totalBalance: 0, confirmedBalance: 0, unconfirmedBalance: 0 };
}
/**
* Creates a payment channel.
*/
public openChannel = async (
{ peerIdentifier, units, currency }:
{ peerIdentifier: string, units: number, currency: string },
): Promise<void> => {
const tokenAddress = this.tokenAddresses.get(currency);
if (!tokenAddress) {
throw(errors.TOKEN_ADDRESS_NOT_FOUND);
}
await this.openChannelRequest({
partner_address: peerIdentifier,
token_address: tokenAddress,
total_deposit: units,
settle_timeout: 20660,
});
}
/**
* Creates a payment channel request.
* @returns The channel_address for the newly created channel.
*/
private openChannelRequest = async (payload: OpenChannelPayload): Promise<string> => {
const endpoint = 'channels';
const res = await this.sendRequest(endpoint, 'PUT', payload);
const body = await parseResponseBody<{ channel_address: string }>(res);
return body.channel_address;
}
/**
* Closes a payment channel.
* @param channel_address the address of the channel to close
*/
public closeChannelRequest = async (channel_address: string): Promise<void> => {
const endpoint = `channels/${channel_address}`;
await this.sendRequest(endpoint, 'PATCH', { state: 'settled' });
}
public closeChannel = async () => {
// unimplemented
}
/**
* Sends a token payment through the Raiden network.
* @param target_address recipient of the payment
* @param token_address contract address of the token
* @param amount
* @param secret_hash optional; provide your own secret hash
*/
private tokenPayment = async (payload: TokenPaymentRequest): Promise<TokenPaymentResponse> => {
const endpoint = `payments/${payload.token_address}/${payload.target_address}`;
if (payload.secret_hash) {
payload.secret_hash = `0x${payload.secret_hash}`;
}
const res = await this.sendRequest(endpoint, 'POST', payload);
const body = await parseResponseBody<TokenPaymentResponse>(res);
return body;
}
/**
* Deposits more of a token to an existing channel.
* @param channel_address the address of the channel to deposit to
* @param balance the amount to deposit to the channel
*/
public depositToChannel = async (channel_address: string, balance: number): Promise<void> => {
const endpoint = `channels/${channel_address}`;
await this.sendRequest(endpoint, 'PATCH', { balance });
}
/**
* Gets the account address for the raiden node.
*/
private getAddress = async (): Promise<string> => {
const endpoint = 'address';
const res = await this.sendRequest(endpoint, 'GET');
const body = await parseResponseBody<{ our_address: string }>(res);
return body.our_address;
}
public deposit = async () => {
const depositAddress = await this.getAddress();
return depositAddress;
}
/** Raiden client specific cleanup. */
protected disconnect = () => {
this.setStatus(ClientStatus.Disconnected);
}
}
export default RaidenClient;