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

100% Statements 59/59
100% Branches 36/36
100% Functions 9/9
100% Lines 59/59

    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
    45x

    3x
    17x
    24x

    17x
    1x

    16x
    16x
    16x

    16x
    16x
    16x
    19x

    3x
    3x
    3x
    3x
    1x

    16x
    4x

    3x
    2x
    2x
    2x
    1x

    3x
    10x
    10x
    10x
    1x

    9x
    9x
    9x
    9x
    9x
    6x
    6x
    6x

    3x
    3x

    9x

    3x
    4x
    4x
    4x

    4x
    3x
    3x
    3x

    1x

    4x

    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, Output, Type } = require('leap-core');
    const isEqual = require('lodash/isEqual');
    const uniq = require('lodash/uniq');

    const groupValuesByColor = (values, { color, value }) =>
      Object.assign({}, values, {
        [color]: Output.isNFT(color)
          ? (values[color] || new Set()).add(value)
          : (values[color] || 0) + 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 = bridgeState.minGasPrice || 0;
      const gas = Math.max(0, tx.outputs.length * 20000 - tx.inputs.length * 10000);
      for (const color of colors) {
        if (
          color === 0 &&
          gas > 0 &&
          minGasPrice > 0 &&
          tx.type === Type.TRANSFER
        ) {
          const inputValue = insValues[color];
          const outputValue = outsValues[color];
          const txGasPrice = (inputValue - outputValue) / gas;
          if (txGasPrice < minGasPrice) {
            throw new Error(`Tx underpriced`);
          }
        } else if (!isEqual(insValues[color], outsValues[color])) {
          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 (Output.isNFT(out.color)) {
          cBalances[out.address] = cBalances[out.address] || [];
          cBalances[out.address].push(out.value);
          cOwners[out.value] = out.address;
        } else {
          cBalances[out.address] = cBalances[out.address] || 0;
          cBalances[out.address] += out.value;
        }

        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 (Output.isNFT(color)) {
          const index = balances[color][address].indexOf(value);
          balances[color][address].splice(index, 1);
          delete owners[color][value];
        } else {
          balances[color][address] -= value;
        }
        delete unspent[outpointId];
      });
    };

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