# All files / dist/indexer-management/resolvers cost-models.js

12.07% Statements 7/58
6.67% Branches 3/45
0% Functions 0/16
13.46% Lines 7/52

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

    3x

    3x
    3x
    3x

    3x

    3x

    3x

     "use strict";
    /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
    /* eslint-disable @typescript-eslint/ban-types */
    var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
        function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
        return new (P || (P = Promise))(function (resolve, reject) {
            function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
            function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
            function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
            step((generator = generator.apply(thisArg, _arguments || [])).next());
        });
    };
    Object.defineProperty(exports, "__esModule", { value: true });
    const models_1 = require("../models");
    const cost_model_1 = require("@graphprotocol/cost-model");
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const getVariable = (vars, name) => {
        if (vars === null) {
            return undefined;
        }
        else {
            try {
                if (Object.prototype.hasOwnProperty.call(vars, name)) {
                    return vars[name];
                }
                else {
                    return undefined;
                }
            }
            catch (e) {
                return undefined;
            }
        }
    };
    const setVariable = (vars, name,
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    value) => {
        if (vars === null) {
            return { [name]: value };
        }
        else {
            try {
                vars[name] = value;
                return vars;
            }
            catch (e) {
                return vars;
            }
        }
    };
    exports.default = {
        costModel: ({ deployment }, { models }) => __awaiter(void 0, void 0, void 0, function* () {
            const model = yield models.CostModel.findOne({
                where: { deployment },
            });
            return (model === null || model === void 0 ? void 0 : model.toGraphQL()) || null;
        }),
        costModels: (
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        { deployments }, { models }) => __awaiter(void 0, void 0, void 0, function* () {
            const costModels = yield models.CostModel.findAll({
                where: deployments ? { deployment: deployments } : undefined,
                order: [['deployment', 'ASC']],
            });
            return costModels.map((model) => model.toGraphQL());
        }),
        setCostModel: ({ costModel }, { models, features, dai }) => __awaiter(void 0, void 0, void 0, function* () {
            const update = models_1.parseGraphQLCostModel(costModel);
            // Validate cost model
            try {
                const modelForValidation = update.model || 'default => 1;';
                const variablesForValidation = JSON.stringify(update.variables || {});
                yield cost_model_1.compileAsync(modelForValidation, variablesForValidation);
            }
            catch (err) {
                throw new Error(`Invalid cost model or variables: ${err.message}`);
            }
            const [model] = yield models.CostModel.findOrBuild({
                where: { deployment: update.deployment },
            });
            model.deployment = costModel.deployment || model.deployment;
            model.model =
                costModel.model !== null && costModel.model !== undefined
                    ? costModel.model
                    : model.model;
            // Update the model variables (fall back to current value if unchanged)
            let variables = update.variables || model.variables;
            if (features.injectDai) {
                const oldDai = getVariable(model.variables, 'DAI');
                const newDai = getVariable(update.variables, 'DAI');
                // Inject the latest DAI value if available
                if (dai.valueReady) {
                    variables = setVariable(variables, 'DAI', yield dai.value());
                }
                else if (newDai === undefined && oldDai !== undefined) {
                    // Otherwise preserve the old DAI value if there is one;
                    // this ensures it's never dropped
                    variables = setVariable(variables, 'DAI', oldDai);
                }
            }
            // Apply new variables
            model.variables = variables;
            return (yield model.save()).toGraphQL();
        }),
    };
    //# sourceMappingURL=cost-models.js.map