-
Notifications
You must be signed in to change notification settings - Fork 85
/
price_to_subticks.go
82 lines (78 loc) · 2.61 KB
/
price_to_subticks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package types
import (
"math/big"
"github.com/dydxprotocol/v4-chain/protocol/lib"
pricestypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types"
)
// PriceToSubticks converts price value from Prices module into subticks.
// By construction of the Clob module:
// `sizeQuoteQuantums = subticks * baseQuantums * 10^quantumConversionExponent`
// Substituting `baseQuantums` with a full coin of the base currency:
//
// `usdcPrice * 10^(-quoteAtomicResolution) = subticks * 10^(-baseAtomicResolution) *
// 10^quantumConversionExponent` (A)
//
// By construction of Prices module:
//
// `usdcPrice = marketPrice.Price * 10^marketPrice.Exponent` (B)
//
// Combining equations (A) & (B), we get:
//
// `subticks = marketPrice.Price * 10^(marketPrice.Exponent - quantumConversionExponent +
// baseAtomicResolution - quoteAtomicResolution)`
func PriceToSubticks(
marketPrice pricestypes.MarketPrice,
clobPair ClobPair,
baseAtomicResolution int32,
quoteAtomicResolution int32,
) (
ratSubticks *big.Rat,
) {
exponent := int32(
marketPrice.Exponent - clobPair.QuantumConversionExponent + baseAtomicResolution - quoteAtomicResolution,
)
return lib.BigMulPow10(
// TODO(DEC-1256): Use index price from the price daemon, instead of oracle price.
new(big.Int).SetUint64(marketPrice.Price),
exponent,
)
}
// SubticksToPrice converts subticks into price value from Prices module.
// By construction of the Clob module:
// `sizeQuoteQuantums = subticks * baseQuantums * 10^quantumConversionExponent`
// Substituting `baseQuantums` with a full coin of the base currency:
//
// `usdcPrice * 10^(-quoteAtomicResolution) = subticks * 10^(-baseAtomicResolution) *
// 10^quantumConversionExponent` (A)
//
// By construction of Prices module:
//
// `usdcPrice = marketPrice.Price * 10^marketPrice.Exponent` (B)
//
// Combining equations (A) & (B), we get:
//
// `marketPrice.Price = subticks * 10^(-marketPrice.Exponent + quantumConversionExponent -
// baseAtomicResolution + quoteAtomicResolution)`
// Note this function rounds down in order to typecast into an int. It should really only be used
// in testing with well-defined integer subticks.
func SubticksToPrice(
subticks Subticks,
marketPriceExponent int32,
clobPair ClobPair,
baseAtomicResolution int32,
quoteAtomicResolution int32,
) (
price uint64,
) {
exponent := int32(
-marketPriceExponent + clobPair.QuantumConversionExponent - baseAtomicResolution + quoteAtomicResolution,
)
return lib.BigRatRound(
lib.BigMulPow10(
// TODO(DEC-1256): Use index price from the price daemon, instead of oracle price.
new(big.Int).SetUint64(uint64(subticks)),
exponent,
),
false,
).Uint64()
}