Skip to content

Commit 99c2779

Browse files
committed
Update TokensForValueExchange to use value locked as the primary input
1 parent c1a4370 commit 99c2779

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

pkg/solana/currencycreator/estimate.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ func EstimateCurrentPrice(currentSupplyInQuarks uint64) *big.Float {
1313
}
1414

1515
type EstimateValueExchangeArgs struct {
16-
ValueInQuarks uint64
17-
CurrentSupplyInQuarks uint64
18-
ValueMintDecimals uint8
16+
ValueInQuarks uint64
17+
CurrentValueInQuarks uint64
18+
ValueMintDecimals uint8
1919
}
2020

2121
func EstimateValueExchange(args *EstimateValueExchangeArgs) uint64 {
2222
scale := big.NewFloat(math.Pow10(int(args.ValueMintDecimals))).SetPrec(defaultCurvePrec)
2323
unscaledValue := big.NewFloat(float64(args.ValueInQuarks)).SetPrec(defaultCurvePrec)
2424
scaledValue := new(big.Float).Quo(unscaledValue, scale)
2525

26-
scale = big.NewFloat(math.Pow10(int(DefaultMintDecimals))).SetPrec(defaultCurvePrec)
27-
unscaledCurrentSupply := big.NewFloat(float64(args.CurrentSupplyInQuarks)).SetPrec(defaultCurvePrec)
28-
scaledCurrentSupply := new(big.Float).Quo(unscaledCurrentSupply, scale)
26+
scale = big.NewFloat(math.Pow10(int(args.ValueMintDecimals))).SetPrec(defaultCurvePrec)
27+
unscaledCurrentValue := big.NewFloat(float64(args.CurrentValueInQuarks)).SetPrec(defaultCurvePrec)
28+
scaledCurrentValue := new(big.Float).Quo(unscaledCurrentValue, scale)
2929

3030
scale = big.NewFloat(math.Pow10(int(DefaultMintDecimals))).SetPrec(defaultCurvePrec)
31-
scaledTokens := DefaultExponentialCurve().TokensForValueExchange(scaledCurrentSupply, scaledValue)
31+
scaledTokens := DefaultExponentialCurve().TokensForValueExchange(scaledCurrentValue, scaledValue)
3232
unscaledTokens := new(big.Float).Mul(scaledTokens, scale)
3333

3434
quarks, _ := unscaledTokens.Int64()

pkg/solana/currencycreator/estimate_test.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package currencycreator
22

33
import (
44
"fmt"
5-
"math"
65
"testing"
76

87
"github.com/stretchr/testify/require"
@@ -15,9 +14,9 @@ func TestEstimateCurrentPrice(t *testing.T) {
1514

1615
func TestEstimatValueExchange(t *testing.T) {
1716
quarks := EstimateValueExchange(&EstimateValueExchangeArgs{
18-
ValueInQuarks: 5000000, // $5
19-
CurrentSupplyInQuarks: 7232649000000000, // 723,264.9 tokens
20-
ValueMintDecimals: 6,
17+
ValueInQuarks: 10000000, // $10
18+
CurrentValueInQuarks: 1000000000, // $1000
19+
ValueMintDecimals: 6,
2120
})
2221

2322
fmt.Printf("%d quarks\n", quarks)
@@ -63,19 +62,19 @@ func TestEstimates_CsvTable(t *testing.T) {
6362
startValue := uint64(10000) // $0.01
6463
endValue := uint64(1000000000000000) // $1T
6564

66-
fmt.Println("value locked,payment value,payment quarks,sell value")
65+
fmt.Println("value locked,total circulating supply,new circulating supply,payment value,payment quarks,sell value")
6766
for valueLocked := startValue; valueLocked <= endValue; valueLocked *= 10 {
68-
circulatingSupply, _ := EstimateBuy(&EstimateBuyArgs{
67+
totalCirculatingSupply, _ := EstimateBuy(&EstimateBuyArgs{
6968
BuyAmountInQuarks: valueLocked,
7069
CurrentSupplyInQuarks: 0,
7170
ValueMintDecimals: 6,
7271
})
7372

7473
for paymentValue := startValue; paymentValue <= valueLocked; paymentValue *= 10 {
7574
paymenQuarks := EstimateValueExchange(&EstimateValueExchangeArgs{
76-
ValueInQuarks: paymentValue,
77-
CurrentSupplyInQuarks: circulatingSupply,
78-
ValueMintDecimals: 6,
75+
ValueInQuarks: paymentValue,
76+
CurrentValueInQuarks: valueLocked,
77+
ValueMintDecimals: 6,
7978
})
8079

8180
sellValue, _ := EstimateSell(&EstimateSellArgs{
@@ -84,9 +83,18 @@ func TestEstimates_CsvTable(t *testing.T) {
8483
ValueMintDecimals: 6,
8584
})
8685

87-
require.True(t, math.Abs(float64(paymentValue)-float64(sellValue)) <= 1)
86+
diff := int64(paymentValue) - int64(sellValue)
87+
require.True(t, diff >= -1 && diff <= 1)
88+
89+
newCirculatingSupply, _ := EstimateBuy(&EstimateBuyArgs{
90+
BuyAmountInQuarks: valueLocked - paymentValue,
91+
CurrentSupplyInQuarks: 0,
92+
ValueMintDecimals: 6,
93+
})
94+
diff = int64(totalCirculatingSupply) - int64(newCirculatingSupply) - int64(paymenQuarks)
95+
require.True(t, diff >= -1 && diff <= 1)
8896

89-
fmt.Printf("%d,%d,%d,%d\n", valueLocked, paymentValue, paymenQuarks, sellValue)
97+
fmt.Printf("%d,%d,%d,%d,%d,%d\n", valueLocked, totalCirculatingSupply, newCirculatingSupply, paymentValue, paymenQuarks, sellValue)
9098
}
9199
}
92100
}

pkg/solana/currencycreator/exponential_curve.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,16 @@ func (curve *ExponentialCurve) TokensBoughtForValue(currentSupply, value *big.Fl
6161
return new(big.Float).Sub(result, currentSupply)
6262
}
6363

64-
// How many tokens should be exchanged for a value given the currentSupply?
65-
func (curve *ExponentialCurve) TokensForValueExchange(currentSupply, value *big.Float) *big.Float {
64+
// How many tokens should be exchanged for a value given the currentValue?
65+
func (curve *ExponentialCurve) TokensForValueExchange(currentValue, value *big.Float) *big.Float {
6666
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)
67+
newValue := new(big.Float).Sub(currentValue, value)
68+
currentValueOverAbOverC := new(big.Float).Quo(currentValue, abOverC)
69+
newValueOverAbOverC := new(big.Float).Quo(newValue, abOverC)
70+
lnTerm1 := logBig(new(big.Float).Add(big.NewFloat(1.0), currentValueOverAbOverC))
71+
lnTerm2 := logBig(new(big.Float).Add(big.NewFloat(1.0), newValueOverAbOverC))
72+
diffLnTerms := new(big.Float).Sub(lnTerm1, lnTerm2)
73+
return new(big.Float).Quo(diffLnTerms, curve.c)
7274
}
7375

7476
func DefaultExponentialCurve() *ExponentialCurve {

0 commit comments

Comments
 (0)