# All files / indexer-management/models indexing-rule.ts

34.38% Statements 11/32
12.5% Branches 2/16
33.33% Functions 2/6
34.38% Lines 11/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209

    1x
    1x

    1x
    1x
    1x
    1x

    1x

    1x

    1x
    14x

    14x

     /* eslint-disable @typescript-eslint/no-empty-interface */

    import { Optional, Model, DataTypes, Sequelize } from 'sequelize'
    import { utils } from 'ethers'

    export enum IndexingDecisionBasis {
      RULES = 'rules',
      NEVER = 'never',
      ALWAYS = 'always',
    }

    export const INDEXING_RULE_GLOBAL = 'global'

    export interface IndexingRuleAttributes {
      id: number
      deployment: string
      allocationAmount: string | null
      parallelAllocations: number | null
      maxAllocationPercentage: number | null
      minSignal: string | null
      maxSignal: string | null
      minStake: string | null
      minAverageQueryFees: string | null
      custom: string | null
      decisionBasis: IndexingDecisionBasis
    }

    export interface IndexingRuleCreationAttributes
      extends Optional<
        IndexingRuleAttributes,
        | 'id'
        | 'allocationAmount'
        | 'parallelAllocations'
        | 'maxAllocationPercentage'
        | 'minSignal'
        | 'maxSignal'
        | 'minStake'
        | 'minAverageQueryFees'
        | 'custom'
        | 'decisionBasis'
      > {}

    export class IndexingRule
      extends Model<IndexingRuleAttributes, IndexingRuleCreationAttributes>
      implements IndexingRuleAttributes {
      public id!: number
      public deployment!: string
      public allocationAmount!: string | null
      public parallelAllocations!: number | null
      public maxAllocationPercentage!: number | null
      public minSignal!: string | null
      public maxSignal!: string | null
      public minStake!: string | null
      public minAverageQueryFees!: string | null
      public custom!: string | null
      public decisionBasis!: IndexingDecisionBasis

      public createdAt!: Date
      public updatedAt!: Date

      // eslint-disable-next-line @typescript-eslint/ban-types
      public toGraphQL(): object {
        return { ...this.toJSON(), __typename: 'IndexingRule' }
      }

      // eslint-disable-next-line @typescript-eslint/ban-types
      public mergeToGraphQL(global: IndexingRule | null): object {
        if (global instanceof IndexingRule) {
          // eslint-disable-next-line @typescript-eslint/no-explicit-any
          const globalRule: { [key: string]: any } | null = global.toJSON()

          // eslint-disable-next-line @typescript-eslint/no-explicit-any
          const rule: { [key: string]: any } | null = this.toJSON()
          for (const k in globalRule) {
            if (null == rule[k]) {
              rule[k] = globalRule[k]
            }
          }
          for (const k in rule) {
            if (rule[k] == undefined) {
              rule[k] = globalRule[k]
            }
          }
          return { ...rule, __typename: 'IndexingRule' }
        } else {
          return this.toGraphQL()
        }
      }
    }

    export interface IndexingRuleModels {
      IndexingRule: typeof IndexingRule
    }

    export const defineIndexingRuleModels = (sequelize: Sequelize): IndexingRuleModels => {
      IndexingRule.init(
        {
          id: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            unique: true,
          },
          deployment: {
            type: DataTypes.STRING,
            allowNull: true,
            primaryKey: true,
            validate: {
              // eslint-disable-next-line @typescript-eslint/no-explicit-any
              isDeploymentID: (value: any) => {
                if (typeof value !== 'string') {
                  throw new Error('Deployment ID must be a string')
                }

                // "global" is ok
                if (value === 'global') {
                  return
                }

                // "0x..." is ok
                if (utils.isHexString(value, 32)) {
                  return
                }

                throw new Error(
                  `Deployment ID must be "global" or a valid subgraph deployment ID`,
                )
              },
            },
          },
          allocationAmount: {
            type: DataTypes.DECIMAL,
            allowNull: true,
            validate: {
              min: 0.0,
            },
          },
          parallelAllocations: {
            type: DataTypes.INTEGER,
            allowNull: true,
            validate: {
              min: 0,
              max: 20,
              emitDeprecationWarning: (value: number) => {
                if (value > 1) {
                  throw new Error(
                    'Parallel allocations are soon to be fully deprecated. Please set parallel allocations to 1 for all your indexing rules',
                  )
                }
              },
            },
          },
          maxAllocationPercentage: {
            type: DataTypes.FLOAT,
            allowNull: true,
            validate: {
              isFloat: true,
              min: 0.0,
              max: 1.0,
            },
          },
          minSignal: {
            type: DataTypes.DECIMAL,
            allowNull: true,
            validate: {
              min: 0.0,
            },
          },
          minStake: {
            type: DataTypes.DECIMAL,
            allowNull: true,
            validate: {
              min: 0.0,
            },
          },
          maxSignal: {
            type: DataTypes.DECIMAL,
            allowNull: true,
            validate: {
              min: 0.0,
            },
          },
          minAverageQueryFees: {
            type: DataTypes.DECIMAL,
            allowNull: true,
            validate: {
              min: 0.0,
            },
          },
          custom: {
            type: DataTypes.STRING,
            allowNull: true,
          },
          decisionBasis: {
            type: DataTypes.ENUM('rules', 'never', 'always'),
            allowNull: false,
            defaultValue: 'rules',
          },
        },
        {
          modelName: 'IndexingRule',
          sequelize,
        },
      )

      return {
        ['IndexingRule']: IndexingRule,
      }
    }