xud
Version:
Exchange Union Daemon
161 lines (160 loc) • 7.15 kB
TypeScript
import { SwapClientType } from '../constants/enums';
import { CurrencyInstance } from '../db/types';
import Logger from '../Logger';
import SwapClient, { ChannelBalance, WalletBalance, SwapClientInfo, PaymentStatus, WithdrawArguments } from '../swaps/SwapClient';
import { SwapDeal, CloseChannelParams, OpenChannelParams, SwapCapacities } from '../swaps/types';
import { UnitConverter } from '../utils/UnitConverter';
import { ConnextInitWalletResponse, ConnextConfigResponse, ConnextClientConfig, ConnextInfo, ProvidePreimageEvent, TransferReceivedEvent } from './types';
interface ConnextClient {
on(event: 'preimage', listener: (preimageRequest: ProvidePreimageEvent) => void): void;
on(event: 'transferReceived', listener: (transferReceivedRequest: TransferReceivedEvent) => void): void;
on(event: 'htlcAccepted', listener: (rHash: string, amount: number, currency: string) => void): this;
on(event: 'connectionVerified', listener: (swapClientInfo: SwapClientInfo) => void): this;
on(event: 'depositConfirmed', listener: (hash: string) => void): this;
once(event: 'initialized', listener: () => void): this;
emit(event: 'htlcAccepted', rHash: string, amount: number, currency: string): boolean;
emit(event: 'connectionVerified', swapClientInfo: SwapClientInfo): boolean;
emit(event: 'initialized'): boolean;
emit(event: 'preimage', preimageRequest: ProvidePreimageEvent): void;
emit(event: 'transferReceived', transferReceivedRequest: TransferReceivedEvent): void;
emit(event: 'depositConfirmed', hash: string): void;
}
/**
* A class representing a client to interact with connext.
*/
declare class ConnextClient extends SwapClient {
readonly type = SwapClientType.Connext;
readonly finalLock = 200;
address?: string;
/** A map of currency symbols to token addresses. */
tokenAddresses: Map<string, string>;
userIdentifier?: string;
/**
* A map of expected invoices by hash.
* This is equivalent to invoices of lnd with the difference
* being that we're managing the state of invoice on xud level.
*/
private expectedIncomingTransfers;
/** The set of hashes for outgoing transfers. */
private outgoingTransferHashes;
private port;
private host;
private webhookport;
private webhookhost;
private unitConverter;
private seed;
/** A map of currencies to promises representing balance requests. */
private getBalancePromises;
private outboundAmounts;
private inboundAmounts;
private pendingRequests;
private criticalRequestPaths;
/** The minimum incremental quantity that we may use for collateral requests. */
private static MIN_COLLATERAL_REQUEST_SIZES;
/**
* Creates a connext client.
*/
constructor({ config, logger, unitConverter, currencyInstances, }: {
unitConverter: UnitConverter;
config: ConnextClientConfig;
currencyInstances: CurrencyInstance[];
logger: Logger;
});
get minutesPerBlock(): number;
get label(): string;
initSpecific: () => Promise<void>;
private onTransferReceived;
setSeed: (seed: string) => void;
/**
* Initiates wallet for the Connext client
*/
initWallet: (seedMnemonic: string) => Promise<ConnextInitWalletResponse>;
initConnextClient: (seedMnemonic: string) => Promise<ConnextConfigResponse>;
private subscribeDeposit;
private subscribePreimage;
private subscribeIncomingTransfer;
/**
* Associate connext with currencies that have a token address
*/
private setTokenAddresses;
/**
* Checks whether we have a pending collateral request for the currency and,
* if one doesn't exist, starts a new request for the specified amount. Then
* calls channelBalance to refresh the inbound capacity for the currency.
*/
private requestCollateralInBackground;
/**
* Checks whether there is sufficient inbound capacity to receive the specified amount
* and throws an error if there isn't, otherwise does nothing.
*/
checkInboundCapacity: (inboundAmount: number, currency: string) => void;
setReservedInboundAmount: (reservedInboundAmount: number, currency: string) => void;
protected updateCapacity: () => Promise<void>;
protected getTokenAddress(currency: string): string;
protected getCurrencyByTokenaddress(tokenAddress: string): string;
protected verifyConnection: () => Promise<void>;
sendSmallestAmount: (rHash: string, destination: string, currency: string) => Promise<string>;
sendPayment: (deal: SwapDeal) => Promise<string>;
addInvoice: ({ rHash: expectedHash, units: expectedUnits, expiry: expectedTimelock, currency: expectedCurrency }: {
rHash: string;
units: number;
expiry?: number;
currency?: string;
}) => Promise<void>;
/**
* Resolves a HashLock Transfer on the Connext network.
*/
settleInvoice: (rHash: string, rPreimage: string, currency: string) => Promise<void>;
removeInvoice: (rHash: string) => Promise<void>;
private getHashLockStatus;
lookupPayment: (rHash: string, currency: string) => Promise<PaymentStatus>;
getRoute: () => Promise<{
getTotalTimeLock: () => number;
}>;
canRouteToNode: () => Promise<boolean>;
getHeight: () => Promise<number>;
getInfo: () => Promise<ConnextInfo>;
/**
* Gets the connext version.
*/
getVersion: () => Promise<string>;
/**
* Gets the configuration of Connext client.
*/
getClientConfig: () => Promise<ConnextConfigResponse>;
channelBalance: (currency?: string | undefined) => Promise<ChannelBalance>;
swapCapacities: (currency: string) => Promise<SwapCapacities>;
/**
* Returns the balances available in wallet for a specified currency.
*/
walletBalance: (currency?: string | undefined) => Promise<WalletBalance>;
private getBalance;
deposit: () => Promise<string>;
openChannel: ({ currency, units }: OpenChannelParams) => Promise<string>;
closeChannel: ({ units, currency, destination }: CloseChannelParams) => Promise<string[]>;
/**
* Create a HashLock Transfer on the Connext network.
* @param targetAddress recipient of the payment
* @param tokenAddress contract address of the token
* @param amount
* @param lockHash
*/
private executeHashLockTransfer;
/**
* Deposits more of a token to an existing client.
* @param multisigAddress the address of the client to deposit to
* @param balance the amount to deposit to the client
*/
depositToChannel: (assetId: string, amount: number) => Promise<void>;
withdraw: ({ all, currency, amount: argAmount, destination, fee, }: WithdrawArguments) => Promise<string>;
/** Connext client specific cleanup. */
protected disconnect: () => Promise<void>;
/**
* Sends a request to the Connext REST API.
* @param endpoint the URL endpoint
* @param method an HTTP request method
* @param payload the request payload
*/
private sendRequest;
}
export default ConnextClient;