diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ecdc84fc..214134a64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ # Changelog +## Unreleased + +- Fix Astroport PCL spot price bug - failure to utilize token out denom for quote estimate in edge cases + ## v0.17.10 - /config-private endpoint, mask OTEL config in /config endpoint diff --git a/router/usecase/pools/routable_cw_pool.go b/router/usecase/pools/routable_cw_pool.go index c217bd343..2e3fe398c 100644 --- a/router/usecase/pools/routable_cw_pool.go +++ b/router/usecase/pools/routable_cw_pool.go @@ -71,6 +71,10 @@ func (r *routableCosmWasmPoolImpl) GetSpreadFactor() math.LegacyDec { // - the token in amount is greater than the balance of the token in // - the token in amount is greater than the balance of the token out func (r *routableCosmWasmPoolImpl) CalculateTokenOutByTokenIn(ctx context.Context, tokenIn sdk.Coin) (sdk.Coin, error) { + return r.calculateTokenOutByTokenIn(ctx, tokenIn, r.TokenOutDenom) +} + +func (r *routableCosmWasmPoolImpl) calculateTokenOutByTokenIn(ctx context.Context, tokenIn sdk.Coin, tokenOutDenom string) (sdk.Coin, error) { poolType := r.GetType() // Ensure that the pool is cosmwasm @@ -79,7 +83,7 @@ func (r *routableCosmWasmPoolImpl) CalculateTokenOutByTokenIn(ctx context.Contex } // Configure the calc query message - calcMessage := msg.NewCalcOutAmtGivenInRequest(tokenIn, r.TokenOutDenom, r.SpreadFactor) + calcMessage := msg.NewCalcOutAmtGivenInRequest(tokenIn, tokenOutDenom, r.SpreadFactor) calcOutAmtGivenInResponse := msg.CalcOutAmtGivenInResponse{} if err := queryCosmwasmContract(ctx, r.wasmClient, r.ChainPool.ContractAddress, &calcMessage, &calcOutAmtGivenInResponse); err != nil { @@ -113,11 +117,6 @@ func (r *routableCosmWasmPoolImpl) GetTakerFee() math.LegacyDec { return r.TakerFee } -// SetTokenOutDenom implements sqsdomain.RoutablePool. -func (r *routableCosmWasmPoolImpl) SetTokenOutDenom(tokenOutDenom string) { - r.TokenOutDenom = tokenOutDenom -} - // CalcSpotPrice implements sqsdomain.RoutablePool. func (r *routableCosmWasmPoolImpl) CalcSpotPrice(ctx context.Context, baseDenom string, quoteDenom string) (osmomath.BigDec, error) { request := msg.SpotPriceQueryMsg{ @@ -133,10 +132,11 @@ func (r *routableCosmWasmPoolImpl) CalcSpotPrice(ctx context.Context, baseDenom codeID := r.ChainPool.CodeId if codeID == astroportCodeID { // Calculate the spot price using the pool's balances - out, err := r.CalculateTokenOutByTokenIn(ctx, sdk.NewCoin(baseDenom, tenE7)) + + out, err := r.calculateTokenOutByTokenIn(ctx, sdk.NewCoin(quoteDenom, tenE7), baseDenom) // If error, proceed to querying cosmwasm - if err == nil { - spotPrice := osmomath.NewBigDecFromBigIntMut(out.Amount.BigIntMut()).QuoMut(osmomath.NewBigDecFromBigInt(tenE7.BigIntMut())) + if err == nil && !out.Amount.IsZero() { + spotPrice := osmomath.NewBigDecFromBigInt(tenE7.BigIntMut()).QuoMut(osmomath.NewBigDecFromBigIntMut(out.Amount.BigIntMut())) // If spot price is not zero, return it if !spotPrice.IsZero() { diff --git a/router/usecase/router_usecase.go b/router/usecase/router_usecase.go index 594d08f80..d4455970a 100644 --- a/router/usecase/router_usecase.go +++ b/router/usecase/router_usecase.go @@ -705,7 +705,7 @@ func (r *routerUseCaseImpl) GetPoolSpotPrice(ctx context.Context, poolID uint64, return osmomath.BigDec{}, fmt.Errorf("taker fee not found for pool %d, denom in (%s), denom out (%s)", poolID, quoteAsset, baseAsset) } - spotPrice, err := r.poolsUsecase.GetPoolSpotPrice(ctx, poolID, poolTakerFee, baseAsset, quoteAsset) + spotPrice, err := r.poolsUsecase.GetPoolSpotPrice(ctx, poolID, poolTakerFee, quoteAsset, baseAsset) if err != nil { return osmomath.BigDec{}, err } diff --git a/sqsdomain/routable_pool.go b/sqsdomain/routable_pool.go index 29df61342..0179ab45f 100644 --- a/sqsdomain/routable_pool.go +++ b/sqsdomain/routable_pool.go @@ -33,9 +33,6 @@ type RoutablePool interface { CalculateTokenOutByTokenIn(ctx context.Context, tokenIn sdk.Coin) (sdk.Coin, error) ChargeTakerFeeExactIn(tokenIn sdk.Coin) (tokenInAfterFee sdk.Coin) - // SetTokenOutDenom sets the token out denom on the routable pool. - SetTokenOutDenom(tokenOutDenom string) - GetTakerFee() osmomath.Dec GetSpreadFactor() osmomath.Dec diff --git a/tokens/usecase/pricing/chain/pricing_chain.go b/tokens/usecase/pricing/chain/pricing_chain.go index e75fec196..89cd705d9 100644 --- a/tokens/usecase/pricing/chain/pricing_chain.go +++ b/tokens/usecase/pricing/chain/pricing_chain.go @@ -221,8 +221,6 @@ func (c *chainPricing) computePrice(ctx context.Context, baseDenom string, quote if useAlternativeMethod { // Compute on-chain price for 1 unit of base denom and quote denom. chainPrice = osmomath.NewBigDecFromBigInt(tenQuoteCoin.Amount.BigIntMut()).QuoMut(osmomath.NewBigDecFromBigInt(quote.GetAmountOut().BigIntMut())) - } else { - chainPrice = osmomath.OneBigDec().QuoMut(chainPrice) } if chainPrice.IsZero() {