# All files / src/tx/applyTx utils.js

98.41% Statements 62/63
97.83% Branches 45/46
100% Functions 9/9
98.41% Lines 62/63

    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

    3x

    3x
    3x
    3x

    3x
    58x

    58x

    3x
    20x
    26x

    20x
    1x

    19x
    19x
    19x

    19x
    19x
    19x
    23x
    23x
    23x

    13x
    13x
    1x

    22x

    6x

    3x
    2x
    2x
    2x
    1x

    3x
    11x
    11x
    11x
    1x

    10x
    10x
    10x
    10x
    10x
    6x
    6x
    6x

    4x
    4x

    10x

    3x
    4x
    4x
    4x

    4x
    3x
    3x
    3x

    1x

    4x

    3x
    3x
    3x
    3x
    3x
     /**
     * Copyright (c) 2018-present, Leap DAO (leapdao.org)
     *
     * This source code is licensed under the Mozilla Public License Version 2.0
     * found in the LICENSE file in the root directory of this source tree.
     */

    const { Outpoint, Type } = require('leap-core');
    const {
      BigInt,
      add,
      subtract,
      divide,
      lessThan,
      greaterThan,
    } = require('jsbi-utils');
    const { isNFT } = require('../../utils');
    const { uniq, isEqual } = require('lodash');

    const groupValuesByColor = (values, { color, value }) => {
      Iif (!isNFT(color) && lessThan(BigInt(value), BigInt(1))) {
        throw new Error('One of the outs has value < 1');
      }
      return Object.assign({}, values, {
        [color]: isNFT(color)
          ? (values[color] || new Set()).add(BigInt(value))
          : add(values[color] || BigInt(0), BigInt(value)),
      });
    };

    const checkInsAndOuts = (tx, state, bridgeState, unspentFilter) => {
      const inputTransactions = tx.inputs
        .map(input => state.unspent[input.prevout.hex()])
        .filter(unspentFilter);
      if (tx.inputs.length !== inputTransactions.length) {
        throw new Error('Wrong inputs');
      }

      const insValues = inputTransactions.reduce(groupValuesByColor, {});
      const outsValues = tx.outputs.reduce(groupValuesByColor, {});
      const colors = uniq([
        ...Object.keys(insValues),
        ...Object.keys(outsValues),
      ]).map(Number);
      const minGasPrice = BigInt(state.gas.minPrice);
      const gas = Math.max(0, tx.outputs.length * 20000 - tx.inputs.length * 10000);
      for (const color of colors) {
        const inputValue = insValues[color] || BigInt(0);
        const outputValue = outsValues[color] || BigInt(0);
        if (
          color === 0 &&
          gas > 0 &&
          greaterThan(minGasPrice, 0) &&
          tx.type === Type.TRANSFER
        ) {
          const txGasPrice = divide(subtract(inputValue, outputValue), BigInt(gas));
          if (lessThan(txGasPrice, minGasPrice)) {
            throw new Error(`Tx underpriced`);
          }
        }
        if (
          (isNFT(color) && !isEqual(inputValue, outputValue)) ||
          (!isNFT(color) && greaterThan(outputValue, inputValue))
        ) {
          throw new Error(`Ins and outs values are mismatch for color ${color}`);
        }
      }
    };

    const checkOutpoints = (state, tx) => {
      tx.inputs.forEach(input => {
        const outpointId = input.prevout.hex();
        if (!state.unspent[outpointId]) {
          throw new Error('Trying to spend non-existing output');
        }
      });
    };

    const addOutputs = ({ balances, owners, unspent }, tx) => {
      tx.outputs.forEach((out, outPos) => {
        const outpoint = new Outpoint(tx.hash(), outPos);
        if (unspent[outpoint.hex()] !== undefined) {
          throw new Error('Attempt to create existing output');
        }
        balances[out.color] = balances[out.color] || {};
        owners[out.color] = owners[out.color] || {};
        const cBalances = balances[out.color];
        const cOwners = owners[out.color];
        if (isNFT(out.color)) {
          cBalances[out.address] = cBalances[out.address] || [];
          cBalances[out.address].push(out.value);
          cOwners[out.value] = out.address;
        } else {
          cBalances[out.address] = BigInt(cBalances[out.address] || 0);
          cBalances[out.address] = add(
            cBalances[out.address],
            out.value
          ).toString();
        }
        unspent[outpoint.hex()] = out.toJSON();
      });
    };

    const removeInputs = ({ unspent, balances, owners }, tx) => {
      tx.inputs.forEach(input => {
        const outpointId = input.prevout.hex();
        const { address, value, color } = unspent[outpointId];

        if (isNFT(color)) {
          const index = balances[color][address].indexOf(value);
          balances[color][address].splice(index, 1);
          delete owners[color][BigInt(value).toString()];
        } else {
          balances[color][address] = subtract(
            BigInt(balances[color][address]),
            BigInt(value)
          ).toString();
        }
        delete unspent[outpointId];
      });
    };

    exports.checkInsAndOuts = checkInsAndOuts;
    exports.checkOutpoints = checkOutpoints;
    exports.groupValuesByColor = groupValuesByColor;
    exports.addOutputs = addOutputs;
    exports.removeInputs = removeInputs;