Skip to content

Commit

Permalink
Merge remote-tracking branch 'dexalot/feat/dexalot' into aburkut/back…
Browse files Browse the repository at this point in the history
…-1307-dexalot
  • Loading branch information
aburkut committed Sep 27, 2023
2 parents 145603f + c07f6c7 commit 189a172
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 92 deletions.
2 changes: 0 additions & 2 deletions src/dex/dexalot/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ export const DEXALOT_API_PRICES_POLLING_INTERVAL_MS = 1000;

export const DEXALOT_API_PAIRS_POLLING_INTERVAL_MS = 1000 * 60 * 10; // 10 mins

export const DEXALOT_API_TOKENS_POLLING_INTERVAL_MS = 1000 * 60 * 10; // 10 mins

export const DEXALOT_API_BLACKLIST_POLLING_INTERVAL_MS = 1000 * 60 * 60; // 1 hour

export const DEXALOT_API_URL = 'https://api.dexalot.com';
Expand Down
17 changes: 8 additions & 9 deletions src/dex/dexalot/dexalot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,11 @@ import {
DEXALOT_GAS_COST,
DEXALOT_PAIRS_CACHES_TTL_S,
DEXALOT_API_PAIRS_POLLING_INTERVAL_MS,
DEXALOT_API_TOKENS_POLLING_INTERVAL_MS,
DEXALOT_TOKENS_CACHES_TTL_S,
DEXALOT_API_BLACKLIST_POLLING_INTERVAL_MS,
DEXALOT_RATE_LIMITED_TTL_S,
} from './constants';
import { BI_MAX_UINT256 } from '../../bigint-constants';
import { TooStrictSlippageCheckError } from '../generic-rfq/types';
import { ethers } from 'ethers';
import BigNumber from 'bignumber.js';

