Skip to content

Commit

Permalink
change formula
Browse files Browse the repository at this point in the history
  • Loading branch information
soring323 committed Mar 20, 2024
1 parent fd2882c commit edb2f5c
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions x/gmm/types/pool_weight.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

import (
fmt "fmt"
math "math"

sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand All @@ -29,11 +28,8 @@ func (p *Pool) estimateShareWithSingleLiquidityInWeightPool(coin sdk.Coin) (sdk.
decAsset := sdk.NewDecCoinFromCoin(asset.Token)
weight := sdk.NewDecFromInt(*asset.Weight).Quo(sdk.NewDec(100)) // divide by 100
ratio := decToken.Amount.Quo(decAsset.Amount).Add(sdk.NewDec(1))
exponent := (math.Pow(ratio.MustFloat64(), weight.MustFloat64()) - 1) * Multiplier
factor, err := sdk.NewDecFromStr(fmt.Sprintf("%f", exponent/Multiplier))
if err != nil {
return sdk.Coin{}, err
}

factor := (ApproximatePow(ratio, weight, 100).Sub(sdk.OneDec()))
issueAmount := p.TotalShares.Amount.Mul(factor.RoundInt()).Quo(sdk.NewInt(1e10))
outputToken := sdk.Coin{
Amount: issueAmount,
Expand Down Expand Up @@ -105,12 +101,22 @@ func (p *Pool) estimateSwapInWeightPool(amountIn sdk.Coin, denomOut string) (sdk
oneMinusRatio := sdk.NewDec(1).Sub(ratio)

power := weightIn.Quo(weightOut)
factor := math.Pow(oneMinusRatio.MustFloat64(), power.MustFloat64()) * Multiplier
finalFactor := factor / 1e8

amountOut := balanceOut.Mul(sdk.MustNewDecFromStr(fmt.Sprintf("%f", finalFactor))).Quo(sdk.NewDec(1e10))
factor := ApproximatePow(oneMinusRatio, power, 100) // 100 iterations for example
amountOut := balanceOut.Mul(factor)
return sdk.Coin{
Amount: amountOut.RoundInt(),
Denom: denomOut,
}, nil
}

// ApproximatePow approximates (base ^ exponent) using a series expansion.
// Here's a simple approximation; you might need a more accurate one depending on your needs.
func ApproximatePow(base sdk.Dec, exponent sdk.Dec, iterations int) sdk.Dec {
result := sdk.OneDec() // Start with 1 as the initial result
term := sdk.OneDec() // The current term starts at 1 (base^0)
for n := 1; n <= iterations; n++ {
term = term.Mul(base).Mul(exponent).QuoInt64(int64(n)) // term *= base * exponent / n
result = result.Sub(term)
}
return result
}

0 comments on commit edb2f5c

Please sign in to comment.