Skip to content

Commit 59992ec

Browse files
committed
Update TokensForValueExchange to use the generic A, B, C parameterized curve
1 parent 7792d8e commit 59992ec

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

pkg/solana/currencycreator/exponential_curve.go

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ func (curve *ExponentialCurve) CostToBuyTokens(currentSupply, tokensToBuy *big.F
3434
newSupply := new(big.Float).Add(currentSupply, tokensToBuy)
3535
cs := new(big.Float).Mul(curve.c, currentSupply)
3636
ns := new(big.Float).Mul(curve.c, newSupply)
37-
expCS := expBig(cs)
38-
expNS := expBig(ns)
37+
expCs := expBig(cs)
38+
expNs := expBig(ns)
3939
abOverC := new(big.Float).Quo(new(big.Float).Mul(curve.a, curve.b), curve.c)
40-
diff := new(big.Float).Sub(expNS, expCS)
40+
diff := new(big.Float).Sub(expNs, expCs)
4141
return new(big.Float).Mul(abOverC, diff)
4242
}
4343

@@ -54,32 +54,21 @@ func (curve *ExponentialCurve) ValueFromSellingTokens(currentValue, tokensToSell
5454
// How many tokens will be bought for a value given the currentSupply?
5555
func (curve *ExponentialCurve) TokensBoughtForValue(currentSupply, value *big.Float) *big.Float {
5656
abOverC := new(big.Float).Quo(new(big.Float).Mul(curve.a, curve.b), curve.c)
57-
expCS := expBig(new(big.Float).Mul(curve.c, currentSupply))
58-
term := new(big.Float).Add(new(big.Float).Quo(value, abOverC), expCS)
57+
expCs := expBig(new(big.Float).Mul(curve.c, currentSupply))
58+
term := new(big.Float).Add(new(big.Float).Quo(value, abOverC), expCs)
5959
lnTerm := logBig(term)
6060
result := new(big.Float).Quo(lnTerm, curve.c)
6161
return new(big.Float).Sub(result, currentSupply)
6262
}
6363

6464
// How many tokens should be exchanged for a value given the currentSupply?
65-
//
66-
// Note: This function assumes the default curve, and is not present in the currency creator program.
6765
func (curve *ExponentialCurve) TokensForValueExchange(currentSupply, value *big.Float) *big.Float {
68-
hundred := big.NewFloat(100)
69-
oneOverHundred := new(big.Float).Quo(big.NewFloat(1), hundred)
70-
71-
hundredTimesA := new(big.Float).Mul(hundred, curve.a)
72-
negativeHundredTimesA := new(big.Float).Mul(big.NewFloat(-1), hundredTimesA)
73-
74-
valueTimesB := new(big.Float).Mul(value, curve.b)
75-
76-
expCsTimesB := expBig(new(big.Float).Mul(currentSupply, curve.b))
77-
oneOverHundredTimesExpCsTimesB := new(big.Float).Mul(oneOverHundred, expCsTimesB)
78-
79-
lnInput := new(big.Float).Quo(valueTimesB, oneOverHundredTimesExpCsTimesB)
80-
lnInput = new(big.Float).Sub(big.NewFloat(1), lnInput)
81-
82-
return new(big.Float).Mul(negativeHundredTimesA, logBig(lnInput))
66+
abOverC := new(big.Float).Quo(new(big.Float).Mul(curve.a, curve.b), curve.c)
67+
expCs := expBig(new(big.Float).Mul(curve.c, currentSupply))
68+
abOverCTimesExpCs := new(big.Float).Mul(abOverC, expCs)
69+
oneMinusFrac := new(big.Float).Sub(big.NewFloat(1), new(big.Float).Quo(value, abOverCTimesExpCs))
70+
ln := logBig(oneMinusFrac)
71+
return new(big.Float).Quo(new(big.Float).Neg(ln), curve.c)
8372
}
8473

8574
func DefaultExponentialCurve() *ExponentialCurve {

0 commit comments

Comments
 (0)