Skip to content

Commit

Permalink
Merge pull request #107 from madfish-solutions/TW-740-fix-sirs-exchan…
Browse files Browse the repository at this point in the history
…ge-rate-calculation
  • Loading branch information
lourenc authored Jul 5, 2023
2 parents 0ea04fb + 9964568 commit 26053f3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
30 changes: 25 additions & 5 deletions src/utils/three-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,19 +97,32 @@ 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<ThreeRouteQueryParams, ThreeRouteQueryResponse>(
THREE_ROUTE_API_URL,
5,
{ headers: { Authorization: `Basic ${THREE_ROUTE_API_AUTH_TOKEN}` } }
);

export const getThreeRouteSwap = threeRouteBuildQueryFn<SwapQueryParams, ThreeRouteSwapResponse>(
({ 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<object, ThreeRouteDex[]>('/dexes', []);

export const getThreeRouteTokens = threeRouteBuildQueryFn<object, ThreeRouteToken[]>('/tokens', []);

export const getChains = (response: ThreeRouteSwapResponse | ThreeRouteSirsSwapResponse) =>
'chains' in response ? response.chains : [...response.xtzChain.chains, ...response.tzbtcChain.chains];
15 changes: 12 additions & 3 deletions src/utils/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import {
getThreeRouteTokens,
ThreeRouteStandardEnum,
ThreeRouteFa12Token,
ThreeRouteFa2Token
ThreeRouteFa2Token,
getChains,
THREE_ROUTE_SIRS_SYMBOL
} from './three-route';
import { BcdTokenData, mapTzktTokenDataToBcdTokenData, tokensMetadataProvider } from './tzkt';

Expand Down Expand Up @@ -184,6 +186,11 @@ blockFinder(EMPTY_BLOCK, async block =>
return false;
}

if (token.symbol === THREE_ROUTE_SIRS_SYMBOL) {
// 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([
Expand All @@ -196,8 +203,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();

Expand Down

0 comments on commit 26053f3

Please sign in to comment.