TxModel.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import http from 'axios'
  2. export default class TxModel {
  3. static MODULES = {
  4. HISTORY: 'history',
  5. PENDING: 'pending',
  6. ADDRESS: 'address'
  7. }
  8. constructor(chainId, module) {
  9. if (!chainId || !module) throw "Must have [chainId, module]."
  10. this.chainId = chainId
  11. this.module = module
  12. }
  13. async find(conditions, pageNumber = 1, pageSize = 200) {
  14. const url = `/${this.module}/findByChainId`
  15. const rst = await http.post(url, {
  16. chainId: this.chainId,
  17. pageNumber: pageNumber,
  18. pageSize: pageSize,
  19. conditions: conditions
  20. })
  21. return rst.data
  22. }
  23. async findByHash(hash) {
  24. const url = `/${this.module}/findByChainIdAndHash`
  25. const rst = await http.post(url, {
  26. chainId: this.chainId,
  27. hash: hash
  28. })
  29. return rst.data
  30. }
  31. async updateTxBaseModel(txBaseItem) {
  32. const url = `/tx/update`
  33. txBaseItem.chainId = this.chainId
  34. const rst = await http.post(url, txBaseItem)
  35. return rst.data
  36. }
  37. static parseLocalRecordList(remoteRecordList) {
  38. let localRecordList = []
  39. for (let remoteRecord of remoteRecordList) {
  40. let localRecord = TxModel.parseLocalRecord(remoteRecord)
  41. if (localRecord) localRecordList.push(localRecord)
  42. }
  43. return localRecordList
  44. }
  45. static parseLocalRecord(remoteRecord) {
  46. try {
  47. let localRecord = {}
  48. localRecord.hash = remoteRecord.hash
  49. localRecord.blockNumber = remoteRecord.blockNumber
  50. localRecord.comment = remoteRecord.comment
  51. localRecord.from = remoteRecord.fromAddress
  52. localRecord.to = remoteRecord.toAddress
  53. localRecord.gasPrice = TxModel.parseGasPrice(remoteRecord.gasPriceStr)
  54. localRecord.timestamp = remoteRecord.timestamp ? remoteRecord.timestamp : 0
  55. localRecord.transferList = remoteRecord.transferList
  56. localRecord.fromName = remoteRecord.fromName
  57. localRecord.toName = remoteRecord.toName
  58. localRecord.status = parseInt(remoteRecord.status)
  59. localRecord.index = parseInt(remoteRecord.transactionIndex)
  60. localRecord.type = parseInt(remoteRecord.tradeType)
  61. if (isNaN( localRecord.type))
  62. localRecord.type = ''
  63. // transferList的format
  64. for (const transfer of localRecord.transferList) {
  65. try {
  66. transfer.amount = parseInt(transfer.amountStr) / (10 ** transfer.tokenDecimals)
  67. } catch (e) {
  68. transfer.amount = parseInt(transfer.amountStr)
  69. }
  70. if (isNaN(transfer.amount))
  71. transfer.amount = 0
  72. }
  73. localRecord.isMev = remoteRecord.isMev === true
  74. localRecord.isBot = remoteRecord.isBot === true
  75. localRecord.maybeBot = remoteRecord.maybeBot === true
  76. return localRecord
  77. } catch (e) {
  78. return undefined
  79. }
  80. }
  81. static parseGasPrice(gasPriceStr) {
  82. try {
  83. return parseFloat(gasPriceStr) / 1e9
  84. } catch (e) {
  85. return gasPriceStr
  86. }
  87. }
  88. }