mirror of https://github.com/ghostfolio/ghostfolio
4 changed files with 67 additions and 1 deletions
@ -0,0 +1,44 @@ |
|||||
|
import { PortfolioPosition } from '@ghostfolio/common/interfaces'; |
||||
|
|
||||
|
import { AssetSubClass } from '@prisma/client'; |
||||
|
|
||||
|
import { getHoldingQuantityPrecision } from './quantity-precision'; |
||||
|
|
||||
|
function createHolding( |
||||
|
quantity: number, |
||||
|
assetSubClass: AssetSubClass |
||||
|
): Pick<PortfolioPosition, 'assetProfile' | 'quantity'> { |
||||
|
return { |
||||
|
assetProfile: { assetSubClass } as PortfolioPosition['assetProfile'], |
||||
|
quantity |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
describe('getHoldingQuantityPrecision', () => { |
||||
|
it('preserves satoshi precision for fractional cryptocurrency quantities', () => { |
||||
|
expect( |
||||
|
getHoldingQuantityPrecision( |
||||
|
createHolding(0.01525217, AssetSubClass.CRYPTOCURRENCY) |
||||
|
) |
||||
|
).toBe(8); |
||||
|
expect( |
||||
|
getHoldingQuantityPrecision( |
||||
|
createHolding(10.12345678, AssetSubClass.CRYPTOCURRENCY) |
||||
|
) |
||||
|
).toBe(8); |
||||
|
}); |
||||
|
|
||||
|
it('does not add decimal places to integer cryptocurrency quantities', () => { |
||||
|
expect( |
||||
|
getHoldingQuantityPrecision( |
||||
|
createHolding(12, AssetSubClass.CRYPTOCURRENCY) |
||||
|
) |
||||
|
).toBe(0); |
||||
|
}); |
||||
|
|
||||
|
it('keeps the existing precision for non-cryptocurrency quantities', () => { |
||||
|
expect( |
||||
|
getHoldingQuantityPrecision(createHolding(12.3456, AssetSubClass.STOCK)) |
||||
|
).toBe(2); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,17 @@ |
|||||
|
import { PortfolioPosition } from '@ghostfolio/common/interfaces'; |
||||
|
|
||||
|
import { AssetSubClass } from '@prisma/client'; |
||||
|
|
||||
|
export function getHoldingQuantityPrecision( |
||||
|
holding: Pick<PortfolioPosition, 'assetProfile' | 'quantity'> |
||||
|
): number { |
||||
|
if (Number.isInteger(holding.quantity)) { |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
if (holding.assetProfile.assetSubClass === AssetSubClass.CRYPTOCURRENCY) { |
||||
|
return 8; |
||||
|
} |
||||
|
|
||||
|
return 2; |
||||
|
} |
||||
Loading…
Reference in new issue