From 1ce120fda008f0a7edbd60b69fa024526ea26c78 Mon Sep 17 00:00:00 2001 From: lendihop Date: Thu, 31 Aug 2023 21:56:42 +0200 Subject: [PATCH 1/7] added tkey and others --- src/utils/gecko-tokens.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/utils/gecko-tokens.ts b/src/utils/gecko-tokens.ts index 391fa71..02db43b 100644 --- a/src/utils/gecko-tokens.ts +++ b/src/utils/gecko-tokens.ts @@ -24,5 +24,8 @@ export const coinGeckoTokens = { 'unobtanium-tezos': 'KT1ErKVqEhG9jxXgUG2KGLW3bNM7zXHX8SDF_0', 'wrap-governance-token': 'KT1LRboPna9yQY9BrjtQYDS1DVxhKESK4VVd_0', 'youves-you-governance': 'KT1Xobej4mc6XgEjDoJoHtTKgbD1ELMvcQuL_0', - 'quipuswap-governance-token': 'KT193D4vozYnhGJQVtw7CoxxqphqUEEwK6Vb_0' + 'quipuswap-governance-token': 'KT193D4vozYnhGJQVtw7CoxxqphqUEEwK6Vb_0', + 'wbtc-plenty-bridge': 'KT1UsSfaXyqcjSVPeiD7U1bWgKy3taYN7NWY_1', + 'weth-plenty-bridge': 'KT1CNyTPmBJ5hcqDPbPkFtoe76LifXyHUvqc_1', + 'temple-key': 'KT1VaEsVNiBoA56eToEK6n6BcPgh1tdx9eXi_0' }; From 61a3294edc77412f4641f62118a4c4013ede9e41 Mon Sep 17 00:00:00 2001 From: lendihop Date: Fri, 29 Sep 2023 21:13:00 +0200 Subject: [PATCH 2/7] pairs added --- src/index.ts | 42 +++++--------- src/interfaces/alice-bob.interfaces.ts | 14 ++++- src/utils/alice-bob/create-alice-bob-order.ts | 2 +- .../alice-bob/get-alice-bob-pair-info.ts | 44 -------------- .../alice-bob/get-alice-bob-pairs-info.ts | 58 +++++++++++++++++++ 5 files changed, 84 insertions(+), 76 deletions(-) delete mode 100644 src/utils/alice-bob/get-alice-bob-pair-info.ts create mode 100644 src/utils/alice-bob/get-alice-bob-pairs-info.ts diff --git a/src/index.ts b/src/index.ts index ac0c5cd..faed72b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,7 +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'; import logger from './utils/logger'; @@ -191,8 +191,6 @@ app.get('/api/exchange-rates', async (_req, res) => { app.get('/api/moonpay-sign', async (_req, res) => { try { const url = _req.query.url; - console.log('url: ', url); - console.log('url: type', typeof url); if (typeof url === 'string') { const signedUrl = getSignedMoonPayUrl(url); @@ -207,20 +205,19 @@ app.get('/api/moonpay-sign', async (_req, res) => { }); app.post('/api/alice-bob/create-order', async (_req, res) => { - const { isWithdraw, amount, userId, walletAddress, cardNumber } = _req.query; - const booleanIsWithdraw = isWithdraw === 'true'; + const { amount, from, to, userId, walletAddress, cardNumber } = _req.query; try { const exchangeInfo = { - from: booleanIsWithdraw ? 'TEZ' : 'CARDUAH', - to: booleanIsWithdraw ? 'CARDUAH' : 'TEZ', + from: String(from), + to: String(to), fromAmount: Number(amount), userId: String(userId), - toPaymentDetails: booleanIsWithdraw ? String(cardNumber) : String(walletAddress), + toPaymentDetails: isDefined(cardNumber) ? String(cardNumber) : String(walletAddress), redirectUrl: 'https://templewallet.com/mobile' }; - const orderInfo = await createAliceBobOrder(booleanIsWithdraw, exchangeInfo); + const orderInfo = await createAliceBobOrder(exchangeInfo); res.status(200).send({ orderInfo }); } catch (error) { @@ -242,13 +239,13 @@ app.post('/api/alice-bob/cancel-order', async (_req, res) => { } }); -app.get('/api/alice-bob/get-pair-info', async (_req, res) => { +app.get('/api/alice-bob/get-pairs-info', async (_req, res) => { const { isWithdraw } = _req.query; try { - const pairInfo = await getAliceBobPairInfo(isWithdraw === 'true'); + const pairsInfo = await getAliceBobPairsInfo(isWithdraw === 'true'); - res.status(200).send({ pairInfo }); + res.status(200).send({ pairsInfo }); } catch (error) { const { status, data } = getExternalApiErrorPayload(error); res.status(status).send(data); @@ -269,16 +266,16 @@ app.get('/api/alice-bob/check-order', async (_req, res) => { }); app.post('/api/alice-bob/estimate-amount', async (_req, res) => { - const { isWithdraw, amount } = _req.query; - const booleanIsWithdraw = isWithdraw === 'true'; + const { amount, from, to } = _req.query; try { const exchangeInfo = { - from: booleanIsWithdraw ? 'TEZ' : 'CARDUAH', - to: booleanIsWithdraw ? 'CARDUAH' : 'TEZ', + from: String(from), + to: String(to), fromAmount: Number(amount) }; const outputAmount = await estimateAliceBobOutput(exchangeInfo); + console.log(outputAmount, 'amount'); res.status(200).send({ outputAmount }); } catch (error) { @@ -288,19 +285,8 @@ app.post('/api/alice-bob/estimate-amount', async (_req, res) => { }); app.get('/api/mobile-check', async (_req, res) => { - console.log(1); - console.log('androidAppId', process.env.ANDROID_APP_ID); - console.log('iosAppId', process.env.IOS_APP_ID); - const platform = _req.query.platform; const appCheckToken = _req.query.appCheckToken; - console.log('token', appCheckToken); - - console.log(1); - console.log('androidAppId', process.env.ANDROID_APP_ID); - console.log('iosAppId', process.env.IOS_APP_ID); - - console.log('A123', platform, appCheckToken); if (!Boolean(appCheckToken) || appCheckToken === undefined) { return res.status(400).send({ error: 'App Check token is not defined' }); @@ -311,7 +297,6 @@ app.get('/api/mobile-check', async (_req, res) => { await iosApp.appCheck().verifyToken(appCheckToken as unknown as string); } else { await androidApp.appCheck().verifyToken(appCheckToken as unknown as string); - console.log('verification successful'); } res.status(200).send({ @@ -320,7 +305,6 @@ app.get('/api/mobile-check', async (_req, res) => { isAppCheckFailed: false }); } catch (err) { - console.log('err', err); res.status(200).send({ minIosVersion: MIN_IOS_APP_VERSION, minAndroidVersion: MIN_ANDROID_APP_VERSION, diff --git a/src/interfaces/alice-bob.interfaces.ts b/src/interfaces/alice-bob.interfaces.ts index eecba5b..7cee050 100644 --- a/src/interfaces/alice-bob.interfaces.ts +++ b/src/interfaces/alice-bob.interfaces.ts @@ -32,8 +32,18 @@ export interface aliceBobOrder { } export interface AliceBobPairInfo { - minamount: number; - maxamount: number; + from: string; + to: string; + fromnetwork: string | null; + tonetwork: string | null; + in: string; + out: string; + ratetype: string; + amount: string; + tofee: string; + fromfee: string; + minamount: string; + maxamount: string; } export interface AliceBobEstimateAmountPayload { diff --git a/src/utils/alice-bob/create-alice-bob-order.ts b/src/utils/alice-bob/create-alice-bob-order.ts index d97999c..536d431 100644 --- a/src/utils/alice-bob/create-alice-bob-order.ts +++ b/src/utils/alice-bob/create-alice-bob-order.ts @@ -3,7 +3,7 @@ import { aliceBobApi } from '../api.sevice'; import { getAliceBobRequestHeaders } from './get-alice-bob-request-headers'; import { getAliceBobSignature } from './get-alice-bob-signature'; -export const createAliceBobOrder = async (isWithdraw: boolean, payload: AliceBobCreateOrderPayload) => { +export const createAliceBobOrder = async (payload: AliceBobCreateOrderPayload) => { const { signature, now } = getAliceBobSignature(payload); const response = await aliceBobApi.post('/create-order', payload, { diff --git a/src/utils/alice-bob/get-alice-bob-pair-info.ts b/src/utils/alice-bob/get-alice-bob-pair-info.ts deleted file mode 100644 index 35fd0bf..0000000 --- a/src/utils/alice-bob/get-alice-bob-pair-info.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { AxiosError } from 'axios'; - -import { AliceBobPairInfo } from '../../interfaces/alice-bob.interfaces'; -import { aliceBobApi } from '../api.sevice'; -import { estimateAliceBobOutput } from './estimate-alice-bob-output'; -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 response = await aliceBobApi.get('/get-pair-info/' + pair, { - headers: getAliceBobRequestHeaders(signature, now) - }); - - /* - Output estimation at AliceBob errors later with `maxAmount` used as input amount. - Double-checking here, to have a valid `maxAmount` value. - */ - - let maxAmount = response.data.maxamount; - - if (isWithdraw === false) - try { - await estimateAliceBobOutput({ - from: 'CARDUAH', - to: 'TEZ', - fromAmount: maxAmount - }); - } catch (error) { - if ( - error instanceof AxiosError && - error.response?.status === 400 && - error.response.data.errorCode === 'EXCEEDING_LIMITS' - ) { - const altMaxAmount = Number(error.response.data.maxAmount); - if (Number.isFinite(altMaxAmount)) maxAmount = altMaxAmount; - } - } - - return { minAmount: response.data.minamount, maxAmount }; -}; diff --git a/src/utils/alice-bob/get-alice-bob-pairs-info.ts b/src/utils/alice-bob/get-alice-bob-pairs-info.ts new file mode 100644 index 0000000..e87d825 --- /dev/null +++ b/src/utils/alice-bob/get-alice-bob-pairs-info.ts @@ -0,0 +1,58 @@ +import { AxiosError } from 'axios'; + +import { AliceBobPairInfo } from '../../interfaces/alice-bob.interfaces'; +import { aliceBobApi } from '../api.sevice'; +import { estimateAliceBobOutput } from './estimate-alice-bob-output'; +import { getAliceBobRequestHeaders } from './get-alice-bob-request-headers'; +import { getAliceBobSignature } from './get-alice-bob-signature'; + +export const getAliceBobPairsInfo = async (isWithdraw = false) => { + const { signature, now } = getAliceBobSignature(); + + const { data } = await aliceBobApi.get('/get-pairs-info', { + headers: getAliceBobRequestHeaders(signature, now) + }); + + const pairsInfo = data.filter(pair => (isWithdraw ? pair.from === 'TEZ' : pair.from !== 'TEZ')); + + /* + Output estimation at AliceBob errors later with `maxAmount` used as input amount. + Double-checking here, to have a valid `maxAmount` value. + */ + if (!isWithdraw) { + const finalPairsInfo: AliceBobPairInfo[] = []; + + for (let i = 0; i < pairsInfo.length; i++) { + const currentPair = pairsInfo[i]; + + const [maxAmountString, currencyCode] = currentPair.maxamount.split(' '); + let maxAmount = Number(maxAmountString); + + try { + await estimateAliceBobOutput({ + from: currentPair.from, + to: 'TEZ', + fromAmount: maxAmount + }); + } catch (error) { + if ( + error instanceof AxiosError && + error.response?.status === 400 && + error.response.data.errorCode === 'EXCEEDING_LIMITS' + ) { + const altMaxAmount = Number(error.response.data.maxAmount); + if (Number.isFinite(altMaxAmount)) maxAmount = altMaxAmount; + } + } + + finalPairsInfo.push({ + ...currentPair, + maxamount: `${maxAmount} ${currencyCode}` + }); + } + + return finalPairsInfo; + } + + return pairsInfo; +}; From 2ab75fbc873099ab23756039098430b5ea4bc96e Mon Sep 17 00:00:00 2001 From: lendihop Date: Mon, 2 Oct 2023 10:52:28 +0200 Subject: [PATCH 3/7] small refactor --- .../alice-bob/get-alice-bob-pairs-info.ts | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/utils/alice-bob/get-alice-bob-pairs-info.ts b/src/utils/alice-bob/get-alice-bob-pairs-info.ts index e87d825..c00b226 100644 --- a/src/utils/alice-bob/get-alice-bob-pairs-info.ts +++ b/src/utils/alice-bob/get-alice-bob-pairs-info.ts @@ -15,44 +15,44 @@ export const getAliceBobPairsInfo = async (isWithdraw = false) => { const pairsInfo = data.filter(pair => (isWithdraw ? pair.from === 'TEZ' : pair.from !== 'TEZ')); + if (isWithdraw) { + return pairsInfo; + } + /* Output estimation at AliceBob errors later with `maxAmount` used as input amount. Double-checking here, to have a valid `maxAmount` value. */ - if (!isWithdraw) { - const finalPairsInfo: AliceBobPairInfo[] = []; - - for (let i = 0; i < pairsInfo.length; i++) { - const currentPair = pairsInfo[i]; - - const [maxAmountString, currencyCode] = currentPair.maxamount.split(' '); - let maxAmount = Number(maxAmountString); - - try { - await estimateAliceBobOutput({ - from: currentPair.from, - to: 'TEZ', - fromAmount: maxAmount - }); - } catch (error) { - if ( - error instanceof AxiosError && - error.response?.status === 400 && - error.response.data.errorCode === 'EXCEEDING_LIMITS' - ) { - const altMaxAmount = Number(error.response.data.maxAmount); - if (Number.isFinite(altMaxAmount)) maxAmount = altMaxAmount; - } - } + const finalPairsInfo: AliceBobPairInfo[] = []; - finalPairsInfo.push({ - ...currentPair, - maxamount: `${maxAmount} ${currencyCode}` + for (let i = 0; i < pairsInfo.length; i++) { + const currentPair = pairsInfo[i]; + + const [maxAmountString, currencyCode] = currentPair.maxamount.split(' '); + let maxAmount = Number(maxAmountString); + + try { + await estimateAliceBobOutput({ + from: currentPair.from, + to: 'TEZ', + fromAmount: maxAmount }); + } catch (error) { + if ( + error instanceof AxiosError && + error.response?.status === 400 && + error.response.data.errorCode === 'EXCEEDING_LIMITS' + ) { + const altMaxAmount = Number(error.response.data.maxAmount); + if (Number.isFinite(altMaxAmount)) maxAmount = altMaxAmount; + } } - return finalPairsInfo; + finalPairsInfo.push({ + ...currentPair, + maxamount: `${maxAmount} ${currencyCode}` + }); } - return pairsInfo; + return finalPairsInfo; }; From 5ffb644d9762d041618622b2782796b7e08a282b Mon Sep 17 00:00:00 2001 From: lendihop Date: Mon, 2 Oct 2023 11:23:47 +0200 Subject: [PATCH 4/7] log removed --- src/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index faed72b..34fa222 100644 --- a/src/index.ts +++ b/src/index.ts @@ -275,7 +275,6 @@ app.post('/api/alice-bob/estimate-amount', async (_req, res) => { fromAmount: Number(amount) }; const outputAmount = await estimateAliceBobOutput(exchangeInfo); - console.log(outputAmount, 'amount'); res.status(200).send({ outputAmount }); } catch (error) { From 5e11c3a40c43c7f6733c07b496712e9f0e11ad3a Mon Sep 17 00:00:00 2001 From: lendihop Date: Tue, 3 Oct 2023 11:08:01 +0200 Subject: [PATCH 5/7] double check removed --- .../alice-bob/get-alice-bob-pairs-info.ts | 46 +------------------ 1 file changed, 1 insertion(+), 45 deletions(-) diff --git a/src/utils/alice-bob/get-alice-bob-pairs-info.ts b/src/utils/alice-bob/get-alice-bob-pairs-info.ts index c00b226..ebe05da 100644 --- a/src/utils/alice-bob/get-alice-bob-pairs-info.ts +++ b/src/utils/alice-bob/get-alice-bob-pairs-info.ts @@ -1,8 +1,5 @@ -import { AxiosError } from 'axios'; - import { AliceBobPairInfo } from '../../interfaces/alice-bob.interfaces'; import { aliceBobApi } from '../api.sevice'; -import { estimateAliceBobOutput } from './estimate-alice-bob-output'; import { getAliceBobRequestHeaders } from './get-alice-bob-request-headers'; import { getAliceBobSignature } from './get-alice-bob-signature'; @@ -13,46 +10,5 @@ export const getAliceBobPairsInfo = async (isWithdraw = false) => { headers: getAliceBobRequestHeaders(signature, now) }); - const pairsInfo = data.filter(pair => (isWithdraw ? pair.from === 'TEZ' : pair.from !== 'TEZ')); - - if (isWithdraw) { - return pairsInfo; - } - - /* - Output estimation at AliceBob errors later with `maxAmount` used as input amount. - Double-checking here, to have a valid `maxAmount` value. - */ - const finalPairsInfo: AliceBobPairInfo[] = []; - - for (let i = 0; i < pairsInfo.length; i++) { - const currentPair = pairsInfo[i]; - - const [maxAmountString, currencyCode] = currentPair.maxamount.split(' '); - let maxAmount = Number(maxAmountString); - - try { - await estimateAliceBobOutput({ - from: currentPair.from, - to: 'TEZ', - fromAmount: maxAmount - }); - } catch (error) { - if ( - error instanceof AxiosError && - error.response?.status === 400 && - error.response.data.errorCode === 'EXCEEDING_LIMITS' - ) { - const altMaxAmount = Number(error.response.data.maxAmount); - if (Number.isFinite(altMaxAmount)) maxAmount = altMaxAmount; - } - } - - finalPairsInfo.push({ - ...currentPair, - maxamount: `${maxAmount} ${currencyCode}` - }); - } - - return finalPairsInfo; + return data.filter(pair => (isWithdraw ? pair.from === 'TEZ' : pair.from !== 'TEZ')); }; From bc93f544758cd005edbdd143d302938c14337854 Mon Sep 17 00:00:00 2001 From: lendihop Date: Wed, 4 Oct 2023 12:20:23 +0200 Subject: [PATCH 6/7] 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 7/7] 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) + }; +};