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

Release everything to prod #131

Merged
merged 15 commits into from
Dec 20, 2023
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: 0 additions & 1 deletion .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ THREE_ROUTE_API_AUTH_TOKEN=
REDIS_URL=
ADD_NOTIFICATION_USERNAME=
ADD_NOTIFICATION_PASSWORD=
TZPRO_API_KEY=
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"bignumber.js": "^9.1.0",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"cross-fetch": "^3.1.5",
"dotenv": "^9.0.2",
"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
3 changes: 1 addition & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ export const EnvVars = {
THREE_ROUTE_API_AUTH_TOKEN: getEnv('THREE_ROUTE_API_AUTH_TOKEN'),
REDIS_URL: getEnv('REDIS_URL'),
ADD_NOTIFICATION_USERNAME: getEnv('ADD_NOTIFICATION_USERNAME'),
ADD_NOTIFICATION_PASSWORD: getEnv('ADD_NOTIFICATION_PASSWORD'),
TZPRO_API_KEY: getEnv('TZPRO_API_KEY')
ADD_NOTIFICATION_PASSWORD: getEnv('ADD_NOTIFICATION_PASSWORD')
};

for (const name in EnvVars) {
Expand Down
24 changes: 7 additions & 17 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 @@ -150,8 +150,8 @@

await redisClient.lpush('notifications', JSON.stringify(newNotification));

res.status(200).send({ message: 'Notification added successfully' });
res.status(200).send({ message: 'Notification added successfully', notification: newNotification });
} 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) {
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
16 changes: 0 additions & 16 deletions src/interfaces/ticker.interface.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/utils/dapp-list-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const dappList: DappList[] = [
},
{
name: 'Yupana',
dappUrl: 'https://yupana.finance',
dappUrl: 'https://app.yupana.finance',
type: DappType.DeFi,
logo: 'https://pbs.twimg.com/profile_images/1450382829062393859/NSu06S5C_400x400.png',
slug: 'yupana',
Expand Down
23 changes: 0 additions & 23 deletions src/utils/fetch.ts

This file was deleted.

37 changes: 24 additions & 13 deletions src/utils/tezos.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { compose, MichelCodecPacker, Signer, TezosToolkit } from '@taquito/taquito';
import { tzip12 } from '@taquito/tzip12';
import { tzip16 } from '@taquito/tzip16';
import { AxiosError } from 'axios';
import memoizee from 'memoizee';

import { EnvVars } from '../config';
import { ITicker } from '../interfaces/ticker.interface';
import fetch from './fetch';
import { getMarketsBySymbols } from './coingecko';
import { isDefined } from './helpers';
import logger from './logger';
import SingleQueryDataProvider from './SingleQueryDataProvider';
import { BcdTokenData } from './tzkt';

Expand Down Expand Up @@ -54,18 +55,28 @@ export const getStorage = memoizee(
);

const getTezExchangeRate = async () => {
const marketTickers = await fetch<Array<ITicker>>(
`https://api.tzpro.io/markets/tickers?api_key=${EnvVars.TZPRO_API_KEY}`
);
const usdTickers = marketTickers.filter(e => e.quote === 'USD' && e.base === 'XTZ');
// price index: use all USD ticker last prices with equal weight
const vol = usdTickers.reduce((s, t) => s + t.volume_base, 0) || null;
const price = vol === null ? 1 : usdTickers.reduce((s, t) => s + (t.last * t.volume_base) / vol, 0);

return price;
try {
const [xtzMarket] = await getMarketsBySymbols(['xtz']);

return xtzMarket.current_price;
} catch (e) {
if (!(e instanceof AxiosError)) {
logger.error('Request for TEZ exchange rate failed with unknown error');
} else if (isDefined(e.response) && isDefined(e.response.data)) {
logger.error(
`Request for TEZ exchange rate failed with status ${e.response.status} and message ${e.response.data}`
);
} else if (isDefined(e.response) && isDefined(e.response.status)) {
logger.error(`Request for TEZ exchange rate failed with status ${e.response.status}`);
} else {
logger.error('Request for TEZ exchange rate failed without response');
}

throw e;
}
};

export const tezExchangeRateProvider = new SingleQueryDataProvider(30000, getTezExchangeRate);
export const tezExchangeRateProvider = new SingleQueryDataProvider(60000, getTezExchangeRate);

export class MetadataParseError extends Error {}

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