Skip to content

Commit

Permalink
New AMM interface (#1066)
Browse files Browse the repository at this point in the history
* New Pool interface

* Make balancer pool implement new API

* Comment out files to fix at the end

* Update most of keeper for new interface

- Does not update JoinPool, will be done in next commit

This implements swap, and exit pool for the keeper,
both using the AMM interface correctly

* Fix many of the cross-module breaks

* Finish ExitPool messages for this PR

* JoinPool logic

* Tests compiling

* Test bug fixes

- Swap spot price args temporarily to be the old style (which should change to new one)
- Spot price rounding fix (needed weight to be decimal)
- Change tests to get spot price from keeper, not pool directly
- Fix bug in CalcAmountOutGivenIn
- Add accounting for TokenInMaxs being nil
- Add breaking change note for tokenInMaxs needs 0 coins or all coins

* Comment out tests for unimplemented functions, fix lint

* Fix CLI tests

* Fix long-standing CLI bug for max amounts in

* Address roman's pr review comments
  • Loading branch information
ValarDragon authored Mar 22, 2022
1 parent cf910d6 commit f1389ee
Show file tree
Hide file tree
Showing 42 changed files with 1,623 additions and 1,493 deletions.
12 changes: 6 additions & 6 deletions app/apptesting/test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,22 @@ func (keeperTestHelper *KeeperTestHelper) SetupGammPoolsWithBondDenomMultiplier(

// SwapAndSetSpotPrice runs a swap to set Spot price of a pool using arbitrary values
// returns spot price after the arbitrary swap
func (keeperTestHelper *KeeperTestHelper) SwapAndSetSpotPrice(poolId uint64, fromAsset gammtypes.PoolAsset, toAsset gammtypes.PoolAsset) sdk.Dec {
func (keeperTestHelper *KeeperTestHelper) SwapAndSetSpotPrice(poolId uint64, fromAsset sdk.Coin, toAsset sdk.Coin) sdk.Dec {
// create a dummy account
acc1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address().Bytes())

// fund dummy account with tokens to swap
coins := sdk.Coins{sdk.NewInt64Coin(fromAsset.Token.Denom, 100000000000000)}
coins := sdk.Coins{sdk.NewInt64Coin(fromAsset.Denom, 100000000000000)}
err := simapp.FundAccount(keeperTestHelper.App.BankKeeper, keeperTestHelper.Ctx, acc1, coins)
keeperTestHelper.Require().NoError(err)

_, _, err = keeperTestHelper.App.GAMMKeeper.SwapExactAmountOut(
_, err = keeperTestHelper.App.GAMMKeeper.SwapExactAmountOut(
keeperTestHelper.Ctx, acc1,
poolId, fromAsset.Token.Denom, fromAsset.Token.Amount,
sdk.NewCoin(toAsset.Token.Denom, toAsset.Token.Amount.Quo(sdk.NewInt(4))))
poolId, fromAsset.Denom, fromAsset.Amount,
sdk.NewCoin(toAsset.Denom, toAsset.Amount.Quo(sdk.NewInt(4))))
keeperTestHelper.Require().NoError(err)

spotPrice, err := keeperTestHelper.App.GAMMKeeper.CalculateSpotPrice(keeperTestHelper.Ctx, poolId, toAsset.Token.Denom, fromAsset.Token.Denom)
spotPrice, err := keeperTestHelper.App.GAMMKeeper.CalculateSpotPrice(keeperTestHelper.Ctx, poolId, toAsset.Denom, fromAsset.Denom)
keeperTestHelper.Require().NoError(err)
return spotPrice
}
Expand Down
6 changes: 3 additions & 3 deletions app/upgrades/v5/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ func CreateUpgradeHandler(mm *module.Manager, configurator module.Configurator,
// https://github.com/cosmos/ibc-go/blob/main/docs/migrations/ibc-migration-043.md#in-place-store-migrations
ibcConnections.SetParams(ctx, ibcconnectiontypes.DefaultParams())

totalLiquidity := gamm.GetLegacyTotalLiquidity(ctx)
gamm.DeleteLegacyTotalLiquidity(ctx)
gamm.SetTotalLiquidity(ctx, totalLiquidity)
// totalLiquidity := gamm.GetLegacyTotalLiquidity(ctx)
// gamm.DeleteLegacyTotalLiquidity(ctx)
// gamm.SetTotalLiquidity(ctx, totalLiquidity)

// Set all modules "old versions" to 1.
// Then the run migrations logic will handle running their upgrade logics
Expand Down
17 changes: 9 additions & 8 deletions app/upgrades/v5/whitelist_feetokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,18 @@ func InitialWhitelistedFeetokens(ctx sdk.Context, gamm *gammkeeper.Keeper) []typ
}

pool, poolExistsErr := gamm.GetPool(ctx, poolId)
_ = pool
if poolExistsErr != nil {
continue
}
_, assetExistsErr := pool.GetPoolAsset(asset[1])
if assetExistsErr != nil {
continue
}
_, osmoExistsErr := pool.GetPoolAsset("uosmo")
if osmoExistsErr != nil {
continue
}
// _, assetExistsErr := pool.GetPoolAsset(asset[1])
// if assetExistsErr != nil {
// continue
// }
// _, osmoExistsErr := pool.GetPoolAsset("uosmo")
// if osmoExistsErr != nil {
// continue
// }

feeToken := types.FeeToken{
Denom: asset[1],
Expand Down
8 changes: 4 additions & 4 deletions cmd/osmosisd/cmd/balances_from_state_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ func underlyingCoins(originCoins sdk.Coins, pools map[string]gammtypes.PoolI) sd
for _, coin := range originCoins {
if pools[coin.Denom] != nil {
pool := pools[coin.Denom]
assets := pool.GetAllPoolAssets()
assets := pool.GetTotalLpBalances(sdk.Context{})
for _, asset := range assets {
balances = balances.Add(sdk.NewCoin(asset.Token.Denom, asset.Token.Amount.Mul(coin.Amount).Quo(pool.GetTotalShares().Amount)))
if pools[asset.Token.Denom] != nil { // this happens when there's a pool for LP token swap
balances = balances.Add(sdk.NewCoin(asset.Denom, asset.Amount.Mul(coin.Amount).Quo(pool.GetTotalShares())))
if pools[asset.Denom] != nil { // this happens when there's a pool for LP token swap
convertAgain = true
}
}
Expand Down Expand Up @@ -294,7 +294,7 @@ Example:
if err != nil {
panic(err)
}
pools[pool.GetTotalShares().Denom] = pool
pools[gammtypes.GetPoolShareDenom(pool.GetId())] = pool
}

// convert balances to underlying coins and sum up balances to total balance
Expand Down
3 changes: 2 additions & 1 deletion proto/osmosis/gamm/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ message QuerySpotPriceRequest {
string tokenInDenom = 2 [ (gogoproto.moretags) = "yaml:\"token_in_denom\"" ];
string tokenOutDenom = 3
[ (gogoproto.moretags) = "yaml:\"token_out_denom\"" ];
bool withSwapFee = 4 [ (gogoproto.moretags) = "yaml:\"with_swap_fee\"" ];
reserved 4;
reserved "withSwapFee";
}
message QuerySpotPriceResponse {
// String of the Dec. Ex) 10.203uatom
Expand Down
8 changes: 8 additions & 0 deletions proto/osmosis/gamm/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ service Msg {
}

// ===================== MsgJoinPool
// This is really MsgJoinPoolNoSwap
message MsgJoinPool {
string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ];
uint64 poolId = 2 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
Expand Down Expand Up @@ -116,18 +117,25 @@ message MsgSwapExactAmountOutResponse {
}

// ===================== MsgJoinSwapExternAmountIn
// TODO: Rename to MsgJoinSwapExactAmountIn
message MsgJoinSwapExternAmountIn {
string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ];
uint64 poolId = 2 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
cosmos.base.v1beta1.Coin tokenIn = 3 [
(gogoproto.moretags) = "yaml:\"token_in\"",
(gogoproto.nullable) = false
];
// reserved 3;
// reserved "token_in";
string shareOutMinAmount = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"share_out_min_amount\"",
(gogoproto.nullable) = false
];
// repeated cosmos.base.v1beta1.Coin tokensIn = 5 [
// (gogoproto.moretags) = "yaml:\"tokens_in\"",
// (gogoproto.nullable) = false
// ];
}

message MsgJoinSwapExternAmountInResponse {
Expand Down
38 changes: 38 additions & 0 deletions x/gamm/breaking_changes_notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# List of every known client breaking change in current AMM refactor

## Queries

* PoolAssetsQuery
* Deleted
* New query is TotalLiquidity (TODO: Write this query)
* Or if you wanted the pool weights, you must now query the pool itself.
* Please give feedback if more queries should be exposed that are pool-type specific
* QuerySpotPrice
* The `withswapfee` param is now removed. If this was needed for anything, please flag it. Its mainly removed due to not having a clear use, and a better query can probably be crafted for.
* Rename TokenInDenom to QuoteAssetDenom
* Rename TokenOutDenom to BaseAssetDenom

## Messages

* (TODO) Rename JoinPool -> JoinPoolNoSwap
* JoinPoolNoSwap
* TokenInMaxs must either contain every token in pool, or no tokens
* JoinPoolSwapExternAmountIn
* Replace sdk.Coin w/ sdk.Coins
* (TODO) Consider renaming to JoinPool, hesistant due to collison with old message
* (TODO) Update the version for all of gamm's proto files
* ExitPool
* Before the message would fail if you had too few shares to get a single token out for any given denom. Now you can 0 of one side out, if the min amount is also not present.
* ExitSwapShareAmountIn
* Switched to a more inefficient algorithm for now, so gas numbers will be much higher.
* Messages now have responses

## Events

## Error message

I anticipate there are lots of error messages that have changed. This is a best-attempt to log ones we know that we changed

* ExitPool when slippage was too high

## Gas numbers
Loading

0 comments on commit f1389ee

Please sign in to comment.