From c6bc7375b5d478537c659030c0b01c19bf9a3c32 Mon Sep 17 00:00:00 2001 From: Alexander Burkut Date: Sat, 30 Sep 2023 00:00:05 +0300 Subject: [PATCH] improve blacklist --- src/dex/dexalot/constants.ts | 8 ++--- src/dex/dexalot/dexalot.ts | 52 +++++++++++++++++++++------------ src/dex/dexalot/rate-fetcher.ts | 16 ++++++++-- src/dex/dexalot/types.ts | 1 + 4 files changed, 50 insertions(+), 27 deletions(-) diff --git a/src/dex/dexalot/constants.ts b/src/dex/dexalot/constants.ts index 46a21689b..f45bc8cf0 100644 --- a/src/dex/dexalot/constants.ts +++ b/src/dex/dexalot/constants.ts @@ -4,10 +4,12 @@ export const DEXALOT_RATE_LIMITED_TTL_S = 60 * 60; // 1 hour export const DEXALOT_PRICES_CACHES_TTL_S = 3; -export const DEXALOT_PAIRS_CACHES_TTL_S = 11; +export const DEXALOT_PAIRS_CACHES_TTL_S = 21 * 60; // 21 mins export const DEXALOT_TOKENS_CACHES_TTL_S = 11; +export const DEXALOT_BLACKLIST_CACHES_TTL_S = 3 * 60; // 3 hours + export const DEXALOT_API_PRICES_POLLING_INTERVAL_MS = 1000; export const DEXALOT_API_PAIRS_POLLING_INTERVAL_MS = 1000 * 60 * 10; // 10 mins @@ -20,10 +22,6 @@ export const DEXALOT_GAS_COST = 120_000; export const DEXALOT_RESTRICT_TTL_S = 60 * 30; // 30 minutes -export const DEXALOT_BLACKLIST_TTL_S = 60 * 60 * 24; // 24 hours - -export const DEXALOT_BLACKLIST_CACHE_VALUE = 'blacklisted'; - export const DEXALOT_RATELIMIT_CACHE_VALUE = 'limited'; export const DEXALOT_RESTRICTED_CACHE_KEY = 'restricted'; diff --git a/src/dex/dexalot/dexalot.ts b/src/dex/dexalot/dexalot.ts index cdd79694c..7eee6b0a6 100644 --- a/src/dex/dexalot/dexalot.ts +++ b/src/dex/dexalot/dexalot.ts @@ -15,7 +15,6 @@ import { SwapSide, Network, ETHER_ADDRESS, - CACHE_PREFIX, NULL_ADDRESS, } from '../../constants'; import * as CALLDATA_GAS_COST from '../../calldata-gas-cost'; @@ -54,9 +53,8 @@ import { DEXALOT_MIN_SLIPPAGE_FACTOR_THRESHOLD_FOR_RESTRICTION, DEXALOT_RESTRICTED_CACHE_KEY, DEXALOT_RESTRICT_TTL_S, - DEXALOT_BLACKLIST_TTL_S, - DEXALOT_BLACKLIST_CACHE_VALUE, DEXALOT_RATELIMIT_CACHE_VALUE, + DEXALOT_BLACKLIST_CACHES_TTL_S, } from './constants'; import { BI_MAX_UINT256 } from '../../bigint-constants'; import { ethers } from 'ethers'; @@ -102,11 +100,11 @@ export class Dexalot extends SimpleExchange implements IDex { ); this.dexalotAuthToken = authToken; - this.pricesCacheKey = `${CACHE_PREFIX}_prices`; - this.pairsCacheKey = `${CACHE_PREFIX}_pairs`; - this.tokensAddrCacheKey = `${CACHE_PREFIX}_tokens_addr`; - this.tokensCacheKey = `${CACHE_PREFIX}_tokens`; - this.blacklistCacheKey = `${CACHE_PREFIX}_blacklist`; + this.pricesCacheKey = 'prices'; + this.pairsCacheKey = 'pairs'; + this.tokensAddrCacheKey = 'tokens_addr'; + this.tokensCacheKey = 'tokens'; + this.blacklistCacheKey = 'blacklist'; this.rateFetcher = new RateFetcher( this.dexHelper, @@ -150,6 +148,7 @@ export class Dexalot extends SimpleExchange implements IDex { tokensCacheKey: this.tokensCacheKey, tokensCacheTTLSecs: DEXALOT_TOKENS_CACHES_TTL_S, blacklistCacheKey: this.blacklistCacheKey, + blacklistCacheTTLSecs: DEXALOT_BLACKLIST_CACHES_TTL_S, }, }, ); @@ -755,29 +754,44 @@ export class Dexalot extends SimpleExchange implements IDex { return result === 'true'; } - async setBlacklist(txOrigin: Address, ttl: number = DEXALOT_BLACKLIST_TTL_S, - ): Promise { - await this.dexHelper.cache.setex( + async setBlacklist(txOrigin: Address, ttl: number = DEXALOT_BLACKLIST_CACHES_TTL_S,): Promise { + const cachedBlacklist = await this.dexHelper.cache.get( this.dexKey, this.network, - this.getBlackListKey(txOrigin), + this.blacklistCacheKey, + ); + + let blacklist: string[] = []; + if (cachedBlacklist) { + blacklist = JSON.parse(cachedBlacklist); + } + + blacklist.push(txOrigin.toLowerCase()); + + this.dexHelper.cache.setex( + this.dexKey, + this.network, + this.blacklistCacheKey, ttl, - DEXALOT_BLACKLIST_CACHE_VALUE, + JSON.stringify(blacklist), ); + return true; } async isBlacklisted(txOrigin: Address): Promise { - const result = await this.dexHelper.cache.get( + const cachedBlacklist = await this.dexHelper.cache.get( this.dexKey, this.network, - this.getBlackListKey(txOrigin), + this.blacklistCacheKey, ); - return result === DEXALOT_BLACKLIST_CACHE_VALUE; - } - getBlackListKey(address: Address) { - return `blacklist_${address}`.toLowerCase(); + if (cachedBlacklist) { + const blacklist = JSON.parse(cachedBlacklist) as string[]; + return blacklist.includes(txOrigin.toLowerCase()); + } + + return false; } getRateLimitedKey(address: Address) { diff --git a/src/dex/dexalot/rate-fetcher.ts b/src/dex/dexalot/rate-fetcher.ts index 5cba7f322..fac39ae0e 100644 --- a/src/dex/dexalot/rate-fetcher.ts +++ b/src/dex/dexalot/rate-fetcher.ts @@ -32,6 +32,7 @@ export class RateFetcher { private blacklistFetcher: Fetcher; private blacklistCacheKey: string; + private blacklistCacheTTL: number; constructor( private dexHelper: IDexHelper, @@ -48,6 +49,7 @@ export class RateFetcher { this.tokensCacheKey = config.rateConfig.tokensCacheKey; this.tokensCacheTTL = config.rateConfig.tokensCacheTTLSecs; this.blacklistCacheKey = config.rateConfig.blacklistCacheKey; + this.blacklistCacheTTL = config.rateConfig.blacklistCacheTTLSecs; this.pairsFetcher = new Fetcher( dexHelper.httpRequest, @@ -138,6 +140,7 @@ export class RateFetcher { decimals: pairs[pair].quoteDecimals, }; }); + this.dexHelper.cache.setex( this.dexKey, this.network, @@ -145,6 +148,7 @@ export class RateFetcher { this.pairsCacheTTL, JSON.stringify(dexPairs), ); + this.dexHelper.cache.setex( this.dexKey, this.network, @@ -152,6 +156,7 @@ export class RateFetcher { this.tokensCacheTTL, JSON.stringify(tokenMap), ); + this.dexHelper.cache.setex( this.dexKey, this.network, @@ -167,6 +172,7 @@ export class RateFetcher { Object.keys(prices).forEach(pair => { dexPrices[pair.toLowerCase()] = prices[pair]; }); + this.dexHelper.cache.setex( this.dexKey, this.network, @@ -180,8 +186,12 @@ export class RateFetcher { resp: DexalotBlacklistResponse, ): Promise { const { blacklist } = resp; - for (const address of blacklist) { - this.dexHelper.cache.sadd(this.blacklistCacheKey, address.toLowerCase()); - } + this.dexHelper.cache.setex( + this.dexKey, + this.network, + this.blacklistCacheKey, + this.blacklistCacheTTL, + JSON.stringify(blacklist.map((item) => item.toLowerCase())), + ); } } diff --git a/src/dex/dexalot/types.ts b/src/dex/dexalot/types.ts index eb3b37f57..dee55de3a 100644 --- a/src/dex/dexalot/types.ts +++ b/src/dex/dexalot/types.ts @@ -115,6 +115,7 @@ export type DexalotRateFetcherConfig = { tokensAddrCacheKey: string; tokensCacheKey: string; blacklistCacheKey: string; + blacklistCacheTTLSecs: number; pairsCacheTTLSecs: number; pricesCacheTTLSecs: number; tokensCacheTTLSecs: number;