Skip to content

Commit

Permalink
Update the swap preview cache
Browse files Browse the repository at this point in the history
  • Loading branch information
skubarenko committed Aug 30, 2022
1 parent 7fd529b commit 935dfa2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/atomex/atomex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class Atomex implements AtomexService {
this.exchangeManager.stop();
this.swapManager.stop();
this.balanceManager.dispose();
this.swapPreviewManager.dispose();

this._isStarted = false;
}
Expand Down Expand Up @@ -101,9 +102,12 @@ export class Atomex implements AtomexService {
if (swapPreview.errors.length)
throw new Error('Swap preview has errors');

const fromAddress = swapPreview.to.address;
const fromAddress = swapPreview.from.address;
if (!fromAddress)
throw new Error('Swap preview doesn\'t have the "from" address');
const toAddress = swapPreview.to.address;
if (!toAddress)
throw new Error('Swap preview doesn\'t have the "to" address');

const [baseCurrencyId, quoteCurrencyId] = symbolsHelper.getBaseQuoteCurrenciesBySymbol(swapPreview.symbol);
const baseCurrencyInfo = this.atomexContext.providers.blockchainProvider.getCurrencyInfo(baseCurrencyId);
Expand Down Expand Up @@ -132,7 +136,7 @@ export class Atomex implements AtomexService {
},
requisites: {
secretHash: null,
receivingAddress: swapPreview.to.address,
receivingAddress: toAddress,
refundAddress: newSwapRequestOrSwapId.refundAddress || null,
rewardForRedeem: rewardForRedeem || new BigNumber(0),
// TODO: from config
Expand All @@ -156,6 +160,7 @@ export class Atomex implements AtomexService {
if (swaps.some(swap => !swap))
throw new Error('Swap not found');

this.swapPreviewManager.clearCache();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return swaps.length === 1 ? swaps[0]! : (swaps as readonly Swap[]);
}
Expand Down
20 changes: 15 additions & 5 deletions src/atomex/atomexSwapPreviewManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import BigNumber from 'bignumber.js';

import type { AtomexProtocolV1, CurrencyInfo, FeesInfo } from '../blockchain/index';
import type { Currency } from '../common/index';
import type { Currency, Disposable } from '../common/index';
import { Mutable, Cache, InMemoryCache } from '../core/index';
import { ExchangeSymbolsProvider, ordersHelper, symbolsHelper, type NormalizedOrderPreviewParameters, type OrderPreview } from '../exchange/index';
import type { Swap } from '../swaps/index';
Expand All @@ -15,9 +15,9 @@ interface UserInvolvedSwapsInfo {
fromTotalAmount: BigNumber;
}

export class AtomexSwapPreviewManager {
private readonly swapPreviewFeesCache: Cache = new InMemoryCache({ absoluteExpirationMs: 10 * 1000 });
private readonly userInvolvedSwapsCache: Cache = new InMemoryCache({ absoluteExpirationMs: 10 * 1000 });
export class AtomexSwapPreviewManager implements Disposable {
private readonly swapPreviewFeesCache: Cache = new InMemoryCache({ absoluteExpirationMs: 20 * 1000 });
private readonly userInvolvedSwapsCache: Cache = new InMemoryCache({ slidingExpirationMs: 30 * 1000 });

constructor(protected readonly atomexContext: AtomexContext) {
}
Expand Down Expand Up @@ -126,6 +126,16 @@ export class AtomexSwapPreviewManager {
};
}

// TODO: Temporarily. Remove this method when we add local swap tracking
clearCache() {
this.swapPreviewFeesCache.clear();
this.userInvolvedSwapsCache.clear();
}

async dispose(): Promise<void> {
this.clearCache();
}

protected normalizeSwapPreviewParametersIfNeeded(swapPreviewParameters: SwapPreviewParameters | NormalizedSwapPreviewParameters): NormalizedSwapPreviewParameters {
return AtomexSwapPreviewManager.isNormalizedSwapPreviewParameters(swapPreviewParameters)
? swapPreviewParameters
Expand Down Expand Up @@ -243,7 +253,7 @@ export class AtomexSwapPreviewManager {
if (swapsInfo)
return swapsInfo;

const swaps = (await this.atomexContext.managers.swapManager.getSwaps(userAddress, { active: true }))
const swaps = (await this.atomexContext.managers.swapManager.getSwaps(userAddress))
.filter(swap => swap.user.status === 'Involved' && swap.from.currencyId === fromCurrencyId);
const fromTotalAmount = swaps.reduce(
(total, swap) => total.plus(swap.from.amount),
Expand Down

0 comments on commit 935dfa2

Please sign in to comment.