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

94.12% Statements 16/17
89.47% Branches 17/19
100% Functions 1/1
94.12% Lines 16/17

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

    1x
    1x
    1x

    1x
    15x
    1x

    14x
    3x

    11x
    1x

    10x
    10x

    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 } = require('leap-core');
    const { BigInt, equal, lessThan } = require('jsbi-utils');
    const { isNFT, isNST, 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 txColor = tx.outputs[0].color;
      Iif (
        !isNFT(txColor) &&
        !isNST(txColor) &&
        lessThan(BigInt(tx.outputs[0].value), BigInt(1))
      ) {
        throw new Error('Deposit out has value < 1');
      }
      const deposit = bridgeState.deposits[tx.options.depositId];
      if (
        !deposit ||
        !equal(BigInt(deposit.amount), BigInt(tx.outputs[0].value)) ||
        Number(deposit.color) !== txColor ||
        !addrCmp(deposit.depositor, tx.outputs[0].address) ||
        // NFTs do not have data :)
        (isNST(txColor) && deposit.data !== tx.outputs[0].data)
      ) {
        throw new Error('Trying to submit incorrect deposit');
      }
      state.processedDeposit += 1;
    };