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

feat(x/perp): the 'q position' query should expose 'marginRatioMark', 'marginRatioIndex', (and 'blockHeight') #763

Closed
onikonychev opened this issue Jul 29, 2022 · 4 comments · Fixed by #766 or #810
Assignees

Comments

@onikonychev
Copy link
Contributor

onikonychev commented Jul 29, 2022

Context

Liquidator is using this margin_ratio value to detect underwater positions. Liquidation condition based on mark price happens much sooner than TWAP and liquidator makes a long series of useless attempts until TWAP price reaches the mark price value.

Request is to return margin_ratio based on TWAP price.

Solution

Return both margin ratios so that the the liquidator has awareness of "underwater" status even if the mark and index prices diverge substantially.

@NibiruHeisenberg
Copy link
Contributor

I'll return the MaxPnL (between spot and twap) since that's what the liquidation codepath checks for when liquidating.

@onikonychev
Copy link
Contributor Author

I think I found a bug here. The updated logic works well for newly created positions. Margin ratio is tracked well and liquidation occurs properly.

But if the same trader after being liquidated opens a new position, his margin ratio is tracked wrongly. Position state is returning margin_ratio=0.059 but attempt to liquidate fails.

nibid q perp trader-position nibi14jvt2vldx8p5nnnmlxshcppqz7s5ys4mkchcym ubtc:unusd
{
   "position":{
      "trader_address":"nibi14jvt2vldx8p5nnnmlxshcppqz7s5ys4mkchcym",
      "pair":{
         "token0":"ubtc",
         "token1":"unusd"
      },
      "size":"1638.742599569168112704",
      "margin":"429686.503503750692187819",
      "open_notional":"4296865.035037506921878188",
      "last_update_cumulative_premium_fraction":"-442.146000081041666666",
      "block_number":"512"
   },
   "position_notional":"4111277.435630248352366047",
   "unrealized_pnl":"-185587.599407258569512141",
   "margin_ratio":"0.059373007032076487"
}


# Here liquidator tries to liquidate this

ERROR:root:Failed tx execution: failed to simulate tx : <_InactiveRpcError of RPC that terminated with:
	status = StatusCode.UNKNOWN
	details = "failed to execute message; message index: 0: margin is higher than required maintenance margin ratio"
	debug_error_string = "{"created":"@1659712567.493972000","description":"Error received from peer ipv6:[::1]:9090","file":"src/core/lib/surface/call.cc","file_line":967,"grpc_message":"failed to execute message; message index: 0: margin is higher than required maintenance margin ratio","grpc_status":2}"
>

^^ @NibiruHeisenberg

@NibiruHeisenberg
Copy link
Contributor

Interesting, I'll take a look.

@onikonychev onikonychev changed the title bug (perp): Trader position should return margin_ratio based on TWAP price, not mark price bug (perp): Trader position should return margin_ratio based on TWAP / Mark or Oracle price depending on chain conditions Aug 8, 2022
@onikonychev
Copy link
Contributor Author

Latest update:
Recent fix returns margin_ratio based on MaxPnL which is correct if Oracle price is close to Mark Price. In case of >10% divergence margin_ratio returned is not actual because internally chain liquidation uses oracle price instead. And this leads to useless liquidation attempts and ugly logging / metrics.

