Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stableswap: Improve the scaling factor API #2908

Merged
merged 8 commits into from
Sep 30, 2022
Merged

Conversation

ValarDragon
Copy link
Member

@ValarDragon ValarDragon commented Sep 30, 2022

Closes: #2903

What is the purpose of the change

Context in 2903, makes the API scale to/from BigDec.

Adds rounding mode for Int / U64 -> BigDec conversion

Testing and Verifying

This updates tests for existing behavior. We need to add tests for other rounding modes.

@ValarDragon ValarDragon requested review from a team and AlpinYukseloglu September 30, 2022 08:44
@github-actions github-actions bot added C:docs Improvements or additions to documentation C:x/gamm Changes, features and bugs related to the gamm module. labels Sep 30, 2022
@ValarDragon ValarDragon added the V:state/compatible/no_backport State machine compatible PR, depends on prior breaks label Sep 30, 2022
@@ -223,7 +223,13 @@ func solveCFMMBinarySearchMulti(constantFunction func(osmomath.BigDec, osmomath.
}
}

func spotPrice(baseReserve, quoteReserve osmomath.BigDec, remReserves []osmomath.BigDec) osmomath.BigDec {
func (p Pool) spotPrice(baseDenom, quoteDenom string) (sdk.Dec, error) {
roundMode := osmomath.RoundBankers // TODO:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to leave a todo here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, need to figure out which rounding mode to use

x/gamm/pool-models/stableswap/amm.go Show resolved Hide resolved
RoundBankers RoundingDirection = 3
)

func DivIntByU64ToBigDec(i sdk.Int, u uint64, round RoundingDirection) (BigDec, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: godocs

return BigDec{}, fmt.Errorf("invalid rounding mode %d", int(round))
}

func DivCoinAmtsByU64ToBigDec(coins []sdk.Coin, scales []uint64, round RoundingDirection) ([]BigDec, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a basic test for this function would be nice to have

x/gamm/pool-models/stableswap/amm.go Show resolved Hide resolved
@@ -223,7 +223,13 @@ func solveCFMMBinarySearchMulti(constantFunction func(osmomath.BigDec, osmomath.
}
}

func spotPrice(baseReserve, quoteReserve osmomath.BigDec, remReserves []osmomath.BigDec) osmomath.BigDec {
func (p Pool) spotPrice(baseDenom, quoteDenom string) (sdk.Dec, error) {
roundMode := osmomath.RoundBankers // TODO:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is TODO meant for explaining the reason for the choice of the rounding direction? I think that would be useful

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its for us to later choose the right rounding direction. All rounding direction choices are currenetly a TODO

return osmomath.DivCoinAmtsByU64ToBigDec(reorderedLiquidity, reorderedScalingFactors, round)
}

// reorderReservesAndScalingFactors takes the pool liquidity and scaling factors, and reorders them s.t.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// reorderReservesAndScalingFactors takes the pool liquidity and scaling factors, and reorders them s.t.
// reorderReservesAndScalingFactors given two denoms - first and second,
// it takes their pool liquidity and scaling factors and reorders them s.t.

if err != nil {
return nil, err
}
return osmomath.DivCoinAmtsByU64ToBigDec(reorderedLiquidity, reorderedScalingFactors, round)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: every existing caller of this function grabs first and second element from the slice returned here. What do you think about simply having 3 returns here?

We could also have a sanity error check that the first 2 elements actually exist

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, could def add it in another PR!

},
"zero scaling factor": {
denoms: [2]string{"foo", "bar"},
poolAssets: twoEvenStablePoolAssets,
scalingFactors: []uint64{0, 1},
expPanic: true,
expError: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice improvement to tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:docs Improvements or additions to documentation C:x/gamm Changes, features and bugs related to the gamm module. V:state/compatible/no_backport State machine compatible PR, depends on prior breaks
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

[x/gamm][stableswap]: Make scaling/descaling logic also convert to and from BigDec
3 participants