-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcreate.go
84 lines (71 loc) · 1.92 KB
/
create.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
package cat
import (
"github.com/fox-one/pando/core"
"github.com/fox-one/pando/pkg/maker"
"github.com/fox-one/pando/pkg/number"
"github.com/fox-one/pando/pkg/uuid"
"github.com/fox-one/pkg/logger"
"github.com/shopspring/decimal"
)
func HandleCreate(
collaterals core.CollateralStore,
oracles core.OracleStore,
) maker.HandlerFunc {
return func(r *maker.Request) error {
ctx := r.Context()
log := logger.FromContext(ctx)
if err := require(r.Gov(), "not-authorized"); err != nil {
return err
}
var (
gem, dai uuid.UUID
name string
)
if err := require(r.Scan(&gem, &dai, &name) == nil, "bad-data"); err != nil {
return err
}
if err := require(gem != uuid.Zero && dai != uuid.Zero, "invalid-asset"); err != nil {
return err
}
if err := require(gem.String() != dai.String(), "same-asset"); err != nil {
return err
}
cat := &core.Collateral{
CreatedAt: r.Now,
TraceID: r.TraceID,
Version: r.Version,
Name: name,
Gem: gem.String(),
Dai: dai.String(),
Art: decimal.Zero,
Rate: number.Decimal("1"),
Rho: r.Now,
Dust: number.Decimal("100"),
Mat: number.Decimal("1.5"),
Duty: number.Decimal("1.05"),
Chop: number.Decimal("1.13"),
Dunk: number.Decimal("5000"),
Box: number.Decimal("500000"),
Beg: number.Decimal("0.03"),
TTL: 15 * 60, // 15m
Tau: 3 * 60 * 60, // 3h
}
switch {
case r.SysVersion >= 4:
cat.Beg = number.Decimal("1.03")
}
prices, err := oracles.ListCurrent(ctx)
if err != nil {
log.WithError(err).Errorln("oracles.ListCurrent")
return err
}
if gp, dp := prices.Get(cat.Gem), prices.Get(cat.Dai); gp.IsPositive() && dp.IsPositive() {
cat.Price = gp.Div(dp).Truncate(12)
}
if err := collaterals.Create(ctx, cat); err != nil {
log.WithError(err).Errorln("collaterals.Create")
return err
}
return nil
}
}