FixedPointDecimal.ts
1 import { BigNumber, BigNumberish } from 'ethers'; 2 import { formatUnits } from 'ethers/lib/utils'; 3 4 export class FixedPointDecimal { 5 private readonly _value: BigNumber; 6 7 constructor(_value: BigNumberish, private readonly _decimals: number) { 8 this._value = BigNumber.from(_value); 9 } 10 11 get value() { 12 return this._value; 13 } 14 15 get decimals() { 16 return this._decimals; 17 } 18 19 add(value: FixedPointDecimal) { 20 return new FixedPointDecimal(this._value.add(value._value), this._decimals); 21 } 22 23 eq(value: FixedPointDecimal) { 24 return this._value.eq(value._value); 25 } 26 27 format() { 28 return formatUnits(this._value, this._decimals); 29 } 30 }