# All files Value.ts

100% Statements 31/31
100% Branches 4/4
100% Functions 23/23
100% Lines 31/31

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
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202

    1x

    54x

    83x

    30x

    4x

    44x

    4x

    72x

    28x

    28x

    96x

    40x

    261x

    2x

    6x
    6x

    6x

    3x
    1x

    2x
    2x

    3x
    1x

    2x

    6x

    4x

    6x

    6x

    6x

    8x
    8x

     import { ICloneable } from './ICloneable';

    /**
     * Represents bitcoin value that can be converted to or from multiple
     * formats.
     */
    export class Value implements ICloneable<Value> {
      /**
       * Creates a value object from value in bitcoin, eg: 1.12345678
       * @param num
       */
      public static fromBitcoin(num: number): Value {
        return Value.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): Value {
        return new Value(BigInt(num) * BigInt(1e12));
      }

      /**
       * Creates a value instance from value in millisatoshis, 1/1000 of a
       * satoshi.
       * eg: 123 millisatoshis equates to 0.123 satoshis
       * eg: 123 millisatoshis equates to 0.00000000123 bitcoin
       * @param num
       */
      public static fromMilliSats(num: bigint | number): Value {
        return new Value(BigInt(num) * BigInt(1e9));
      }

      /**
       * Creates a value instance from value in microsatoshis, 1/1e6 of a
       * satoshi.
       * eg: 123 microsatoshis equates to 0.000123 satoshis
       * eg: 123 microsatoshis equates to 0.00000000000123 bitcoin
       * @param num
       */
      public static fromMicroSats(num: bigint | number): Value {
        return new Value(BigInt(num) * BigInt(1e6));
      }

      /**
       * Creates a value instance from value in picosatoshis, 1/1e12 of a
       * satoshi.
       * eg: 123 picosatoshis equates to 0.000000000123 satoshis
       * eg: 123 picosatoshis equates to 0.00000000000000000123 bitcoin
       * @param num
       */
      public static fromPicoSats(num: bigint | number): Value {
        return new Value(BigInt(num));
      }

      /**
       * Generates a value instance of zero
       */
      public static zero(): Value {
        return new Value(BigInt(0));
      }

      protected _picoSats: bigint;

      /**
       * Gets the value in picosatoshis (1/1e12 satoshis)
       */
      public get psats(): bigint {
        return this._picoSats;
      }

      /**
       * Gets the value in millionth of satoshis (1/1e6 satoshis)
       */
      public get microsats(): bigint {
        return this._picoSats / BigInt(1e6);
      }

      /**
       * Gets the value in millisatoshis (1/1000 satoshis)
       */
      public get msats(): bigint {
        return this._picoSats / BigInt(1e9);
      }

      /**
       * Gets the value in satoshis (1/1e8 bitcoin)
       */
      public get sats(): bigint {
        return this._picoSats / BigInt(1e12);
      }

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

      protected constructor(picoSats: bigint) {
        this._picoSats = picoSats;
      }

      /**
       * Clone via deep copy
       */
      public clone(): Value {
        return new Value(this._picoSats);
      }

      /**
       * Adds another value to this instance and returns this instance
       * @param other
       */
      public add(other: Value): this {
        this._picoSats += other.psats;
        return this;
      }

      /**
       * Adds another value to the current value and returns a new Value instance
       * @param other
       */
      public addn(other: Value): Value {
        return new Value(this._picoSats + other.psats);
      }

      /**
       * Subtracts another value from this instance and returns this instance
       * @param other
       */
      public sub(other: Value): this {
        if (this._picoSats < other.psats) {
          throw new Error('Value underflow');
        }
        this._picoSats -= other.psats;
        return this;
      }

      /**
       * Subtracts another value from the current value and returns a new Value instance
       * @param other
       */
      public subn(other: Value): Value {
        if (this._picoSats < other.psats) {
          throw new Error('Value underflow');
        }
        return new Value(this._picoSats - other.psats);
      }

      /**
       * Returns true if this value is less than the other value
       * @param other
       */
      public lt(other: Value): boolean {
        return this._picoSats < other.psats;
      }

      /**
       * Returns true if this value is equal to the other value
       * @param other
       */
      public eq(other: Value): boolean {
        return this._picoSats === other.psats;
      }

      /**
       * Returns true if this value is greater than the other value
       * @param other
       */
      public gt(other: Value): boolean {
        return this._picoSats > other.psats;
      }

      /**
       * Returns true if this value is greater than or equal to the other value
       * @param other
       */
      public gte(other: Value): boolean {
        return this._picoSats >= other.psats;
      }

      /**
       * Returns true if this value is less than or equal to the other value
       * @param other
       */
      public lte(other: Value): boolean {
        return this._picoSats <= other.psats;
      }

      /**
       * Returns a string representation of the value in bitcoin format
       * with 8 decimal places
       */
      public toString(): string {
        const btc = this.bitcoin;
        return btc.toFixed(8);
      }
    }