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 latest changes to master #119

Merged
merged 11 commits into from
Oct 11, 2023
55 changes: 24 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ 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';
import { coinGeckoTokens } from './utils/gecko-tokens';
import { getExternalApiErrorPayload, isDefined, isNonEmptyString } from './utils/helpers';
import logger from './utils/logger';
Expand Down Expand Up @@ -191,8 +193,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);
Expand All @@ -207,20 +207,17 @@ 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 { isWithdraw, amount, from, to, userId, walletAddress, cardNumber } = _req.query;

try {
const exchangeInfo = {
from: booleanIsWithdraw ? 'TEZ' : 'CARDUAH',
to: booleanIsWithdraw ? 'CARDUAH' : 'TEZ',
fromAmount: Number(amount),
const payload = {
...getAliceBobEstimationPayload(isWithdraw, from, to, 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(payload);

res.status(200).send({ orderInfo });
} catch (error) {
Expand Down Expand Up @@ -255,6 +252,19 @@ 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 pairsInfo = await getAliceBobPairsInfo(isWithdraw === 'true');

res.status(200).send({ pairsInfo });
} catch (error) {
const { status, data } = getExternalApiErrorPayload(error);
res.status(status).send(data);
}
});

app.get('/api/alice-bob/check-order', async (_req, res) => {
const { orderId } = _req.query;

Expand All @@ -269,16 +279,12 @@ 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 { isWithdraw, amount, from, to } = _req.query;

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

const outputAmount = await estimateAliceBobOutput(payload);

res.status(200).send({ outputAmount });
} catch (error) {
Expand All @@ -288,19 +294,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' });
Expand All @@ -311,7 +306,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({
Expand All @@ -320,7 +314,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,
Expand Down
14 changes: 12 additions & 2 deletions src/interfaces/alice-bob.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/alice-bob/create-alice-bob-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<aliceBobOrder>('/create-order', payload, {
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)
};
};
33 changes: 2 additions & 31 deletions src/utils/alice-bob/get-alice-bob-pair-info.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
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';

Expand All @@ -11,34 +7,9 @@ export const getAliceBobPairInfo = async (isWithdraw = false) => {

const { signature, now } = getAliceBobSignature();

const response = await aliceBobApi.get<AliceBobPairInfo>('/get-pair-info/' + pair, {
const { data } = await aliceBobApi.get<{ minamount: number; maxamount: number }>('/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 };
return { minAmount: data.minamount, maxAmount: data.maxamount };
};
14 changes: 14 additions & 0 deletions src/utils/alice-bob/get-alice-bob-pairs-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { AliceBobPairInfo } from '../../interfaces/alice-bob.interfaces';
import { aliceBobApi } from '../api.sevice';
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<AliceBobPairInfo[]>('/get-pairs-info', {
headers: getAliceBobRequestHeaders(signature, now)
});

return data.filter(pair => (isWithdraw ? pair.from === 'TEZ' : pair.from !== 'TEZ'));
};
5 changes: 4 additions & 1 deletion src/utils/gecko-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
};