Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rpc): runtime addcurrency for lnd & connext #1746

Merged
merged 13 commits into from
Nov 5, 2020
9 changes: 5 additions & 4 deletions lib/orderbook/OrderBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,13 @@ class OrderBook extends EventEmitter {
if (this.currencyInstances.has(currency.id)) {
throw errors.CURRENCY_ALREADY_EXISTS(currency.id);
}
if (currency.swapClient === SwapClientType.Raiden && !currency.tokenAddress) {
if ((currency.swapClient === SwapClientType.Raiden || currency.swapClient === SwapClientType.Connext)
&& !currency.tokenAddress) {
throw errors.CURRENCY_MISSING_ETHEREUM_CONTRACT_ADDRESS(currency.id);
}
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 Expand Up @@ -968,8 +969,8 @@ class OrderBook extends EventEmitter {
try {
const removeResult = this.removePeerOrder(oi.id, oi.pairId, peerPubKey, oi.quantity);
this.emit('peerOrder.invalidation', removeResult.order);
} catch {
this.logger.error(`failed to remove order (${oi.id}) of peer ${peerPubKey} (${pubKeyToAlias(peerPubKey)})`);
} catch (err) {
this.logger.error(`failed to remove order (${oi.id}) of peer ${peerPubKey} (${pubKeyToAlias(peerPubKey)})`, err);
// TODO: Penalize peer
}
}
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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should remove the raiden part since it's never going to be used and it would make the function more readable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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