task-svr/src/models/chain/GeneralScription.ts

67 lines
1.8 KiB
TypeScript

import { getModelForClass, index, modelOptions, prop } from '@typegoose/typegoose'
import { dbconn } from 'decorators/dbconn'
import { BaseModule } from '../Base'
import { hexToUtf8 } from 'zutils/utils/string.util'
import logger from 'logger/logger'
@dbconn('chain')
@index({ from: 1, op: 1 }, { unique: false })
@index({ from: 1, op: 1, data: 1 }, { unique: true })
@index({ hash: 1 }, { unique: true })
@index({ from: 1, blockTime: 1 }, { unique: false })
@modelOptions({
schemaOptions: { collection: 'general_scription_record', timestamps: true },
})
export class GeneralScriptionClass extends BaseModule {
@prop({ required: true })
public from!: string
@prop()
public to: string
@prop({ required: true })
public hash: string
@prop()
public blockNumber: string
@prop()
public blockHash: string
@prop()
public blockTime: number
@prop()
public dateTag: string
@prop()
public data: string
@prop()
public op: string
@prop()
public value: string
@prop()
public input: string
@prop({ default: 0 })
public stat: number
public static async saveEvent(event: any) {
const dataStr = hexToUtf8(event.input)
const regexp = /data:,{\"p\":\"cf-20\",\"op\":\"(.+?)\",\"val\":\"(.+?)\"}/
const match = dataStr.match(regexp)
if (!match) {
logger.log('not a general scription:', event.hash)
return
}
event.op = match[1]
event.data = match[2]
logger.log('general scription with op:', event.op, ' data:', event.data)
return GeneralScription.insertOrUpdate({ hash: event.hash }, event)
}
public toJson() {
return {
address: this.from,
day: this.dateTag,
time: this.blockTime,
}
}
}
export const GeneralScription = getModelForClass(GeneralScriptionClass, {
existingConnection: GeneralScriptionClass['db'],
})