Skip to content

Commit

Permalink
[DEC-2099] lib/quantums.go audit: improve comments, add unit tests (#701
Browse files Browse the repository at this point in the history
)

* Add test, fix comments

* nit

* nit
  • Loading branch information
teddyding authored Oct 25, 2023
1 parent 65f556f commit fe2422e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
12 changes: 8 additions & 4 deletions protocol/lib/quantums.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// (10^quoteCurrencyAtomicResolution)
// =
// baseQuantums * priceValue *
// 10^(priceExponent + baseCurrencyAtomicResolution - quoteCurrencyAtomicResolution)
// 10^(priceExponent + baseCurrencyAtomicResolution - quoteCurrencyAtomicResolution) [expression 1]
//
// The result is rounded down.
func BaseToQuoteQuantums(
Expand Down Expand Up @@ -87,7 +87,12 @@ func QuoteToBaseQuantums(
// multiplyByPrice multiples a value by price, factoring in exponents of base
// and quote currencies.
// Given `value`, returns result of the following:
// `value * priceValue * 10^(priceExponent + baseCurrencyAtomicResolution - quoteCurrencyAtomicResolution)`
//
// `value * priceValue * 10^(priceExponent + baseAtomicResolution - quoteAtomicResolution)` [expression 2]
//
// Note that both `BaseToQuoteQuantums` and `FundingRateToIndex` directly wrap around this function.
// - For `BaseToQuoteQuantums`, substituing `value` with `baseQuantums` in expression 2 yields expression 1.
// - For `FundingRateToIndex`, substituing `value` with `fundingRatePpm * time` in expression 2 yields expression 3.
func multiplyByPrice(
value *big.Rat,
baseCurrencyAtomicResolution int32,
Expand Down Expand Up @@ -122,7 +127,7 @@ func multiplyByPrice(
// - right side:
// ```
// fundingRate * time * quoteQuantums / baseQuantums = fundingRatePpm / 1_000_000 *
// priceValue * 10^(priceExponent + baseCurrencyAtomicResolution - quoteCurrencyAtomicResolution)
// priceValue * 10^(priceExponent + baseCurrencyAtomicResolution - quoteCurrencyAtomicResolution) [expression 3]
// ```
//
// Hence, further multiplying both sides by 1_000_000, we have:
Expand All @@ -134,7 +139,6 @@ func multiplyByPrice(
// Arguments:
//
// proratedFundingRate: prorated funding rate adjusted by time delta, in parts-per-million
// timeSinceLastFunding: time (in seconds) since last funding index update
// baseCurrencyAtomicResolution: atomic resolution of the base currency
// priceValue: index price of the perpetual market according to the pricesKeeper
// priceExponent: priceExponent of the market according to the pricesKeeper
Expand Down
14 changes: 14 additions & 0 deletions protocol/lib/quantums_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ func TestQuoteToBaseQuantums(t *testing.T) {
priceExponent: 0,
bigExpectedBaseQuantums: big.NewInt(5_000_000),
},
"realistic values: 1 BTC at $29001": {
bigQuoteQuantums: big.NewInt(29_001_000_000), // $29_001
baseCurrencyAtomicResolution: -10,
priceValue: 2_900_100_000,
priceExponent: -5,
bigExpectedBaseQuantums: big.NewInt(10_000_000_000),
},
"realistic values: 25.123 BTC at $29001": {
bigQuoteQuantums: big.NewInt(728_592_123_000), // $728_592.123
baseCurrencyAtomicResolution: -10,
priceValue: 2_900_100_000,
priceExponent: -5,
bigExpectedBaseQuantums: big.NewInt(251_230_000_000),
},
"baseCurrencyAtomicResolution is greater than 10^6": {
bigQuoteQuantums: big.NewInt(350_000),
baseCurrencyAtomicResolution: -8,
Expand Down

0 comments on commit fe2422e

Please sign in to comment.