Skip to content

Commit

Permalink
Merge pull request #106 from madfish-solutions/TW-740-research-wrong-…
Browse files Browse the repository at this point in the history
…token-price-in-dollars

TW-740 Implement preventing blockFinder callback freezing
  • Loading branch information
lourenc authored Jun 29, 2023
2 parents 9c073ec + 46a0a06 commit 0ea04fb
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions src/utils/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const getTokensExchangeRates = async (): Promise<TokenExchangeRateEntry[]> => {
};
} catch (e) {
if (e instanceof TimeoutError) {
logger.error('Timeout error while getting exchange rate for token', token.symbol);
logger.error(`Timeout error while getting exchange rate for token ${token.symbol}`);

return undefined;
}
Expand Down Expand Up @@ -153,7 +153,10 @@ const getTokensExchangeRates = async (): Promise<TokenExchangeRateEntry[]> => {
exchangeRate: tokenPrice,
metadata: mapTzktTokenDataToBcdTokenData(aspencoinMetadata?.[0])
});
} catch (e) {}
} catch (e) {
logger.error('Failed to get exchange rate for Aspencoin');
logger.error(e as Error);
}
}

logger.info('Successfully got tokens exchange rates');
Expand Down Expand Up @@ -181,28 +184,37 @@ blockFinder(EMPTY_BLOCK, async block =>
return false;
}

await probeSwapsProvider.subscribe(token.symbol);
const { data: probeSwaps, error: swapError } = await probeSwapsProvider.get(token.symbol);
try {
await probeSwapsProvider.subscribe(token.symbol);
const { data: probeSwaps, error: swapError } = await Promise.race([
probeSwapsProvider.get(token.symbol),
new Promise<never>((_, rej) => setTimeout(() => rej(new TimeoutError()), 10000))
]);

if (swapError) {
throw swapError;
}
if (swapError) {
throw swapError;
}

const { directSwap, invertedSwap } = probeSwaps;
const dexesAddresses = directSwap.chains
.concat(invertedSwap.chains)
.map(chain => chain.hops.map(hop => dexes.find(dex => dex.id === hop.dex)?.contract).filter(isDefined))
.flat();
const { directSwap, invertedSwap } = probeSwaps;
const dexesAddresses = directSwap.chains
.concat(invertedSwap.chains)
.map(chain => chain.hops.map(hop => dexes.find(dex => dex.id === hop.dex)?.contract).filter(isDefined))
.flat();

const firstUpdatedDexAddress = dexesAddresses.find(dexAddress => recentDestinations.includes(dexAddress));
if (isDefined(firstUpdatedDexAddress)) {
logger.info(`updating swap output for token ${token.symbol} because of dex ${firstUpdatedDexAddress}`);
await probeSwapsProvider.refetchInSubscription(token.symbol);
const firstUpdatedDexAddress = dexesAddresses.find(dexAddress => recentDestinations.includes(dexAddress));
if (isDefined(firstUpdatedDexAddress)) {
logger.info(`updating swap output for token ${token.symbol} because of dex ${firstUpdatedDexAddress}`);
await probeSwapsProvider.refetchInSubscription(token.symbol);

return true;
}
return true;
}

return false;
return false;
} catch (e) {
logger.error(e as Error);

return false;
}
})
);
if (outputsUpdatesFlags.some(flag => flag)) {
Expand Down

0 comments on commit 0ea04fb

Please sign in to comment.