wallet.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. const Config = require('./Config.js')
  2. const ListenConfig = require('./ListenConfig.js')
  3. const PrivateConfig = require('./PrivateConfig.js')
  4. const Web3 = require('web3')
  5. const Tx = require('ethereumjs-tx')
  6. const MyKit = require('./kit/MyKit.js')
  7. class Wallet {
  8. constructor () {
  9. const rpcURL = 'https://bsc-dataseed1.binance.org:443'
  10. this.web3 = new Web3(rpcURL)
  11. Config.baseToken.contractModel = new this.web3.eth.Contract(
  12. Config.baseToken.abi,
  13. Config.baseToken.contract, {
  14. from: PrivateConfig.address
  15. })
  16. this.baseTokenContract = Config.baseToken.contractModel
  17. for (const token of ListenConfig.tokenList) {
  18. token.contractModel = new this.web3.eth.Contract(token.abi, token.contract, {
  19. from: PrivateConfig.address
  20. })
  21. }
  22. }
  23. async getBalance (contract) {
  24. const wei = await this.web3.eth.getBalance(PrivateConfig.address)
  25. return this.web3.utils.fromWei(wei, 'ether')
  26. }
  27. async transferByTXDoDo (txObject) {
  28. const txCount = await this.web3.eth.getTransactionCount(PrivateConfig.address)
  29. txObject.nonce = this.web3.utils.toHex(txCount)
  30. txObject.gasPrice = this.web3.utils.toHex(txObject.gasPrice)
  31. txObject.gas = this.web3.utils.toHex(txObject.gas)
  32. txObject.gasLimit = this.web3.utils.toHex(txObject.gasLimit)
  33. const tx = new Tx(txObject)
  34. tx.sign(Buffer.from(PrivateConfig.privateKey, 'hex'))
  35. const serializedTx = tx.serialize()
  36. const raw = '0x' + serializedTx.toString('hex')
  37. Config.baseToken.isSwap = true
  38. this.web3.eth.sendSignedTransaction(raw, async (err, txHash) => {
  39. if (!txHash) {
  40. console.log(err.toString())
  41. txObject.data = undefined
  42. console.log(txObject)
  43. Config.baseToken.isSwap = false
  44. } else {
  45. const time = MyKit.getTimeByMillisecond(new Date().getTime())
  46. txObject.data = undefined
  47. console.log(`[发起新的交易]tx: ${JSON.stringify(txObject)}`)
  48. console.log(`交易时间: ${time}, 交易回执: ${txHash}\n`)
  49. await MyKit.sleep(10 * 1000)
  50. Config.baseToken.isSwap = false
  51. }
  52. })
  53. }
  54. async transferByTX1Inch (txObject) {
  55. const txCount = await this.web3.eth.getTransactionCount(PrivateConfig.address)
  56. txObject.nonce = this.web3.utils.toHex(txCount)
  57. // txObject.gasPrice = this.web3.utils.toHex(txObject.gasPrice)
  58. txObject.gasPrice = '0x165a0bc00'
  59. txObject.gas = this.web3.utils.toHex(txObject.gas)
  60. txObject.value = this.web3.utils.toHex(txObject.value)
  61. const tx = new Tx(txObject)
  62. tx.sign(Buffer.from(PrivateConfig.privateKey, 'hex'))
  63. const serializedTx = tx.serialize()
  64. const raw = '0x' + serializedTx.toString('hex')
  65. Config.baseToken.isSwap = true
  66. this.web3.eth.sendSignedTransaction(raw, async (err, txHash) => {
  67. if (!txHash) {
  68. console.log(err.toString())
  69. txObject.data = undefined
  70. console.log(txObject)
  71. Config.baseToken.isSwap = false
  72. } else {
  73. const time = MyKit.getTimeByMillisecond(new Date().getTime())
  74. txObject.data = undefined
  75. console.log(`[发起新的交易]tx: ${JSON.stringify(txObject)}`)
  76. console.log(`交易时间: ${time}, 交易回执: ${txHash}\n`)
  77. await MyKit.sleep(10 * 1000)
  78. Config.baseToken.isSwap = false
  79. }
  80. })
  81. }
  82. async transferByContract (amount,
  83. to='0xA9D841B81da81c5B94fCe514211e3292FE3f882B',
  84. token=Config.baseToken) {
  85. const balance = amount
  86. const BN = this.web3.utils.BN
  87. const decimals = token.decimals
  88. if (decimals > 10) {
  89. const balanceBN = new BN(parseInt(balance * (10 ** 10)))
  90. const additionalBN = new BN(10 ** (decimals - 10))
  91. amount = balanceBN.mul(additionalBN).toString()
  92. } else {
  93. amount = balance * (10 ** decimals) + ''
  94. }
  95. amount = amount.split('.')[0]
  96. const txCount = await this.web3.eth.getTransactionCount(PrivateConfig.address)
  97. const txObject = {
  98. from: PrivateConfig.address,
  99. nonce: this.web3.utils.toHex(txCount),
  100. gasPrice: this.web3.utils.toHex(this.web3.utils.toWei('7', 'gwei')),
  101. gasLimit: this.web3.utils.toHex(70000),
  102. to: token.contract,
  103. value: '0x0',
  104. data: token.contractModel.methods.transfer(to, amount).encodeABI()
  105. }
  106. const tx = new Tx(txObject)
  107. tx.sign(Buffer.from(PrivateConfig.privateKey, 'hex'))
  108. const serializedTx = tx.serialize()
  109. const raw = '0x' + serializedTx.toString('hex')
  110. token.isTransfer = true
  111. this.web3.eth.sendSignedTransaction(raw, async (err, txHash) => {
  112. if (!txHash) {
  113. console.log(err)
  114. token.isTransfer = false
  115. } else {
  116. const time = MyKit.getTimeByMillisecond(new Date().getTime())
  117. console.log(`[${time} 到交易所的转账]${token.symbol} 数额: ${balance}, 回执: ${txHash}\n`)
  118. await MyKit.sleep(20 * 1000)
  119. token.isTransfer = false
  120. }
  121. })
  122. }
  123. async transfer (amount, to = '0xA9D841B81da81c5B94fCe514211e3292FE3f882B') {
  124. amount = amount + ''
  125. const txCount = Config.nonce
  126. const txObject = {
  127. nonce: this.web3.utils.toHex(txCount),
  128. to: to,
  129. value: this.web3.utils.toHex(this.web3.utils.toWei(amount, 'ether')),
  130. gasLimit: this.web3.utils.toHex(21000),
  131. gasPrice: this.web3.utils.toHex(this.web3.utils.toWei('10', 'gwei'))
  132. }
  133. const tx = new Tx(txObject)
  134. tx.sign(Buffer.from(PrivateConfig.privateKey, 'hex'))
  135. const serializedTx = tx.serialize()
  136. const raw = '0x' + serializedTx.toString('hex')
  137. this.web3.eth.sendSignedTransaction(raw, (err, txHash) => {
  138. if (!txHash) {
  139. console.log(err)
  140. } else {
  141. console.log('txHash:', txHash)
  142. }
  143. })
  144. }
  145. }
  146. module.exports = Wallet