| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import http from 'axios'
- export default class TxModel {
- static MODULES = {
- HISTORY: 'history',
- PENDING: 'pending',
- ADDRESS: 'address'
- }
- 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
- }
- async updateTxBaseModel(txBaseItem) {
- const url = `/tx/update`
- txBaseItem.chainId = this.chainId
- const rst = await http.post(url, txBaseItem)
- return rst.data
- }
- static parseLocalRecordList(remoteRecordList) {
- let localRecordList = []
- for (let remoteRecord of remoteRecordList) {
- let localRecord = TxModel.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 = TxModel.parseGasPrice(remoteRecord.gasPriceStr)
- localRecord.timestamp = remoteRecord.timestamp ? remoteRecord.timestamp : 0
- localRecord.transferList = remoteRecord.transferList
- localRecord.fromName = remoteRecord.fromName
- localRecord.toName = remoteRecord.toName
- localRecord.status = parseInt(remoteRecord.status)
- localRecord.index = parseInt(remoteRecord.transactionIndex)
- localRecord.type = parseInt(remoteRecord.tradeType)
- if (isNaN( localRecord.type))
- localRecord.type = ''
- // transferList的format
- for (const transfer of localRecord.transferList) {
- try {
- transfer.amount = parseInt(transfer.amountStr) / (10 ** transfer.tokenDecimals)
- } catch (e) {
- transfer.amount = parseInt(transfer.amountStr)
- }
- if (isNaN(transfer.amount))
- transfer.amount = 0
- }
- 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
- }
- }
- }
|