simple-web3.js 4.1 KB

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