Twilight
Search…
SettlementTx
There are two ideal cases in settlement. One when the trader loses margin in the position, and the other when trader gains margin in the position.
There are rather 2 scenarios when the trader would lose part of the margin. One is when the liquidation price hits. The other is when the settlementPrice is below the entryPrice.

Liquidations

The constraints below are used for liquidation of OrderTx.
(IMAM)LP=PLPANDlendState1lendState0=P(IM-AM)*LP=P*LP \\ AND \\ lendState_1-lendState_0=P
  • IM: Initial Margin, AM: Available Margin, P: Payment
  • LP: Liquidation Price
  • AND is a conjunction constraint
  • 0 and 1 subcripts are referrence to previous and new state
The payment is deducted from the OrderTx and added to LendTx, changing the LendState.
1
p.push(Commitment::blinded(IM))
2
.var()
3
.expr()
4
.push(Commitment::blinded(AM))

5
.var()
6
.expr()

7
.neg()

8
.add()

9
.push(LP)
10
.dup()

11
.r#const()
12
.roll:2()
13
.mul()
14
.push(Commitment::blinded(P))
15
.var()

16
.dup()
17
.expr()

18
.roll:3()
19
.mul()

20
.roll:2()

21
.eq()

22
.push(Commitment::blinded(newlendstate))
23
.var()
24
.expr()
25
.push(Commitment::blinded(lendstate))
26
.var()

27
.expr()
28
.neg()
29
.add()
30
.roll:2()
31
.expr()

32
.eq()

33
.and()
34
.verify()
Copied!
The liquidation price LP is pushed in cleartext to the stack.

Earn Settlement

When a OrderTx gains margin balance, the payment is deducted from the Lendtx and added to the OrderTx. The settlement price is not pushed to the stack in this case.
(AMIM)=PANDlendState1lendState0=P(AM-IM)=P \\ AND \\ lendState_1-lendState_0=P
  • IM: Initial Margin, AM: Available Margin, P: Payment
  • AND is the conjunction constraint
  • 0 and 1 subscripts are reference to previous and new state
1
p.push(Commitment::blinded(AM))
2
.var()

3
.expr()

4
.push(Commitment::blinded(IM))
5
.var()

6
.expr()

7
.neg()

8
.add()
9
.push(Commitment::blinded(P))

10
.var()
11
.dup()

12
.expr()

13
.roll:2()

14
.eq()

15
.push(Commitment::blinded(lendState))
16
.var()
17
.expr()
18
.push(Commitment::blinded(newLendState))

19
.var()

20
.expr()

21
.neg()
22
.add()

23
.roll:2()

24
.expr()

25
.eq()

26
.and()
27
.verify()
Copied!
Last modified 1yr ago