Skip to content

Commit

Permalink
feat(rpc): runtime addcurrency for lnd & connext
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sangaman committed Jul 23, 2020
1 parent 86ed586 commit ab03e59
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/orderbook/OrderBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
41 changes: 27 additions & 14 deletions lib/swaps/SwapClientManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down

0 comments on commit ab03e59

Please sign in to comment.