-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: calcTotalPrice calcPrice calcUnits with bigint
- Loading branch information
Showing
5 changed files
with
93 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { expect, test } from 'vitest' | ||
|
||
import { calcPrice, calcTotalPrice, calcUnits } from './price' | ||
|
||
test('calculate unit price from units and total price', () => { | ||
expect(calcPrice(BigInt(2e6), BigInt(1e5), 6, 6)).toMatchInlineSnapshot('50000n') | ||
expect(calcPrice(BigInt(100e18), BigInt(1e5), 18, 6)).toMatchInlineSnapshot('1000n') | ||
expect(calcPrice(BigInt(-123e26), BigInt(1e5), 26, 6)).toMatchInlineSnapshot('813n') | ||
expect(calcPrice(BigInt(-100e18), BigInt(1e5), 18, 6, true)).toMatchInlineSnapshot('-1000n') | ||
}) | ||
|
||
test('calculate units from unit price and total price', () => { | ||
expect(calcUnits(BigInt(1e5), BigInt(2e6))).toMatchInlineSnapshot('50000000000000000n') | ||
expect(calcUnits('723400000', '2124000000', 6, 8)).toMatchInlineSnapshot('34058380n') | ||
expect(calcUnits(BigInt(723400000e6), BigInt(21240000e6), 36, 18)).toMatchInlineSnapshot('34058380414312617702n') | ||
expect(calcUnits('100000000', '1000000')).toMatchInlineSnapshot('100000000000000000000n') | ||
expect(calcUnits('1000', '1200', 2, 2)).toMatchInlineSnapshot('83n') | ||
}) | ||
|
||
test('calculate total price from units and unit price', () => { | ||
expect(calcTotalPrice('100', '12', 2, 2)).toMatchInlineSnapshot('12n') | ||
expect(calcTotalPrice(BigInt(2223e17), '12', 18, 1)).toMatchInlineSnapshot('2667n') | ||
expect(calcTotalPrice(BigInt(2223e17), '1200000', 18, 6)).toMatchInlineSnapshot('266760000n') | ||
expect(calcTotalPrice(BigInt(2223e17), '12000000', 18, 6)).toMatchInlineSnapshot('2667600000n') | ||
expect(calcTotalPrice(BigInt(20e9), BigInt(2e6), 9, 6)).toMatchInlineSnapshot('40000000n') | ||
expect(calcTotalPrice('20', '2', 0, 0)).toMatchInlineSnapshot('40n') | ||
expect(calcTotalPrice('20', '2', 1, 0)).toMatchInlineSnapshot('4n') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { absBig } from './bigUtils' | ||
import { scale } from './scale' | ||
import { unScaleToBase } from './unscale' | ||
|
||
const precisionCatalyst = 6 // to avoid loss of precision if the given decimals are not enough | ||
|
||
/** | ||
* Calculates the unit price based on the number of units and total price. | ||
* @param noAbs pass `true` to allow -ve values for resulted unit price | ||
* @example | ||
* calcPrice(BigInt(2e6), BigInt(1e5), 6, 6) // 50000n | ||
*/ | ||
export function calcPrice( | ||
units: string | bigint, totalPrice: string | bigint, unitDecimals = 18, priceDecimals = 6, noAbs = false, | ||
) { | ||
if (!noAbs) // due to nature of the exchange, we need to support negative values for units and return +ve unit price | ||
units = absBig(units) | ||
|
||
// to avoid underflow and retain precision | ||
const priceDecimalsFactor = priceDecimals + priceDecimals + unitDecimals + precisionCatalyst | ||
|
||
return unScaleToBase( | ||
scale(totalPrice.toString(), priceDecimalsFactor) / BigInt(units), | ||
priceDecimalsFactor, | ||
unitDecimals, | ||
) | ||
} | ||
|
||
/** | ||
* Calculates units based on the total price and number of units. | ||
* @example | ||
* calcUnits(BigInt(1e5), BigInt(2e6)) // 50000000000000000n | ||
* calcUnits('1000', '1200', 2, 2) // 83n === 0.83 * 10^2 | ||
*/ | ||
export function calcUnits( | ||
totalPrice: string | bigint, unitPrice: string | bigint, priceDecimals = 6, unitDecimals = 18, | ||
) { | ||
const priceDecimalsFactor = priceDecimals + priceDecimals + unitDecimals + precisionCatalyst | ||
return unScaleToBase( | ||
scale(totalPrice.toString(), priceDecimalsFactor) / BigInt(unitPrice), | ||
priceDecimalsFactor, | ||
unitDecimals, | ||
) | ||
} | ||
|
||
/** | ||
* Calculates the total price based on the number of units and unit price. | ||
* @param noAbs pass `true` to allow -ve values for resulted total price | ||
* @example | ||
* calcTotalPrice('100', '12', 2, 2) // 12n | ||
* BigInt(2223e17), '12000000', 18, 6) // 2667600000n | ||
*/ | ||
export function calcTotalPrice( | ||
units: string | bigint, unitPrice: string | bigint, unitDecimals = 18, priceDecimals = 6, noAbs = false, | ||
) { | ||
if (!noAbs) // due to nature of the exchange, we need to support negative values for units and return +ve total price | ||
units = absBig(units) | ||
|
||
return unScaleToBase( | ||
BigInt(units) * BigInt(unitPrice), | ||
unitDecimals + priceDecimals, | ||
priceDecimals, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters