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

chore: Fix liquidation migration script for coordinator leverage = 1 #2373

Merged
merged 1 commit into from
Apr 9, 2024

Conversation

holzeis
Copy link
Contributor

@holzeis holzeis commented Apr 8, 2024

follow up to review remarks on #2354

@holzeis holzeis self-assigned this Apr 8, 2024
@@ -2,4 +2,5 @@ ALTER TABLE "positions"
ADD COLUMN coordinator_liquidation_price REAL NOT NULL DEFAULT 0;

UPDATE positions SET coordinator_liquidation_price = average_entry_price * coordinator_leverage / (coordinator_leverage + 1) where trader_direction='short';
UPDATE positions SET coordinator_liquidation_price = average_entry_price * coordinator_leverage / (coordinator_leverage - 1) where trader_direction='long';
UPDATE positions SET coordinator_liquidation_price = average_entry_price * coordinator_leverage / (coordinator_leverage - 1) where trader_direction='long' and coordinator_leverage > 1;
UPDATE positions SET coordinator_liquidation_price = average_entry_price * coordinator_leverage where trader_direction='long' and coordinator_leverage = 1;
Copy link
Contributor

@luckysori luckysori Apr 8, 2024

Choose a reason for hiding this comment

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

🤔 The formula doesn't look right to me. Elsewhere we've set the liquidation price of a party going short at 1x leverage to the maximum price the oracle can attest to. I'm not sure if that's correct either though cc @bonomat.

Copy link
Contributor

@bonomat bonomat Apr 9, 2024

Choose a reason for hiding this comment

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

Yea, if a party goes short with a leverage of 1 he basically never gets liquidated. So we can set this to a very high number.

On the contrary, if a party goes long with a leverage of 1 his liquidation price is at about 50%, i.e.
(average_entry_price*leverage)/((leverage+1-(maintenance_margin*leverage))) --> average_entry_price/2

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can I get an approval then? 🙂

Copy link
Contributor

@luckysori luckysori Apr 9, 2024

Choose a reason for hiding this comment

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

It still looks wrong to me though. AFAICT, you're setting the coordinator's liquidation price to

average_entry_price * 1

if the coordinator goes short. To be consistent with our internal formulas, I think we want to set this to BTCUSD_MAX_PRICE.

Copy link
Contributor

Choose a reason for hiding this comment

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

Here we go, this should fix it:

Suggested change
UPDATE positions SET coordinator_liquidation_price = average_entry_price * coordinator_leverage where trader_direction='long' and coordinator_leverage = 1;
UPDATE positions SET coordinator_liquidation_price = 21_000_000 where trader_direction='long' and coordinator_leverage = 1;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fair, I will adapt that.

Copy link
Contributor Author

@holzeis holzeis Apr 9, 2024

Choose a reason for hiding this comment

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

@bonomat I used the const from the code, to be consistent with our implementation.

pub const BTCUSD_MAX_PRICE: u64 = 1_048_575;

Copy link
Contributor

@luckysori luckysori left a comment

Choose a reason for hiding this comment

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

Not sure about the formula.

@holzeis holzeis enabled auto-merge April 9, 2024 07:13
@holzeis holzeis added this pull request to the merge queue Apr 9, 2024
Merged via the queue into main with commit eebd61f Apr 9, 2024
21 checks passed
@holzeis holzeis deleted the fix/migration-script branch April 9, 2024 07:38
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.

3 participants