You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Calculate the cost to buy a certain number of tokens given the current supply.
60
+
* This function answers the question: "How much does it cost to buy X tokens?"
61
+
* The formula used is: `(a * b / c) * (e^(c * new_supply) - e^(c * current_supply))`
62
+
* where `new_supply = current_supply + tokensToBuy`.
63
+
*
64
+
* @param currentSupply The current supply of tokens.
65
+
* @param tokensToBuy The number of tokens to be purchased.
66
+
* @return A [Result] containing the calculated cost to buy the tokens, or an exception if an arithmetic error occurs.
67
+
*/
68
+
funcostToBuyTokens(
69
+
currentSupply:BigDecimal,
70
+
tokensToBuy:BigDecimal,
71
+
): Result<BigDecimal> {
72
+
return runCatching {
73
+
val newSupply = currentSupply.add(tokensToBuy)
74
+
val cs = c.multiply(currentSupply, mc)
75
+
val ns = c.multiply(newSupply, mc)
76
+
val expCs = cs.exp(mc)
77
+
val expNs = ns.exp(mc)
78
+
val abOverC = a.multiply(b, mc).divide(c, mc)
79
+
val diff = expNs.subtract(expCs, mc)
80
+
abOverC.multiply(diff, mc)
81
+
}
82
+
}
83
+
84
+
85
+
/**
86
+
* Calculate the value received when selling `tokensToSell` tokens.
87
+
* This function answers the question: "How much value can I get for X tokens?"
88
+
* The formula used is: `(a * b / c) * (e^(c * current_supply) - e^(c * new_supply))`
89
+
* where `new_supply = current_supply - tokensToSell`.
90
+
*
91
+
* @param currentValue The current value before selling any tokens.
92
+
* @param tokensToSell The number of tokens to be sold.
93
+
* @return A [Result] containing the calculated value received from selling the tokens, or an exception if an arithmetic error occurs.
94
+
*/
95
+
funvalueFromSellingTokens(
96
+
currentValue:BigDecimal,
97
+
tokensToSell:BigDecimal,
98
+
): Result<BigDecimal> {
99
+
return runCatching {
100
+
val abOverC = a.multiply(b, mc).divide(c, mc)
101
+
val cvPlusAbOverC = currentValue.add(abOverC, mc)
102
+
val cTimesTokensToSell = c.multiply(tokensToSell, mc)
103
+
val negCTimesTokensToSell = cTimesTokensToSell.negate(mc)
104
+
val exp = negCTimesTokensToSell.exp(mc)
105
+
val oneMinsExp =BigDecimal.ONE.subtract(exp, mc)
106
+
cvPlusAbOverC.multiply(oneMinsExp, mc)
107
+
}
108
+
}
109
+
110
+
/**
111
+
* Calculate the number of tokens that can be bought for a given value, starting from the current supply.
112
+
* This function answers the question: "How many tokens can I get for Y value?"
113
+
* The formula used is: `num_tokens = (1/c) * ln(value / (a * b / c) + e^(c * current_supply)) - current_supply`
114
+
*
115
+
* @param currentSupply The current supply of tokens.
116
+
* @param value The amount of value (e.g., currency) to be spent on tokens.
117
+
* @return A [Result] containing the calculated number of tokens that can be bought, or an exception if an arithmetic error occurs.
118
+
*/
119
+
funtokensBoughtForValue(
120
+
currentSupply:BigDecimal,
121
+
value:BigDecimal,
122
+
): Result<BigDecimal> {
123
+
return runCatching {
124
+
val abOverC = a.multiply(b, mc).divide(c, mc)
125
+
val expCs = currentSupply.multiply(c, mc).exp(mc)
126
+
val term = value.divide(abOverC, mc).add(expCs, mc)
127
+
val lnTerm = term.ln(mc)
128
+
val result = lnTerm.divide(c, mc)
129
+
result.subtract(currentSupply, mc)
130
+
}
131
+
}
132
+
133
+
/**
134
+
* Calculate the number of tokens that can be exchanged for a given value, starting from the current supply.
135
+
* This is the inverse of `valueFromSellingTokens`. It answers the question: "How many tokens should be exchanged for a value given the currentSupply?"
136
+
* @param currentSupply The current supply of tokens.
137
+
* @param value The target value to receive from the exchange.
138
+
* @return A [Result] containing the calculated number of tokens for the exchange, or an exception if an arithmetic error occurs (e.g., input to log is not positive).
139
+
*/
140
+
funtokensForValueExchange(
141
+
currentSupply:BigDecimal,
142
+
value:BigDecimal,
143
+
): Result<BigDecimal> {
144
+
return runCatching {
145
+
val abOverC = a.multiply(b, mc).divide(c, mc)
146
+
val expCs = currentSupply.multiply(c, mc).exp(mc)
147
+
val abOverCTimesExpCs = abOverC.multiply(expCs, mc)
148
+
val oneMinusFrac =BigDecimal.ONE.subtract(value.divide(abOverCTimesExpCs, mc), mc)
0 commit comments