BaseModel.js 2.2 KB

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