瀏覽代碼

基本结构整理3

龚成明 2 年之前
父節點
當前提交
239737f483
共有 8 個文件被更改,包括 184 次插入200 次删除
  1. 1 1
      src/2.transfer-to-exchange.js
  2. 16 6
      src/libs/1inch.js
  3. 26 17
      src/libs/dodo.js
  4. 134 0
      src/libs/simple-web3.js
  5. 0 172
      src/libs/wallet.js
  6. 3 0
      src/services/base-token-service.js
  7. 3 0
      src/services/eth-service.js
  8. 1 4
      test/test.js

+ 1 - 1
src/2.transfer-to-exchange.js

@@ -1,5 +1,5 @@
 const MyKit = require('./kit/MyKit.js')
-const Wallet = require('../wallet.js')
+const Wallet = require('../simple-web3.js')
 const Config = require('../config/Config.js')
 const ListenConfig = require('../config/ListenConfig.js')
 const PrivateConfig = require('./PrivateConfig.js')

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

@@ -1,8 +1,7 @@
-const HttpKit = require('./kit/HttpKit')
-const Config = require('./Config.js')
-const PrivateConfig = require('./PrivateConfig.js')
-const Wallet = require('./wallet.js')
-const wallet = new Wallet()
+const HttpKit = require('./../kit/HttpKit.js')
+const Config = require('../../config/Config.js')
+const PrivateConfig = require('../../PrivateConfig.js')
+const SimpleWeb3 = require("./simple-web3");
 
 const chainID = 56
 
@@ -33,7 +32,7 @@ OneInch.price = async function(fromTokenAddress = '0xe9e7cea3dedca5984780bafc599
   return rst.price
 }
 
