All files Value.ts

96.43% Statements 27/28
100% Branches 2/2
95.45% Functions 21/22
96.43% Lines 27/28

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 2011x             1x           90x                 144x                     15x                     2x                     22x             2x                 8x             14x             14x             175x             36x       232x             47x               4x                 2x                                   3x                   3x               3x                   3x                 3x 3x                   3x 1x   2x 2x      
import { BitcoinError, BitcoinErrorCode } from ".";
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.trunc(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) {
        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) {
        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) {
        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) {
        return new Value(BigInt(num));
    }
 
    /**
     * Generates a value instance of zero
     */
    public static zero(): Value {
        return new Value(BigInt(0));
    }
 
    private _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);
    }
 
    private constructor(picoSats: bigint) {
        this._picoSats = picoSats;
    }
 
    /**
     * Clone via deep copy
     */
    public clone(): Value {
        return new Value(this._picoSats);
    }
 
    /**
     * Returns the value as a string of Bitcoin. This is a fixed eight
     * decimal string, as such value below satoshis will be truncated.
     */
    public toString(): string {
        return this.bitcoin.toFixed(8);
    }
 
    /**
     * Returns true if the current value is equal to the other value
     * @param other
     * @returns
     */
    public eq(other: Value): boolean {
        return other._picoSats === this._picoSats;
    }
 
    /**
     * Returns true if the current value is not equal to the other value
     * @param other
     * @returns
     */
    public neq(other: Value): boolean {
        return other._picoSats !== this._picoSats;
    }
 
    /**
     * Returns true if the current value is greater than the other value.
     * @param other
     * @returns
     */
    public gt(other: Value): boolean {
        return this._picoSats > other._picoSats;
    }
 
    /**
     * Returns true if the current value is greater than or equal to the
     * other value.
     * @param other
     * @returns
     */
    public gte(other: Value): boolean {
        return this._picoSats >= other._picoSats;
    }
 
    /**
     * Returns true if the current value is less than the other value.
     * @param other
     */
    public lt(other: Value): boolean {
        return this._picoSats < other._picoSats;
    }
 
    /**
     * Returns true if the current value is less than or equal to the
     * other value.
     * @param other
     * @returns
     */
    public lte(other: Value): boolean {
        return this._picoSats <= other._picoSats;
    }
 
    /**
     * Modifies the current instance by adding the other value to the
     * existing value.
     * @param other
     */
    public add(other: Value): this {
        this._picoSats += other._picoSats;
        return this;
    }
 
    /**
     * Modifies the current instance by subtracting the other value
     * from our existing value. Since Value is unsigned, this throws
     * if subtraction results in a value that is less than zero.
     * @param other
     */
    public sub(other: Value): this {
        if (this._picoSats - other._picoSats < 0) {
            throw new BitcoinError(BitcoinErrorCode.ValueUnderflow);
        }
        this._picoSats -= other._picoSats;
        return this;
    }
}