| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- import http from 'axios'
- export default class BaseModel {
- static MODULES = {
- HISTORY: 'history',
- PENDING: 'pending'
- }
- constructor(chainId, module) {
- if (!chainId || !module) throw "Must have [chainId, module]."
- this.chainId = chainId
- this.module = module
- }
- async find(conditions, pageNumber=1, pageSize=200) {
- const url = `/${this.module}/findByChainId`
- const rst = await http.post(url, {
- chainId: this.chainId,
- pageNumber: pageNumber,
- pageSize: pageSize,
- conditions: conditions
- })
- return rst.data
- }
- async findByHash(hash) {
- const url = `/${this.module}/findByChainIdAndHash`
- const rst = await http.post(url, {
- chainId: this.chainId,
- hash: hash
- })
- return rst.data
- }
- static parseLocalRecordList(remoteRecordList) {
- let localRecordList = []
- for (let remoteRecord of remoteRecordList) {
- let localRecord = BaseModel.parseLocalRecord(remoteRecord)
- if (localRecord) localRecordList.push(localRecord)
- }
- return localRecordList
- }
- static parseLocalRecord(remoteRecord) {
- try {
- let localRecord = {}
- localRecord.hash = remoteRecord.hash
- localRecord.blockNumber = remoteRecord.blockNumber
- localRecord.comment= remoteRecord.comment
- localRecord.from = remoteRecord.fromAddress
- localRecord.to = remoteRecord.toAddress
- localRecord.gasPrice = BaseModel.parseGasPrice(remoteRecord.gasPriceStr)
- localRecord.timestamp = remoteRecord.timestamp
- localRecord.transferList = remoteRecord.transferList
- // transferList的format
- for (const transfer of localRecord.transferList) {
- try {
- transfer.amount = parseInt(transfer.amountStr) / (10 ** transfer.tokenDecimals)
- } catch (e) {
- transfer.amount = parseInt(transfer.amountStr)
- }
- }
- localRecord.isMev = remoteRecord.isMev === true
- localRecord.isBot = remoteRecord.isBot === true
- localRecord.maybeBot = remoteRecord.maybeBot === true
- return localRecord
- } catch (e) {
- return undefined
- }
- }
- static parseGasPrice(gasPriceStr) {
- try {
- return parseFloat(gasPriceStr) / 1e9
- } catch (e) {
- return gasPriceStr
- }
- }
- }
|