Skip to content

Commit ca1bd6d

Browse files
authored
Fix dtl and percentages (#112)
1 parent 5b07b6f commit ca1bd6d

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

econ/fi.go

+17-19
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package econ
22

33
import (
4-
"math"
54
"math/big"
65

76
"github.com/glifio/go-pools/constants"
7+
"github.com/glifio/go-pools/util"
88
)
99

1010
func NewBaseFi(
@@ -200,16 +200,12 @@ func (bfi *BaseFi) LiquidationValue() *big.Int {
200200
return new(big.Int).Sub(bfi.Balance, bfi.TerminationFee)
201201
}
202202

203-
func (bfi *BaseFi) RecoveryRate() float64 {
203+
func (bfi *BaseFi) RecoveryRate() *big.Float {
204204
if bfi.Balance.Cmp(big.NewInt(0)) == 0 {
205-
return 0
205+
return big.NewFloat(0)
206206
}
207207

208-
rr, _ := new(big.Float).Quo(
209-
new(big.Float).SetInt(bfi.LiquidationValue()),
210-
new(big.Float).SetInt(bfi.Balance)).Float64()
211-
212-
return rr
208+
return computePerc(bfi.LiquidationValue(), bfi.Balance)
213209
}
214210

215211
// MaxBorrowAndSeal = margin / (1 - max borrow DTL) - margin
@@ -294,30 +290,32 @@ func (afi *AgentFi) LeverageRatio() *big.Float {
294290
return new(big.Float).SetInf(true)
295291
}
296292

297-
return new(big.Float).Quo(
298-
new(big.Float).SetInt(afi.LiquidationValue()),
299-
new(big.Float).SetInt(afi.Margin()))
293+
return computePerc(afi.LiquidationValue(), afi.Margin())
300294
}
301295

302-
func (afi *AgentFi) DTL() float64 {
296+
func (afi *AgentFi) DTL() *big.Float {
303297
// if debt is zero then DTL is 0
304298
if afi.Debt().Cmp(big.NewInt(0)) == 0 {
305-
return 0
299+
return big.NewFloat(0)
306300
}
307301
lv := afi.LiquidationValue()
308302
// if liquidation value is zero and debt is non zero then DTL is infinite
309303
if lv.Cmp(big.NewInt(0)) == 0 {
310304
// return positive infinite float
311-
return math.Inf(1)
305+
return new(big.Float).SetInf(true)
312306
}
313-
dtl, _ := new(big.Float).Quo(
314-
new(big.Float).SetInt(afi.Debt()),
315-
new(big.Float).SetInt(afi.LiquidationValue()),
316-
).Float64()
317307

318-
return dtl
308+
return computePerc(afi.Debt(), afi.LiquidationValue())
319309
}
320310

321311
func (l *Liability) Debt() *big.Int {
322312
return new(big.Int).Add(l.Principal, l.Interest)
323313
}
314+
315+
func computePerc(numerator, denominator *big.Int) *big.Float {
316+
if denominator.Cmp(big.NewInt(0)) == 0 {
317+
return new(big.Float).SetInf(true)
318+
}
319+
320+
return util.ToFIL(util.DivWad(numerator, denominator))
321+
}

econ/terminate_test.go

+22-2
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ type agentFiTest struct {
480480
withdrawLimit *big.Int
481481
marginCall *big.Int
482482
leverageRatio float64
483+
dtl float64
483484
}
484485

485486
var agentTests = []struct {
@@ -513,6 +514,7 @@ var agentTests = []struct {
513514
withdrawLimit: bigFromStr("8911457546764252091903"),
514515
marginCall: bigFromStr("1272158912732776255650271"),
515516
leverageRatio: 0.5743316423053819,
517+
dtl: 0.574289111476492565,
516518
}},
517519
// agent 27
518520
{"agent no miners", "0xDBa96B0FDbc87C7eEb641Ee37EAFC55B355079E4", BLOCK_HEIGHT, agentFiTest{
@@ -525,6 +527,7 @@ var agentTests = []struct {
525527
withdrawLimit: big.NewInt(0),
526528
marginCall: big.NewInt(0),
527529
leverageRatio: 0,
530+
dtl: 0,
528531
}},
529532
// agent 2
530533
{"agent single miner", "0xf0F1ceCCF78D411EeD9Ca190ca7F157140cCB2d3", BLOCK_HEIGHT, agentFiTest{
@@ -551,6 +554,7 @@ var agentTests = []struct {
551554
withdrawLimit: bigFromStr("57665444302030819344"),
552555
marginCall: bigFromStr("2359669285522428148"),
553556
leverageRatio: 0.016656085317377764,
557+
dtl: 0.016608593934123496,
554558
}},
555559
// agent 91
556560
{"agent miner with fee debt", "0xFF65F5f3D309fEA7aA2d4cB2727E918FAb0aE7F7", BLOCK_HEIGHT, agentFiTest{
@@ -577,6 +581,7 @@ var agentTests = []struct {
577581
withdrawLimit: bigFromStr("0"),
578582
marginCall: bigFromStr("0"),
579583
leverageRatio: 0,
584+
dtl: 0,
580585
}},
581586
{"agent with balance no miners", "0xf0F1ceCCF78D411EeD9Ca190ca7F157140cCB2d3", big.NewInt(4238800), agentFiTest{
582587
AgentFi: &AgentFi{
@@ -602,6 +607,7 @@ var agentTests = []struct {
602607
withdrawLimit: bigFromStr("3454284493328524959"),
603608
marginCall: bigFromStr("0"),
604609
leverageRatio: 0,
610+
dtl: 0,
605611
}},
606612
{"agent with balance no miners with principal", "0xf0F1ceCCF78D411EeD9Ca190ca7F157140cCB2d3", big.NewInt(4238810), agentFiTest{
607613
AgentFi: &AgentFi{
@@ -627,6 +633,7 @@ var agentTests = []struct {
627633
withdrawLimit: bigFromStr("3120950589218935919"),
628634
marginCall: bigFromStr("1176471091861402094"),
629635
leverageRatio: 0.22450304410954403,
636+
dtl: 0.224502948003829978,
630637
}},
631638
}
632639

@@ -662,6 +669,18 @@ func TestEstimateTermationFeeAgent(t *testing.T) {
662669
}
663670
}
664671

672+
func TestComputePerc(t *testing.T) {
673+
lv := bigFromStr("871702482492442116952350")
674+
p := bigFromStr("343733044178305628615912")
675+
i := bigFromStr("28202982363374106229")
676+
677+
perc := computePerc(new(big.Int).Add(p, i), lv)
678+
exp := big.NewFloat(0.394356164017979036)
679+
if perc.Cmp(exp) != 0 {
680+
t.Fatalf("Expected perc: %v, actual: %v", exp, perc)
681+
}
682+
}
683+
665684
func assertAgentFiEqual(t *testing.T, expected *agentFiTest, actual *AgentFi) {
666685
if expected.AvailableBalance.Cmp(actual.AvailableBalance) != 0 {
667686
t.Fatalf("Expected available balance: %v, actual: %v", expected.AvailableBalance, actual.AvailableBalance)
@@ -709,8 +728,9 @@ func assertAgentFiEqual(t *testing.T, expected *agentFiTest, actual *AgentFi) {
709728
if expected.marginCall.Cmp(actual.MarginCall()) != 0 {
710729
t.Fatalf("Expected margin call: %v, actual: %v", expected.marginCall, actual.MarginCall())
711730
}
712-
if expected.leverageRatio != actual.DTL() {
713-
t.Fatalf("Expected leverage ratio: %v, actual: %v", expected.leverageRatio, actual.DTL())
731+
dtl, _ := actual.DTL().Float64()
732+
if expected.leverageRatio != dtl {
733+
t.Fatalf("Expected leverage ratio: %v, actual: %v", expected.leverageRatio, dtl)
714734
}
715735
}
716736

0 commit comments

Comments
 (0)