wallet-svr/src/modules/TranRecord.ts

94 lines
2.4 KiB
TypeScript

import { getModelForClass, index, modelOptions, mongoose, prop, Severity } from '@typegoose/typegoose'
import { dbconn } from 'decorators/dbconn'
import { BaseModule } from './Base'
export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
@modelOptions({ schemaOptions: { _id: false } })
export class TranDetail {
@prop()
public address: string
@prop({ default: ZERO_ADDRESS })
public from: string
@prop({ default: ZERO_ADDRESS })
public to: string
// 对于ERC20, 该值永远为'0'
@prop({ default: '0' })
public id: string
// 对于ERC721, 该值永远为'1'
// 如果address为eth, 那么该值为转账的金额
@prop({ default: '1' })
public value: string
public toJson() {
return {
address: this.address,
from: this.from,
to: this.to,
id: this.id,
value: this.value,
}
}
}
@dbconn()
@index({ transactionHash: 1 }, { unique: true })
@index({ 'details.address': 1, account: 1, chain: 1 })
@index({ 'details.address': 1 })
@modelOptions({
schemaOptions: { collection: 'transaction_record', timestamps: true },
})
class TranRecordClass extends BaseModule {
@prop({ required: true })
public transactionHash!: string
@prop()
public title: string
@prop()
public chain: string
@prop({ required: true })
public account!: string
@prop()
public blockNumber: number
@prop()
public blockHash: string
@prop()
public gas: string
@prop()
public gasPrice: string
@prop({ type: () => [TranDetail], default: [] })
public details: TranDetail[]
/**
* 交易状态
* 0: 默认, 未确认
* 1: 已获取receipt, 能通过getTransactionReceipt获取状态
* 2: 已确认, 交易已经经过6个块确认
* 10: revert
* 11: other error
*/
@prop({ default: 0 })
public status: number
@prop()
public startTime: number
@prop()
public confirmTime: number
public toJson() {
return {
transactionHash: this.transactionHash,
chain: this.chain,
title: this.title,
blockNumber: this.blockNumber,
blockHash: this.blockHash,
gas: this.gas,
gasPrice: this.gasPrice,
status: this.status,
startTime: this.startTime,
confirmTime: this.confirmTime,
details: this.details.map(o => o.toJson()),
}
}
}
export const TranRecord = getModelForClass(TranRecordClass, { existingConnection: TranRecordClass.db })