The idea: call a contract during liquidation (like a "margin call") that can potentially "fix" the position by adding margin.
Motivation
The motivation is to allow contracts to manage multiple market positions on behalf of a user (or strategies) to in a capital efficient manner. Without a margin-call callback this would depend on active monitoring and keepers, but with a margin-call callback this could be done without keepers, all on-chain.
Of course great care would need to be taken to consider all the possible reentrancy issues and risks.
The benefit of this approach over switching to cross margin architecture is that this will preserve the ability to use isolated positions for simple risk-management and gas-efficiency for composability, but will add the more capital-efficient ability to have shared margin. While not fragmenting the market, and not requiring any additional keepers to work.
Rough implementation idea
Add a _marginCall method that calls the owning account as a contract that is implementing a marginCallback when a liquidation transaction for a position happens.
This call happens in a try-catch (the hacky 0.5 version of it with a low level call).
Is gas-limited to prevent gas-blocking of liquidations.
If the call succeeds, and it "fixes" the position (e.g. added sufficient margin), liquidation is avoided, but liquidator keeper is paid a small fee (to prevent griefing the keepers).
Reentrancy is not allowed, the only method that will be allowed is an addMargin call (which will have liquidation checks only after adding margin).
Liquidator min-fee will still need to be paid to prevent griefing liquidators.
if the user isn't a contract, margin-call isn't called.
Cross-margin (shared margin) example usage:
The shared-margin account is a contract that opens isolated positions on behalf of a user, and implements a marginCallback method that moves margin around between isolated positions when one of these positions is being liquidated (e.g. by checking which one has highest accessible margin and moving part of that into the at-risk position).
A keeper will not be required for this to work, but can help avoid running out of gas during the marginCallback (if e.g. multiple positions need to have margin withdrawn), and to avoid paying liquidators for margin-calling.
TL;DR
The idea: call a contract during liquidation (like a "margin call") that can potentially "fix" the position by adding margin.
Motivation
The motivation is to allow contracts to manage multiple market positions on behalf of a user (or strategies) to in a capital efficient manner. Without a margin-call callback this would depend on active monitoring and keepers, but with a margin-call callback this could be done without keepers, all on-chain.
Of course great care would need to be taken to consider all the possible reentrancy issues and risks.
The benefit of this approach over switching to cross margin architecture is that this will preserve the ability to use isolated positions for simple risk-management and gas-efficiency for composability, but will add the more capital-efficient ability to have shared margin. While not fragmenting the market, and not requiring any additional keepers to work.
Rough implementation idea
marginCallback
when a liquidation transaction for a position happens.addMargin
call (which will have liquidation checks only after adding margin).Cross-margin (shared margin) example usage:
marginCallback
method that moves margin around between isolated positions when one of these positions is being liquidated (e.g. by checking which one has highest accessible margin and moving part of that into the at-risk position).