From 0fc7ffefb19affcda18f0760a242e2afacd04cd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira?= Date: Tue, 10 May 2022 11:28:21 +0100 Subject: [PATCH] feat: binance exchange support --- lib/new-admin/config/accounts.js | 4 ++- lib/new-settings-loader.js | 1 + lib/plugins/common/ccxt.js | 5 ++- lib/plugins/exchange/binance.js | 20 +++++++++++ lib/plugins/exchange/binanceus.js | 4 ++- lib/plugins/exchange/cex.js | 4 ++- lib/plugins/exchange/ftx.js | 4 ++- .../src/pages/Services/schemas/binance.js | 36 +++++++++++++++++++ .../src/pages/Services/schemas/index.js | 4 ++- package.json | 2 +- 10 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 lib/plugins/exchange/binance.js create mode 100644 new-lamassu-admin/src/pages/Services/schemas/binance.js diff --git a/lib/new-admin/config/accounts.js b/lib/new-admin/config/accounts.js index 9ab0476c..4cb267bc 100644 --- a/lib/new-admin/config/accounts.js +++ b/lib/new-admin/config/accounts.js @@ -4,7 +4,7 @@ const _ = require('lodash/fp') const { ALL } = require('../../plugins/common/ccxt') const { BTC, BCH, DASH, ETH, LTC, ZEC, XMR } = COINS -const { bitpay, coinbase, itbit, bitstamp, kraken, binanceus, cex, ftx } = ALL +const { bitpay, coinbase, itbit, bitstamp, kraken, binanceus, cex, ftx, binance } = ALL const TICKER = 'ticker' const WALLET = 'wallet' @@ -17,6 +17,7 @@ const ZERO_CONF = 'zeroConf' const WALLET_SCORING = 'wallet_scoring' const ALL_ACCOUNTS = [ + { code: 'binance', display: 'Binance', class: TICKER, cryptos: binance.CRYPTO }, { code: 'binanceus', display: 'Binance.us', class: TICKER, cryptos: binanceus.CRYPTO }, { code: 'cex', display: 'CEX', class: TICKER, cryptos: cex.CRYPTO }, { code: 'ftx', display: 'FTX', class: TICKER, cryptos: ftx.CRYPTO }, @@ -39,6 +40,7 @@ const ALL_ACCOUNTS = [ { code: 'bitstamp', display: 'Bitstamp', class: EXCHANGE, cryptos: bitstamp.CRYPTO }, { code: 'itbit', display: 'itBit', class: EXCHANGE, cryptos: itbit.CRYPTO }, { code: 'kraken', display: 'Kraken', class: EXCHANGE, cryptos: kraken.CRYPTO }, + { code: 'binance', display: 'Binance', class: EXCHANGE, cryptos: binance.CRYPTO }, { code: 'binanceus', display: 'Binance.us', class: EXCHANGE, cryptos: binanceus.CRYPTO }, { code: 'cex', display: 'CEX', class: EXCHANGE, cryptos: cex.CRYPTO }, { code: 'ftx', display: 'FTX', class: EXCHANGE, cryptos: ftx.CRYPTO }, diff --git a/lib/new-settings-loader.js b/lib/new-settings-loader.js index a9af0f30..52ffe19e 100644 --- a/lib/new-settings-loader.js +++ b/lib/new-settings-loader.js @@ -23,6 +23,7 @@ const SECRET_FIELDS = [ 'binanceus.privateKey', 'ftx.privateKey', 'cex.privateKey', + 'binance.privateKey', 'twilio.authToken' ] diff --git a/lib/plugins/common/ccxt.js b/lib/plugins/common/ccxt.js index 44038d13..41eac58e 100644 --- a/lib/plugins/common/ccxt.js +++ b/lib/plugins/common/ccxt.js @@ -8,6 +8,8 @@ const binanceus = require('../exchange/binanceus') const cex = require('../exchange/cex') const ftx = require('../exchange/ftx') const bitpay = require('../ticker/bitpay') +const binance = require('../exchange/binance') + const { BTC, BCH, DASH, ETH, LTC, ZEC } = COINS const ALL = { @@ -21,7 +23,8 @@ const ALL = { coinbase: { CRYPTO: [BTC, ETH, LTC, DASH, ZEC, BCH], FIAT: 'ALL_CURRENCIES' - } + }, + binance: binance } function buildMarket (fiatCode, cryptoCode, serviceName) { diff --git a/lib/plugins/exchange/binance.js b/lib/plugins/exchange/binance.js new file mode 100644 index 00000000..0de4c9f9 --- /dev/null +++ b/lib/plugins/exchange/binance.js @@ -0,0 +1,20 @@ +const { COINS } = require('@lamassu/coins') +const _ = require('lodash/fp') + +const { ORDER_TYPES } = require('./consts') + +const ORDER_TYPE = ORDER_TYPES.MARKET +const { BTC, BCH, XMR, ETH, LTC, ZEC } = COINS +const CRYPTO = [BTC, ETH, LTC, ZEC, BCH, XMR] +const FIAT = ['USD', 'EUR'] +const REQUIRED_CONFIG_FIELDS = ['apiKey', 'privateKey'] + +const loadConfig = (account) => { + const mapper = { + 'privateKey': 'secret' + } + const mapped = _.mapKeys(key => mapper[key] ? mapper[key] : key)(account) + return { ...mapped, timeout: 3000 } +} + +module.exports = { loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE } diff --git a/lib/plugins/exchange/binanceus.js b/lib/plugins/exchange/binanceus.js index fe2c96de..dd966c36 100644 --- a/lib/plugins/exchange/binanceus.js +++ b/lib/plugins/exchange/binanceus.js @@ -10,7 +10,9 @@ const FIAT = ['USD'] const REQUIRED_CONFIG_FIELDS = ['apiKey', 'privateKey'] const loadConfig = (account) => { - const mapper = {} + const mapper = { + 'privateKey': 'secret' + } const mapped = _.mapKeys(key => mapper[key] ? mapper[key] : key)(account) return { ...mapped, timeout: 3000 } } diff --git a/lib/plugins/exchange/cex.js b/lib/plugins/exchange/cex.js index 8bb1863e..294cb1fe 100644 --- a/lib/plugins/exchange/cex.js +++ b/lib/plugins/exchange/cex.js @@ -10,7 +10,9 @@ const FIAT = ['USD', 'EUR'] const REQUIRED_CONFIG_FIELDS = ['apiKey', 'privateKey'] const loadConfig = (account) => { - const mapper = {} + const mapper = { + 'privateKey': 'secret' + } const mapped = _.mapKeys(key => mapper[key] ? mapper[key] : key)(account) return { ...mapped, timeout: 3000 } } diff --git a/lib/plugins/exchange/ftx.js b/lib/plugins/exchange/ftx.js index 2661c8c2..c8a61fd7 100644 --- a/lib/plugins/exchange/ftx.js +++ b/lib/plugins/exchange/ftx.js @@ -10,7 +10,9 @@ const FIAT = ['USD'] const REQUIRED_CONFIG_FIELDS = ['apiKey', 'privateKey'] const loadConfig = (account) => { - const mapper = {} + const mapper = { + 'privateKey': 'secret' + } const mapped = _.mapKeys(key => mapper[key] ? mapper[key] : key)(account) return { ...mapped, timeout: 3000 } } diff --git a/new-lamassu-admin/src/pages/Services/schemas/binance.js b/new-lamassu-admin/src/pages/Services/schemas/binance.js new file mode 100644 index 00000000..8e4e9bd4 --- /dev/null +++ b/new-lamassu-admin/src/pages/Services/schemas/binance.js @@ -0,0 +1,36 @@ +import * as Yup from 'yup' + +import SecretInputFormik from 'src/components/inputs/formik/SecretInput' +import TextInputFormik from 'src/components/inputs/formik/TextInput' + +import { secretTest } from './helper' + +export default { + code: 'binance', + name: 'Binance', + title: 'Binance (Exchange)', + elements: [ + { + code: 'apiKey', + display: 'API Key', + component: TextInputFormik, + face: true, + long: true + }, + { + code: 'privateKey', + display: 'Private Key', + component: SecretInputFormik + } + ], + getValidationSchema: account => { + return Yup.object().shape({ + apiKey: Yup.string('The API key must be a string') + .max(100, 'The API key is too long') + .required('The API key is required'), + privateKey: Yup.string('The private key must be a string') + .max(100, 'The private key is too long') + .test(secretTest(account?.privateKey, 'private key')) + }) + } +} diff --git a/new-lamassu-admin/src/pages/Services/schemas/index.js b/new-lamassu-admin/src/pages/Services/schemas/index.js index 5b5b8825..a399756b 100644 --- a/new-lamassu-admin/src/pages/Services/schemas/index.js +++ b/new-lamassu-admin/src/pages/Services/schemas/index.js @@ -1,3 +1,4 @@ +import binance from './binance' import binanceus from './binanceus' import bitgo from './bitgo' import bitstamp from './bitstamp' @@ -23,5 +24,6 @@ export default { [binanceus.code]: binanceus, [cex.code]: cex, [ftx.code]: ftx, - [ciphertrace.code]: ciphertrace + [ciphertrace.code]: ciphertrace, + [binance.code]: binance } diff --git a/package.json b/package.json index c7b96c45..f5ab3c99 100644 --- a/package.json +++ b/package.json @@ -125,7 +125,7 @@ "server": "nodemon bin/lamassu-server --mockSms --logLevel silly", "admin-server": "nodemon bin/lamassu-admin-server --dev --logLevel silly", "graphql-server": "nodemon bin/new-graphql-dev-insecure", - "watch": "concurrently \"npm:server\" \"npm:admin-server\" \"npm:graphql-server\"", + "watch": "concurrently \"npm:server\" \"npm:admin-server\"", "stress-test": "cd ./test/stress/ && node index.js 50 -v" }, "nodemonConfig": {