UNPKG

xud

Version:
204 lines (203 loc) 9.06 kB
/// <reference types="node" /> import { EventEmitter } from 'events'; import Config from '../Config'; import ConnextClient from '../connextclient/ConnextClient'; import { SwapClientType } from '../constants/enums'; import { Models } from '../db/DB'; import LndClient from '../lndclient/LndClient'; import { LndInfo } from '../lndclient/types'; import { Level, Loggers } from '../Logger'; import NodeKey from '../nodekey/NodeKey'; import { Currency, OwnLimitOrder } from '../orderbook/types'; import Peer from '../p2p/Peer'; import { UnitConverter } from '../utils/UnitConverter'; import SwapClient from './SwapClient'; import { TradingLimits } from './types'; export declare function isConnextClient(swapClient: SwapClient): swapClient is ConnextClient; export declare function isLndClient(swapClient: SwapClient): swapClient is LndClient; declare type LndUpdate = { currency: string; pubKey: string; chain?: string; uris?: string[]; }; interface SwapClientManager { on(event: 'lndUpdate', listener: (lndUpdate: LndUpdate) => void): this; on(event: 'connextUpdate', listener: (tokenAddresses: Map<string, string>, pubKey?: string) => void): this; on(event: 'htlcAccepted', listener: (swapClient: SwapClient, rHash: string, amount: number, currency: string) => void): this; emit(event: 'lndUpdate', lndUpdate: LndUpdate): boolean; emit(event: 'connextUpdate', tokenAddresses: Map<string, string>, pubKey?: string): boolean; emit(event: 'htlcAccepted', swapClient: SwapClient, rHash: string, amount: number, currency: string): boolean; } declare class SwapClientManager extends EventEmitter { private config; private loggers; private unitConverter; private models; /** A map between currencies and all enabled swap clients */ swapClients: Map<string, SwapClient>; connextClient?: ConnextClient; misconfiguredClients: Set<SwapClient>; /** A map of supported currency tickers to the inbound amount that is reserved by existing orders. */ private inboundReservedAmounts; /** A map of supported currency tickers to the outbound amount that is reserved by existing orders. */ private outboundReservedAmounts; constructor(config: Config, loggers: Loggers, unitConverter: UnitConverter, models: Models); /** * Starts all swap clients, binds event listeners * and waits for the swap clients to initialize. * @returns A promise that resolves upon successful initialization, rejects otherwise. */ init: () => Promise<void>; /** * Initializes Connext client with a seed * @returns true if Connext */ initConnext: (seed: string) => Promise<boolean>; /** * Checks to make sure all enabled lnd instances are available and waits * up to a short time for them to become available in case they are not. * Throws an error if any lnd clients remain unreachable. */ waitForLnd: () => Promise<void>; tradingLimits: (currency: string) => Promise<TradingLimits>; /** * Checks whether a given order with would exceed our inbound or outbound swap * capacities when taking into consideration reserved amounts for standing * orders, throws an exception if so and returns otherwise. */ checkSwapCapacities: ({ quantity, price, isBuy, pairId }: OwnLimitOrder) => Promise<void>; getOutboundReservedAmount: (currency: string) => number | undefined; getInboundReservedAmount: (currency: string) => number | undefined; addOutboundReservedAmount: (currency: string, amount: number) => void; addInboundReservedAmount: (currency: string, amount: number) => void; subtractOutboundReservedAmount: (currency: string, amount: number) => void; subtractInboundReservedAmount: (currency: string, amount: number) => void; setLogLevel: (level: Level) => void; /** * Initializes wallets with seed and password. */ initWallets: ({ walletPassword, seedMnemonic, restore, lndBackups, nodeKey }: { walletPassword: string; seedMnemonic: string[]; restore?: boolean | undefined; lndBackups?: Map<string, Uint8Array> | undefined; nodeKey: NodeKey; }) => Promise<{ initializedLndWallets: string[]; initializedConnext: boolean; }>; /** * Unlocks wallets with a password. * @returns an array of currencies for each lnd client that was unlocked */ unlockWallets: ({ walletPassword, nodeKey }: { walletPassword: string; nodeKey: NodeKey; connextSeed: string; }) => Promise<{ unlockedLndClients: string[]; lockedLndClients: string[]; }>; /** * Changes the wallet passwords for all lnd clients by either calling ChangePassword * right away if lnd is in a WaitingUnlock state or, more often, by persisting the * current wallet password so that xud will try to automatically change it the next * time it is unlocked. */ changeLndPasswords: (oldPassword: string, newPassword: string) => Promise<void>; /** * Gets a swap client instance. * @param currency a currency that the swap client is linked to. * @returns swap client instance upon success, undefined otherwise. */ get: (currency: string) => SwapClient | undefined; /** * Returns whether the swap client manager has a client for a given currency. * @param currency the currency that the swap client is linked to. * @returns `true` if a swap client exists, false otherwise. */ has: (currency: string) => boolean; /** Gets the type of swap client for a given currency. */ getType: (currency: string) => SwapClientType | undefined; /** * Returns whether the swap client for a specified currency is connected. * @returns `true` if a swap client exists and is connected, otherwise `false` */ isConnected: (currency: string) => boolean; /** * Adds a new swap client and currency association. * @param currency a currency that should be linked with a swap client. * @returns Nothing upon success, throws otherwise. */ add: (currency: Currency) => Promise<void>; /** * Removes a new swap client and currency association. * @param currency a currency that should be unlinked from a swap client. * @returns Nothing upon success, throws otherwise. */ remove: (currency: string) => void; /** * Gets a map of all lnd clients. * @returns A map of currencies to lnd clients. */ getLndClientsMap: () => Map<string, LndClient>; /** * Gets all lnd clients' info. * @returns A promise that resolves to an object containing lnd * clients' info, throws otherwise. */ getLndClientsInfo: () => Promise<Map<string, LndInfo>>; /** * Closes all swap client instances gracefully. * @returns Nothing upon success, throws otherwise. */ close: () => void; deposit: (currency: string) => Promise<string>; withdraw: ({ currency, amount, destination, all, fee }: { currency: string; destination: string; amount?: number | undefined; all?: boolean | undefined; fee?: number | undefined; }) => Promise<string>; /** * Closes a payment channel. * @param remoteIdentifier the identifier for the remote side of the channel. * @param currency a currency for the payment channel. * @param amount the amount to extract from the channel. If 0 or unspecified, * the entire off-chain balance for the specified currency will be extracted. * @returns Nothing upon success, throws otherwise. */ closeChannel: ({ remoteIdentifier, currency, force, destination, amount, fee }: { remoteIdentifier?: string | undefined; currency: string; force: boolean; destination?: string | undefined; amount?: number | undefined; fee?: number | undefined; }) => Promise<string[]>; /** * Opens a payment channel. * @param remoteIdentifier the identifier for the remote side of the channel. * @param currency a currency for the payment channel. * @param amount the size of the payment channel local balance * @returns Nothing upon success, throws otherwise. */ openChannel: ({ remoteIdentifier, amount, currency, pushAmount, fee, uris }: { remoteIdentifier?: string | undefined; amount: number; currency: string; pushAmount?: number | undefined; fee?: number | undefined; uris?: string[] | undefined; }) => Promise<string>; /** * Checks whether it is possible to route a payment to a peer for a given currency. This does not * guarantee or test that a payment can be routed successfully, only determiens whether it is * possible to do so currently given the state of the specified currency's network and graph. */ canRouteToPeer: (peer: Peer, currency: string) => Promise<boolean>; private bind; } export default SwapClientManager;