Skip to content

Instantly share code, notes, and snippets.

@dgpv
Last active December 16, 2020 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgpv/49bba5ac33764301204493f2837be270 to your computer and use it in GitHub Desktop.
Save dgpv/49bba5ac33764301204493f2837be270 to your computer and use it in GitHub Desktop.
This is the sketch of the loan contract that allows price-dependent liquidation without involving an oracle.
Author: Dmitry Petukhov (https://github.com/dgpv/)
Concrete implementation is not yet thought out.
This sketch does not take into account the fees that the debtor has to pay, for simplicity of illustration.
Fees will make calculations more complex, but the basic principle should still work.
Loan contract:
- Alice puts 1 btc collateral,
- Bob gives 10K usdt loan
- the price of 1 btc at the start of the contract: 20K
- the LTV is 50%
- liquidation at LTV 90.9%
During the loan term, the price of 1 btc falls to 11K, LTV is 90,9%, Bob wants to liquidate
Replenishment or liquidation:
- *Bob* adds 10.818 btc to the contract, 11.818 btc now in the contract
- After that,
within "replenishment" window:
either:
- Alice adds 0.818 btc to the contract, 10.818 btc is sent to Bob, 1.818 btc is left in the contract,
LTV is decreased
or:
- Alice adds 118998 usdt to the contract, 118998 usdt is sent to Bob and 10 btc is sent to Alice.
1.818 btc is left in the contract.
Alice in effect buys 10.818 btc for 11K, 0.818 of which is added to collateral to decrease LTV,
and 10 btc goes to Alice (or liquidity provider)
otherwise, after "replenishment" window:
- Bob takes the collateral (takes his 10.818 btc back and gets 1 btc of Alice's collateral)
If Bob initiates liquidation at the price of 12K:
- The LTV is at 83% - Bob initiated too early
- Alice sends 118998 usdt to Bob, Alice gets 10 btc and ~0.818 btc is additionally locked as collateral.
Alice in effect just bought 10.818 btc at 11K usdt, and can immediately sell 10 btc for 12k usdt,
getting 10K usdt direct profit, and additional 818 usdt of profit on the locked collateral.
In effect, Bob just have sold 10.818 btc to alice at 11K while the current price is 12K, at
loss of 10818 usdt.
- Therefore, Bob is not incentivized to initiate the liquidation early
If Bob initiates liquidation at the price of 9K:
- The loan is underwater - bob initiated too late
- If Alice does not want to default, she adds 0.818 btc to the contract, equivalent of
7362 usdt, because sending 118998 usdt to get Bob's 10 btc would be too expensive at current price.
- The loan is now at LTV 61%
If Bob initiates liquidation at the price of 10K:
- Alice can use either strategy, but likely just adds 0.818 btc, because otherwise she would
need a large sum to pay for Bob's 10 btc
- Or Alice can default, and Bob will get 1 btc of collateral that he can immediately sell for 10K
(note that we ignore the loan fees, if we account for fees, 50% ltv is probably too low for htis example)
If Bob initiates liquidation at the price of 10500:
- Alice sends 118998 usdt to Bob, Alice gets 10 btc and ~0.818 btc is additionally locked as collateral.
Alice in effect just bought 10.818 btc, 0.818 of which is added to the collateral, at the price of 11K usdt,
and she lost 5409 usdt.
Would she default, she would lose 1 btc (equiv. 10500 usdt),
Rather than buy the Bob's challenge, Alice is incentivized to just add 0.818 collateral,
that she can buy at the price of 10500 per 1 btc, not 11K per 1 btc.
- If Alice buys Bob's challenge, Bob just have sold 10.818 btc to alice at 11K while the current price is 10500, at
a profit of 5409 usdt
- If Alice just adds 0.818 to the collateral, Bob reached his original goal, to force Alice to increase the collateral
The next stage of the contract might enforce lower LTV, say, 75%.
This means that in the second example above (at 9K), if the price falls further, Bob will
be able to initiate another liquidation shortly.
There may be no next stage, and participants would be expected to settle interactively to create a new contract
before LTV approached the threshold value. In addition to upper threshold (LTV 90%), there may also be lower
threshold value - say, at LTV 25% Alice will be able to just repay 10K without any fees, if Bob did not settle
to reduce collateral via interactive settlement. This exact-10K-repayment at LTV 25% will be forced by Alice moving
usdt to the contract at the expected price (40K per 1btc) and allowing Bob to either take 10K usdt and send
30K usdt + 1 btc of collateral to Alice, or adding 0.75 btc and taking 40K usdt (for a profit of 10K),
and 0.75 btc + 1 btc of collateral is sent to Alice.
After timeout, Alice would be able to just take 30K usdt + 1 btc but she need to send 10K to Bob
Participants can use liquidity providers to get additional sums needed to execute their actions.
----
Previous version of this sketch, that did not use leverage:
https://gist.github.com/dgpv/49bba5ac33764301204493f2837be270/083cae0e2706da4f1dd34ee9e78d30136b42db13
The difference between no-leverate/use-leverage version is the amount that Bob risks
at malicious liquidation attempt.
Without leverage, at price 20K, Bob risks to lose 7362 on the price difference of 0.818 btc,
plus 10K of the principal (for total of 17362 usdt loss), to have a shot at illegigimately ackquiring
1 btc (worth 20K usdt) while still losing the 10K of the principal, so the potential profit is 10K usdt
At price 30K, Bob risks to lose 15542+10K=25542 usdt, for a shot at illegitimate profit of 30K-10K=20K usdt.
At price 15K, Bob risks to lose 3162 usdt for a shot at illegitimate profit of 5K usdt
At price 12K, Bob risks to lose 708 usdt for a shot at illegitimate profit of 2K usdt
With leverage (10 btc), at price 20K, Bob risks to lose 97362 usdt on the price difference of 10.818 btc,
plus 10K of the principal (for total of 107362 usdt loss), for the same potential profit of 10K usdt
At price of 12K, Bob risks to lose 10818 usdt for a shot at illegitimate profit of 2K usdt
With leverage of 2 btc:
At price 12K, Bob risks to lose 2700 usdt for a shot at illegitimate profit of 2K usdt
At price 11200, Bob risk to lose 453 usdt for a shot at illegitimate profit of 1200 usdt.
(would leverage be 6 btc istead, at price 11200 Bob would risk 1200 for potential 1253 profit)
If we say that Bob will attempt malicious liquidation only if potential profit exceeds potential loss,
we can say that leverage protection is most relevant when the price approaches the potential point of
legitimate liquidation threshold.
Because Bob doesn't know how good Alice watches the contract and what is the chance that she will not
counter the malicious liquidation attempt, it is likely that Bob will be more cautious and potential profit
from malicious liquidation need to be much bigger than potential loss for him to act maliciously.
The above calculation also does not account for the loss of the future loan fees that Bob will lose in case of
malicious liquidation.
----
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment