Skip to content

Commit

Permalink
Merge branch 'development' of https://github.com/madfish-solutions/te…
Browse files Browse the repository at this point in the history
…mplewallet-backend into TW-1489-smarty-ads-integration
  • Loading branch information
keshan3262 committed Aug 19, 2024
2 parents 93f2f1f + acc023c commit f6c44aa
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 43 deletions.
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ THREE_ROUTE_API_AUTH_TOKEN=
REDIS_URL=
ADMIN_USERNAME=
ADMIN_PASSWORD=
COVALENT_API_KEY=
2 changes: 1 addition & 1 deletion .github/workflows/code-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: macos-latest
steps:
- name: Clone repository
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Repository setup
uses: ./.github/workflows/repository-setup
Expand Down
13 changes: 5 additions & 8 deletions .github/workflows/repository-setup/action.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
name: Repository setup

description: Setups Node 16.x & installs dependencies
description: Setups Node.js & installs dependencies

runs:
using: 'composite'
steps:
- name: Setup Node 16.x
uses: actions/setup-node@v2
- name: Setup Node 20.x
uses: actions/setup-node@v4
with:
node-version: '16.x'

- name: Get branch name
uses: EthanSK/git-branch-name-action@v1
node-version: '20.x'

- name: Get yarn cache directory path
id: yarn-cache-dir-path
shell: bash
run: echo "::set-output name=dir::$(yarn cache dir)"

- uses: actions/cache@v3
- uses: actions/cache@v4
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ dist
# DynamoDB Local files
.dynamodb/

# Redis
*.rdb

# TernJS port file
.tern-port

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:14
FROM node:20-alpine3.18
WORKDIR /usr/src/app
COPY package.json yarn.lock ./
RUN yarn
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ import { getAliceBobEstimationPayload } from './utils/alice-bob/get-alice-bob-es
import { getAliceBobOrderInfo } from './utils/alice-bob/get-alice-bob-order-info';
import { getAliceBobPairInfo } from './utils/alice-bob/get-alice-bob-pair-info';
import { getAliceBobPairsInfo } from './utils/alice-bob/get-alice-bob-pairs-info';
import { btcExchangeRateProvider, tezExchangeRateProvider } from './utils/coingecko';
import { CodedError } from './utils/errors';
import { coinGeckoTokens } from './utils/gecko-tokens';
import { getExternalApiErrorPayload, isDefined, isNonEmptyString } from './utils/helpers';
import logger from './utils/logger';
import { getSignedMoonPayUrl } from './utils/moonpay/get-signed-moonpay-url';
import { getSigningNonce } from './utils/signing-nonce';
import SingleQueryDataProvider from './utils/SingleQueryDataProvider';
import { tezExchangeRateProvider } from './utils/tezos';
import { getExchangeRates } from './utils/tokens';

const PINO_LOGGER = {
Expand Down Expand Up @@ -175,6 +175,7 @@ app.get('/api/abtest', (_, res) => {
});

app.get('/api/exchange-rates/tez', makeProviderDataRequestHandler(tezExchangeRateProvider));
app.get('/api/exchange-rates/btc', makeProviderDataRequestHandler(btcExchangeRateProvider));

app.get('/api/exchange-rates', async (_req, res) => {
const tokensExchangeRates = await getExchangeRates();
Expand Down
31 changes: 30 additions & 1 deletion src/utils/coingecko.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { range } from './helpers';
import { AxiosError } from 'axios';

import { isDefined, range } from './helpers';
import logger from './logger';
import { makeBuildQueryFn } from './makeBuildQueryFn';
import SingleQueryDataProvider from './SingleQueryDataProvider';

Expand Down Expand Up @@ -89,3 +92,29 @@ export const getMarketsBySymbols = async (symbols: string[]) => {

return chunks.flat();
};

const createCoingeckoExchangeRateProvider = (tokenSymbol: string) =>
new SingleQueryDataProvider(60000, async () => {
try {
const [market] = await getMarketsBySymbols([tokenSymbol]);

return market.current_price;
} catch (e) {
if (!(e instanceof AxiosError)) {
logger.error(`Request for ${tokenSymbol} exchange rate failed with unknown error`);
} else if (isDefined(e.response) && isDefined(e.response.data)) {
logger.error(
`Request for ${tokenSymbol} 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 ${tokenSymbol} exchange rate failed with status ${e.response.status}`);
} else {
logger.error(`Request for ${tokenSymbol} exchange rate failed without response`);
}

throw e;
}
});

export const tezExchangeRateProvider = createCoingeckoExchangeRateProvider('xtz');
export const btcExchangeRateProvider = createCoingeckoExchangeRateProvider('btc');
2 changes: 1 addition & 1 deletion src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface CodedErrorForResponse {
type StatusCodeNumber = (typeof StatusCodes)[keyof typeof StatusCodes];

export class CodedError extends Error {
constructor(public code: StatusCodeNumber | number, message: string, public errorCode?: string) {
constructor(public code: StatusCodeNumber, message: string, public errorCode?: string) {
super(message);
}

Expand Down
29 changes: 0 additions & 29 deletions src/utils/tezos.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
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 { getMarketsBySymbols } from './coingecko';
import { isDefined } from './helpers';
import logger from './logger';
import SingleQueryDataProvider from './SingleQueryDataProvider';
import { BcdTokenData } from './tzkt';

const RPC_URL = process.env.RPC_URL ?? 'https://mainnet-node.madfish.solutions';
Expand Down Expand Up @@ -54,30 +49,6 @@ export const getStorage = memoizee(
{ promise: true, maxAge: 30000 }
);

const getTezExchangeRate = async () => {
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(60000, getTezExchangeRate);

export class MetadataParseError extends Error {}

export const getTokenMetadata = memoizee(
Expand Down
2 changes: 1 addition & 1 deletion src/utils/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { BigNumber } from 'bignumber.js';

import { redisClient } from '../redis';
import { tezExchangeRateProvider } from './coingecko';
import { isDefined } from './helpers';
import logger from './logger';
import SingleQueryDataProvider, { SingleQueryDataProviderState } from './SingleQueryDataProvider';
import { tezExchangeRateProvider } from './tezos';
import {
getThreeRouteExchangeRates,
getThreeRouteTokens,
Expand Down

0 comments on commit f6c44aa

Please sign in to comment.