Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TW-1147 Improve getting exchange rates #123

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { 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 @@ -151,7 +151,7 @@
await redisClient.lpush('notifications', JSON.stringify(newNotification));

res.status(200).send({ message: 'Notification added successfully' });
} catch (error: any) {

Check warning on line 154 in src/index.ts

View workflow job for this annotation

GitHub Actions / Checks if ts and lint works

Unexpected any. Specify a different type
res.status(500).send({ error: error.message });
}
});
Expand All @@ -166,28 +166,18 @@
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) {
keshan3262 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading