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

feat: initial binance connect integration #541

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions packages/app-support-buy-sell/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
root: true,
extends: ['@cypherock/eslint-config/browser'],
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.eslint.json'],
},
};
3 changes: 3 additions & 0 deletions packages/app-support-buy-sell/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.turbo
dist
coverage
1 change: 1 addition & 0 deletions packages/app-support-buy-sell/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"@cypherock/prettier-config"
3 changes: 3 additions & 0 deletions packages/app-support-buy-sell/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# App Support Buy/Sell

- All coin operations related to Buy/Sell
5 changes: 5 additions & 0 deletions packages/app-support-buy-sell/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const baseConfig = require('@cypherock/jest-config/node');

module.exports = {
...baseConfig,
};
57 changes: 57 additions & 0 deletions packages/app-support-buy-sell/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"name": "@cypherock/app-support-buy-sell",
"version": "0.0.0",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
"license": "AGPL-3.0",
"private": true,
"scripts": {
"lint": "eslint --ext .ts,tsx,js,jsx src/ --fix",
"lint:check": "eslint --ext .ts,tsx,js,jsx src/",
"pretty": "prettier --write \"src/**/*.ts?(x)\"",
"pretty:check": "prettier --check \"src/**/*.ts?(x)\"",
"build": "rimraf dist && pnpm build:esm && pnpm build:cjs",
"build:cjs": "tsc -p tsconfig_cjs.json",
"build:esm": "tsc -p tsconfig.json",
"build:dirty": "pnpm build:esm",
"pre-commit": "lint-staged"
},
"devDependencies": {
"@cypherock/eslint-config": "workspace:^",
"@cypherock/jest-config": "workspace:^",
"@cypherock/prettier-config": "workspace:^",
"@cypherock/tsconfig": "workspace:^",
"@jest/globals": "^29.5.0",
"@stryker-mutator/core": "^7.0.2",
"@stryker-mutator/jest-runner": "^7.0.2",
"@stryker-mutator/typescript-checker": "^7.0.2",
"@types/jest": "^29.5.2",
"@types/lodash": "^4.14.195",
"@types/node": "18.15.11",
"eslint": "^8.43.0",
"jest": "^29.5.0",
"lint-staged": "^13.2.2",
"prettier": "^2.8.8",
"rimraf": "^5.0.1",
"ts-jest": "^29.1.0",
"typescript": "^4.9.5"
},
"dependencies": {
"@cypherock/coins": "workspace:^",
"@cypherock/cysync-interfaces": "workspace:^",
"@cypherock/cysync-utils": "workspace:^",
"@cypherock/db-interfaces": "workspace:^",
"lodash": "^4.17.21",
"zod": "^3.21.4"
},
"lint-staged": {
"*.{ts,tsx}": [
"eslint --ext ts,tsx --quiet --fix --",
"prettier --write"
],
"*.{js,jsx,md,mdx,mjs,yml,yaml,css,json}": [
"prettier --write"
]
}
}
5 changes: 5 additions & 0 deletions packages/app-support-buy-sell/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { getEnvVariable } from '@cypherock/cysync-utils';

export const config = {
API_CYPHEROCK: getEnvVariable('API_CYPHEROCK', 'https://api.cypherock.com'),
};
28 changes: 28 additions & 0 deletions packages/app-support-buy-sell/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-disable class-methods-use-this */
import * as operations from './operations';

export { updateLogger } from './utils/logger';

export * from './operations/types';

export class BuySellSupport {
public getTradingPairs() {
return operations.getTradingPairs();
}

public getEstimatedQuote(params: operations.IGetEstimatedQuoteParams) {
return operations.getEstimatedQuote(params);
}

public getPaymentMethods(params: operations.IGetPaymentMethodsParams) {
return operations.getPaymentMethods(params);
}

public preorder(params: operations.IPreorderParams) {
return operations.preorder(params);
}

public getOrderDetails(params: operations.IGetOrderDetailsParams) {
return operations.getOrderDetails(params);
}
}
16 changes: 16 additions & 0 deletions packages/app-support-buy-sell/src/operations/commonTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ICoinInfo, IFiatCurrency } from '@cypherock/coins';

export interface ISupportedCryptoCurrency {
coin: ICoinInfo;
cryptoCurrency: string;
network: string;
withdrawFee?: string;
withdrawMinAmount?: string;
withdrawMaxAmount?: string;
contractAddress?: string;
}

export interface ISupportedFiatCurrency {
currency: IFiatCurrency;
code: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { assert } from '@cypherock/cysync-utils';

import { IEstimatedQuote, IGetEstimatedQuoteParams } from './types';

import { binanceService } from '../../services';

export * from './types';

export const getEstimatedQuote = async (
params: IGetEstimatedQuoteParams,
): Promise<IEstimatedQuote> => {
assert(
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
params.cryptoAmount || params.fiatAmount,
'Either cryptoAmount or fiatAmount must be provided',
);

const quote = await binanceService.getEstimatedQuote({
requestedAmount: params.fiatAmount ?? params.cryptoAmount ?? '',
fiatCurrency: params.fiatCurrency.code,
cryptoCurrency: params.cryptoCurrency.cryptoCurrency,
network: params.cryptoCurrency.network,
amountType: params.fiatAmount ? 1 : 2,
});

return quote;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { BinanceGetEstimatedQuoteResponse } from '../../services';
import {
ISupportedFiatCurrency,
ISupportedCryptoCurrency,
} from '../commonTypes';

export interface IGetEstimatedQuoteParams {
fiatCurrency: ISupportedFiatCurrency;
cryptoCurrency: ISupportedCryptoCurrency;
fiatAmount?: string;
cryptoAmount?: string;
}

export type IEstimatedQuote = BinanceGetEstimatedQuoteResponse['data'];
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IGetOrderDetailsParams, IGetOrderDetailsResult } from './types';

import { binanceService } from '../../services';

export * from './types';

export const getOrderDetails = async (
params: IGetOrderDetailsParams,
): Promise<IGetOrderDetailsResult> => {
const result = await binanceService.getOrderDetails(params);

return result;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { BinanceGetOrderDetailsResponse } from '../../services';

export interface IGetOrderDetailsParams {
orderId: string;
language?: string;
}

export type IGetOrderDetailsResult = BinanceGetOrderDetailsResponse['data'];
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { assert } from '@cypherock/cysync-utils';

import { IGetPaymentMethodsParams, IPaymentMethod } from './types';

import { binanceService } from '../../services';

export * from './types';

export const getPaymentMethods = async (
params: IGetPaymentMethodsParams,
): Promise<IPaymentMethod[]> => {
assert(
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
params.cryptoAmount || params.fiatAmount,
'Either cryptoAmount or fiatAmount must be provided',
);

const result = await binanceService.getPaymentMethodList({
totalAmount: params.fiatAmount ?? params.cryptoAmount ?? '',
fiatCurrency: params.fiatCurrency.code,
cryptoCurrency: params.cryptoCurrency.cryptoCurrency,
amountType: params.fiatAmount ? 1 : 2,
language: params.language,
});

return result.paymentMethods;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { BinanceGetPaymentMethodListResponse } from '../../services';
import {
ISupportedFiatCurrency,
ISupportedCryptoCurrency,
} from '../commonTypes';

export interface IGetPaymentMethodsParams {
fiatCurrency: ISupportedFiatCurrency;
cryptoCurrency: ISupportedCryptoCurrency;
fiatAmount?: string;
cryptoAmount?: string;
language?: string;
}

export type IPaymentMethod =
BinanceGetPaymentMethodListResponse['data']['paymentMethods'][0];
139 changes: 139 additions & 0 deletions packages/app-support-buy-sell/src/operations/getTradingPairs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import {
btcCoinList,
BtcIdMap,
evmCoinList,
EvmIdMap,
ICoinInfo,
solanaCoinList,
SolanaIdMap,
tronCoinList,
TronIdMap,
fiatCurrencyList,
TokenTypes,
} from '@cypherock/coins';
import lodash from 'lodash';

import { ITradingPairs } from './types';

import { binanceService } from '../../services';
import {
ISupportedCryptoCurrency,
ISupportedFiatCurrency,
} from '../commonTypes';

export * from './types';

const networkMapping: Record<string, ICoinInfo | undefined> = {
ETH: evmCoinList[EvmIdMap.ethereum],
BSC: evmCoinList[EvmIdMap.binance],
ARBITRUM: evmCoinList[EvmIdMap.arbitrum],
AVAXC: evmCoinList[EvmIdMap.avalanche],
OPTIMISM: evmCoinList[EvmIdMap.optimism],
FTM: evmCoinList[EvmIdMap.fantom],
MATIC: evmCoinList[EvmIdMap.polygon],

BTC: btcCoinList[BtcIdMap.bitcoin],
DOGE: btcCoinList[BtcIdMap.dogecoin],
DASH: btcCoinList[BtcIdMap.dash],
LTC: btcCoinList[BtcIdMap.litecoin],

SOL: solanaCoinList[SolanaIdMap.solana],
TRX: tronCoinList[TronIdMap.tron],
};

const tokenList: Record<string, TokenTypes> = Object.fromEntries(
lodash
.concat(
...[...Object.values(evmCoinList), ...Object.values(tronCoinList)]
.filter(
c =>
window.cysyncEnv.IS_PRODUCTION === 'false' || !c.isUnderDevelopment,
)
.map(coin => Object.values(coin.tokens)),
)
.map(token => [token.address.toLowerCase(), token]),
);

export const getTradingPairs = async (): Promise<ITradingPairs> => {
const [pairs, cryptoCurrencies] = await Promise.all([
binanceService.getTradingPairs(),
binanceService.getCryptoNetworks(),
]);

const { fiatCurrencies } = pairs;

const supportedNativeCryptoCurrencies: ISupportedCryptoCurrency[] = [];
const supportedTokenCryptoCurrencies: ISupportedCryptoCurrency[] = [];

for (const currency of cryptoCurrencies) {
if (!currency.cryptoCurrency) continue;

for (const network of currency.networks) {
const chain = networkMapping[network.network];

if (!chain) continue;

if (network.network === currency.cryptoCurrency) {
supportedNativeCryptoCurrencies.push({
cryptoCurrency: currency.cryptoCurrency,
network: network.network,
coin: chain,
withdrawFee: network.withdrawFee
? network.withdrawFee.toString()
: undefined,
withdrawMinAmount: network.withdrawMinAmount
? network.withdrawMinAmount.toString()
: undefined,
withdrawMaxAmount: network.withdrawMaxAmount
? network.withdrawMaxAmount.toString()
: undefined,
contractAddress: network.contractAddress ?? undefined,
});
}

const token =
network.contractAddress &&
tokenList[network.contractAddress.toLowerCase()];

if (!token) continue;

if (network.network !== currency.cryptoCurrency) {
supportedTokenCryptoCurrencies.push({
cryptoCurrency: token.name,
network: network.network,
coin: token,
withdrawFee: network.withdrawFee
? network.withdrawFee.toString()
: undefined,
withdrawMinAmount: network.withdrawMinAmount
? network.withdrawMinAmount.toString()
: undefined,
withdrawMaxAmount: network.withdrawMaxAmount
? network.withdrawMaxAmount.toString()
: undefined,
contractAddress: network.contractAddress ?? undefined,
});
}
}
}

const supportedFiatCurrencies: ISupportedFiatCurrency[] = [];

for (const currency of fiatCurrencies) {
const currencyDetails = fiatCurrencyList[currency.toUpperCase()];
if (!currencyDetails) continue;

supportedFiatCurrencies.push({
currency: currencyDetails,
code: currency,
});
}

return {
fiatCurrencies: supportedFiatCurrencies,
cryptoCurrencies: [
...supportedNativeCryptoCurrencies,
...supportedTokenCryptoCurrencies,
],
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {
ISupportedFiatCurrency,
ISupportedCryptoCurrency,
} from '../commonTypes';

export interface ITradingPairs {
fiatCurrencies: ISupportedFiatCurrency[];
cryptoCurrencies: ISupportedCryptoCurrency[];
}
Loading
Loading