# All files / src/txHelpers submitPeriod.js

100% Statements 17/17
100% Branches 6/6
100% Functions 3/3
100% Lines 16/16

    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

    1x
    1x

    1x
    3x

    3x
    3x
    2x
    2x
    2x
    2x
    2x
    1x
    1x

    1x
    1x

    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 {
      getSlotsByAddr,
      sendTransaction,
      getCurrentSlotId,
      GENESIS,
    } = require('../utils');
    const { logPeriod } = require('../utils/debug');

    /* istanbul ignore next */
    const logError = height => err => {
      logPeriod('submitPeriod error: %s (height: %d)', err.message, height);
    };

    module.exports = async (period, slots, height, bridgeState) => {
      const submittedPeriod = await bridgeState.bridgeContract.methods
        .periods(period.merkleRoot())
        .call();

      // period not found
      logPeriod('submittedPeriod', period.merkleRoot(), submittedPeriod);
      if (submittedPeriod.timestamp === '0') {
        const mySlots = getSlotsByAddr(slots, bridgeState.account.address);
        const currentSlotId = getCurrentSlotId(slots, height);
        const currentSlot = mySlots.find(slot => slot.id === currentSlotId);
        logPeriod('mySlots', currentSlotId, mySlots);
        if (currentSlot) {
          logPeriod('submitPeriod. Slot %d', currentSlot.id);
          const tx = sendTransaction(
            bridgeState.web3,
            bridgeState.operatorContract.methods.submitPeriod(
              currentSlot.id,
              period.prevHash || GENESIS,
              period.merkleRoot()
            ),
            bridgeState.operatorContract.options.address,
            bridgeState.account
          ).catch(logError(height));

          tx.then(receipt => {
            logPeriod('submitPeriod tx', receipt);
          });
        }
      }

      return submittedPeriod;
    };