Skip to content

Commit

Permalink
fix: load raiden token addresses from db
Browse files Browse the repository at this point in the history
This fixes the initialization logic for the raiden client and swap
manager to load existing raiden currencies from the database.
  • Loading branch information
sangaman committed May 24, 2019
1 parent 80e2e7b commit d011c80
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/Xud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Xud extends EventEmitter {
await this.db.init(this.config.network, this.config.initdb);
this.pool = new Pool(this.config.p2p, this.config.network, loggers.p2p, this.db.models);
this.swapClientManager = new SwapClientManager(this.config, loggers, this.pool);
await this.swapClientManager.init();
await this.swapClientManager.init(this.db.models);

this.swaps = new Swaps(loggers.swaps, this.db.models, this.pool, this.swapClientManager);
initPromises.push(this.swaps.init());
Expand Down
13 changes: 9 additions & 4 deletions lib/swaps/SwapClientManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Pool from '../p2p/Pool';
import { errors } from './errors';
import { Currency } from '../orderbook/types';
import { isLndClient, isRaidenClient } from './types';
import { Models } from '../db/DB';

class SwapClientManager {
/** A map between currencies and all swap clients */
Expand Down Expand Up @@ -42,7 +43,7 @@ class SwapClientManager {
* and waits for the swap clients to initialize.
* @returns A promise that resolves upon successful initialization, rejects otherwise.
*/
public init = async (): Promise<void> => {
public init = async (models: Models): Promise<void> => {
const initPromises = [];
// setup LND clients and initialize
for (const currency in this.config.lnd) {
Expand Down Expand Up @@ -70,9 +71,13 @@ class SwapClientManager {
await Promise.all(initPromises);
// associate swap clients with currencies managed by raiden client
if (raidenEnabled) {
for (const currency of this.raidenClient.tokenAddresses.keys()) {
this.swapClients.set(currency, this.raidenClient);
}
const currencyInstances = await models.Currency.findAll();
currencyInstances.forEach((currency) => {
if (currency.tokenAddress) {
this.raidenClient.tokenAddresses.set(currency.id, currency.tokenAddress);
this.swapClients.set(currency.id, this.raidenClient);
}
});
}
}

Expand Down
25 changes: 18 additions & 7 deletions test/jest/SwapClientManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@ import SwapClientManager from '../../lib/swaps/SwapClientManager';
import Config from '../../lib/Config';
import { SwapClientType } from '../../lib/constants/enums';

jest.mock('../../lib/db/DB');
jest.mock('../../lib/db/DB', () => {
return jest.fn().mockImplementation(() => {
return {
models: {
Currency: {
findAll: () => { return [{ id: 'WETH', tokenAddress: '0x1234' }]; },
},
},
};
});
});
jest.mock('../../lib/Config');
jest.mock('../../lib/Logger');
jest.mock('../../lib/p2p/Pool');
Expand All @@ -30,7 +40,6 @@ const mockRaidenAddress = 1234567890;
jest.mock('../../lib/raidenclient/RaidenClient', () => {
return jest.fn().mockImplementation(() => {
const tokenAddresses = new Map<string, string>();
tokenAddresses.set('WETH', '0x1234');
return {
tokenAddresses,
on: onListenerMock,
Expand Down Expand Up @@ -99,12 +108,14 @@ describe('Swaps.SwapClientManager', () => {

test('it initializes lnd-ltc, lnd-btc and raiden', async () => {
swapClientManager = new SwapClientManager(config, loggers, pool);
await swapClientManager.init();
await swapClientManager.init(db.models);
expect(swapClientManager['swapClients'].size).toEqual(3);
expect(onListenerMock).toHaveBeenCalledTimes(3);
expect(swapClientManager.get('BTC')).not.toBeUndefined();
expect(swapClientManager.get('LTC')).not.toBeUndefined();
expect(swapClientManager.get('WETH')).not.toBeUndefined();
expect(swapClientManager.raidenClient.tokenAddresses.size).toEqual(1);
expect(swapClientManager.raidenClient.tokenAddresses.get('WETH')).not.toBeUndefined();
swapClientManager.remove('WETH');
expect(swapClientManager['swapClients'].size).toEqual(2);
expect(swapClientManager.getLndPubKeys()).toEqual(
Expand All @@ -120,7 +131,7 @@ describe('Swaps.SwapClientManager', () => {
test('it initializes lnd-ltc and lnd-btc', async () => {
config.raiden.disable = true;
swapClientManager = new SwapClientManager(config, loggers, pool);
await swapClientManager.init();
await swapClientManager.init(db.models);
expect(swapClientManager['swapClients'].size).toEqual(2);
expect(onListenerMock).toHaveBeenCalledTimes(2);
expect(swapClientManager.get('BTC')).not.toBeUndefined();
Expand All @@ -133,7 +144,7 @@ describe('Swaps.SwapClientManager', () => {
config.lnd.LTC!.disable = true;
config.raiden.disable = true;
swapClientManager = new SwapClientManager(config, loggers, pool);
await swapClientManager.init();
await swapClientManager.init(db.models);
expect(swapClientManager['swapClients'].size).toEqual(1);
expect(onListenerMock).toHaveBeenCalledTimes(1);
expect(swapClientManager.get('BTC')).not.toBeUndefined();
Expand All @@ -146,7 +157,7 @@ describe('Swaps.SwapClientManager', () => {
config.lnd.LTC!.disable = true;
config.raiden.disable = true;
swapClientManager = new SwapClientManager(config, loggers, pool);
await swapClientManager.init();
await swapClientManager.init(db.models);
expect(swapClientManager['swapClients'].size).toEqual(0);
expect(onListenerMock).toHaveBeenCalledTimes(0);
expect(swapClientManager.get('BTC')).toBeUndefined();
Expand All @@ -157,7 +168,7 @@ describe('Swaps.SwapClientManager', () => {

it('closes lnd-btc, lnd-ltc and raiden', async () => {
swapClientManager = new SwapClientManager(config, loggers, pool);
await swapClientManager.init();
await swapClientManager.init(db.models);
expect(swapClientManager['swapClients'].size).toEqual(3);
swapClientManager.close();
expect(closeMock).toHaveBeenCalledTimes(3);
Expand Down

0 comments on commit d011c80

Please sign in to comment.