Skip to content

Commit

Permalink
Merge pull request #123 from madfish-solutions/TW-1147-modify-the-exc…
Browse files Browse the repository at this point in the history
…hange-rate-service-for-tokens-in-the-temple-wallet-extension-to-resolve-value-issue

TW-1147 Improve getting exchange rates
  • Loading branch information
lourenc authored Nov 22, 2023
2 parents 8313bdc + 0754355 commit d1ddaa9
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 92 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"express": "^4.18.2",
"firebase-admin": "^10.0.2",
"ioredis": "^5.3.2",
"lodash": "^4.17.21",
"memoizee": "^0.4.15",
"pino": "^6.11.2",
"pino-http": "^5.5.0",
Expand Down
22 changes: 6 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import logger from './utils/logger';
import { getSignedMoonPayUrl } from './utils/moonpay/get-signed-moonpay-url';
import SingleQueryDataProvider from './utils/SingleQueryDataProvider';
import { tezExchangeRateProvider } from './utils/tezos';
import { tokensExchangeRatesProvider } from './utils/tokens';
import { getExchangeRatesFromDB } from './utils/tokens';

const PINO_LOGGER = {
logger: logger.child({ name: 'web' }),
Expand Down Expand Up @@ -166,28 +166,18 @@ app.get('/api/abtest', (_, res) => {
app.get('/api/exchange-rates/tez', makeProviderDataRequestHandler(tezExchangeRateProvider));

app.get('/api/exchange-rates', async (_req, res) => {
const { data: tokensExchangeRates, error: tokensExchangeRatesError } = await getProviderStateWithTimeout(
tokensExchangeRatesProvider
);
const tokensExchangeRates = await getExchangeRatesFromDB();
const { data: tezExchangeRate, error: tezExchangeRateError } = await getProviderStateWithTimeout(
tezExchangeRateProvider
);
if (tokensExchangeRatesError !== undefined) {
return res.status(500).send({
error: tokensExchangeRatesError.message
});
} else if (tezExchangeRateError !== undefined) {

if (tezExchangeRateError !== undefined) {
return res.status(500).send({
error: tezExchangeRateError.message
});
} else {
if (tokensExchangeRates !== undefined && tezExchangeRate !== undefined) {
return res.json([
...tokensExchangeRates.map(({ ...restProps }) => restProps),
{ exchangeRate: tezExchangeRate.toString() }
]);
}
}

res.json([...tokensExchangeRates, { exchangeRate: tezExchangeRate.toString() }]);
});

app.get('/api/moonpay-sign', async (_req, res) => {
Expand Down
25 changes: 13 additions & 12 deletions src/utils/three-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface ThreeRouteChain {
}

// TODO: add axios adapter and change type if precision greater than of standard js number type is necessary
export interface ThreeRouteSwapResponse {
export interface ThreeRouteClassicSwapResponse {
input: number;
output: number;
chains: ThreeRouteChain[];
Expand All @@ -34,8 +34,8 @@ export interface ThreeRouteSwapResponse {
export interface ThreeRouteSirsSwapResponse {
input: number;
output: number;
tzbtcChain: ThreeRouteSwapResponse;
xtzChain: ThreeRouteSwapResponse;
tzbtcChain: ThreeRouteClassicSwapResponse;
xtzChain: ThreeRouteClassicSwapResponse;
}

interface ThreeRouteTokenCommon {
Expand Down Expand Up @@ -98,11 +98,13 @@ export interface ThreeRouteDex {

type ThreeRouteQueryParams = object | SwapQueryParams;
type ThreeRouteQueryResponse =
| ThreeRouteSwapResponse
| ThreeRouteClassicSwapResponse
| ThreeRouteSirsSwapResponse
| ThreeRouteDex[]
| ThreeRouteToken[];

export type ThreeRouteSwapResponse = ThreeRouteClassicSwapResponse | ThreeRouteSirsSwapResponse;

export const THREE_ROUTE_SIRS_SYMBOL = 'SIRS';

const threeRouteBuildQueryFn = makeBuildQueryFn<ThreeRouteQueryParams, ThreeRouteQueryResponse>(
Expand All @@ -111,18 +113,17 @@ const threeRouteBuildQueryFn = makeBuildQueryFn<ThreeRouteQueryParams, ThreeRout
{ headers: { Authorization: `Basic ${EnvVars.THREE_ROUTE_API_AUTH_TOKEN}` } }
);

export const getThreeRouteSwap = threeRouteBuildQueryFn<
SwapQueryParams,
ThreeRouteSwapResponse | ThreeRouteSirsSwapResponse
>(({ inputTokenSymbol, outputTokenSymbol, realAmount }) => {
const isSirsSwap = inputTokenSymbol === THREE_ROUTE_SIRS_SYMBOL || outputTokenSymbol === THREE_ROUTE_SIRS_SYMBOL;
export const getThreeRouteSwap = threeRouteBuildQueryFn<SwapQueryParams, ThreeRouteSwapResponse>(
({ inputTokenSymbol, outputTokenSymbol, realAmount }) => {
const isSirsSwap = inputTokenSymbol === THREE_ROUTE_SIRS_SYMBOL || outputTokenSymbol === THREE_ROUTE_SIRS_SYMBOL;

return `/${isSirsSwap ? 'swap-sirs' : 'swap'}/${inputTokenSymbol}/${outputTokenSymbol}/${realAmount}`;
});
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) =>
export const getChains = (response: ThreeRouteSwapResponse) =>
'chains' in response ? response.chains : [...response.xtzChain.chains, ...response.tzbtcChain.chains];
Loading

0 comments on commit d1ddaa9

Please sign in to comment.