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

100% Statements 13/13
100% Branches 14/14
100% Functions 1/1
100% Lines 13/13

    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

    1x
    1x

    1x
    15x
    1x

    14x
    3x

    11x
    1x

    10x
    10x

    4x

    6x

     /**
     * 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 { Type, Output } = require('leap-core');
    const { addrCmp } = require('../../utils');

    module.exports = (state, tx, bridgeState) => {
      if (tx.type !== Type.DEPOSIT) {
        throw new Error('Deposit tx expected');
      }

      if (tx.options.depositId <= state.processedDeposit) {
        throw new Error('Deposit ID already used.');
      }
      if (tx.options.depositId > state.processedDeposit + 1) {
        throw new Error(
          `Deposit ID skipping ahead. want ${state.processedDeposit + 1}, found ${
            tx.options.depositId
          }`
        );
      }
      const deposit = bridgeState.deposits[tx.options.depositId];
      if (
        !deposit ||
        (Output.isNFT(Number(deposit.color))
          ? deposit.amount !== tx.outputs[0].value
          : Number(deposit.amount) !== tx.outputs[0].value) ||
        Number(deposit.color) !== tx.outputs[0].color ||
        !addrCmp(deposit.depositor, tx.outputs[0].address)
      ) {
        throw new Error('Trying to submit incorrect deposit');
      }
      state.processedDeposit += 1;
    };