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

fix: DYDX spot price bug #212

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions router/usecase/pools/routable_cw_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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{
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion router/usecase/router_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: This is a drive-by change. We were negating this in pricing_chain.go so it did not result in any bug

if err != nil {
return osmomath.BigDec{}, err
}
Expand Down
3 changes: 0 additions & 3 deletions sqsdomain/routable_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we remove this at a later date? Will this break the node, if sqsdomain isn't updated?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, this will not break the node.

It would only break if we changed serialization.

Copy link
Contributor

Choose a reason for hiding this comment

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

Great, happy to approve then


GetTakerFee() osmomath.Dec

GetSpreadFactor() osmomath.Dec
Expand Down
2 changes: 0 additions & 2 deletions tokens/usecase/pricing/chain/pricing_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines -224 to -225
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: This is a drive-by change. This was needed because we were confusing base and quote in router_usecase.go

}

if chainPrice.IsZero() {
Expand Down
Loading