-
Notifications
You must be signed in to change notification settings - Fork 68
/
formula.go
136 lines (104 loc) Β· 4.66 KB
/
formula.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
package formula
import (
"github.com/MinterTeam/minter-go-node/math"
"math/big"
)
const (
precision = 100
)
// CalculatePurchaseReturn calculates amount of coin that user will receive by depositing given amount of BIP
// Return = supply * ((1 + deposit / reserve) ^ (crr / 100) - 1)
func CalculatePurchaseReturn(supply *big.Int, reserve *big.Int, crr uint32, deposit *big.Int) *big.Int {
if deposit.Sign() == 0 {
return big.NewInt(0)
}
if crr == 100 {
result := big.NewInt(0).Mul(supply, deposit)
return result.Div(result, reserve)
}
tSupply := newFloat(0).SetInt(supply)
tReserve := newFloat(0).SetInt(reserve)
tDeposit := newFloat(0).SetInt(deposit)
res := newFloat(0).Quo(tDeposit, tReserve) // deposit / reserve
res.Add(res, newFloat(1)) // 1 + (deposit / reserve)
res = math.Pow(res, newFloat(float64(crr)/100)) // (1 + deposit / reserve) ^ (crr / 100)
res.Sub(res, newFloat(1)) // ((1 + deposit / reserve) ^ (crr / 100) - 1)
res.Mul(res, tSupply) // supply * ((1 + deposit / reserve) ^ (crr / 100) - 1)
result, _ := res.Int(nil)
return result
}
// CalculatePurchaseAmount is the reversed version of function CalculatePurchaseReturn
// Deposit = reserve * (((wantReceive + supply) / supply)^(100/c) - 1)
func CalculatePurchaseAmount(supply *big.Int, reserve *big.Int, crr uint32, wantReceive *big.Int) *big.Int {
if wantReceive.Sign() == 0 {
return big.NewInt(0)
}
if crr == 100 {
result := big.NewInt(0).Mul(wantReceive, reserve)
return result.Div(result, supply)
}
tSupply := newFloat(0).SetInt(supply)
tReserve := newFloat(0).SetInt(reserve)
tWantReceive := newFloat(0).SetInt(wantReceive)
res := newFloat(0).Add(tWantReceive, tSupply) // reserve + supply
res.Quo(res, tSupply) // (reserve + supply) / supply
res = math.Pow(res, newFloat(100/float64(crr))) // ((reserve + supply) / supply)^(100/c)
res.Sub(res, newFloat(1)) // (((reserve + supply) / supply)^(100/c) - 1)
res.Mul(res, tReserve) // reserve * (((reserve + supply) / supply)^(100/c) - 1)
result, _ := res.Int(nil)
return result
}
// CalculateSaleReturn returns amount of BIP user will receive by depositing given amount of coins
// Return = reserve * (1 - (1 - sellAmount / supply) ^ (100 / crr))
func CalculateSaleReturn(supply *big.Int, reserve *big.Int, crr uint32, sellAmount *big.Int) *big.Int {
// special case for 0 sell amount
if sellAmount.Sign() == 0 {
return big.NewInt(0)
}
// special case for selling the entire supply
if sellAmount.Cmp(supply) == 0 {
return big.NewInt(0).Set(reserve)
}
if crr == 100 {
ret := big.NewInt(0).Mul(reserve, sellAmount)
ret.Div(ret, supply)
return ret
}
tSupply := newFloat(0).SetInt(supply)
tReserve := newFloat(0).SetInt(reserve)
tSellAmount := newFloat(0).SetInt(sellAmount)
res := newFloat(0).Quo(tSellAmount, tSupply) // sellAmount / supply
res.Sub(newFloat(1), res) // (1 - sellAmount / supply)
res = math.Pow(res, newFloat(100/(float64(crr)))) // (1 - sellAmount / supply) ^ (100 / crr)
res.Sub(newFloat(1), res) // (1 - (1 - sellAmount / supply) ^ (1 / (crr / 100)))
res.Mul(res, tReserve) // reserve * (1 - (1 - sellAmount / supply) ^ (1 / (crr / 100)))
result, _ := res.Int(nil)
return result
}
// CalculateSaleAmount is the reversed version of function CalculateSaleReturn
// Deposit = -(-1 + (-(wantReceive - reserve)/reserve)^(1/crr)) * supply
func CalculateSaleAmount(supply *big.Int, reserve *big.Int, crr uint32, wantReceive *big.Int) *big.Int {
if wantReceive.Sign() == 0 {
return big.NewInt(0)
}
if crr == 100 {
ret := big.NewInt(0).Mul(wantReceive, supply)
ret.Div(ret, reserve)
return ret
}
tSupply := newFloat(0).SetInt(supply)
tReserve := newFloat(0).SetInt(reserve)
tWantReceive := newFloat(0).SetInt(wantReceive)
res := newFloat(0).Sub(tWantReceive, tReserve) // (wantReceive - reserve)
res.Neg(res) // -(wantReceive - reserve)
res.Quo(res, tReserve) // -(wantReceive - reserve)/reserve
res = math.Pow(res, newFloat(float64(crr)/100)) // (-(wantReceive - reserve)/reserve)^(1/crr)
res.Add(res, newFloat(-1)) // -1 + (-(wantReceive - reserve)/reserve)^(1/crr)
res.Neg(res) // -(-1 + (-(wantReceive - reserve)/reserve)^(1/crr))
res.Mul(res, tSupply) // -(-1 + (-(wantReceive - reserve)/reserve)^(1/crr)) * supply
result, _ := res.Int(nil)
return result
}
func newFloat(x float64) *big.Float {
return big.NewFloat(x).SetPrec(precision)
}