ethHelper.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import API from '@/api'
  2. // import Web3 from 'web3'
  3. import { showError } from '@/util/util'
  4. import { Promise } from 'es6-promise'
  5. let ethHelper = {
  6. isInitEth: false,
  7. curAccount: '',
  8. network: 0,
  9. checkMainNetwork () {
  10. return new Promise((resolve, reject) => {
  11. window.web3.version.getNetwork((err, netId) => {
  12. if (netId != 1) {
  13. console.log(err)
  14. return reject(new Error(''))
  15. } else {
  16. resolve()
  17. }
  18. })
  19. })
  20. },
  21. /**
  22. * @des 签名认证
  23. */
  24. async initEth () {
  25. if (!window.ethereum || !window.web3) {
  26. showError('你还没有安装eth插件')
  27. return Promise.reject(new Error(''))
  28. }
  29. if (window.ethereum && !this.isInitEth) {
  30. await window.ethereum.enable()
  31. this.isInitEth = true
  32. }
  33. await this.checkMainNetwork()
  34. let accounts = window.web3.eth.accounts
  35. let account = this.account = accounts[0]
  36. if (account) {
  37. this.curAccount = account
  38. let randomRes = await API.user.getRandom2({ account: account })
  39. return new Promise((resolve, reject) => {
  40. try {
  41. window.web3.personal.sign(window.web3.fromUtf8(`Welcome to MeeChat! Decentralization Social Platform (${randomRes.data.data.random})`), window.web3.eth.coinbase, (err, rs) => {
  42. if (err) {
  43. reject(err)
  44. } else {
  45. resolve({
  46. account: account,
  47. sign: rs
  48. })
  49. }
  50. })
  51. } catch (err) {
  52. reject(err)
  53. }
  54. })
  55. } else {
  56. return Promise.reject(new Error(''))
  57. }
  58. },
  59. /**
  60. * @des 转账
  61. * @param {Number} money 转账金额
  62. */
  63. async doTransfer (money) {
  64. try {
  65. await this.initEth()
  66. let balance = await window.web3.eth.getBalance(this.curAccount)
  67. if (10e17 * money > balance) {
  68. showError('The balance of account is insufficient.')
  69. return
  70. }
  71. const txData = {
  72. from: this.account,
  73. to: '0x42eb768f2244c8811c63729a21a3569731535f06',
  74. value: window.web3.utils.toWei(money.toString(), 'ether')
  75. }
  76. return new Promise((resolve, reject) => {
  77. window.web3.eth.sendTransaction(txData, (err, txHash) => {
  78. if (err) {
  79. reject(err)
  80. } else {
  81. // this.waitEthTransfer(txHash)
  82. resolve()
  83. }
  84. })
  85. })
  86. } catch (e) {
  87. return Promise.reject(e)
  88. }
  89. },
  90. // 检测转账进度
  91. waitEthTransfer (txHash) {
  92. window.web3.eth.getTransactionReceipt(txHash, (err, res) => {
  93. console.log('waitEthTransfer', err, res)
  94. if (err) {
  95. console.error(err)
  96. }
  97. if (!res) {
  98. setTimeout(this.waitEthTransfer, 1000, txHash)
  99. } else if (res && res.status) {
  100. this.visible = false
  101. }
  102. console.log('res', res)
  103. })
  104. }
  105. }
  106. export default ethHelper