All files / app/pages/stacking/direct-stacking/components direct-stacking-info-card.tsx

0% Statements 0/26
0% Branches 0/17
0% Functions 0/3
0% Lines 0/25

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 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132                                                                                                                                                                                                                                                                       
import {
  InfoCard,
  InfoCardLabel as Label,
  InfoCardRow as Row,
  InfoCardGroup as Group,
  InfoCardValue as Value,
  InfoCardSection as Section,
} from '../../../../components/info-card';
import { calculateRewardSlots, calculateStackingBuffer } from '../../utils/calc-stacking-buffer';
import { Hr } from '@components/hr';
import { UI_IMPOSED_MAX_STACKING_AMOUNT_USTX } from '@constants/index';
import { Box, Flex, FlexProps, Text } from '@stacks/ui';
import { selectPoxInfo } from '@store/stacking';
import { parseNumericalFormInput } from '@utils/form/parse-numerical-form-input';
import { truncateMiddle } from '@utils/tx-utils';
import { stxToMicroStx, toHumanReadableStx } from '@utils/unit-convert';
import { BigNumber } from 'bignumber.js';
import dayjs from 'dayjs';
import React, { FC, useMemo } from 'react';
import { useSelector } from 'react-redux';
 
interface StackingInfoCardProps extends FlexProps {
  cycles: number;
  duration: string;
  startDate: Date;
  blocksPerCycle: number;
  btcAddress: string;
  amount: number | string | null;
  fee: BigNumber;
}
export const DirectStackingInfoCard: FC<StackingInfoCardProps> = props => {
  const { cycles, duration, amount, btcAddress, blocksPerCycle, startDate, fee, ...rest } = props;
 
  const poxInfo = useSelector(selectPoxInfo);
 
  const amountToBeStacked = useMemo(
    () => stxToMicroStx(parseNumericalFormInput(amount)).integerValue(),
    [amount]
  );
 
  const humanReadableAmount = useMemo(() => {
    // There is no enforced upper limit for direct stacking
    // but for rididuclous numbers we don't display in UI to prevent layouts breaking
    Iif (amountToBeStacked.isGreaterThan(UI_IMPOSED_MAX_STACKING_AMOUNT_USTX.multipliedBy(100))) {
      return '—';
    }
    return toHumanReadableStx(amountToBeStacked);
  }, [amountToBeStacked]);
 
  const numberOfRewardSlots = calculateRewardSlots(
    amountToBeStacked,
    new BigNumber(poxInfo?.min_amount_ustx || 0)
  ).integerValue();
 
  const buffer = calculateStackingBuffer(
    amountToBeStacked,
    new BigNumber(poxInfo?.min_amount_ustx || 0)
  );
 
  return (
    <InfoCard minHeight="84px" {...rest}>
      <Box mx={['loose', 'extra-loose']}>
        <Flex flexDirection="column" pt="extra-loose" pb="base-loose">
          <Text textStyle="body.large.medium">You&apos;ll lock</Text>
          <Text
            fontSize="24px"
            mt="extra-tight"
            fontWeight={500}
            fontFamily="Open Sauce"
            letterSpacing="-0.02em"
          >
            {humanReadableAmount}
          </Text>
        </Flex>
        <Hr />
        <Group width="100%" mt="base-loose" mb="extra-loose">
          <Section>
            <Row>
              <Label explainer="This is the estimated number of reward slots. The minimum can change before the next cycle begins.">
                Reward slots
              </Label>
              <Value>{numberOfRewardSlots.toString()}</Value>
            </Row>
 
            <Row>
              <Label>Buffer</Label>
              <Value>{buffer.isEqualTo(0) ? 'No buffer' : toHumanReadableStx(buffer)}</Value>
            </Row>
          </Section>
 
          <Section>
            <Row>
              <Label
                explainer={`One cycle lasts ${blocksPerCycle} blocks on the Bitcoin blockchain`}
              >
                Cycles
              </Label>
              <Value>{cycles}</Value>
            </Row>
 
            <Row>
              <Label>Start date</Label>
              <Value>{dayjs(startDate).format('MMMM DD')}</Value>
            </Row>
 
            <Row>
              <Label explainer="The duration is an estimation that varies depending on the Bitcoin block time">
                Duration
              </Label>
              <Value>~{duration}</Value>
            </Row>
          </Section>
 
          <Section>
            <Row>
              <Label>Bitcoin address</Label>
              <Value>{btcAddress ? truncateMiddle(btcAddress) : '—'}</Value>
            </Row>
          </Section>
 
          <Section>
            <Row>
              <Label>Fee</Label>
              <Value>{toHumanReadableStx(fee).toString()}</Value>
            </Row>
          </Section>
        </Group>
      </Box>
    </InfoCard>
  );
};