/
vaults.go
160 lines (132 loc) · 3.33 KB
/
vaults.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package keeper
import (
"context"
"fmt"
"time"
"github.com/fox-one/pando/core"
"github.com/fox-one/pando/pkg/mtg/types"
"github.com/fox-one/pando/pkg/number"
"github.com/fox-one/pando/pkg/uuid"
"github.com/fox-one/pkg/logger"
"github.com/patrickmn/go-cache"
"github.com/shopspring/decimal"
"golang.org/x/sync/errgroup"
)
func (w *Keeper) scanVaults(ctx context.Context) error {
running := cache.New(cache.NoExpiration, cache.NoExpiration)
var g errgroup.Group
for {
select {
case <-ctx.Done():
return g.Wait()
case <-time.After(time.Second):
cats, err := w.cats.List(ctx)
if err != nil {
logger.FromContext(ctx).WithError(err).Errorln("cats.List")
break
}
oracles, err := w.oracles.List(ctx)
if err != nil {
logger.FromContext(ctx).WithError(err).Errorln("oracles.List")
return err
}
for idx := range cats {
cat := cats[idx]
if cat.Live == 0 {
continue
}
if _, ok := running.Get(cat.TraceID); ok {
continue
}
running.SetDefault(cat.TraceID, nil)
nextPrice, _ := nextPrice(
lookupOracle(oracles, cat.Gem),
lookupOracle(oracles, cat.Dai),
)
g.Go(func() error {
defer running.Delete(cat.TraceID)
return w.scanUnsafeVaults(ctx, cat, nextPrice)
})
}
}
}
}
func (w *Keeper) scanUnsafeVaults(ctx context.Context, cat *core.Collateral, nextPrice decimal.Decimal) error {
// v.Ink * c.Price >= v.Art * c.Rate * c.Mat
// v.Rate = v.Art / v.Ink <= c.Price / c.Rate / c.Mat
rate := cat.Price.Div(cat.Rate).Div(cat.Mat)
nextRate := rate
if nextPrice.IsPositive() {
nextRate = nextPrice.Div(cat.Rate).Div(cat.Mat)
}
scanRate := decimal.Min(rate.Mul(number.Decimal("0.8")))
var (
from int64
limit = 100
)
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
vats, err := w.vaults.List(ctx, core.ListVaultRequest{
CollateralID: cat.TraceID,
Rate: scanRate,
Desc: true,
FromID: from,
Limit: limit,
})
if err != nil {
logger.FromContext(ctx).WithError(err).Errorln("vaults.List")
break
}
for idx := range vats {
vat := vats[idx]
from = vat.ID
switch {
case vat.Rate.GreaterThan(rate):
trace := uuid.Modify(vat.TraceID, fmt.Sprintf("%s-%d", rate, vat.Version))
_ = w.handleTransfer(ctx, trace, core.ActionFlipKick, types.UUID(vat.TraceID))
case vat.Rate.GreaterThan(nextRate):
if err := w.notifier.VaultLiquidatedSoon(ctx, cat, vat); err != nil {
logger.FromContext(ctx).WithError(err).Errorln("notifier.VaultLiquidatedSoon")
return err
}
default:
if err := w.notifier.VaultUnsafe(ctx, cat, vat); err != nil {
logger.FromContext(ctx).WithError(err).Errorln("notifier.VaultUnsafe")
return err
}
}
}
if len(vats) < limit {
break
}
}
return nil
}
func nextPrice(gem, dai *core.Oracle) (next decimal.Decimal, at time.Time) {
if gem == nil || dai == nil {
return
}
if n1, n2 := gem.NextPeekAt(), dai.NextPeekAt(); n1.Before(n2) {
next = gem.Next.Div(dai.Current)
at = n1
} else if n1.After(n2) {
next = gem.Current.Div(dai.Next)
at = n2
} else {
next = gem.Next.Div(dai.Next)
at = n2
}
return
}
func lookupOracle(oracles []*core.Oracle, assetID string) *core.Oracle {
for _, oracle := range oracles {
if oracle.AssetID == assetID {
return oracle
}
}
return nil
}