simple-web3.js 4.3 KB

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