# All files Amount.ts

90% Statements 9/10
100% Branches 0/0
85.71% Functions 6/7
90% Lines 9/10

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
    711x

    1x

    54x

    83x

    20x

    86x

    3x
    3x

    3x

     import { Value } from './Value';

    /**
     * Represents bitcoin amount that can be converted to or from multiple
     * formats.
     *
     * The key difference between Amount and Value, is Amount can be negative
     *
     * Value should be used for Bitcoin transactions, Amount should only be used
     * for UI display, i.e. PnL
     */
    export class Amount extends Value {
      /**
       * Creates a value object from amount in bitcoin, eg: 1.12345678
       * @param num
       */
      public static fromBitcoin(num: number): Amount {
        return Amount.fromSats(Math.round(num * 1e8));
      }

      /**
       * Creates a value instance from value in satoshis where 1 satoshis
       * equates to 0.00000001 bitcoin.
       * @param num
       */
      public static fromSats(num: bigint | number): Amount {
        return new Amount(BigInt(num) * BigInt(1e12));
      }

      /**
       * Creates a value instance from amount
       * @param value
       * @returns
       */
      public static fromValue(value: Value): Amount {
        return Amount.fromSats(value.sats);
      }

      /**
       * Gets the value in bitcoin
       */
      public get bitcoin(): number {
        return Number(this.sats) / 1e8;
      }

      private constructor(picoSats: bigint) {
        super(picoSats);
      }

      /**
       * Modifies the current instance by subtracting the other value
       * from our existing value. Since Amount is signed, the value can
       * be less than zero.
       * @param other
       */
      public sub(other: Value): this {
        this._picoSats -= other.psats;
        return this;
      }

      /**
       * Subtracts supplied value from the current value and returns a new
       * value instance. Since Amount is signed, the value can
       * be less than zero.
       * @param other
       */
      public subn(other: Value): Amount {
        return new Amount(this._picoSats - other.psats);
      }
    }