From 51cc5cbd74daf873b697997783b1d8140177c754 Mon Sep 17 00:00:00 2001 From: Inokentii Mazhara Date: Mon, 3 Jul 2023 17:05:24 +0300 Subject: [PATCH 1/2] TW-740 Fix exchange rate calculation for SIRS token --- src/utils/three-route.ts | 30 +++++++++++++++++++++++++----- src/utils/tokens.ts | 18 ++++++++++++++---- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/utils/three-route.ts b/src/utils/three-route.ts index 19c2615..e89fcc7 100644 --- a/src/utils/three-route.ts +++ b/src/utils/three-route.ts @@ -31,6 +31,13 @@ export interface ThreeRouteSwapResponse { chains: ThreeRouteChain[]; } +export interface ThreeRouteSirsSwapResponse { + input: number; + output: number; + tzbtcChain: ThreeRouteSwapResponse; + xtzChain: ThreeRouteSwapResponse; +} + interface ThreeRouteTokenCommon { id: number; symbol: string; @@ -90,7 +97,13 @@ export interface ThreeRouteDex { } type ThreeRouteQueryParams = object | SwapQueryParams; -type ThreeRouteQueryResponse = ThreeRouteSwapResponse | ThreeRouteDex[] | ThreeRouteToken[]; +type ThreeRouteQueryResponse = + | ThreeRouteSwapResponse + | ThreeRouteSirsSwapResponse + | ThreeRouteDex[] + | ThreeRouteToken[]; + +export const THREE_ROUTE_SIRS_SYMBOL = 'SIRS'; const threeRouteBuildQueryFn = makeBuildQueryFn( THREE_ROUTE_API_URL, @@ -98,11 +111,18 @@ const threeRouteBuildQueryFn = makeBuildQueryFn( - ({ inputTokenSymbol, outputTokenSymbol, realAmount }) => - `/swap/${inputTokenSymbol}/${outputTokenSymbol}/${realAmount}` -); +export const getThreeRouteSwap = threeRouteBuildQueryFn< + SwapQueryParams, + ThreeRouteSwapResponse | ThreeRouteSirsSwapResponse +>(({ inputTokenSymbol, outputTokenSymbol, realAmount }) => { + const isSirsSwap = inputTokenSymbol === THREE_ROUTE_SIRS_SYMBOL || outputTokenSymbol === THREE_ROUTE_SIRS_SYMBOL; + + return `/${isSirsSwap ? 'swap-sirs' : 'swap'}/${inputTokenSymbol}/${outputTokenSymbol}/${realAmount}`; +}); export const getThreeRouteDexes = threeRouteBuildQueryFn('/dexes', []); export const getThreeRouteTokens = threeRouteBuildQueryFn('/tokens', []); + +export const getChains = (response: ThreeRouteSwapResponse | ThreeRouteSirsSwapResponse) => + 'chains' in response ? response.chains : [...response.xtzChain.chains, ...response.tzbtcChain.chains]; diff --git a/src/utils/tokens.ts b/src/utils/tokens.ts index f061532..864d0f3 100644 --- a/src/utils/tokens.ts +++ b/src/utils/tokens.ts @@ -16,7 +16,9 @@ import { getThreeRouteTokens, ThreeRouteStandardEnum, ThreeRouteFa12Token, - ThreeRouteFa2Token + ThreeRouteFa2Token, + getChains, + THREE_ROUTE_SIRS_SYMBOL } from './three-route'; import { BcdTokenData, mapTzktTokenDataToBcdTokenData, tokensMetadataProvider } from './tzkt'; @@ -80,7 +82,7 @@ const getTokensExchangeRates = async (): Promise => { (token): token is ThreeRouteFa12Token | ThreeRouteFa2Token => token.standard !== ThreeRouteStandardEnum.xtz ) .map(async (token): Promise => { - logger.info(`Getting exchange rate for ${token.symbol}`); + logger.info(token.symbol); const { contract, tokenId: rawTokenId } = token; const tokenId = isDefined(rawTokenId) ? Number(rawTokenId) : undefined; await probeSwapsProvider.subscribe(token.symbol); @@ -184,6 +186,12 @@ blockFinder(EMPTY_BLOCK, async block => return false; } + if (token.symbol === THREE_ROUTE_SIRS_SYMBOL) { + logger.info('swap output for SIRS should be updated each block because of baking subsidy'); + + return true; + } + try { await probeSwapsProvider.subscribe(token.symbol); const { data: probeSwaps, error: swapError } = await Promise.race([ @@ -196,8 +204,10 @@ blockFinder(EMPTY_BLOCK, async block => } const { directSwap, invertedSwap } = probeSwaps; - const dexesAddresses = directSwap.chains - .concat(invertedSwap.chains) + const directSwapChains = getChains(directSwap); + const invertedSwapChains = getChains(invertedSwap); + const dexesAddresses = directSwapChains + .concat(invertedSwapChains) .map(chain => chain.hops.map(hop => dexes.find(dex => dex.id === hop.dex)?.contract).filter(isDefined)) .flat(); From 9964568c27a05015345656e2aeea4ccc3718b580 Mon Sep 17 00:00:00 2001 From: Inokentii Mazhara Date: Mon, 3 Jul 2023 17:22:53 +0300 Subject: [PATCH 2/2] TW-740 Change logging according to comments --- src/utils/tokens.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/utils/tokens.ts b/src/utils/tokens.ts index 864d0f3..70a6a53 100644 --- a/src/utils/tokens.ts +++ b/src/utils/tokens.ts @@ -82,7 +82,7 @@ const getTokensExchangeRates = async (): Promise => { (token): token is ThreeRouteFa12Token | ThreeRouteFa2Token => token.standard !== ThreeRouteStandardEnum.xtz ) .map(async (token): Promise => { - logger.info(token.symbol); + logger.info(`Getting exchange rate for ${token.symbol}`); const { contract, tokenId: rawTokenId } = token; const tokenId = isDefined(rawTokenId) ? Number(rawTokenId) : undefined; await probeSwapsProvider.subscribe(token.symbol); @@ -187,8 +187,7 @@ blockFinder(EMPTY_BLOCK, async block => } if (token.symbol === THREE_ROUTE_SIRS_SYMBOL) { - logger.info('swap output for SIRS should be updated each block because of baking subsidy'); - + // Swap output for SIRS should be updated each block because of baking subsidy return true; }