Skip to content

Commit

Permalink
multi-asset swap / LP equations, and tests (#1383)
Browse files Browse the repository at this point in the history
Closes: #1369 

## What is the purpose of the change

This change extends the CFMM equation we use for stableswap to multiple assets.

## Brief change log
 
  - Create multi-asset CFMM function
  - [wip] Create multi-asset CFMM solver
  - [wip] Create tests for new CFMM


## Testing and Verifying

This change added tests and can be verified as follows:

*(example:)*
  - [wip]

## Documentation and Release Note

  - Does this pull request introduce a new feature or user-facing behavior changes? (yes)
  - Is a relevant changelog entry added to the `Unreleased` section in `CHANGELOG.md`? (yes / no)
  - How is the feature or change documented? (not applicable   /   specification (`x/<module>/spec/`)  /  [Osmosis docs repo](https://github.com/osmosis-labs/docs)   /   not documented)
  • Loading branch information
AlpinYukseloglu authored May 4, 2022
1 parent 6e1f93a commit 18a3bb3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
12 changes: 8 additions & 4 deletions osmoutils/partialord/partialord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (
func TestAPI(t *testing.T) {
// begin block use case, we have a dozen modules, but only care about a couple orders.
// In practice this will be gotten from some API, e.g. app.AllModuleNames()
moduleNames := []string{"auth", "authz", "bank", "capabilities",
moduleNames := []string{
"auth", "authz", "bank", "capabilities",
"staking", "distribution", "epochs", "mint", "upgrades", "wasm", "ibc",
"ibctransfers", "bech32ibc"}
"ibctransfers", "bech32ibc",
}
beginBlockOrd := partialord.NewPartialOrdering(moduleNames)
beginBlockOrd.FirstElements("upgrades", "epochs", "capabilities")
beginBlockOrd.After("ibctransfers", "ibc")
Expand All @@ -23,9 +25,11 @@ func TestAPI(t *testing.T) {
beginBlockOrd.LastElements("auth", "authz", "wasm")

totalOrd := beginBlockOrd.TotalOrdering()
expTotalOrd := []string{"upgrades", "epochs", "capabilities",
expTotalOrd := []string{
"upgrades", "epochs", "capabilities",
"bank", "staking", "mint", "ibc", "distribution", "ibctransfers", "bech32ibc",
"auth", "authz", "wasm"}
"auth", "authz", "wasm",
}
require.Equal(t, expTotalOrd, totalOrd)
}

Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/chain_init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {

b, _ := json.Marshal(createdChain)
fileName := fmt.Sprintf("%v/%v-encode", dataDir, chainId)
if err = os.WriteFile(fileName, b, 0777); err != nil {
if err = os.WriteFile(fileName, b, 0o777); err != nil {

This comment has been minimized.

Copy link
@czarcas7ic

czarcas7ic May 4, 2022

Member

Was this just some auto linter change?

This comment has been minimized.

Copy link
@ValarDragon

ValarDragon May 4, 2022

Member

Yeah, theres some linter that people use that does this. I'm not sure which one

panic(err)
}
}
1 change: 0 additions & 1 deletion tests/e2e/e2e_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@ func (s *IntegrationTestSuite) configureChain(chainId string) {
}
}
s.chains = append(s.chains, &newChain)

}

func (s *IntegrationTestSuite) configureDockerResources(chainIDOne, chainIDTwo string) {
Expand Down
1 change: 0 additions & 1 deletion x/gamm/pool-models/balancer/amm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ func TestCalcSingleAssetInAndOut_InverseRelationship(t *testing.T) {
for _, tc := range testcases {
for _, swapFee := range swapFeeCases {
t.Run(getTestCaseName(tc, swapFee), func(t *testing.T) {

swapFeeDec, err := sdk.NewDecFromStr(swapFee)
require.NoError(t, err)

Expand Down
12 changes: 12 additions & 0 deletions x/gamm/pool-models/stableswap/amm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ func cfmmConstant(xReserve, yReserve sdk.Dec) sdk.Dec {
return xy.Mul(x2.Add(y2))
}

// multi-asset CFMM is xyu(x^2 + y^2 + v) = k,
// where u is the product of the reserves of assets
// outside of x and y (e.g. u = wz), and v is the sum
// of their squares (e.g. v = w^2 + z^2).
// When u = 1 and v = 0, this is equivalent to solidly's CFMM
func cfmmConstantMulti(xReserve, yReserve, uReserve, vSumSquares sdk.Dec) sdk.Dec {
xyu := xReserve.Mul(yReserve.Mul(uReserve))
x2 := xReserve.Mul(xReserve)
y2 := yReserve.Mul(yReserve)
return xyu.Mul(x2.Add(y2).Add(vSumSquares))
}

// solidly CFMM is xy(x^2 + y^2) = k
// So we want to solve for a given addition of `b` units of y into the pool,
// how many units `a` of x do we get out.
Expand Down

0 comments on commit 18a3bb3

Please sign in to comment.