Skip to content

Commit

Permalink
feat: switching to isolated margin on a fully collateralised margin i…
Browse files Browse the repository at this point in the history
…s not allowed

Signed-off-by: Elias Van Ootegem <[email protected]>
  • Loading branch information
EVODelavega committed May 15, 2024
1 parent 9710000 commit ba83167
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions core/execution/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,6 @@ var (
ErrAMMCannotRebase = errors.New("not enough liquidity for AMM to rebase")
// ErrInvalidOrderPrice is returned when an order is submitted to a capped future with a price > max price.
ErrInvalidOrderPrice = errors.New("invalid order price")
// ErrIsolatedMarginFullyCollateralised is returned when a party tries to switch margin modes on a fully collateralised market.
ErrIsolatedMarginFullyCollateralised = errors.New("isolated margin not permitted on fully collateralised markets")
)
3 changes: 3 additions & 0 deletions core/execution/future/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -4990,6 +4990,9 @@ func (m *Market) GetRiskFactors() *types.RiskFactor {
}

func (m *Market) UpdateMarginMode(ctx context.Context, party string, marginMode types.MarginMode, marginFactor num.Decimal) error {
if m.fCap != nil && m.fCap.FullyCollateralised {
return common.ErrIsolatedMarginFullyCollateralised
}
if err := m.switchMarginMode(ctx, party, marginMode, marginFactor); err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,13 @@ Feature: When `max_price` is specified and the market is ran in a fully-collater
# aux1: position * 1100 + 999*2 = 1100 + 1998 = 3098
# aux2: then placing the order (max price - average order price) * 3 = (1500 - (1301 + 1301 + 1100)/3) * 3 = (1500 - 1234) * 3 = 266 * 3 = 798
# aux2's short position and potential margins are calculated separately as 2 * (1500-1301) + 1 * (1500 - 1100) = 398 + 400 = 798
Then the parties should have the following account balances:
And the parties should have the following account balances:
| party | asset | market id | margin | general |
| party1 | USD | ETH/DEC21 | 5000 | 5500 |
| party2 | USD | ETH/DEC21 | 2500 | 7000 |
| aux1 | USD | ETH/DEC21 | 3098 | 96908 |
| aux2 | USD | ETH/DEC21 | 798 | 99174 |
# The market is fully collateralised, switching to isolated margin is not supported
When the parties submit update margin mode:
| party | market | margin_mode | margin_factor | error |
| party1 | ETH/DEC21 | isolated margin | 0.5 | isolated margin not permitted on fully collateralised markets |

0 comments on commit ba83167

Please sign in to comment.