Skip to content

Commit

Permalink
calculate liquidation value without discounting the balance with reco…
Browse files Browse the repository at this point in the history
…very rate (#87)

* calculate liquidation value without discounting the balance with recovery rate

* add check for negative liquidation value
  • Loading branch information
ganzai-san authored Jul 19, 2024
1 parent 7120631 commit 2533bdd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
29 changes: 11 additions & 18 deletions terminate/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,20 @@ import (
)

func (ats PreviewAgentTerminationSummary) LiquidationValue() *big.Int {
// if the termination penalty is greater than the initial pledge,
// OR if the initial pledge is 0,
// we report liquidation value as 0
if ats.InitialPledge.Cmp(ats.TerminationPenalty) < 0 || ats.InitialPledge.Cmp(big.NewInt(0)) == 0 {
return big.NewInt(0)
}
// total available balance is the sum of the miners available balance and the agent available balance
totalAvailableBalance := new(big.Int).Add(ats.MinersAvailableBal, ats.AgentAvailableBal)

totalAvail := new(big.Int).Add(ats.MinersAvailableBal, ats.AgentAvailableBal)
// first we multiply the available balance by the recovery rate
// recovery rate = (initial pledge - termination penalty) / initial pledge
// discounted avail = (available * initial pledge - available * termination penalty) / initial pledge
availTimesPledge := new(big.Int).Mul(totalAvail, ats.InitialPledge)
availTimesTermPenalty := new(big.Int).Mul(totalAvail, ats.TerminationPenalty)
// we add the total available balance to the vesting balance and initial pledge, subtract the termination penalty to get the liquidation value
totalAssets := new(big.Int).Add(totalAvailableBalance, ats.VestingBalance)
totalAssets.Add(totalAssets, ats.InitialPledge)

discountedAvail := new(big.Int).Sub(availTimesPledge, availTimesTermPenalty)
discountedAvail.Div(discountedAvail, ats.InitialPledge)
// if the total assets is less than or equal the termination penalty, we return 0
if totalAssets.Cmp(ats.TerminationPenalty) <= 0 {
return big.NewInt(0)
}

// we add the discounted available balance to the vesting balance and initial pledge, subtract the termination penalty to get the liquidation value
liquidationValue := new(big.Int).Add(discountedAvail, ats.VestingBalance)
liquidationValue.Add(liquidationValue, ats.InitialPledge)
liquidationValue.Sub(liquidationValue, ats.TerminationPenalty)
// liquidation value = total assets - termination penalty
liquidationValue := new(big.Int).Sub(totalAssets, ats.TerminationPenalty)

return liquidationValue
}
Expand Down
4 changes: 2 additions & 2 deletions terminate/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestAgentLiquidationValue(t *testing.T) {
MinersAvailableBal: util.ToAtto(big.NewFloat(100)),
AgentAvailableBal: util.ToAtto(big.NewFloat(0)),
VestingBalance: util.ToAtto(big.NewFloat(100)),
}, util.ToAtto(big.NewFloat(650))},
}, util.ToAtto(big.NewFloat(700))},
// case 2 - 90% recovery rate, 0 available
{PreviewAgentTerminationSummary{
TerminationPenalty: util.ToAtto(big.NewFloat(100)),
Expand Down Expand Up @@ -52,7 +52,7 @@ func TestAgentLiquidationValue(t *testing.T) {
MinersAvailableBal: util.ToAtto(big.NewFloat(50)),
AgentAvailableBal: util.ToAtto(big.NewFloat(50)),
VestingBalance: util.ToAtto(big.NewFloat(100)),
}, util.ToAtto(big.NewFloat(650))},
}, util.ToAtto(big.NewFloat(700))},
}

for i, tc := range testCases {
Expand Down

0 comments on commit 2533bdd

Please sign in to comment.