增加通用事件处理

This commit is contained in:
CounterFire2023 2023-06-30 15:37:21 +08:00
parent 07fa89a7bf
commit 2b4dede97d
2 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,83 @@
import { Severity, getModelForClass, index, mongoose, modelOptions, prop } from '@typegoose/typegoose'
import { dbconn } from 'decorators/dbconn'
import { BaseModule } from './Base'
import logger from 'logger/logger'
import { sha1 } from 'utils/security.util'
// 通用的事件存储表
@dbconn()
@index({ address: 1, chain: 1 }, { unique: false })
@index({ chain: 1, transactionHash: 1, event: 1, address: 1, eventHash: 1 }, { unique: true })
@modelOptions({
schemaOptions: { collection: 'universal_event', timestamps: true },
options: { allowMixed: Severity.ALLOW },
})
export class UniversalEventClass extends BaseModule {
@prop()
public chain: number
@prop({ required: true })
public address!: string
@prop()
public event: string
@prop({ required: true })
public transactionHash: string
// 自己计算的, 用于区分批处理的不同的事件
@prop({ required: true })
public eventHash: string
@prop()
public transactionIndex: number
@prop()
public logIndex: number
@prop()
public blockNumber: number
@prop()
public blockHash: string
@prop()
public removed: boolean
@prop()
public eventId: string
@prop()
public blockTime: number
@prop({ type: mongoose.Schema.Types.Mixed })
public originData: any
@prop({ default: 0 })
public version: number
public static async saveEvent(event: any) {
const address = (event.address || event.tokenAddress).toLowerCase()
const transactionHash = event.transactionHash || event.hash
const blockNumber = event.blockHeight || event.blockNumber
const eventHash = sha1(JSON.stringify(event))
const data = {
chain: event.chain,
address,
blockNumber,
blockHash: event.blockHash,
removed: event.removed,
event: event.event,
transactionHash,
transactionIndex: event.transactionIndex,
eventId: event.id,
logIndex: event.logIndex,
blockTime: event.timestamp,
originData: event,
$inc: { version: 1 },
}
//chain: 1, transactionHash: 1, event: 1, address: 1, eventHash: 1
return UniversalEvent.insertOrUpdate(
{
chain: event.chain,
transactionHash,
event: event.event,
address,
eventHash,
},
data,
)
}
}
export const UniversalEvent = getModelForClass(UniversalEventClass, {
existingConnection: UniversalEventClass['db'],
})

View File

@ -6,6 +6,7 @@ dotenv.config({ path: envFile })
import { EventSyncSvr } from 'service/event.sync.service' import { EventSyncSvr } from 'service/event.sync.service'
import { NftTransferEvent } from 'models/NftTransferEvent' import { NftTransferEvent } from 'models/NftTransferEvent'
import { FtTransferEvent } from 'models/FtTransferEvent' import { FtTransferEvent } from 'models/FtTransferEvent'
import { UniversalEvent } from 'models/UniversalEvent'
import 'common/Extend' import 'common/Extend'
@ -15,6 +16,7 @@ let lock = false
let eventProcessers = { let eventProcessers = {
NftTransferEvent: NftTransferEvent, NftTransferEvent: NftTransferEvent,
FtTransferEvent: FtTransferEvent, FtTransferEvent: FtTransferEvent,
UniversalEvent: UniversalEvent,
} }
const events = require('config/events.json') const events = require('config/events.json')