simple-web3.js 4.0 KB

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