-
Notifications
You must be signed in to change notification settings - Fork 3
/
sqrt_price_math.go
162 lines (137 loc) · 4.42 KB
/
sqrt_price_math.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package uniswap_v3_simulator
import (
"fmt"
"github.com/shopspring/decimal"
"math/big"
"strconv"
)
func GetAmount1Delta(
sqrtRatioAX96 decimal.Decimal,
sqrtRatioBX96 decimal.Decimal,
liquidity decimal.Decimal,
) (decimal.Decimal, error) {
if liquidity.IsNegative() {
r, err := GetAmount1DeltaWithRoundUp(sqrtRatioAX96, sqrtRatioBX96, liquidity.Neg(), false)
if err != nil {
return ZERO, err
}
return r.Neg(), nil
} else {
r, err := GetAmount1DeltaWithRoundUp(sqrtRatioAX96, sqrtRatioBX96, liquidity, true)
if err != nil {
return ZERO, err
}
return r, nil
}
}
func GetAmount0Delta(
sqrtRatioAX96 decimal.Decimal,
sqrtRatioBX96 decimal.Decimal,
liquidity decimal.Decimal,
) (decimal.Decimal, error) {
if liquidity.IsNegative() {
r, err := GetAmount0DeltaWithRoundUp(sqrtRatioAX96, sqrtRatioBX96, liquidity.Neg(), false)
if err != nil {
return ZERO, err
}
return r.Neg(), nil
} else {
r, err := GetAmount0DeltaWithRoundUp(sqrtRatioAX96, sqrtRatioBX96, liquidity, true)
if err != nil {
return ZERO, err
}
return r, nil
}
}
func GetAmount1DeltaWithRoundUp(
sqrtRatioAX96 decimal.Decimal,
sqrtRatioBX96 decimal.Decimal,
liquidity decimal.Decimal,
roundUp bool) (decimal.Decimal, error) {
if sqrtRatioAX96.GreaterThan(sqrtRatioBX96) {
sqrtRatioAX96 = sqrtRatioBX96
sqrtRatioBX96 = sqrtRatioAX96
}
tmp2, err := MulDivRoundingUp(liquidity, sqrtRatioBX96.Sub(sqrtRatioAX96), Q96)
if err != nil {
return ZERO, err
}
if roundUp {
return tmp2, nil
} else {
return liquidity.Mul(sqrtRatioBX96.Sub(sqrtRatioAX96)).Div(Q96).RoundDown(0), nil
}
}
func GetAmount0DeltaWithRoundUp(
sqrtRatioAX96 decimal.Decimal,
sqrtRatioBX96 decimal.Decimal,
liquidity decimal.Decimal,
roundUp bool) (decimal.Decimal, error) {
if sqrtRatioAX96.GreaterThan(sqrtRatioBX96) {
sqrtRatioAX96 = sqrtRatioBX96
sqrtRatioBX96 = sqrtRatioAX96
}
numerator1_bi := liquidity.BigInt()
numerator1 := decimal.NewFromBigInt(numerator1_bi.Lsh(numerator1_bi, 96), 0)
numerator2 := sqrtRatioBX96.Sub(sqrtRatioAX96)
tmp1, err := MulDivRoundingUp(numerator1, numerator2, sqrtRatioBX96)
if err != nil {
return ZERO, err
}
tmp2, err := MulDivRoundingUp(tmp1, ONE, sqrtRatioAX96)
if err != nil {
return ZERO, err
}
if roundUp {
return tmp2, nil
} else {
return numerator1.Mul(numerator2).Div(sqrtRatioBX96).RoundDown(0).Div(sqrtRatioAX96).RoundDown(0), nil
}
}
func SqrtRatioX962HumanPrice(sqrtRatioX96, price *big.Int, decimals0, decimals1 int) float64 {
squared := new(big.Int).Mul(sqrtRatioX96, sqrtRatioX96)
multiplier := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(decimals0)), nil)
squared.Mul(squared, multiplier)
result := new(big.Int).Mul(squared, price)
divisor := new(big.Int).Lsh(big.NewInt(1), 192)
result.Div(result, divisor)
tenToTheDecimals := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(decimals1)), nil)
quotient, remainder := new(big.Int).QuoRem(result, tenToTheDecimals, new(big.Int))
intPart, _ := strconv.ParseFloat(quotient.String(), 64)
remainderPart, _ := strconv.ParseFloat(remainder.String(), 64)
decimalPart := remainderPart / float64(tenToTheDecimals.Int64())
return intPart + decimalPart
}
func HumanPrice2SqrtRatioX96(price float64, decimals0, decimals1 int) (*big.Int, error) {
twoTo192 := new(big.Int).Exp(big.NewInt(2), big.NewInt(192), nil)
twoToDecimals0 := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(decimals0)), nil)
twoToDecimals1 := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(decimals1)), nil)
valueBigFloat := big.NewFloat(price)
result := new(big.Int)
valueBigFloat.Mul(valueBigFloat, new(big.Float).SetInt(twoToDecimals1))
valueBigFloat.Int(result)
fmt.Printf("%v\n", valueBigFloat.String())
fmt.Printf("%v\n", result.String())
numerator := new(big.Int).Mul(result, twoTo192)
denominator := new(big.Int).Mul(twoToDecimals0, big.NewInt(1))
divResult := new(big.Int).Div(numerator, denominator)
return sqrt(divResult)
}
func sqrt(value *big.Int) (*big.Int, error) {
if value.Sign() < 0 {
return nil, fmt.Errorf("square root of negative numbers is not supported")
}
if value.Cmp(big.NewInt(1)) < 0 {
return value, nil
}
return newtonIteration(value, big.NewInt(1)), nil
}
func newtonIteration(n, x0 *big.Int) *big.Int {
// x1 = (n / x0 + x0) / 2
div := new(big.Int).Div(n, x0)
x1 := new(big.Int).Rsh(div.Add(div, x0), 1)
if x0.Cmp(x1) == 0 || x0.Cmp(new(big.Int).Sub(x1, big.NewInt(1))) == 0 {
return x0
}
return newtonIteration(n, x1)
}