84 lines
2.1 KiB
TypeScript
84 lines
2.1 KiB
TypeScript
import { getModelForClass, index, modelOptions, prop } from '@typegoose/typegoose'
|
|
import { dbconn } from 'decorators/dbconn'
|
|
import { BaseModule } from './Base'
|
|
|
|
@dbconn()
|
|
@index({ address: 1, chain: 1 }, { unique: false })
|
|
@index({ chain: 1, transactionHash: 1, address: 1, from: 1, to: 1 }, { unique: true })
|
|
@modelOptions({
|
|
schemaOptions: { collection: 'ft_transfer_event', timestamps: true },
|
|
})
|
|
export class FtTransferEventClass extends BaseModule {
|
|
@prop()
|
|
public chain: number
|
|
@prop({ required: true })
|
|
public address!: string
|
|
@prop()
|
|
public event: string
|
|
@prop({ required: true })
|
|
public transactionHash: string
|
|
@prop()
|
|
public transactionIndex: number
|
|
@prop()
|
|
public logIndex: number
|
|
@prop()
|
|
public blockNumber: number
|
|
@prop()
|
|
public blockHash: string
|
|
@prop()
|
|
public removed: boolean
|
|
@prop()
|
|
public from: string
|
|
@prop()
|
|
public to: string
|
|
@prop()
|
|
public amount: string
|
|
@prop()
|
|
public eventId: string
|
|
@prop()
|
|
public blockTime: number
|
|
@prop({ default: 0 })
|
|
public version: number
|
|
|
|
public static async saveEvent(event: any) {
|
|
const amount = event.returnValues?.value
|
|
if (amount == undefined) {
|
|
return
|
|
}
|
|
const from = (event.source || event.returnValues?.from).toLowerCase()
|
|
const to = (event.target || event.returnValues?.to).toLowerCase()
|
|
const address = (event.address || event.tokenAddress).toLowerCase()
|
|
const data = {
|
|
chain: event.chain,
|
|
address,
|
|
blockNumber: event.blockHeight || event.blockNumber,
|
|
blockHash: event.blockHash,
|
|
removed: event.removed,
|
|
event: event.event,
|
|
from,
|
|
to,
|
|
transactionHash: event.transactionHash || event.hash,
|
|
transactionIndex: event.transactionIndex,
|
|
amount,
|
|
eventId: event.id,
|
|
logIndex: event.logIndex,
|
|
blockTime: event.timestamp,
|
|
$inc: { version: 1 },
|
|
}
|
|
return FtTransferEvent.insertOrUpdate(
|
|
{
|
|
chain: event.chain,
|
|
address,
|
|
amount,
|
|
from,
|
|
to,
|
|
},
|
|
data,
|
|
)
|
|
}
|
|
}
|
|
|
|
export const FtTransferEvent = getModelForClass(FtTransferEventClass, {
|
|
existingConnection: FtTransferEventClass['db'],
|
|
})
|