Browse Source

基本结构整理4

龚成明 2 years ago
parent
commit
c1b3f4a8b2

+ 11 - 15
src/1.index.js

@@ -3,27 +3,23 @@ const DoDo = require('./libs/dodo.js')
 const EthService = require('./services/eth-service.js')
 const BaseTokenService = require('./services/base-token-service.js')
 
-const BinanceSpot = require('./libs/binance-spot.js')
+const BinanceSpot = require('./libs/binance/binance-spot.js')
 const Config = require('../config/Config.js')
 const MyKit = require('./kit/MyKit.js')
 
 const bs = new BinanceSpot('-', '-')
 
-async function main () {
-  console.log('初始化中...')
-  console.log(`交易所:${Config.exchange}。`)
-  await MyKit.sleep(1000)
+console.log('初始化中...')
+console.log(`交易所:${Config.exchange}。`)
+await MyKit.sleep(1000)
 
-  EthService.Start()
-  BaseTokenService.Start()
+EthService.Start()
+BaseTokenService.Start()
 
-  for (const token of Config.tokenList) {
-    if (Config.exchange === '1inch') {
-      OneInch.Start(token)
-    } else if (Config.exchange === 'dodo') {
-      DoDo.Start(token)
-    }
+for (const token of Config.tokenList) {
+  if (Config.exchange === '1inch') {
+    OneInch.Start(token)
+  } else if (Config.exchange === 'dodo') {
+    DoDo.Start(token)
   }
 }
-
-main()

+ 4 - 8
src/2.transfer-to-exchange.js

@@ -23,13 +23,9 @@ async function onTick() {
   }
 }
 
-async function main() {
-  while (true) {
-    await MyKit.sleep(isTransfer ? 10000 : 5000)
-    isTransfer = false
+while (true) {
+  await MyKit.sleep(isTransfer ? 10000 : 5000)
+  isTransfer = false
 
-    await onTick()
-  }
+  await onTick()
 }
-
-main()

+ 7 - 11
src/3.secondary-sell.js

@@ -35,16 +35,12 @@ async function onTick() {
   }
 }
 
-async function main() {
-  while (true) {
-    try {
-      await onTick()
-    } catch (e) {
-      console.log(e)
-    }
-
-    await MyKit.sleep(5000)
+while (true) {
+  try {
+    await onTick()
+  } catch (e) {
+    console.log(e)
   }
-}
 
-main()
+  await MyKit.sleep(5000)
+}

+ 3 - 7
src/4.withdraw.js

@@ -32,12 +32,8 @@ async function onTick() {
   }
 }
 
-async function main() {
-  while (true) {
-    await onTick()
+while (true) {
+  await onTick()
 
-    await MyKit.sleep(5000)
-  }
+  await MyKit.sleep(5000)
 }
-
-main()

+ 1 - 1
src/libs/1inch.js

@@ -1,7 +1,7 @@
 const HttpKit = require('./../kit/HttpKit.js')
 const Config = require('../../config/Config.js')
 const PrivateConfig = require('../../PrivateConfig.js')
-const SimpleWeb3 = require("./simple-web3");
+const SimpleWeb3 = require("./web3/simple-web3");
 
 const chainID = 56
 

+ 46 - 38
src/libs/binance-spot.js → src/libs/binance/binance-spot.js

@@ -1,27 +1,38 @@
-const Tik = require('./kit/BinanceTik')
-const HttpKit = require('./kit/HttpKit')
+const Tik = require('../../kit/BinanceTik')
+const HttpKit = require('../../kit/HttpKit')
+const MyKit = require('../../kit/MyKit.js')
 const HTTPRequest = require('request')
-const MyKit = require('./kit/MyKit.js')
 
 const isDev = process.env.USER === 'skyfffire'
+const proxy = 'http://localhost:9080'
 
 class BinanceSpot {
-  constructor (apiKey, secretKey) {
-    this.baseURL = 'https://api.binance.com'
+  static BASE_URL = 'https://api.binance.com'
+  static TRADE_TYPE = {
+    MARKET: 'MARKET'
+  }
+  static TRADE_SIDE = {
+    BUY: 'buy',
+    SELL: 'sell'
+  }
+  static PRICE = {
+    MARKET: -1
+  }
+  static NETWORK = {
+    BSC: 'BSC'
+  }
+  static BASE_TOKEN = {
+    BUSD: 'BUSD'
+  }
+  
+  constructor(apiKey, secretKey) {
     this.apiKey = apiKey
     this.secretKey = secretKey
     this.tik = new Tik()
   }
 
-  async exchangeInfo () {
-    const url = this.baseURL + '/api/v3/exchangeInfo'
-
-    const { data: rst } = await HttpKit.get(url)
-    return rst
-  }
-
-  async price (symbol='BNBUSDT', side = 'sell') {
-    const url = `${this.baseURL}/api/v3/depth?symbol=${symbol}&limit=${5}`
+  async static realPrice(symbol='BNB_USDT', side=BinanceSpot.TRADE_SIDE.BUY) {
+    const url = `${BinanceSpot.BASE_URL}/api/v3/depth?symbol=${symbol}&limit=${5}`
 
     const { data: rst } = await HttpKit.get(url)
 
@@ -36,8 +47,15 @@ class BinanceSpot {
     }
   }
 
-  async accountInfo () {
-    const url = this.baseURL + '/api/v3/account'
+  async exchangeInfo() {
+    const url = `${BinanceSpot.BASE_URL}/api/v3/exchangeInfo`
+
+    const { data: rst } = await HttpKit.get(url)
+    return rst
+  }
+
+  async accountInfo() {
+    const url = `${BinanceSpot.BASE_URL}/api/v3/account`
     let timestamp = new Date().getTime()
     let data = {
       timestamp: timestamp
@@ -52,11 +70,9 @@ class BinanceSpot {
     return rst
   }
 
-  async withdraw (amount= -1,
-                  coin='BUSD',
-                  address='0xCfB3Cb29EeAE464ABD44E35d53e12c555705e824',
-                  network='BSC') {
-    const url = this.baseURL + '/sapi/v1/capital/withdraw/apply'
+  async withdraw(amount, coin=BinanceSpot.BASE_TOKEN.BUSD,
+                 address='0xCfB3Cb29EeAE464ABD44E35d53e12c555705e824', network=BinanceSpot.NETWORK.BSC) {
+    const url = `${BinanceSpot.BASE_URL}/sapi/v1/capital/withdraw/apply`
     let timestamp = new Date().getTime()
     let data = {
       timestamp: timestamp,
@@ -76,13 +92,8 @@ class BinanceSpot {
     return rst
   }
 
-  async takeOrder (symbol='BTC_USDT',
-                   price=-1,
-                   side='BUY',
-                   quantity=0.001,
-                   quoteOrderQty=10,
-                   type='MARKET') {
-    const url = this.baseURL + '/api/v3/order'
+  async takeOrder(symbol='BTC_USDT', price, side, quantity, quoteOrderQty, type=BinanceSpot.TRADE_TYPE.MARKET) {
+    const url = `${BinanceSpot.BASE_URL}/api/v3/order`
     let timestamp = new Date().getTime()
     let data = {
       timestamp: timestamp,
@@ -108,11 +119,8 @@ class BinanceSpot {
     const params = {
       url: finalQueryURL,
       method: 'POST',
-      headers: headers
-    }
-
-    if (isDev) {
-      params.proxy = 'http://localhost:9080'
+      headers: headers,
+      proxy: isDev ? proxy : undefined
     }
 
     let finish = false
@@ -130,27 +138,27 @@ class BinanceSpot {
     })
 
     while (!finish) {
-      await MyKit.sleep(500)
+      await MyKit.sleep(10)
     }
 
     return rst
   }
 
-  async buy (symbol, price, amount, type='MARKET') {
+  async buy(symbol, price, amount, type=BinanceSpot.TRADE_TYPE.MARKET) {
     return await this.takeOrder(
       symbol,
       price,
-      'BUY',
+      BinanceSpot.TRADE_SIDE.BUY,
       amount,
       0,
       type)
   }
 
-  async sell (symbol, price, amount, type='MARKET') {
+  async sell(symbol, price, amount, type=BinanceSpot.TRADE_TYPE.MARKET) {
     return await this.takeOrder(
       symbol,
       price,
-      'SELL',
+      BinanceSpot.TRADE_SIDE.SELL,
       amount,
       0,
       type)

+ 1 - 1
src/libs/dodo.js

@@ -1,7 +1,7 @@
 const HttpKit = require('./../kit/HttpKit.js')
 const Config = require('../../config/Config.js')
 const PrivateConfig = require('../../PrivateConfig.js')
-const SimpleWeb3 = require("./simple-web3");
+const SimpleWeb3 = require("./web3/simple-web3");
 
 const chainID = 56
 const rpc = 'https://bsc-dataseed1.binance.org'

+ 1 - 1
src/libs/simple-web3.js → src/libs/web3/simple-web3.js

@@ -1,6 +1,6 @@
 const Config = require('./../Config.js')
 const ListenConfig = require('./config/ListenConfig.js')
-const PrivateConfig = require('../../')
+const PrivateConfig = require('../../.')
 const Web3 = require('web3')
 const Tx = require('ethereumjs-tx')
 const MyKit = require('./kit/MyKit.js')