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

[Bug]: estimatePosition GraphQL API returns error #8313

Closed
bglownia opened this issue May 17, 2023 · 4 comments · Fixed by #8379
Closed

[Bug]: estimatePosition GraphQL API returns error #8313

bglownia opened this issue May 17, 2023 · 4 comments · Fixed by #8379
Assignees
Labels

Comments

@bglownia
Copy link

bglownia commented May 17, 2023

Problem encountered

Graphql request to https://api.n07.testnet.vega.xyz/graphql

query EstimatePosition($marketId: ID!, $openVolume: String!, $orders: [OrderInfo!], $collateralAvailable: String) {
  estimatePosition(
    marketId: $marketId
    openVolume: $openVolume
    orders: $orders
    collateralAvailable: $collateralAvailable
  ) {
    margin {
      worstCase {
        maintenanceLevel
        searchLevel
        initialLevel
        collateralReleaseLevel
        __typename
      }
      bestCase {
        maintenanceLevel
        searchLevel
        initialLevel
        collateralReleaseLevel
        __typename
      }
      __typename
    }
    liquidation {
      worstCase {
        open_volume_only
        including_buy_orders
        including_sell_orders
        __typename
      }
      bestCase {
        open_volume_only
        including_buy_orders
        including_sell_orders
        __typename
      }
      __typename
    }
    __typename
  }
}
{
  "marketId": "4cb06ca74c44c2c4aea5018ca9106bd771ee0f8904fa4127bcab637bd17801a1",
  "openVolume": "1",
  "orders": [  
    {
      "isMarketOrder": true,
      "price": "0",
      "remaining": "1",
      "side": "SIDE_SELL"
    }
  ],
  "collateralAvailable": "321578449"
}

returns error

{"errors":[{"message":"","path":["estimatePosition"],"extensions":{"code":2,"type":"Unknown"}}],"data":{"estimatePosition":null}}

It happens when openPosition + remaining can be zero. Open volume = 1 and order remaining is 1 on sell side .

@bglownia bglownia added the bug label May 17, 2023
@gordsport gordsport removed their assignment May 18, 2023
@gordsport
Copy link
Contributor

@MuthuVega - please can you catch up with @bglownia and see if we can reproduce this in tests.

@EVODelavega
Copy link
Contributor

After reading through the code for the EstimatePosition call in datanode/api/trading_data_v2.go, it would appear that this is caused by the liquidation estimate calculations returning an error in case the liquidation price could not be determined. This error is not handled, but rather returned straight back to EstimatePosition where it results, and sends back an error response:

calculateLiquidationPrice returns an error if the price is not defined here:

func calculateLiquidationPrice(openVolume num.Decimal, currentPrice, collateralAvailable num.Decimal, linearSlippageFactor, quadraticSlippageFactor, riskFactorLong, riskFactorShort num.Decimal) (num.Decimal, error) {
	rf := riskFactorLong
	if openVolume.IsNegative() {
		rf = riskFactorShort
	}

	denominator := calculateSlippageFactor(openVolume, linearSlippageFactor, quadraticSlippageFactor).Add(openVolume.Abs().Mul(rf)).Sub(openVolume)
	if denominator.IsZero() {
		return num.DecimalZero(), fmt.Errorf("liquidation price not defined")
	}

	ret := collateralAvailable.Sub(openVolume.Mul(currentPrice)).Div(denominator)
	if ret.IsNegative() {
		return num.DecimalZero(), nil
	}
	return ret, nil
}

Which is passed back up to EstimatePosition here:

	var liquidationEstimate *v2.LiquidationEstimate
	if req.CollateralAvailable != nil && len(*req.CollateralAvailable) > 0 {
		liquidationEstimate, err = t.computeLiquidationPriceRange(
			collateralAvailable,
			req.OpenVolume,
			buyOrders,
			sellOrders,
			marketObservable,
			positionFactor,
			linearSlippageFactor,
			quadraticSlippageFactor,
			rf)

		if err != nil {
			return nil, err
		}
	}

Where we just check if err != nil, and in that case return nil, err. Whether we should return an empty liquidation estimate, return some type of approximation (if we can), or are right in returning an error is something that needs to be decided on

@gordsport gordsport assigned EVODelavega and unassigned MuthuVega May 23, 2023
@gordsport gordsport added this to the ☄️ Cosmic Elevator milestone May 23, 2023
@bglownia
Copy link
Author

bglownia commented May 24, 2023

API fails also with

{
  "marketId": "4cb06ca74c44c2c4aea5018ca9106bd771ee0f8904fa4127bcab637bd17801a1",
  "openVolume": "1",
  "orders": [  
    {
      "isMarketOrder": true,
      "price": "0",
      "remaining": "1",
      "side": "SIDE_SELL"
    },
    {
      "isMarketOrder": true,
      "price": "0",
      "remaining": "100",
      "side": "SIDE_SELL"
    }
  ],
  "collateralAvailable": "123"
}

but not with

{
  "marketId": "4cb06ca74c44c2c4aea5018ca9106bd771ee0f8904fa4127bcab637bd17801a1",
  "openVolume": "1",
  "orders": [  
    {
      "isMarketOrder": true,
      "price": "0",
      "remaining": "2",
      "side": "SIDE_SELL"
    },
    {
      "isMarketOrder": true,
      "price": "0",
      "remaining": "99",
      "side": "SIDE_SELL"
    }
  ],
  "collateralAvailable": "123"
}

@gordsport gordsport assigned witgaw and unassigned EVODelavega May 24, 2023
@gordsport
Copy link
Contributor

gordsport commented May 24, 2023

Tests for this can be seen in the market sim:

@gordsport gordsport changed the title [Bug]: estimatePosition grapql API returns error [Bug]: estimatePosition GraphQL API returns error May 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

5 participants