osmosis icon indicating copy to clipboard operation
osmosis copied to clipboard

Estimate swap

Open vuong177 opened this issue 2 years ago • 1 comments

Closes: #2337

What is the purpose of the change

Change EstimateSwap to not require assets.

Brief Changelog

  • Remove field Sender in EstimateSwap queries.

Testing and Verifying

This change is a trivial rework / code cleanup without any test coverage.

Documentation and Release Note

  • Does this pull request introduce a new feature or user-facing behavior changes? (yes / no) no
  • Is a relevant changelog entry added to the Unreleased section in CHANGELOG.md? (yes / no) no
  • How is the feature or change documented? (not applicable / specification (x/<module>/spec/) / Osmosis docs repo / not documented) not applicable

vuong177 avatar Aug 12 '22 05:08 vuong177

I've created a PR https://github.com/osmosis-labs/bindings/pull/37 to remove sender field. And use osmo_reflect.wasm in this contract.

vuong177 avatar Aug 13 '22 08:08 vuong177

Thanks @ValarDragon @mattverse. I'll make change from your suggestion.

vuong177 avatar Aug 19 '22 19:08 vuong177

This absolutely needs a changelog update. @mattverse can you PR one?

ValarDragon avatar Aug 22 '22 13:08 ValarDragon

Wait this code completely changed since last review, this is so much code duplication and very different. I don't think this is something we should've merged

ValarDragon avatar Aug 22 '22 13:08 ValarDragon

Reverted the merged PR for now, would be committing on top of this branch for future refactoring and fixes and then re open PR for this

mattverse avatar Aug 22 '22 13:08 mattverse

hey duplicate code because estimate swap have similar logic as the real swap. Should we add a boolean parameter estimate in UpdatePoolForSwap. Like that

func (k Keeper) updatePoolForSwap(
	ctx sdk.Context,
	pool types.PoolI,
	sender sdk.AccAddress,
	tokenIn sdk.Coin,
	tokenOut sdk.Coin,
	estimate bool,
) error {
	tokensIn := sdk.Coins{tokenIn}
	tokensOut := sdk.Coins{tokenOut}

	err := k.setPool(ctx, pool)
	if err != nil {
		return err
	}

	if estimate {
		err = k.bankKeeper.SendCoins(ctx, sender, pool.GetAddress(), sdk.Coins{
			tokenIn,
		})
		if err != nil {
			return err
		}

		err = k.bankKeeper.SendCoins(ctx, pool.GetAddress(), sender, sdk.Coins{
			tokenOut,
		})
		if err != nil {
			return err
		}
		events.EmitSwapEvent(ctx, sender, pool.GetId(), tokensIn, tokensOut)
		k.hooks.AfterSwap(ctx, sender, pool.GetId(), tokensIn, tokensOut)
	}

	k.RecordTotalLiquidityIncrease(ctx, tokensIn)
	k.RecordTotalLiquidityDecrease(ctx, tokensOut)

	return err
}

vuong177 avatar Aug 22 '22 14:08 vuong177

Also add estimate in SwapExactAmountIn, SwapExactAmountOut ,MultihopSwapExactAmountIn, MultihopSwapExactAmountOut

vuong177 avatar Aug 22 '22 14:08 vuong177

Yeah, with the scope of this PR getting big, the approach I would want to take on re-merging this PR is me shooting a secondary PR on top of this branch for further refactoring and reducing code duplication, after we merge that second PR into this branch, we can re open this PR, review and then re merge this PR

mattverse avatar Aug 24 '22 07:08 mattverse

@vuong177 https://github.com/osmosis-labs/osmosis/pull/2501 would appreciate it if you could also review this :)

mattverse avatar Aug 24 '22 08:08 mattverse