import { Severity, getModelForClass, index, modelOptions, mongoose, prop } from '@typegoose/typegoose' import { dbconn } from 'decorators/dbconn' import { BaseModule } from './Base' import { NftHolder } from './NftHolder' @dbconn() @index({ chain: 1, hash: 1, logIndex: 1 }, { unique: true }) @index({ chain: 1, address: 1, blockNumber: 1 }, { unique: false }) @modelOptions({ schemaOptions: { collection: 'general_event', timestamps: true }, options: { allowMixed: Severity.ALLOW }, }) export class GeneralEventClass extends BaseModule { @prop({ required: true }) public address!: string @prop({ required: true }) public chain: string @prop({ required: true }) public logIndex: number @prop() public event: string // event hash @prop({ required: true }) public hash: string @prop() public blockNumber: number @prop() public blockHash: string @prop() public removed: boolean @prop({ type: mongoose.Schema.Types.Mixed }) public decodedData: any @prop() public blockTime: number @prop({ default: 0 }) public version: number public static async saveEvent(event: any) { const logIndex = parseInt(event.logIndex || '0') const hash = event.hash || event.transactionHash const data = { address: event.address.toLowerCase(), blockNumber: parseInt(event.blockNumber), removed: event.removed, decodedData: event.decodedData, blockHash: event.blockHash, event: event.event, $inc: { version: 1 }, } let record = await GeneralEvent.insertOrUpdate({ hash, logIndex, chain: event.chain }, data) return record } } export const GeneralEvent = getModelForClass(GeneralEventClass, { existingConnection: GeneralEventClass['db'], })