Skip to content

Commit

Permalink
improve blacklist
Browse files Browse the repository at this point in the history
  • Loading branch information
aburkut committed Sep 29, 2023
1 parent f9b6c32 commit c6bc737
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
8 changes: 3 additions & 5 deletions src/dex/dexalot/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';
Expand Down
52 changes: 33 additions & 19 deletions src/dex/dexalot/dexalot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
SwapSide,
Network,
ETHER_ADDRESS,
CACHE_PREFIX,
NULL_ADDRESS,
} from '../../constants';
import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -102,11 +100,11 @@ export class Dexalot extends SimpleExchange implements IDex<DexalotData> {
);
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,
Expand Down Expand Up @@ -150,6 +148,7 @@ export class Dexalot extends SimpleExchange implements IDex<DexalotData> {
tokensCacheKey: this.tokensCacheKey,
tokensCacheTTLSecs: DEXALOT_TOKENS_CACHES_TTL_S,
blacklistCacheKey: this.blacklistCacheKey,
blacklistCacheTTLSecs: DEXALOT_BLACKLIST_CACHES_TTL_S,
},
},
);
Expand Down Expand Up @@ -755,29 +754,44 @@ export class Dexalot extends SimpleExchange implements IDex<DexalotData> {
return result === 'true';
}

async setBlacklist(txOrigin: Address, ttl: number = DEXALOT_BLACKLIST_TTL_S,
): Promise<boolean> {
await this.dexHelper.cache.setex(
async setBlacklist(txOrigin: Address, ttl: number = DEXALOT_BLACKLIST_CACHES_TTL_S,): Promise<boolean> {
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<boolean> {
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) {
Expand Down
16 changes: 13 additions & 3 deletions src/dex/dexalot/rate-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class RateFetcher {

private blacklistFetcher: Fetcher<DexalotBlacklistResponse>;
private blacklistCacheKey: string;
private blacklistCacheTTL: number;

constructor(
private dexHelper: IDexHelper,
Expand All @@ -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<DexalotPairsResponse>(
dexHelper.httpRequest,
Expand Down Expand Up @@ -138,20 +140,23 @@ export class RateFetcher {
decimals: pairs[pair].quoteDecimals,
};
});

this.dexHelper.cache.setex(
this.dexKey,
this.network,
this.pairsCacheKey,
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,
Expand All @@ -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,
Expand All @@ -180,8 +186,12 @@ export class RateFetcher {
resp: DexalotBlacklistResponse,
): Promise<void> {
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())),
);
}
}
1 change: 1 addition & 0 deletions src/dex/dexalot/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export type DexalotRateFetcherConfig = {
tokensAddrCacheKey: string;
tokensCacheKey: string;
blacklistCacheKey: string;
blacklistCacheTTLSecs: number;
pairsCacheTTLSecs: number;
pricesCacheTTLSecs: number;
tokensCacheTTLSecs: number;
Expand Down

0 comments on commit c6bc737

Please sign in to comment.