Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lendihop committed Oct 4, 2023
1 parent bc93f54 commit bddbcb5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { getABData } from './utils/ab-test';
import { cancelAliceBobOrder } from './utils/alice-bob/cancel-alice-bob-order';
import { createAliceBobOrder } from './utils/alice-bob/create-alice-bob-order';
import { estimateAliceBobOutput } from './utils/alice-bob/estimate-alice-bob-output';
import { getAliceBobEstimationPayload } from './utils/alice-bob/get-alice-bob-estimation-payload';
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';
Expand Down Expand Up @@ -207,19 +208,16 @@ app.get('/api/moonpay-sign', async (_req, res) => {

app.post('/api/alice-bob/create-order', async (_req, res) => {
const { isWithdraw, amount, from, to, userId, walletAddress, cardNumber } = _req.query;
const booleanIsWithdraw = isWithdraw === 'true';

try {
const exchangeInfo = {
from: isDefined(isWithdraw) ? (booleanIsWithdraw ? 'TEZ' : 'CARDUAH') : String(from),
to: isDefined(isWithdraw) ? (booleanIsWithdraw ? 'CARDUAH' : 'TEZ') : String(to),
fromAmount: Number(amount),
const payload = {
...getAliceBobEstimationPayload(isWithdraw, from, to, amount),
userId: String(userId),
toPaymentDetails: isDefined(cardNumber) ? String(cardNumber) : String(walletAddress),
redirectUrl: 'https://templewallet.com/mobile'
};

const orderInfo = await createAliceBobOrder(exchangeInfo);
const orderInfo = await createAliceBobOrder(payload);

res.status(200).send({ orderInfo });
} catch (error) {
Expand Down Expand Up @@ -282,16 +280,11 @@ app.get('/api/alice-bob/check-order', async (_req, res) => {

app.post('/api/alice-bob/estimate-amount', async (_req, res) => {
const { isWithdraw, amount, from, to } = _req.query;
const booleanIsWithdraw = isWithdraw === 'true';

try {
const exchangeInfo = {
from: isDefined(isWithdraw) ? (booleanIsWithdraw ? 'TEZ' : 'CARDUAH') : String(from),
to: isDefined(isWithdraw) ? (booleanIsWithdraw ? 'CARDUAH' : 'TEZ') : String(to),
fromAmount: Number(amount)
};
const payload = getAliceBobEstimationPayload(isWithdraw, from, to, amount);

const outputAmount = await estimateAliceBobOutput(exchangeInfo);
const outputAmount = await estimateAliceBobOutput(payload);

res.status(200).send({ outputAmount });
} catch (error) {
Expand Down
20 changes: 20 additions & 0 deletions src/utils/alice-bob/get-alice-bob-estimation-payload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ParsedQs } from 'qs';

import { isDefined } from '../helpers';

type QueryParam = string | ParsedQs | string[] | ParsedQs[] | undefined;

export const getAliceBobEstimationPayload = (
isWithdraw: QueryParam,
from: QueryParam,
to: QueryParam,
amount: QueryParam
) => {
const booleanIsWithdraw = isWithdraw === 'true';

return {
from: isDefined(isWithdraw) ? (booleanIsWithdraw ? 'TEZ' : 'CARDUAH') : String(from),
to: isDefined(isWithdraw) ? (booleanIsWithdraw ? 'CARDUAH' : 'TEZ') : String(to),
fromAmount: Number(amount)
};
};

0 comments on commit bddbcb5

Please sign in to comment.