@node-dlc/bitcoin
Version:
68 lines • 1.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Amount = void 0;
const Value_1 = require("./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
*/
class Amount extends Value_1.Value {
/**
* Creates a value object from amount in bitcoin, eg: 1.12345678
* @param num
*/
static fromBitcoin(num) {
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
*/
static fromSats(num) {
return new Amount(BigInt(num) * BigInt(1e12));
}
/**
* Creates a value instance from amount
* @param value
* @returns
*/
static fromValue(value) {
return Amount.fromSats(value.sats);
}
/**
* Gets the value in bitcoin
*/
get bitcoin() {
return Number(this.sats) / 1e8;
}
constructor(picoSats) {
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
*/
sub(other) {
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
*/
subn(other) {
return new Amount(this._picoSats - other.psats);
}
}
exports.Amount = Amount;
//# sourceMappingURL=Amount.js.map