Skip to content

Commit

Permalink
feat: add 0-conf limits to getPairs endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Aug 14, 2019
1 parent 335cac3 commit 769b79f
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 59 deletions.
90 changes: 48 additions & 42 deletions lib/Boltz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { generateMnemonic } from 'bip39';
import Api from './api/Api';
import Logger from './Logger';
import Report from './data/Report';
import { stringify } from './Utils';
import Database from './db/Database';
import Service from './service/Service';
import GrpcServer from './grpc/GrpcServer';
Expand All @@ -28,11 +29,11 @@ class Boltz {
private swapManager: SwapManager;
private walletManager: WalletManager;

private service: Service;
private notifications: NotificationProvider;
private service!: Service;
private notifications!: NotificationProvider;

private api: Api;
private grpcServer: GrpcServer;
private api!: Api;
private grpcServer!: GrpcServer;

constructor(config: Arguments<any>) {
this.config = new Config().load(config);
Expand Down Expand Up @@ -65,44 +66,49 @@ class Boltz {
],
);

this.service = new Service(
this.logger,
this.swapManager,
this.walletManager,
this.currencies,
this.config.rates.interval,
);

const backup = new BackupScheduler(
this.logger,
this.config.dbpath,
this.config.backup,
this.service.eventHandler,
new Report(
this.service.swapRepository,
this.service.reverseSwapRepository,
),
);

this.notifications = new NotificationProvider(
this.logger,
this.service,
backup,
this.config.notification,
this.config.currencies,
);

this.grpcServer = new GrpcServer(
this.logger,
this.config.grpc,
new GrpcService(this.service),
);

this.api = new Api(
this.logger,
this.config.api,
this.service,
);
try {
this.service = new Service(
this.logger,
this.swapManager,
this.walletManager,
this.currencies,
this.config.rates.interval,
);

const backup = new BackupScheduler(
this.logger,
this.config.dbpath,
this.config.backup,
this.service.eventHandler,
new Report(
this.service.swapRepository,
this.service.reverseSwapRepository,
),
);

this.notifications = new NotificationProvider(
this.logger,
this.service,
backup,
this.config.notification,
this.config.currencies,
);

this.grpcServer = new GrpcServer(
this.logger,
this.config.grpc,
new GrpcService(this.service),
);

this.api = new Api(
this.logger,
this.config.api,
this.service,
);
} catch (error) {
this.logger.error(`Could not start Boltz: ${stringify(error)}`);
process.exit(1);
}
}

public start = async () => {
Expand Down
4 changes: 4 additions & 0 deletions lib/rates/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ export default {
message: `could not get rate: ${error}`,
code: concatErrorCode(ErrorCodePrefix.Rates, 0),
}),
CONFIGURATION_INCOMPLETE: (symbol: string, missingValue: string): Error => ({
message: `could not init currency ${symbol} because of missing config value: ${missingValue}`,
code: concatErrorCode(ErrorCodePrefix.Rates, 1),
}),
};
61 changes: 44 additions & 17 deletions lib/rates/RateProvider.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import Errors from './Errors';
import Logger from '../Logger';
import FeeProvider from './FeeProvider';
import { PairConfig } from '../consts/Types';
import DataProvider from './data/DataProvider';
import { Currency } from '../wallet/WalletManager';
import { stringify, mapToObject, minutesToMilliseconds, getPairId } from '../Utils';

type Limits = {
type CurrencyLimits = {
minimal: number;
maximal: number;

maximalZeroConf: number;
};

type ReverseMinerFees = {
Expand All @@ -22,7 +25,15 @@ type MinerFees = {

type PairType = {
rate: number;
limits: Limits;
limits: {
minimal: number;
maximal: number;

maximalZeroConf: {
baseAsset: number;
quoteAsset: number;
}
};
fees: {
percentage: number;
minerFees: {
Expand All @@ -39,14 +50,11 @@ class RateProvider {
// An array of tuples between the base and quote asset of all pairs for which the rate should be queried
public pairsToQuery: [string, string][] = [];

// A map between the symbol and max allowed zero conf amount
private maxZeroConfAmounts = new Map<string, number>();

// A map of all pairs with hardcoded rates
private hardcodedPairs = new Map<string, { base: string, quote: string }>();

// A map between assets and their limits
private limits = new Map<string, Limits>();
private limits = new Map<string, CurrencyLimits>();

// A copy of the "percentageFees" Map in the FeeProvider but all values are multiplied with 100
private percentageFees = new Map<string, number>();
Expand Down Expand Up @@ -120,10 +128,10 @@ class RateProvider {
* Returns whether 0-conf should be accepted for a specific amount on a specified chain
*/
public acceptZeroConf = (chainCurrency: string, amount: number) => {
const maxAllowedAmount = this.maxZeroConfAmounts.get(chainCurrency);
const limits = this.limits.get(chainCurrency);

if (maxAllowedAmount) {
return amount <= maxAllowedAmount;
if (limits) {
return amount <= limits.maximalZeroConf;
} else {
return false;
}
Expand Down Expand Up @@ -190,13 +198,18 @@ class RateProvider {
return {
maximal: Math.min(baseLimits.maximal, reverseQuoteLimits.maximal),
minimal: Math.max(baseLimits.minimal, reverseQuoteLimits.minimal),

maximalZeroConf: {
baseAsset: baseLimits.maximalZeroConf,
quoteAsset: quoteLimits.maximalZeroConf,
},
};
}

throw `Could not get limits for pair ${pair}`;
}

private calculateQuoteLimits = (rate: number, limits: Limits) => {
private calculateQuoteLimits = (rate: number, limits: CurrencyLimits) => {
const reverseRate = 1 / rate;

return {
Expand All @@ -206,15 +219,29 @@ class RateProvider {
}

private parseCurrencies = (currencies: Currency[]) => {
currencies.forEach((currency) => {
// TODO: throw error if undefined
for (const currency of currencies) {
const config = currency.config;

if (config.maxZeroConfAmount === undefined) {
this.logger.warn(`Maximal 0-conf amount not set for ${currency.symbol}`);
}

if (config.maxSwapAmount === undefined) {
throw Errors.CONFIGURATION_INCOMPLETE(currency.symbol, 'maxSwapAmount');
}

if (config.minSwapAmount === undefined) {
throw Errors.CONFIGURATION_INCOMPLETE(currency.symbol, 'minSwapAmount');
}

this.limits.set(currency.symbol, {
maximal: currency.config.maxSwapAmount,
minimal: currency.config.minSwapAmount,
});
maximal: config.maxSwapAmount,
minimal: config.minSwapAmount,

this.maxZeroConfAmounts.set(currency.symbol, currency.config.maxZeroConfAmount);
});
// Set the maximal 0-conf amount to 0 if it wasn't set explicitly
maximalZeroConf: config.maxZeroConfAmount || 0,
});
}
}

private getMinerFees = async () => {
Expand Down

0 comments on commit 769b79f

Please sign in to comment.