From ab03e5914f23c0aaa1c3ff28072f64f7238f41b2 Mon Sep 17 00:00:00 2001 From: Daniel McNally Date: Thu, 23 Jul 2020 00:41:24 -0400 Subject: [PATCH] feat(rpc): runtime addcurrency for lnd & connext This PR allows for adding Connext and Lnd currencies via the `AddCurrency` rpc call and have them available for trading immediately without requiring a restart of xud. In the case of Connext it merely registers the tokenaddress and currency symbol for the new currency with the existing ConnextClient, as we had previously done with Raiden. In the case of Lnd, we read from the configuration and use that to instantiate and initialize a new `LndClient` for the specified currency. Closes #1111. --- lib/orderbook/OrderBook.ts | 2 +- lib/swaps/SwapClientManager.ts | 41 ++++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/lib/orderbook/OrderBook.ts b/lib/orderbook/OrderBook.ts index 967b80bb7..f4e734adf 100644 --- a/lib/orderbook/OrderBook.ts +++ b/lib/orderbook/OrderBook.ts @@ -295,7 +295,7 @@ class OrderBook extends EventEmitter { } const currencyInstance = await this.repository.addCurrency({ ...currency, decimalPlaces: currency.decimalPlaces || 8 }); this.currencyInstances.set(currencyInstance.id, currencyInstance); - this.swaps.swapClientManager.add(currencyInstance); + await this.swaps.swapClientManager.add(currencyInstance); } public removeCurrency = async (currencyId: string) => { diff --git a/lib/swaps/SwapClientManager.ts b/lib/swaps/SwapClientManager.ts index 4a09f9faf..a4a2d5901 100644 --- a/lib/swaps/SwapClientManager.ts +++ b/lib/swaps/SwapClientManager.ts @@ -330,25 +330,38 @@ class SwapClientManager extends EventEmitter { * @param currency a currency that should be linked with a swap client. * @returns Nothing upon success, throws otherwise. */ - public add = (currency: Currency): void => { - if (currency.swapClient === SwapClientType.Raiden && currency.tokenAddress && this.raidenClient) { - this.swapClients.set(currency.id, this.raidenClient); - this.raidenClient.tokenAddresses.set(currency.id, currency.tokenAddress); - this.emit('raidenUpdate', this.raidenClient.tokenAddresses, this.raidenClient.address); + public add = async (currency: Currency) => { + if (currency.tokenAddress) { + if (currency.swapClient === SwapClientType.Connext) { + if (!this.connextClient) { + throw errors.SWAP_CLIENT_NOT_CONFIGURED(currency.id); + } + this.swapClients.set(currency.id, this.connextClient); + this.connextClient.tokenAddresses.set(currency.id, currency.tokenAddress); + this.emit('connextUpdate', this.connextClient.tokenAddresses, this.connextClient.address); + } else if (currency.swapClient === SwapClientType.Raiden) { + if (!this.raidenClient) { + throw errors.SWAP_CLIENT_NOT_CONFIGURED(currency.id); + } + this.swapClients.set(currency.id, this.raidenClient); + this.raidenClient.tokenAddresses.set(currency.id, currency.tokenAddress); + this.emit('raidenUpdate', this.raidenClient.tokenAddresses, this.raidenClient.address); + } } else if (currency.swapClient === SwapClientType.Lnd) { // in case of lnd we check if the configuration includes swap client // for the specified currency - let isCurrencyConfigured = false; - for (const lndCurrency in this.config.lnd) { - if (lndCurrency === currency.id) { - isCurrencyConfigured = true; - break; - } - } - // adding a new lnd client at runtime is currently not supported - if (!isCurrencyConfigured) { + const config = this.config.lnd[currency.id]; + if (!config) { throw errors.SWAP_CLIENT_NOT_CONFIGURED(currency.id); } + + const lndClient = new LndClient({ + config, + logger: this.loggers.lnd.createSubLogger(currency.id), + currency: currency.id, + }); + this.swapClients.set(currency.id, lndClient); + await lndClient.init(); } }