/ src / models / TransferEventModel.ts
TransferEventModel.ts
 1  import type {
 2    InferAttributes,
 3    InferCreationAttributes,
 4    Sequelize,
 5  } from 'sequelize';
 6  import { DataTypes, Model } from 'sequelize';
 7  import { ZeroAddress } from 'ethers';
 8  import type { Address, NftDriverId } from '../core/types';
 9  import getSchema from '../utils/getSchema';
10  import { COMMON_EVENT_INIT_ATTRIBUTES } from '../core/constants';
11  import type { IEventModel } from '../events/types';
12  
13  export default class TransferEventModel
14    extends Model<
15      InferAttributes<TransferEventModel>,
16      InferCreationAttributes<TransferEventModel>
17    >
18    implements IEventModel
19  {
20    declare public tokenId: NftDriverId; // The `tokenId` from `Transfer` event.
21    declare public from: Address;
22    declare public to: Address;
23    declare public logIndex: number;
24    declare public blockNumber: number;
25    declare public blockTimestamp: Date;
26    declare public transactionHash: string;
27  
28    public static initialize(sequelize: Sequelize): void {
29      this.init(
30        {
31          tokenId: {
32            allowNull: false,
33            type: DataTypes.STRING,
34          },
35          from: {
36            allowNull: false,
37            type: DataTypes.STRING,
38          },
39          to: {
40            allowNull: false,
41            type: DataTypes.STRING,
42          },
43          ...COMMON_EVENT_INIT_ATTRIBUTES,
44        },
45        {
46          sequelize,
47          schema: getSchema(),
48          tableName: 'transfer_events',
49          underscored: true,
50          timestamps: true,
51          indexes: [
52            {
53              fields: ['from', 'tokenId'],
54              name: 'idx_transfer_events_mint',
55              where: {
56                from: ZeroAddress,
57              },
58            },
59            {
60              fields: ['blockTimestamp'],
61              name: 'idx_transfer_events_timestamp',
62            },
63          ],
64        },
65      );
66    }
67  }