70 lines
1.6 KiB
TypeScript
70 lines
1.6 KiB
TypeScript
import { getModelForClass, index, modelOptions, prop } from '@typegoose/typegoose'
|
|
import { dbconn } from 'decorators/dbconn'
|
|
import { BaseModule } from './Base'
|
|
|
|
@dbconn()
|
|
@index({ chain: 1, address: 1 }, { unique: false })
|
|
@index({ user: 1 }, { unique: false })
|
|
@index({ chain: 1, address: 1, tokenId: 1 }, { unique: true })
|
|
@modelOptions({
|
|
schemaOptions: { collection: 'nft_holder', timestamps: true },
|
|
})
|
|
export class NftHolderClass extends BaseModule {
|
|
@prop()
|
|
public chain: number
|
|
@prop({ required: true })
|
|
public address!: string
|
|
@prop()
|
|
public user: string
|
|
@prop()
|
|
public tokenId: string
|
|
@prop({ default: 0 })
|
|
public version: number
|
|
@prop()
|
|
public lastTxHash: string
|
|
|
|
public toQlModel() {
|
|
return {
|
|
id: this.tokenId,
|
|
address: this.user,
|
|
chain: this.chain + '',
|
|
contract: this.address,
|
|
}
|
|
}
|
|
|
|
public static async queryNFT({
|
|
address,
|
|
chain,
|
|
contract,
|
|
id,
|
|
}: {
|
|
address?: string
|
|
chain?: string
|
|
contract?: string
|
|
id?: string
|
|
}) {
|
|
chain = chain || process.env.CHAIN_DEFAULT
|
|
const chainNum = parseInt(chain)
|
|
const query: any = { chain: chainNum }
|
|
if (address) {
|
|
query.user = address.toLowerCase()
|
|
}
|
|
if (contract) {
|
|
query.address = contract.toLowerCase()
|
|
}
|
|
if (id) {
|
|
query.tokenId = id
|
|
}
|
|
let records = await NftHolder.find(query)
|
|
let results: any = []
|
|
for (let record of records) {
|
|
results.push(record.toQlModel())
|
|
}
|
|
return results
|
|
}
|
|
}
|
|
|
|
export const NftHolder = getModelForClass(NftHolderClass, {
|
|
existingConnection: NftHolderClass['db'],
|
|
})
|