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

Funding fee feature #2584

Merged
merged 12 commits into from
Jun 4, 2024
Merged

Funding fee feature #2584

merged 12 commits into from
Jun 4, 2024

Conversation

luckysori
Copy link
Contributor

@luckysori luckysori commented May 29, 2024

Fixes https://github.com/get10101/meta/issues/330.
Fixes #2535.

Supersedes #2532.

Sorry for the ginormous PR. I tried to build on top of #2532, but too many things changed.

The main patch is c3e5ee8. To be fair, some things do change slightly in the patches that follow that one, so patch by patch review is not ideal. But I think it's good enough.

I think this feature can be deployed if we don't find any flaws, but there are some missing bits:

  • More e2e tests. I focused on rollover tests, but we can also apply funding fee events when settling, resizing and liquidating.
  • UI improvements for the mobile app e.g. get rid of trades in wallet history; include fee rates in trades tab; display time to next funding rate; etc.
  • Adapt web app. This PR focuses on the mobile app, but the web app currently shares the same backend behaviour without displaying all the funding fee/rate information.

@luckysori luckysori requested review from bonomat and holzeis May 29, 2024 14:50
@luckysori luckysori self-assigned this May 29, 2024
@luckysori luckysori force-pushed the feat/funding-fee-extravaganza branch 2 times, most recently from e39b523 to 279eb4e Compare May 30, 2024 03:44
Copy link
Contributor

@holzeis holzeis left a comment

Choose a reason for hiding this comment

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

Nice work!

coordinator/src/db/funding_fee_events.rs Show resolved Hide resolved
coordinator/src/db/funding_fee_events.rs Show resolved Hide resolved
coordinator/src/db/funding_fee_events.rs Outdated Show resolved Hide resolved
mobile/native/src/db/models.rs Show resolved Hide resolved
/// Update the position after the rollover protocol has ended.
pub fn finish_rollover(conn: &mut SqliteConnection, updated_position: Position) -> Result<()> {
let affected_rows = diesel::update(positions::table)
.filter(positions::state.eq(PositionState::Rollover))
Copy link
Contributor

Choose a reason for hiding this comment

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

If we always assume that there is ever only one position, we might want to consider removing this filter as it would potentially only lead to errors if the position is not in the state we assume it should be.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes some sense. Although in theory we could be catching an actual error here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I opened an issue because I think your comment applies to other spots too: #2599.

@@ -370,7 +377,11 @@ impl Node {
"Finished rollover protocol"
);

position::handler::set_position_state(PositionState::Open)?;
let position = update_position_after_rollover()
Copy link
Contributor

Choose a reason for hiding this comment

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

🔧 these two updates should probably run within the same database transaction.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I totally agree, but this requires reworking get_positions, which is used in 11 spots.

mobile/native/src/trade/position/handler.rs Outdated Show resolved Hide resolved
mobile/native/src/trade/position/handler.rs Outdated Show resolved Hide resolved
The `Position` model should be as strongly typed as possible.
Converting to i64 should happen _just_ before inserting into the
database.
This server can be used during local development to query the
coordinator database conveniently e.g.

```
$ curl http://localhost:3002/dlc_protocols | jq .
[
  {
    "id": 1,
    "protocol_id": "fb4c6f27-db9d-4b2d-8539-6e609e1559b9",
    "previous_protocol_id": null,
    "channel_id": "f689230c5477176961301cb22093cfd52050a52356812039f9c298fe8859eba2",
    "contract_id": "972c5fcafbd6bbbf874a9a5cd44d9ddd05a2e3f3ae96d5bd771e5af1b8cae1a6",
    "protocol_state": "Success",
    "trader_pubkey": "02e9f7a0e0d6eeab989dcbb4e91f0bf95f583b09d8d324cc9635807e844eea82bc",
    "timestamp": "2024-05-13T05:32:36.452958+00:00",
    "protocol_type": "open-channel"
  }
]
```

It can also be used in the e2e tests to easily assert on the state of
the coordinator, without needing to add dedicated HTTP API endpoints.
It might even be used to modify the coordinator state to create
interesting test scenarios.
@luckysori luckysori force-pushed the feat/funding-fee-extravaganza branch from 279eb4e to 9972e55 Compare June 4, 2024 09:09
luckysori and others added 9 commits June 4, 2024 20:41
The coordinator generates funding fee events as funding rates are
added to the database and applies them on rollover.

The app displays funding fee events in a dedicated `Trades` tab,
together with regular trades. The funding fee events are added as soon
as the app learns about them from the coordinator, which is usually
before they are resolved.

Additionally, the app will update the position after rolling over
based on the funding fee events that were resolved with the rollover.

Missing bits:

- Apply funding fee events on closing and resizing.

- Considering funding fee events when deciding if positions need to be
liquidated.

- Add some unit tests.

- Expanding e2e tests.

- Add fee rate to app's `Trades` tab.

- Display next fee rate in app's `Trade` screen.

Co-authored-by: Philipp Hoenisch <[email protected]>
We only support one position at a time at the moment.

Co-authored-by: Richard Holzeis <[email protected]>
@luckysori luckysori force-pushed the feat/funding-fee-extravaganza branch from 9972e55 to 3a85bee Compare June 4, 2024 10:43
@luckysori luckysori added this pull request to the merge queue Jun 4, 2024
Merged via the queue into main with commit c507a18 Jun 4, 2024
23 checks passed
@luckysori luckysori deleted the feat/funding-fee-extravaganza branch June 4, 2024 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use outstanding funding fees when considering liquidation
3 participants