All files / src/tx/applyTx checkDeposit.js

100% Statements 17/17
95% Branches 19/20
100% Functions 1/1
100% Lines 17/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 17x 1x     16x   16x 2x     14x 2x     12x   12x 1x     11x             3x           8x 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 { isNFT, isNST, addrCmp } = require('../../utils');
 
module.exports = (state, tx, bridgeState, nodeConfig, isCheck) => {
  if (tx.type !== Type.DEPOSIT) {
    throw new Error('Deposit tx expected');
  }
 
  const deposit = bridgeState.deposits[tx.options.depositId];
 
  if (!deposit) {
    throw new Error('Unexpected deposit: no Deposit event on the root chain');
  }
 
  if (deposit.included) {
    throw new Error('Deposit ID already used.');
  }
 
  const { color, value, address, data } = tx.outputs[0];
 
  if (!isNFT(color) && !isNST(color) && BigInt(value) < 1n) {
    throw new Error('Deposit out has value < 1');
  }
 
  if (
    BigInt(deposit.amount) !== BigInt(value) ||
    Number(deposit.color) !== color ||
    !addrCmp(deposit.depositor, address) ||
    // NFTs do not have data :)
    (isNST(color) && deposit.data !== data)
  ) {
    throw new Error(
      `Incorrect deposit tx. DepositId: ${tx.options.depositId} ` +
        `Expected: ${deposit.color}:${deposit.amount}:${deposit.depositor}:${deposit.data} ` +
        `Actual: ${color}:${value}:${address}:${data}`
    );
  }
  if (!isCheck) {
    deposit.included = true;
  }
};