-
Notifications
You must be signed in to change notification settings - Fork 0
/
perpetuals.go
108 lines (90 loc) · 2.93 KB
/
perpetuals.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package perpetuals
import (
"math/big"
"github.com/furyanprotocol/v4-chain/protocol/dtypes"
perptypes "github.com/furyanprotocol/v4-chain/protocol/x/perpetuals/types"
)
type PerpetualModifierOption func(cp *perptypes.Perpetual)
func WithId(id uint32) PerpetualModifierOption {
return func(cp *perptypes.Perpetual) {
cp.Params.Id = id
}
}
func WithMarketId(id uint32) PerpetualModifierOption {
return func(cp *perptypes.Perpetual) {
cp.Params.MarketId = id
}
}
func WithPerpetual(perp perptypes.Perpetual) PerpetualModifierOption {
return func(cp *perptypes.Perpetual) {
cp.Params = perp.Params
}
}
func WithTicker(ticker string) PerpetualModifierOption {
return func(cp *perptypes.Perpetual) {
cp.Params.Ticker = ticker
}
}
func WithLiquidityTier(liquidityTier uint32) PerpetualModifierOption {
return func(cp *perptypes.Perpetual) {
cp.Params.LiquidityTier = liquidityTier
}
}
// GeneratePerpetual returns a `Perpetual` object set to default values.
// Passing in `PerpetualModifierOption` methods alters the value of the `Perpetual` returned.
// It will start with the default, valid `Perpetual` value defined within the method
// and make the requested modifications before returning the object.
//
// Example usage:
// `GeneratePerpetual(WithId(10))`
// This will start with the default `Perpetual` object defined within the method and
// return the newly-created object after overriding the values of
// `Id` to 10.
func GeneratePerpetual(optionalModifications ...PerpetualModifierOption) *perptypes.Perpetual {
perpetual := &perptypes.Perpetual{
Params: perptypes.PerpetualParams{
Id: 0,
Ticker: "BTC-USD",
MarketId: 0,
AtomicResolution: -8,
DefaultFundingPpm: 0,
LiquidityTier: 0,
},
FundingIndex: dtypes.ZeroInt(),
}
for _, opt := range optionalModifications {
opt(perpetual)
}
return perpetual
}
func MustHumanSizeToBaseQuantums(
humanSize string,
atomicResolution int32,
) (baseQuantums uint64) {
// Parse the humanSize string to a big rational
ratValue, ok := new(big.Rat).SetString(humanSize)
if !ok {
panic("Failed to parse humanSize to big.Rat")
}
// Convert atomicResolution to int64 for calculations
resolution := int64(atomicResolution)
// Create a multiplier which is 10 raised to the power of the absolute atomicResolution
multiplier := new(big.Int).Exp(big.NewInt(10), big.NewInt(abs(resolution)), nil)
// Depending on the sign of atomicResolution, multiply or divide
if atomicResolution > 0 {
ratValue.Mul(ratValue, new(big.Rat).SetInt(multiplier))
} else if atomicResolution < 0 {
divisor := new(big.Rat).SetInt(multiplier)
ratValue.Mul(ratValue, divisor)
}
// Convert the result to an unsigned 64-bit integer
resultInt := ratValue.Num() // Get the numerator which now represents the whole value
return resultInt.Uint64()
}
// Helper function to get the absolute value of an int64
func abs(n int64) int64 {
if n < 0 {
return -n
}
return n
}