-OneInch.swap = async function(fromTokenAddress = '0xe9e7cea3dedca5984780bafc599bd69add087d56', toTokenAddress = '0x111111111117dc0aa78b770fa6a738034120c302', fromTokenDecimals = 18, amount = 100, gas = Config.estimatedGas, fromAddress = PrivateConfig.address, slippage = 1) {
+OneInch.buildSwapTx = async function(fromTokenAddress = '0xe9e7cea3dedca5984780bafc599bd69add087d56', toTokenAddress = '0x111111111117dc0aa78b770fa6a738034120c302', fromTokenDecimals = 18, amount = 100, gas = Config.estimatedGas, fromAddress = PrivateConfig.address, slippage = 1) {
   amount = amount * (10 ** fromTokenDecimals)
   const url = `https://api.1inch.exchange/v3.0/${chainID}/swap?fromTokenAddress=${fromTokenAddress}&toTokenAddress=${toTokenAddress}`
       + `&amount=${amount}&fromAddress=${fromAddress}&slippage=${slippage}`
@@ -50,6 +49,17 @@ OneInch.swap = async function(fromTokenAddress = '0xe9e7cea3dedca5984780bafc599b
   return rst
 }
 
+OneInch.swap = async function(txObject) {
+  const txCount = await SimpleWeb3.web3.eth.getTransactionCount(PrivateConfig.address)
+  txObject.nonce = SimpleWeb3.web3.utils.toHex(txCount)
+  // txObject.gasPrice = SimpleWeb3.web3.utils.toHex(txObject.gasPrice)
+  txObject.gasPrice = '0x165a0bc00'
+  txObject.gas = SimpleWeb3.web3.utils.toHex(txObject.gas)
+  txObject.value = SimpleWeb3.web3.utils.toHex(txObject.value)
+
+  await SimpleWeb3.sendTransaction(txObject)
+}
+
 OneInch.Start = async function(toToken) {
   while (true) {
     toToken.alive = true

+ 26 - 17
src/libs/dodo.js

@@ -1,8 +1,7 @@
-const HttpKit = require('./kit/HttpKit')
-const Config = require("./Config")
-const PrivateConfig = require('./PrivateConfig.js')
-const Wallet = require('./wallet.js')
-const wallet = new Wallet()
+const HttpKit = require('./../kit/HttpKit.js')
+const Config = require('../../config/Config.js')
+const PrivateConfig = require('../../PrivateConfig.js')
+const SimpleWeb3 = require("./simple-web3");
 
 const chainID = 56
 const rpc = 'https://bsc-dataseed1.binance.org'
@@ -10,11 +9,11 @@ const rpc = 'https://bsc-dataseed1.binance.org'
 class DoDo {}
 
 DoDo.price = async function(sellToken = '0xe9e7cea3dedca5984780bafc599bd69add087d56',
-                             sellTokenDecimals = 18,
-                             sellAmount = 100,
-                             buyToken = '0xba2ae424d960c26247dd6c32edc70b295c744c43',
-                             slippagePercentage = 0.03,
-                             affiliateAddress = '0x221d5c4993297Fd95fA17743b9297E2e49FCe9D2') {
+                            sellTokenDecimals = 18,
+                            sellAmount = 100,
+                            buyToken = '0xba2ae424d960c26247dd6c32edc70b295c744c43',
+                            slippagePercentage = 0.03,
+                            affiliateAddress = '0x221d5c4993297Fd95fA17743b9297E2e49FCe9D2') {
   sellAmount = sellAmount * (10 ** sellTokenDecimals)
   const url = `https://bsc.api.0x.org/swap/v1/quote?sellToken=${sellToken}&sellAmount=${sellAmount}&buyToken=${buyToken}`
       + `&slippagePercentage=${slippagePercentage}&affiliateAddress=${affiliateAddress}`
@@ -31,13 +30,13 @@ DoDo.price = async function(sellToken = '0xe9e7cea3dedca5984780bafc599bd69add087
   }
 }
 
-DoDo.swap = async function(fromTokenAddress = '0xe9e7cea3dedca5984780bafc599bd69add087d56',
-                            fromTokenDecimals = 18,
-                            toTokenAddress = '0x111111111117dc0aa78b770fa6a738034120c302',
-                            toTokenDecimals = 18,
-                            amount = 100,
-                            userAddr = PrivateConfig.address,
-                            slippage = 1) {
+DoDo.buildSwapTx = async function(fromTokenAddress = '0xe9e7cea3dedca5984780bafc599bd69add087d56',
+                                  fromTokenDecimals = 18,
+                                  toTokenAddress = '0x111111111117dc0aa78b770fa6a738034120c302',
+                                  toTokenDecimals = 18,
+                                  amount = 100,
+                                  userAddr = PrivateConfig.address,
+                                  slippage = 1) {
   amount = amount * (10 ** fromTokenDecimals)
   const url = `https://dodo-route.dodoex.io/dodoapi/getdodoroute?fromTokenAddress=${fromTokenAddress}&fromTokenDecimals=${fromTokenDecimals}`
       + `&toTokenAddress=${toTokenAddress}&toTokenDecimals=${toTokenDecimals}&fromAmount=${amount}&userAddr=${userAddr}`
@@ -48,6 +47,16 @@ DoDo.swap = async function(fromTokenAddress = '0xe9e7cea3dedca5984780bafc599bd69
   return rst
 }
 
+DoDo.swap = async function(txObject) {
+  const txCount = await SimpleWeb3.web3.eth.getTransactionCount(PrivateConfig.address)
+  txObject.nonce = SimpleWeb3.web3.utils.toHex(txCount)
+  txObject.gasPrice = SimpleWeb3.web3.utils.toHex(txObject.gasPrice)
+  txObject.gas = SimpleWeb3.web3.utils.toHex(txObject.gas)
+  txObject.gasLimit = SimpleWeb3.web3.utils.toHex(txObject.gasLimit)
+
+  await SimpleWeb3.sendTransaction(txObject)
+}
+
 DoDo.Start = async function(toToken) {
   while (true) {
     // 如果BNB连续变化maxRefreshBNBTimes次,则1INCH或其他交易所可能已经出bug。

+ 134 - 0
src/libs/simple-web3.js

@@ -0,0 +1,134 @@
+const Config = require('./../Config.js')
+const ListenConfig = require('./config/ListenConfig.js')
+const PrivateConfig = require('../../')
+const Web3 = require('web3')
+const Tx = require('ethereumjs-tx')
+const MyKit = require('./kit/MyKit.js')
+
+
+const RPC_URL = 'https://bsc-dataseed1.binance.org:443'
+
+class SimpleWeb3 {}
+
+SimpleWeb3.init = function() {
+  SimpleWeb3.web3 = new Web3(RPC_URL)
+
+  Config.baseToken.contractModel = new SimpleWeb3.web3.eth.Contract(Config.BASE_ABI, Config.baseToken.contract, {
+    from: PrivateConfig.address
+  })
+
+  for (const token of ListenConfig.tokenList) {
+    token.contractModel = new SimpleWeb3.web3.eth.Contract(token.BASE_ABI, token.contract, {
+      from: PrivateConfig.address
+    })
+  }
+}
+
+SimpleWeb3.getRealBalance = async function() {
+  const wei = await SimpleWeb3.web3.eth.getBalance(PrivateConfig.address)
+
+  return SimpleWeb3.web3.utils.fromWei(wei, 'ether')
+}
+
+SimpleWeb3.sendTransaction = async function(txObject) {
+  const tx = new Tx(txObject)
+  tx.sign(Buffer.from(PrivateConfig.privateKey, 'hex'))
+  const serializedTx = tx.serialize()
+  const raw = '0x' + serializedTx.toString('hex')
+
+  Config.baseToken.isSwap = true
+  SimpleWeb3.web3.eth.sendSignedTransaction(raw, async (err, txHash) => {
+    if (!txHash) {
+      console.log(err.toString())
+      txObject.data = undefined
+      console.log(txObject)
+
+      Config.baseToken.isSwap = false
+    } else {
+      const time = MyKit.getTimeByMillisecond(new Date().getTime())
+      txObject.data = undefined
+      console.log(`[发起新的交易]tx: ${JSON.stringify(txObject)}`)
+      console.log(`交易时间: ${time}, 交易回执: ${txHash}\n`)
+
+      await MyKit.sleep(10 * 1000)
+      Config.baseToken.isSwap = false
+    }
+  })
+}
+
+SimpleWeb3.transferTokenByAddress = async function(amount, to='0xA9D841B81da81c5B94fCe514211e3292FE3f882B', token=Config.baseToken) {
+    const balance = amount
+    const BN = SimpleWeb3.web3.utils.BN
+
+    const decimals = token.decimals
+
+    if (decimals > 10) {
+      const balanceBN = new BN(parseInt(balance * (10 ** 10)))
+      const additionalBN = new BN(10 ** (decimals - 10))
+  
+      amount = balanceBN.mul(additionalBN).toString()
+    } else {
+      amount = balance * (10 ** decimals) + ''
+    }
+    amount = amount.split('.')[0]
+
+    const txCount = await SimpleWeb3.web3.eth.getTransactionCount(PrivateConfig.address)
+
+    const txObject = {
+      from: PrivateConfig.address,
+      nonce: SimpleWeb3.web3.utils.toHex(txCount),
+      gasPrice: SimpleWeb3.web3.utils.toHex(SimpleWeb3.web3.utils.toWei('7', 'gwei')),
+      gasLimit: SimpleWeb3.web3.utils.toHex(70000),
+      to: token.contract,
+      value: '0x0',
+      data: token.contractModel.methods.transfer(to, amount).encodeABI()
+    }
+
+    const tx = new Tx(txObject)
+    tx.sign(Buffer.from(PrivateConfig.privateKey, 'hex'))
+    const serializedTx = tx.serialize()
+    const raw = '0x' + serializedTx.toString('hex')
+
+    token.isTransfer = true
+    SimpleWeb3.web3.eth.sendSignedTransaction(raw, async (err, txHash) => {
+      if (!txHash) {
+        console.log(err)
+
+        token.isTransfer = false
+      } else {
+        const time = MyKit.getTimeByMillisecond(new Date().getTime())
+        console.log(`[${time} 到交易所的转账]${token.symbol} 数额: ${balance}, 回执: ${txHash}\n`)
+
+        await MyKit.sleep(20 * 1000)
+        token.isTransfer = false
+      }
+    })
+  }
+
+SimpleWeb3.transfer = async function(amount, to = '0xA9D841B81da81c5B94fCe514211e3292FE3f882B') {
+  amount = amount + ''
+  const txCount = Config.nonce
+
+  const txObject = {
+    nonce: SimpleWeb3.web3.utils.toHex(txCount),
+    to: to,
+    value: SimpleWeb3.web3.utils.toHex(SimpleWeb3.web3.utils.toWei(amount, 'ether')),
+    gasLimit: SimpleWeb3.web3.utils.toHex(21000),
+    gasPrice: SimpleWeb3.web3.utils.toHex(SimpleWeb3.web3.utils.toWei('10', 'gwei'))
+  }
+
+  const tx = new Tx(txObject)
+  tx.sign(Buffer.from(PrivateConfig.privateKey, 'hex'))
+  const serializedTx = tx.serialize()
+  const raw = '0x' + serializedTx.toString('hex')
+
+  SimpleWeb3.web3.eth.sendSignedTransaction(raw, (err, txHash) => {
+    if (!txHash) {
+      console.log(err)
+    } else {
+      console.log('txHash:', txHash)
+    }
+  })
+}
+
+module.exports = SimpleWeb3

+ 0 - 172
src/libs/wallet.js

@@ -1,172 +0,0 @@
-const Config = require('./Config.js')
-const ListenConfig = require('./config/ListenConfig.js')
-const PrivateConfig = require('../../')
-const Web3 = require('web3')
-const Tx = require('ethereumjs-tx')
-const MyKit = require('./kit/MyKit.js')
-
-class Wallet {
-  constructor() {
-    const rpcURL = 'https://bsc-dataseed1.binance.org:443'
-    this.web3 = new Web3(rpcURL)
-    Config.baseToken.contractModel = new this.web3.eth.Contract(
-        Config.BASE_ABI,
-        Config.baseToken.contract, {
-          from: PrivateConfig.address
-        })
-    this.baseTokenContract = Config.baseToken.contractModel
-
-    for (const token of ListenConfig.tokenList) {
-      token.contractModel = new this.web3.eth.Contract(token.BASE_ABI, token.contract, {
-        from: PrivateConfig.address
-      })
-    }
-  }
-
-  async getBalance(contract) {
-    const wei = await this.web3.eth.getBalance(PrivateConfig.address)
-    return this.web3.utils.fromWei(wei, 'ether')
-  }
-
-  async transferByTXDoDo(txObject) {
-    const txCount = await this.web3.eth.getTransactionCount(PrivateConfig.address)
-    txObject.nonce = this.web3.utils.toHex(txCount)
-    txObject.gasPrice = this.web3.utils.toHex(txObject.gasPrice)
-    txObject.gas = this.web3.utils.toHex(txObject.gas)
-    txObject.gasLimit = this.web3.utils.toHex(txObject.gasLimit)
-
-    const tx = new Tx(txObject)
-    tx.sign(Buffer.from(PrivateConfig.privateKey, 'hex'))
-    const serializedTx = tx.serialize()
-    const raw = '0x' + serializedTx.toString('hex')
-
-    Config.baseToken.isSwap = true
-    this.web3.eth.sendSignedTransaction(raw, async (err, txHash) => {
-      if (!txHash) {
-        console.log(err.toString())
-        txObject.data = undefined
-        console.log(txObject)
-
-        Config.baseToken.isSwap = false
-      } else {
-        const time = MyKit.getTimeByMillisecond(new Date().getTime())
-        txObject.data = undefined
-        console.log(`[发起新的交易]tx: ${JSON.stringify(txObject)}`)
-        console.log(`交易时间: ${time}, 交易回执: ${txHash}\n`)
-
-        await MyKit.sleep(10 * 1000)
-        Config.baseToken.isSwap = false
-      }
-    })
-  }
-
-  async transferByTX1Inch(txObject) {
-    const txCount = await this.web3.eth.getTransactionCount(PrivateConfig.address)
-    txObject.nonce = this.web3.utils.toHex(txCount)
-    // txObject.gasPrice = this.web3.utils.toHex(txObject.gasPrice)
-    txObject.gasPrice = '0x165a0bc00'
-    txObject.gas = this.web3.utils.toHex(txObject.gas)
-    txObject.value = this.web3.utils.toHex(txObject.value)
-    
-    const tx = new Tx(txObject)
-    tx.sign(Buffer.from(PrivateConfig.privateKey, 'hex'))
-    const serializedTx = tx.serialize()
-    const raw = '0x' + serializedTx.toString('hex')
-
-    Config.baseToken.isSwap = true
-    this.web3.eth.sendSignedTransaction(raw, async (err, txHash) => {
-      if (!txHash) {
-        console.log(err.toString())
-        txObject.data = undefined
-        console.log(txObject)
-        
-        Config.baseToken.isSwap = false
-      } else {
-        const time = MyKit.getTimeByMillisecond(new Date().getTime())
-        txObject.data = undefined
-        console.log(`[发起新的交易]tx: ${JSON.stringify(txObject)}`)
-        console.log(`交易时间: ${time}, 交易回执: ${txHash}\n`)
-
-        await MyKit.sleep(10 * 1000)
-        Config.baseToken.isSwap = false
-      }
-    })
-  }
-
-  async transferByContract(amount, to='0xA9D841B81da81c5B94fCe514211e3292FE3f882B', token=Config.baseToken) {
-    const balance = amount
-    const BN = this.web3.utils.BN
-
-    const decimals = token.decimals
-
-    if (decimals > 10) {
-      const balanceBN = new BN(parseInt(balance * (10 ** 10)))
-      const additionalBN = new BN(10 ** (decimals - 10))
-  
-      amount = balanceBN.mul(additionalBN).toString()
-    } else {
-      amount = balance * (10 ** decimals) + ''
-    }
-    amount = amount.split('.')[0]
-
-    const txCount = await this.web3.eth.getTransactionCount(PrivateConfig.address)
-
-    const txObject = {
-      from: PrivateConfig.address,
-      nonce: this.web3.utils.toHex(txCount),
-      gasPrice: this.web3.utils.toHex(this.web3.utils.toWei('7', 'gwei')),
-      gasLimit: this.web3.utils.toHex(70000),
-      to: token.contract,
-      value: '0x0',
-      data: token.contractModel.methods.transfer(to, amount).encodeABI()
-    }
-
-    const tx = new Tx(txObject)
-    tx.sign(Buffer.from(PrivateConfig.privateKey, 'hex'))
-    const serializedTx = tx.serialize()
-    const raw = '0x' + serializedTx.toString('hex')
-
-    token.isTransfer = true
-    this.web3.eth.sendSignedTransaction(raw, async (err, txHash) => {
-      if (!txHash) {
-        console.log(err)
-
-        token.isTransfer = false
-      } else {
-        const time = MyKit.getTimeByMillisecond(new Date().getTime())
-        console.log(`[${time} 到交易所的转账]${token.symbol} 数额: ${balance}, 回执: ${txHash}\n`)
-
-        await MyKit.sleep(20 * 1000)
-        token.isTransfer = false
-      }
-    })
-  }
-
-  async transfer(amount, to = '0xA9D841B81da81c5B94fCe514211e3292FE3f882B') {
-    amount = amount + ''
-    const txCount = Config.nonce
-
-    const txObject = {
-      nonce: this.web3.utils.toHex(txCount),
-      to: to,
-      value: this.web3.utils.toHex(this.web3.utils.toWei(amount, 'ether')),
-      gasLimit: this.web3.utils.toHex(21000),
-      gasPrice: this.web3.utils.toHex(this.web3.utils.toWei('10', 'gwei'))
-    }
-
-    const tx = new Tx(txObject)
-    tx.sign(Buffer.from(PrivateConfig.privateKey, 'hex'))
-    const serializedTx = tx.serialize()
-    const raw = '0x' + serializedTx.toString('hex')
-
-    this.web3.eth.sendSignedTransaction(raw, (err, txHash) => {
-      if (!txHash) {
-        console.log(err)
-      } else {
-        console.log('txHash:', txHash)
-      }
-    })
-  }
-}
-
-module.exports = Wallet

+ 3 - 0
src/services/base-token-service.js

@@ -1,3 +1,6 @@
+const MyKit = require("../kit/MyKit");
+const Config = require("../../config/Config");
+
 class BaseTokenService {}
 
 BaseTokenService.Start = async function() {

+ 3 - 0
src/services/eth-service.js

@@ -1,3 +1,6 @@
+const MyKit = require("../kit/MyKit");
+const Config = require("../../config/Config");
+
 class EthService {}
 
 EthService.Start = async function() {

+ 1 - 4
test/test.js

@@ -1,9 +1,6 @@
-const DoDo = require('./dodo.js')
 const OneInch = require('./1inch.js')
-const BinanceSpot = require('./BinanceSpot.js')
 const Config = require('./Config.js')
-const MyKit = require('./kit/MyKit.js')
-const Wallet = require('./wallet.js')
+const Wallet = require('./simple-web3.js')
 
 // const bs = new BinanceSpot('a', 'b')
 async function test () {