From bc93f544758cd005edbdd143d302938c14337854 Mon Sep 17 00:00:00 2001 From: lendihop Date: Wed, 4 Oct 2023 12:20:23 +0200 Subject: [PATCH 1/2] reverse compatibility of Alice Bob endpoints --- src/index.ts | 29 +++++++++++++++---- .../alice-bob/get-alice-bob-pair-info.ts | 15 ++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 src/utils/alice-bob/get-alice-bob-pair-info.ts diff --git a/src/index.ts b/src/index.ts index 34fa222..c432a06 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,6 +22,7 @@ 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 { 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 { coinGeckoTokens } from './utils/gecko-tokens'; import { getExternalApiErrorPayload, isDefined, isNonEmptyString } from './utils/helpers'; @@ -205,12 +206,13 @@ app.get('/api/moonpay-sign', async (_req, res) => { }); app.post('/api/alice-bob/create-order', async (_req, res) => { - const { amount, from, to, userId, walletAddress, cardNumber } = _req.query; + const { isWithdraw, amount, from, to, userId, walletAddress, cardNumber } = _req.query; + const booleanIsWithdraw = isWithdraw === 'true'; try { const exchangeInfo = { - from: String(from), - to: String(to), + from: isDefined(isWithdraw) ? (booleanIsWithdraw ? 'TEZ' : 'CARDUAH') : String(from), + to: isDefined(isWithdraw) ? (booleanIsWithdraw ? 'CARDUAH' : 'TEZ') : String(to), fromAmount: Number(amount), userId: String(userId), toPaymentDetails: isDefined(cardNumber) ? String(cardNumber) : String(walletAddress), @@ -239,6 +241,19 @@ app.post('/api/alice-bob/cancel-order', async (_req, res) => { } }); +app.get('/api/alice-bob/get-pair-info', async (_req, res) => { + const { isWithdraw } = _req.query; + + try { + const pairInfo = await getAliceBobPairInfo(isWithdraw === 'true'); + + res.status(200).send({ pairInfo }); + } catch (error) { + const { status, data } = getExternalApiErrorPayload(error); + res.status(status).send(data); + } +}); + app.get('/api/alice-bob/get-pairs-info', async (_req, res) => { const { isWithdraw } = _req.query; @@ -266,14 +281,16 @@ app.get('/api/alice-bob/check-order', async (_req, res) => { }); app.post('/api/alice-bob/estimate-amount', async (_req, res) => { - const { amount, from, to } = _req.query; + const { isWithdraw, amount, from, to } = _req.query; + const booleanIsWithdraw = isWithdraw === 'true'; try { const exchangeInfo = { - from: String(from), - to: String(to), + from: isDefined(isWithdraw) ? (booleanIsWithdraw ? 'TEZ' : 'CARDUAH') : String(from), + to: isDefined(isWithdraw) ? (booleanIsWithdraw ? 'CARDUAH' : 'TEZ') : String(to), fromAmount: Number(amount) }; + const outputAmount = await estimateAliceBobOutput(exchangeInfo); res.status(200).send({ outputAmount }); diff --git a/src/utils/alice-bob/get-alice-bob-pair-info.ts b/src/utils/alice-bob/get-alice-bob-pair-info.ts new file mode 100644 index 0000000..87959c3 --- /dev/null +++ b/src/utils/alice-bob/get-alice-bob-pair-info.ts @@ -0,0 +1,15 @@ +import { aliceBobApi } from '../api.sevice'; +import { getAliceBobRequestHeaders } from './get-alice-bob-request-headers'; +import { getAliceBobSignature } from './get-alice-bob-signature'; + +export const getAliceBobPairInfo = async (isWithdraw = false) => { + const pair = isWithdraw ? 'TEZ/CARDUAH' : 'CARDUAH/TEZ'; + + const { signature, now } = getAliceBobSignature(); + + const { data } = await aliceBobApi.get<{ minamount: number; maxamount: number }>('/get-pair-info/' + pair, { + headers: getAliceBobRequestHeaders(signature, now) + }); + + return { minAmount: data.minamount, maxAmount: data.maxamount }; +}; From bddbcb58429be564e1ffe969b61d9b3180d35cb1 Mon Sep 17 00:00:00 2001 From: lendihop Date: Wed, 4 Oct 2023 12:42:57 +0200 Subject: [PATCH 2/2] refactor --- src/index.ts | 19 ++++++------------ .../get-alice-bob-estimation-payload.ts | 20 +++++++++++++++++++ 2 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 src/utils/alice-bob/get-alice-bob-estimation-payload.ts diff --git a/src/index.ts b/src/index.ts index c432a06..c3c5826 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; @@ -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) { @@ -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) { diff --git a/src/utils/alice-bob/get-alice-bob-estimation-payload.ts b/src/utils/alice-bob/get-alice-bob-estimation-payload.ts new file mode 100644 index 0000000..d1223ba --- /dev/null +++ b/src/utils/alice-bob/get-alice-bob-estimation-payload.ts @@ -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) + }; +};