@Unique-Divine Unique-Divine changed the title bug (perp): Trader position should return margin_ratio based on TWAP / Mark or Oracle price depending on chain conditions feat(x/perp): the q position query should expose marginRatioMark, marginRatioIndex, (and blockHeight) Aug 9, 2022
@Unique-Divine Unique-Divine self-assigned this Aug 9, 2022
@Unique-Divine Unique-Divine changed the title feat(x/perp): the q position query should expose marginRatioMark, marginRatioIndex, (and blockHeight) feat(x/perp): the 'q position' query should expose 'marginRatioMark', 'marginRatioIndex', (and 'blockHeight') Aug 9, 2022
Unique-Divine added a commit that referenced this issue Aug 9, 2022
AgentSmithMatrix pushed a commit that referenced this issue Aug 10, 2022
…Position (#810)

* feat(x/perp): add marginRatioIndex to the position query

* add blockNumber to QueryTraderPosition

* update CHANGELOG.md #763

* fix(CHANGELOG)
Unique-Divine added a commit that referenced this issue Aug 12, 2022
commit b496438338d5cd136661e6cf79a694e47a8198b9
Author: Unique-Divine <realuniquedivine@gmail.com>
Date:   Fri Aug 12 18:03:02 2022 -0500

    remove deperecated docs from x/perp/README

commit fc260a5
Author: Unique Divine <51418232+Unique-Divine@users.noreply.github.com>
Date:   Thu Aug 11 16:37:34 2022 -0500

    chore: release v0.12.1-alpha (#820)

    update changelog

commit 8ec477a
Author: AgentSmithMatrix <98403347+AgentSmithMatrix@users.noreply.github.com>
Date:   Thu Aug 11 19:47:03 2022 +0200

    feat: add golangci-lint to makefile (#819)

    * add linter to makefile

    * update changelog

commit 208a102
Author: Unique Divine <51418232+Unique-Divine@users.noreply.github.com>
Date:   Wed Aug 10 13:17:05 2022 -0500

    fix: missing max leverage on the pools in app_state.vpool.vpools (#818)

commit 75e8f03
Author: Unique Divine <51418232+Unique-Divine@users.noreply.github.com>
Date:   Wed Aug 10 07:38:00 2022 -0500

    feat(vpool): expose markPrice, markTwap, indexPrice, (and blockHeight?) on the all-pools query #789 (#813)

    * (vpool): add logger to keeper.go

    * feat: GetPoolPrices, fn to return the mark price, mark twap, index price, and swap inv. for a pool

    * docs: fix the fn description for GetCurrentTWAP

    * feat(vpool): implemenet PoolPrices and add it to 'nibid q vpool all-pools' command

    * update changelog

    * (vpool): add block number to all-pools.prices. Fix changelog

    Co-authored-by: AgentSmithMatrix <98403347+AgentSmithMatrix@users.noreply.github.com>
    Co-authored-by: Agent Smith <agentsmith@matrixsystems.co>

commit 674da1b
Author: Unique Divine <51418232+Unique-Divine@users.noreply.github.com>
Date:   Wed Aug 10 07:01:20 2022 -0500

    feat(perp): expose 'marginRatioIndex' and block number on QueryTraderPosition (#810)

    * feat(x/perp): add marginRatioIndex to the position query

    * add blockNumber to QueryTraderPosition

    * update CHANGELOG.md #763

    * fix(CHANGELOG)

commit c6ad354
Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Date:   Wed Aug 10 01:52:40 2022 -0500

    chore(deps): bump github.com/cosmos/ibc-go/v3 from 3.1.0 to 3.1.1 (#804)

    * chore(deps): bump github.com/cosmos/ibc-go/v3 from 3.1.0 to 3.1.1

    Bumps [github.com/cosmos/ibc-go/v3](https://github.com/cosmos/ibc-go) from 3.1.0 to 3.1.1.
    - [Release notes](https://github.com/cosmos/ibc-go/releases)
    - [Changelog](https://github.com/cosmos/ibc-go/blob/main/CHANGELOG.md)
    - [Commits](cosmos/ibc-go@v3.1.0...v3.1.1)

    ---
    updated-dependencies:
    - dependency-name: github.com/cosmos/ibc-go/v3
      dependency-type: direct:production
      update-type: version-update:semver-patch
    ...

    Signed-off-by: dependabot[bot] <support@github.com>

    * Update CHANGELOG.md

    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    Co-authored-by: NibiruHeisenberg <101130700+NibiruHeisenberg@users.noreply.github.com>
    Co-authored-by: AgentSmithMatrix <98403347+AgentSmithMatrix@users.noreply.github.com>
NibiruHeisenberg added a commit that referenced this issue Aug 15, 2022
* docs(perp): events #wip

* docs(perp): Added events specification for the perp module.

* Squashed commit of the following:

commit b496438338d5cd136661e6cf79a694e47a8198b9
Author: Unique-Divine <realuniquedivine@gmail.com>
Date:   Fri Aug 12 18:03:02 2022 -0500

    remove deperecated docs from x/perp/README

commit fc260a5
Author: Unique Divine <51418232+Unique-Divine@users.noreply.github.com>
Date:   Thu Aug 11 16:37:34 2022 -0500

    chore: release v0.12.1-alpha (#820)

    update changelog

commit 8ec477a
Author: AgentSmithMatrix <98403347+AgentSmithMatrix@users.noreply.github.com>
Date:   Thu Aug 11 19:47:03 2022 +0200

    feat: add golangci-lint to makefile (#819)

    * add linter to makefile

    * update changelog

commit 208a102
Author: Unique Divine <51418232+Unique-Divine@users.noreply.github.com>
Date:   Wed Aug 10 13:17:05 2022 -0500

    fix: missing max leverage on the pools in app_state.vpool.vpools (#818)

commit 75e8f03
Author: Unique Divine <51418232+Unique-Divine@users.noreply.github.com>
Date:   Wed Aug 10 07:38:00 2022 -0500

    feat(vpool): expose markPrice, markTwap, indexPrice, (and blockHeight?) on the all-pools query #789 (#813)

    * (vpool): add logger to keeper.go

    * feat: GetPoolPrices, fn to return the mark price, mark twap, index price, and swap inv. for a pool

    * docs: fix the fn description for GetCurrentTWAP

    * feat(vpool): implemenet PoolPrices and add it to 'nibid q vpool all-pools' command

    * update changelog

    * (vpool): add block number to all-pools.prices. Fix changelog

    Co-authored-by: AgentSmithMatrix <98403347+AgentSmithMatrix@users.noreply.github.com>
    Co-authored-by: Agent Smith <agentsmith@matrixsystems.co>

commit 674da1b
Author: Unique Divine <51418232+Unique-Divine@users.noreply.github.com>
Date:   Wed Aug 10 07:01:20 2022 -0500

    feat(perp): expose 'marginRatioIndex' and block number on QueryTraderPosition (#810)

    * feat(x/perp): add marginRatioIndex to the position query

    * add blockNumber to QueryTraderPosition

    * update CHANGELOG.md #763

    * fix(CHANGELOG)

commit c6ad354
Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Date:   Wed Aug 10 01:52:40 2022 -0500

    chore(deps): bump github.com/cosmos/ibc-go/v3 from 3.1.0 to 3.1.1 (#804)

    * chore(deps): bump github.com/cosmos/ibc-go/v3 from 3.1.0 to 3.1.1

    Bumps [github.com/cosmos/ibc-go/v3](https://github.com/cosmos/ibc-go) from 3.1.0 to 3.1.1.
    - [Release notes](https://github.com/cosmos/ibc-go/releases)
    - [Changelog](https://github.com/cosmos/ibc-go/blob/main/CHANGELOG.md)
    - [Commits](cosmos/ibc-go@v3.1.0...v3.1.1)

    ---
    updated-dependencies:
    - dependency-name: github.com/cosmos/ibc-go/v3
      dependency-type: direct:production
      update-type: version-update:semver-patch
    ...

    Signed-off-by: dependabot[bot] <support@github.com>

    * Update CHANGELOG.md

    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    Co-authored-by: NibiruHeisenberg <101130700+NibiruHeisenberg@users.noreply.github.com>
    Co-authored-by: AgentSmithMatrix <98403347+AgentSmithMatrix@users.noreply.github.com>

Co-authored-by: NibiruHeisenberg <101130700+NibiruHeisenberg@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment