Skip to content

Commit e136cc7

Browse files
author
Jeff Yanta
committed
Update currency creator curve functions and estimators
1 parent 24f9acb commit e136cc7

File tree

4 files changed

+91
-39
lines changed

4 files changed

+91
-39
lines changed

pkg/solana/currencycreator/estimate.go

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,79 +3,73 @@ package currencycreator
33
import (
44
"math"
55
"math/big"
6+
7+
"github.com/code-payments/code-server/pkg/usdc"
68
)
79

810
type EstimateCurrentPriceArgs struct {
9-
Curve *ExponentialCurve
1011
CurrentSupplyInQuarks uint64
11-
MintDecimals uint8
1212
}
1313

14-
func EstimateCurrentPrice(args *EstimateCurrentPriceArgs) *big.Float {
15-
scale := big.NewFloat(math.Pow10(int(args.MintDecimals))).SetPrec(defaultCurvePrec)
16-
unscaledCurrentSupply := big.NewFloat(float64(args.CurrentSupplyInQuarks)).SetPrec(defaultCurvePrec)
14+
func EstimateCurrentPrice(currentSupplyInQuarks uint64) *big.Float {
15+
scale := big.NewFloat(math.Pow10(int(DefaultMintDecimals))).SetPrec(defaultCurvePrec)
16+
unscaledCurrentSupply := big.NewFloat(float64(currentSupplyInQuarks)).SetPrec(defaultCurvePrec)
1717
scaledCurrentSupply := new(big.Float).Quo(unscaledCurrentSupply, scale)
18-
return args.Curve.SpotPriceAtSupply(scaledCurrentSupply)
18+
return DefaultExponentialCurve().SpotPriceAtSupply(scaledCurrentSupply)
1919
}
2020

21-
type EstimateBuyArgs struct {
21+
type EstimateBuyInUsdcArgs struct {
2222
BuyAmountInQuarks uint64
23-
Curve *ExponentialCurve
2423
CurrentSupplyInQuarks uint64
2524
BuyFeeBps uint16
26-
TargetMintDecimals uint8
27-
BaseMintDecimals uint8
2825
}
2926

30-
func EstimateBuy(args *EstimateBuyArgs) (uint64, uint64) {
31-
scale := big.NewFloat(math.Pow10(int(args.BaseMintDecimals))).SetPrec(defaultCurvePrec)
27+
func EstimateBuyInUsdc(args *EstimateBuyInUsdcArgs) (uint64, uint64) {
28+
scale := big.NewFloat(math.Pow10(int(usdc.Decimals))).SetPrec(defaultCurvePrec)
3229
unscaledBuyAmount := big.NewFloat(float64(args.BuyAmountInQuarks)).SetPrec(defaultCurvePrec)
3330
scaledBuyAmount := new(big.Float).Quo(unscaledBuyAmount, scale)
3431

35-
scale = big.NewFloat(math.Pow10(int(args.TargetMintDecimals))).SetPrec(defaultCurvePrec)
32+
scale = big.NewFloat(math.Pow10(int(DefaultMintDecimals))).SetPrec(defaultCurvePrec)
3633
unscaledCurrentSupply := big.NewFloat(float64(args.CurrentSupplyInQuarks)).SetPrec(defaultCurvePrec)
3734
scaledCurrentSupply := new(big.Float).Quo(unscaledCurrentSupply, scale)
3835

39-
scale = big.NewFloat(math.Pow10(int(args.TargetMintDecimals))).SetPrec(defaultCurvePrec)
40-
scaledTotalValue := args.Curve.ValueToTokens(scaledCurrentSupply, scaledBuyAmount)
41-
unscaledTotalValue := new(big.Float).Mul(scaledTotalValue, scale)
36+
scale = big.NewFloat(math.Pow10(int(DefaultMintDecimals))).SetPrec(defaultCurvePrec)
37+
scaledTokens := DefaultExponentialCurve().ValueToTokens(scaledCurrentSupply, scaledBuyAmount)
38+
unscaledTokens := new(big.Float).Mul(scaledTokens, scale)
4239

4340
feePctValue := new(big.Float).SetPrec(defaultCurvePrec).Quo(big.NewFloat(float64(args.BuyFeeBps)), big.NewFloat(10000))
44-
scaledFees := new(big.Float).Mul(scaledTotalValue, feePctValue)
41+
scaledFees := new(big.Float).Mul(scaledTokens, feePctValue)
4542
unscaledFees := new(big.Float).Mul(scaledFees, scale)
4643

47-
total, _ := unscaledTotalValue.Int64()
44+
tokens, _ := unscaledTokens.Int64()
4845
fees, _ := unscaledFees.Int64()
49-
return uint64(total - fees), uint64(fees)
46+
return uint64(tokens - fees), uint64(fees)
5047
}
5148

52-
type EstimateSaleArgs struct {
53-
SellAmountInQuarks uint64
54-
Curve *ExponentialCurve
55-
CurrentSupplyInQuarks uint64
56-
SellFeeBps uint16
57-
TargetMintDecimals uint8
58-
BaseMintDecimals uint8
49+
type EstimateSellInUsdcArgs struct {
50+
SellAmountInQuarks uint64
51+
CurrentValueInQuarks uint64
52+
SellFeeBps uint16
5953
}
6054

61-
func EstimateSale(args *EstimateSaleArgs) (uint64, uint64) {
62-
scale := big.NewFloat(math.Pow10(int(args.TargetMintDecimals))).SetPrec(defaultCurvePrec)
55+
func EstimateSellInUsdc(args *EstimateSellInUsdcArgs) (uint64, uint64) {
56+
scale := big.NewFloat(math.Pow10(int(DefaultMintDecimals))).SetPrec(defaultCurvePrec)
6357
unscaledSellAmount := big.NewFloat(float64(args.SellAmountInQuarks)).SetPrec(defaultCurvePrec)
6458
scaledSellAmount := new(big.Float).Quo(unscaledSellAmount, scale)
6559

66-
scale = big.NewFloat(math.Pow10(int(args.TargetMintDecimals))).SetPrec(defaultCurvePrec)
67-
unscaledCurrentSupply := big.NewFloat(float64(args.CurrentSupplyInQuarks)).SetPrec(defaultCurvePrec)
68-
scaledCurrentSupply := new(big.Float).Quo(unscaledCurrentSupply, scale)
60+
scale = big.NewFloat(math.Pow10(int(DefaultMintDecimals))).SetPrec(defaultCurvePrec)
61+
unscaledCurrentValue := big.NewFloat(float64(args.CurrentValueInQuarks)).SetPrec(defaultCurvePrec)
62+
scaledCurrentValue := new(big.Float).Quo(unscaledCurrentValue, scale)
6963

70-
scale = big.NewFloat(math.Pow10(int(args.BaseMintDecimals))).SetPrec(defaultCurvePrec)
71-
scaledTotalValue := args.Curve.TokensToValue(scaledCurrentSupply, scaledSellAmount)
72-
unscaledTotalValue := new(big.Float).Mul(scaledTotalValue, scale)
64+
scale = big.NewFloat(math.Pow10(int(usdc.Decimals))).SetPrec(defaultCurvePrec)
65+
scaledValue := DefaultExponentialCurve().TokensToValueFromCurrentValue(scaledCurrentValue, scaledSellAmount)
66+
unscaledValue := new(big.Float).Mul(scaledValue, scale)
7367

7468
feePctValue := new(big.Float).SetPrec(defaultCurvePrec).Quo(big.NewFloat(float64(args.SellFeeBps)), big.NewFloat(10000))
75-
scaledFees := new(big.Float).Mul(scaledTotalValue, feePctValue)
69+
scaledFees := new(big.Float).Mul(scaledValue, feePctValue)
7670
unscaledFees := new(big.Float).Mul(scaledFees, scale)
7771

78-
total, _ := unscaledTotalValue.Int64()
72+
value, _ := unscaledValue.Int64()
7973
fees, _ := unscaledFees.Int64()
80-
return uint64(total - fees), uint64(fees)
74+
return uint64(value - fees), uint64(fees)
8175
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package currencycreator
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestEstimateCurrentPrice(t *testing.T) {
9+
t.Skip()
10+
11+
fmt.Println(EstimateCurrentPrice(0).Text('f', DefaultCurveDecimals))
12+
fmt.Println(EstimateCurrentPrice(DefaultMintMaxSupply).Text('f', DefaultCurveDecimals))
13+
}
14+
15+
func TestEstimateBuyInUsdc(t *testing.T) {
16+
t.Skip()
17+
18+
received, fees := EstimateBuyInUsdc(&EstimateBuyInUsdcArgs{
19+
BuyAmountInQuarks: 50_000_000,
20+
CurrentSupplyInQuarks: 0,
21+
BuyFeeBps: 0,
22+
})
23+
fmt.Printf("%d total, %d received, %d fees\n", received+fees, received, fees)
24+
25+
received, fees = EstimateBuyInUsdc(&EstimateBuyInUsdcArgs{
26+
BuyAmountInQuarks: 50_000_000,
27+
CurrentSupplyInQuarks: 4_989_067_263,
28+
BuyFeeBps: 100,
29+
})
30+
fmt.Printf("%d total, %d received, %d fees\n", received+fees, received, fees)
31+
}
32+
33+
func TestEstimateSelInUsdc(t *testing.T) {
34+
t.Skip()
35+
36+
received, fees := EstimateSellInUsdc(&EstimateSellInUsdcArgs{
37+
SellAmountInQuarks: 1_234_567_890,
38+
CurrentValueInQuarks: 50_000_000,
39+
SellFeeBps: 0,
40+
})
41+
fmt.Printf("%d total, %d received, %d fees\n", received+fees, received, fees)
42+
43+
received, fees = EstimateSellInUsdc(&EstimateSellInUsdcArgs{
44+
SellAmountInQuarks: 1_234_567_890,
45+
CurrentValueInQuarks: 50_000_000,
46+
SellFeeBps: 100,
47+
})
48+
fmt.Printf("%d total, %d received, %d fees\n", received+fees, received, fees)
49+
}

pkg/solana/currencycreator/exponential_curve.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (curve *ExponentialCurve) SpotPriceAtSupply(currentSupply *big.Float) *big.
2929
return new(big.Float).Mul(new(big.Float).Mul(curve.a, curve.b), exp)
3030
}
3131

32-
func (curve *ExponentialCurve) TokensToValue(currentSupply, tokens *big.Float) *big.Float {
32+
func (curve *ExponentialCurve) TokensToValueFromCurrentSupply(currentSupply, tokens *big.Float) *big.Float {
3333
newSupply := new(big.Float).Add(currentSupply, tokens)
3434
cs := new(big.Float).Mul(curve.c, currentSupply)
3535
ns := new(big.Float).Mul(curve.c, newSupply)
@@ -40,6 +40,15 @@ func (curve *ExponentialCurve) TokensToValue(currentSupply, tokens *big.Float) *
4040
return new(big.Float).Mul(abOverC, diff)
4141
}
4242

43+
func (curve *ExponentialCurve) TokensToValueFromCurrentValue(currentValue, tokens *big.Float) *big.Float {
44+
abOverC := new(big.Float).Quo(new(big.Float).Mul(curve.a, curve.b), curve.c)
45+
cvPlusAbOverC := new(big.Float).Add(currentValue, abOverC)
46+
cTimesTokens := new(big.Float).Mul(curve.c, tokens)
47+
exp := expBig(new(big.Float).Neg(cTimesTokens))
48+
oneMinusExp := new(big.Float).Sub(big.NewFloat(1.0), exp)
49+
return new(big.Float).Mul(cvPlusAbOverC, oneMinusExp)
50+
}
51+
4352
func (curve *ExponentialCurve) ValueToTokens(currentSupply, value *big.Float) *big.Float {
4453
abOverC := new(big.Float).Quo(new(big.Float).Mul(curve.a, curve.b), curve.c)
4554
expCS := expBig(new(big.Float).Mul(curve.c, currentSupply))

pkg/solana/currencycreator/exponential_curve_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestGenerateCurveTable(t *testing.T) {
4343
supply := new(big.Float).Copy(zero)
4444

4545
for i := 0; i <= 100; i++ {
46-
cost := curve.TokensToValue(zero, supply)
46+
cost := curve.TokensToValueFromCurrentSupply(zero, supply)
4747
spotPrice := curve.SpotPriceAtSupply(supply)
4848

4949
fmt.Printf("| %3d%% | %14s | %32s | %26s |\n",

0 commit comments

Comments
 (0)