NibiruChain / nibiru

Nibiru Chain: The breakthrough smart contract platform ushering in the next era of money. Nibiru powers an ecosystem of dApps including perps, RWAs, and more.
https://nibiru.fi
Apache License 2.0
187 stars 201 forks source link

feat: A query for all of a trader's positions (every vpool) #931

Closed Unique-Divine closed 2 years ago

Unique-Divine commented 2 years ago

Context

It would be very useful to have a query that returns an array of all of a trader's active positions indexed by the string identifier for each vpool:

Requested Functionality

nibid query perp all-positions [address]

The returned response would be a an array of maps from string to QueryTraderPositionResponse types.

[
  "ubtc:unusd": {
    "position": {
      "trader_address": "nibi1ah8gqrtjllhc5ld4rxgl4uglvwl93ag0sh6e6v",
      "pair": {
        "token0": "ubtc",
        "token1": "unusd"
      },
      "size": "200015.306895339315011385",
      "margin": "1999425346.948817872092699127",
      "open_notional": "4000000000.000000000000000000",
      "last_update_cumulative_premium_fraction": "-6831.818083040603478395",
      "block_number": "856652"
    },
    "position_notional": "3999999999.999999999999993448",
    "unrealized_pnl": "-0.000000000000006552",
    "margin_ratio_mark": "0.482808126123902361",
    "margin_ratio_index": "0.484930504069882468",
    "block_number": "925768"
},
"ueth:unusd": {
  "position": {"..."},
    "position_notional": "20000000000.50",
    "unrealized_pnl": "-0.000000000000006552",
    "margin_ratio_mark": "0.482808126123902361",
    "margin_ratio_index": "0.484930504069882468",
    "block_number": "925768"
}, 
"uatom:unusd": {
  "position": {"..."}
    "position_notional:": "..."
    // ...
  }
  // ...
]

The proto RPC method would look something like this:

  rpc QueryAllPositions(QueryAllPositionsRequest) returns (QueryAllPositionsResponse) {
    option (google.api.http).get = "/nibiru/perp/all_positions";
  }
testinginprod commented 2 years ago

@NibiruHeisenberg moved this issue to myself since I'm taking care of perp state refactoring.