Expand Down Expand Up @@ -111,24 +109,23 @@ export class Dexalot extends SimpleExchange implements IDex<DexalotData> {
rateConfig: {
pairsIntervalMs: DEXALOT_API_PAIRS_POLLING_INTERVAL_MS,
pricesIntervalMs: DEXALOT_API_PRICES_POLLING_INTERVAL_MS,
tokensIntervalMs: DEXALOT_API_TOKENS_POLLING_INTERVAL_MS,
blacklistIntervalMs: DEXALOT_API_BLACKLIST_POLLING_INTERVAL_MS,
pairsReqParams: {
url: `${DEXALOT_API_URL}/api/rfq/pairs`,
headers: {
'x-apikey': this.dexalotAuthToken,
},
params: {
chainid: this.network,
},
},
pricesReqParams: {
url: `${DEXALOT_API_URL}/api/rfq/prices`,
headers: {
'x-apikey': this.dexalotAuthToken,
},
},
tokensReqParams: {
url: `${DEXALOT_API_URL}/api/rfq/tokens`,
headers: {
'x-apikey': this.dexalotAuthToken,
params: {
chainid: this.network,
},
},
blacklistReqParams: {
Expand Down Expand Up @@ -504,6 +501,7 @@ export class Dexalot extends SimpleExchange implements IDex<DexalotData> {
takerAmount:
side === SwapSide.SELL ? optimalSwapExchange.srcAmount : undefined,
userAddress: options.txOrigin,
chainid: this.network,
};

const rfq: RFQResponse = await this.dexHelper.httpRequest.post(
Expand All @@ -517,9 +515,10 @@ export class Dexalot extends SimpleExchange implements IDex<DexalotData> {
'Missing quote data',
`RFQ ${swapIdentifier} ${JSON.stringify(rfq)}`,
);
} else if (!rfq.order.signature) {
} else if (!rfq.signature) {
this.generateRFQError('Missing signature', swapIdentifier);
}
rfq.order.signature = rfq.signature;

const { order } = rfq;

Expand Down
84 changes: 31 additions & 53 deletions src/dex/dexalot/rate-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ import {
PairDataMap,
DexalotPricesResponse,
PriceDataMap,
DexalotTokensResponse,
DexalotBlacklistResponse,
} from './types';
import {
pricesResponseValidator,
pairsResponseValidator,
tokensResponseValidator,
blacklistResponseValidator,
} from './validators';
import { Network } from '../../constants';
Expand All @@ -28,7 +26,6 @@ export class RateFetcher {
private pricesCacheKey: string;
private pricesCacheTTL: number;

private tokensFetcher: Fetcher<DexalotTokensResponse>;
private tokensAddrCacheKey: string;
private tokensCacheKey: string;
private tokensCacheTTL: number;
Expand Down Expand Up @@ -88,24 +85,6 @@ export class RateFetcher {
logger,
);

this.tokensFetcher = new Fetcher<DexalotTokensResponse>(
dexHelper.httpRequest,
{
info: {
requestOptions: config.rateConfig.tokensReqParams,
caster: (data: unknown) => {
return validateAndCast<DexalotTokensResponse>(
data,
tokensResponseValidator,
);
},
},
handler: this.handleTokensResponse.bind(this),
},
config.rateConfig.tokensIntervalMs,
logger,
);

this.blacklistFetcher = new Fetcher<DexalotBlacklistResponse>(
dexHelper.httpRequest,
{
Expand All @@ -128,22 +107,36 @@ export class RateFetcher {
start() {
this.pairsFetcher.startPolling();
this.rateFetcher.startPolling();
this.tokensFetcher.startPolling();
this.blacklistFetcher.startPolling();
}

stop() {
this.pairsFetcher.stopPolling();
this.rateFetcher.stopPolling();
this.tokensFetcher.stopPolling();
this.blacklistFetcher.stopPolling();
}

private handlePairsResponse(resp: DexalotPairsResponse): void {
const { pairs } = resp;
const pairs = resp;
const dexPairs: PairDataMap = {};
const tokenMap: { [address: string]: Token } = {};
const tokenAddrMap: { [symbol: string]: string } = {};
Object.keys(pairs).forEach(pair => {
dexPairs[pair.toLowerCase()] = pairs[pair];
tokenAddrMap[pairs[pair].base.toLowerCase()] =
pairs[pair].baseAddress.toLowerCase();
tokenAddrMap[pairs[pair].quote.toLowerCase()] =
pairs[pair].quoteAddress.toLowerCase();
tokenMap[pairs[pair].baseAddress.toLowerCase()] = {
address: pairs[pair].baseAddress.toLowerCase(),
symbol: pairs[pair].base,
decimals: pairs[pair].baseDecimals,
};
tokenMap[pairs[pair].quoteAddress.toLowerCase()] = {
address: pairs[pair].quoteAddress.toLowerCase(),
symbol: pairs[pair].quote,
decimals: pairs[pair].quoteDecimals,
};
});
this.dexHelper.cache.setex(
this.dexKey,
Expand All @@ -152,6 +145,20 @@ export class RateFetcher {
this.pairsCacheTTL,
JSON.stringify(dexPairs),
);
this.dexHelper.cache.setex(
this.dexKey,
this.network,
this.tokensCacheKey,
this.tokensCacheTTL,
JSON.stringify(tokenMap),
);
this.dexHelper.cache.setex(
this.dexKey,
this.network,
this.tokensAddrCacheKey,
this.tokensCacheTTL,
JSON.stringify(tokenAddrMap),
);
}

private handleRatesResponse(resp: DexalotPricesResponse): void {
Expand All @@ -169,35 +176,6 @@ export class RateFetcher {
);
}

private handleTokensResponse(resp: DexalotTokensResponse): void {
const { tokens } = resp;
const tokenMap: { [address: string]: Token } = {};
const tokenAddrMap: { [pair: string]: string } = {};
Object.keys(tokens).forEach(symbol => {
const token = tokens[symbol];
tokenMap[token.address.toLowerCase()] = {
address: token.address.toLowerCase(),
symbol: token.symbol,
decimals: token.decimals,
};
tokenAddrMap[token.symbol.toLowerCase()] = token.address.toLowerCase();
});
this.dexHelper.cache.setex(
this.dexKey,
this.network,
this.tokensCacheKey,
this.tokensCacheTTL,
JSON.stringify(tokenMap),
);
this.dexHelper.cache.setex(
this.dexKey,
this.network,
this.tokensAddrCacheKey,
this.tokensCacheTTL,
JSON.stringify(tokenAddrMap),
);
}

private async handleBlacklistResponse(
resp: DexalotBlacklistResponse,
): Promise<void> {
Expand Down
36 changes: 13 additions & 23 deletions src/dex/dexalot/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ type RFQOrder = {
taker: string;
makerAmount: string;
takerAmount: string;
signature: string;
signature?: string;
};

export type RFQResponse = {
order: RFQOrder;
signature: string;
};

export type RFQResponseError = {
Expand Down Expand Up @@ -52,8 +53,18 @@ export type PairDataMap = {
[pair: string]: PairData;
};

export type PairDataResp = {
base: string;
quote: string;
liquidityUSD: number;
baseAddress: string;
quoteAddress: string;
baseDecimals: number;
quoteDecimals: number;
};

export type DexalotPairsResponse = {
pairs: PairDataMap;
[pair: string]: PairDataResp;
};

type PriceData = {
Expand All @@ -73,25 +84,10 @@ export type TokenAddrDataMap = {
[symbol: string]: string;
};

type TokenData = {
symbol: string;
name: string;
description: string;
address: any;
decimals: number;
type: string;
};

export type TokenDataMap = {
[address: string]: Token;
};

export type DexalotTokensResponse = {
tokens: {
[token: string]: TokenData;
};
};

export type DexalotBlacklistResponse = {
blacklist: string[];
};
Expand All @@ -108,19 +104,13 @@ export type DexalotRateFetcherConfig = {
headers?: RequestHeaders;
params?: any;
};
tokensReqParams: {
url: string;
headers?: RequestHeaders;
params?: any;
};
blacklistReqParams: {
url: string;
headers?: RequestHeaders;
params?: any;
};
pairsIntervalMs: number;
pricesIntervalMs: number;
tokensIntervalMs: number;
blacklistIntervalMs: number;
pairsCacheKey: string;
pricesCacheKey: string;
Expand Down
13 changes: 8 additions & 5 deletions src/dex/dexalot/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ const pairValidator = joi.object({
base: joi.string().min(1).required(),
quote: joi.string().min(1).required(),
liquidityUSD: joi.number().min(0).required(),
baseAddress: joi.string().min(1).required(),
quoteAddress: joi.string().min(1).required(),
baseDecimals: joi.number().min(0).required(),
quoteDecimals: joi.number().min(0).required(),
});

export const pairsResponseValidator = joi.object({
pairs: joi.object().pattern(joi.string(), pairValidator),
});
export const pairsResponseValidator = joi
.object()
.pattern(joi.string(), pairValidator);

const orderbookRecordValidator = joi
.array()
.items(joi.string().min(1))
.length(2)
.required();
.length(2);

const orderbookValidator = joi.object({
bids: joi.array().items(orderbookRecordValidator).required(),
Expand Down

0 comments on commit 189a172

Please sign in to comment.