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

TW-1096: Add new Alice-Bob exchange pairs #116

Merged
merged 3 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 12 additions & 29 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
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';
Expand Down Expand Up @@ -149,7 +149,7 @@
await redisClient.lpush('notifications', JSON.stringify(newNotification));

res.status(200).send({ message: 'Notification added successfully' });
} catch (error: any) {

Check warning on line 152 in src/index.ts

View workflow job for this annotation

GitHub Actions / Checks if ts and lint works

Unexpected any. Specify a different type
res.status(500).send({ error: error.message });
}
});
Expand Down Expand Up @@ -191,8 +191,6 @@
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 +205,19 @@
});

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) {
Expand All @@ -242,13 +239,13 @@
}
});

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);
Expand All @@ -269,13 +266,12 @@
});

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);
Expand All @@ -288,19 +284,8 @@
});

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 +296,6 @@
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 +304,6 @@
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,34 @@ 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';

export const getAliceBobPairsInfo = async (isWithdraw = false) => {
const { signature, now } = getAliceBobSignature();

const response = await aliceBobApi.get<AliceBobPairInfo>('/get-pair-info/' + pair, {
const { data } = await aliceBobApi.get<AliceBobPairInfo[]>('/get-pairs-info', {
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[] = [];

let maxAmount = response.data.maxamount;
for (let i = 0; i < pairsInfo.length; i++) {
const currentPair = pairsInfo[i];

const [maxAmountString, currencyCode] = currentPair.maxamount.split(' ');
let maxAmount = Number(maxAmountString);

if (isWithdraw === false)
try {
await estimateAliceBobOutput({
from: 'CARDUAH',
from: currentPair.from,
to: 'TEZ',
fromAmount: maxAmount
});
Expand All @@ -40,5 +48,11 @@ export const getAliceBobPairInfo = async (isWithdraw = false) => {
}
}

return { minAmount: response.data.minamount, maxAmount };
finalPairsInfo.push({
...currentPair,
maxamount: `${maxAmount} ${currencyCode}`
});
}

return finalPairsInfo;
};
Loading