-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
cdp.go
124 lines (112 loc) · 3.3 KB
/
cdp.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
package types
import (
"errors"
fmt "fmt"
"strings"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// NewCdp creates a new Cdp object
func NewCdp(id uint64, owner sdk.AccAddress, collateral sdk.Coin, collateralType string, principal sdk.Coin, time time.Time, interestFactor sdk.Dec) Cdp {
fees := sdk.NewCoin(principal.Denom, sdk.ZeroInt())
return Cdp{
Id: id,
Owner: owner.Bytes(),
Type: collateralType,
Collateral: collateral,
Principal: principal,
AccumulatedFees: fees,
FeesUpdated: time,
InterestFactor: interestFactor,
}
}
// NewCdpWithFees creates a new Cdp object, for use during migration
func NewCdpWithFees(id uint64, owner sdk.AccAddress, collateral sdk.Coin, collateralType string, principal, fees sdk.Coin, time time.Time, interestFactor sdk.Dec) Cdp {
return Cdp{
Id: id,
Owner: owner.Bytes(),
Type: collateralType,
Collateral: collateral,
Principal: principal,
AccumulatedFees: fees,
FeesUpdated: time,
InterestFactor: interestFactor,
}
}
// Validate performs a basic validation of the Cdp fields.
func (cdp Cdp) Validate() error {
if cdp.Id == 0 {
return errors.New("cdp id cannot be 0")
}
if cdp.Owner.AccAddress().Empty() {
return errors.New("cdp owner cannot be empty")
}
if !cdp.Collateral.IsValid() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "collateral %s", cdp.Collateral)
}
if !cdp.Principal.IsValid() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "principal %s", cdp.Principal)
}
if !cdp.AccumulatedFees.IsValid() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "accumulated fees %s", cdp.AccumulatedFees)
}
if cdp.FeesUpdated.Unix() <= 0 {
return errors.New("cdp updated fee time cannot be zero")
}
if strings.TrimSpace(cdp.Type) == "" {
return fmt.Errorf("cdp type cannot be empty")
}
return nil
}
// GetTotalPrincipal returns the total principle for the cdp
func (cdp Cdp) GetTotalPrincipal() sdk.Coin {
return cdp.Principal.Add(cdp.AccumulatedFees)
}
// Cdps a collection of Cdp objects
type Cdps []Cdp
// String implements stringer
func (cdps Cdps) String() string {
out := ""
for _, cdp := range cdps {
out += cdp.String() + "\n"
}
return out
}
// Validate validates each Cdp
func (cdps Cdps) Validate() error {
for _, cdp := range cdps {
if err := cdp.Validate(); err != nil {
return err
}
}
return nil
}
// NewAugmentedCdp creates a new AugmentedCdp object
func NewAugmentedCdp(cdp Cdp, collateralValue sdk.Coin, collateralizationRatio sdk.Dec) AugmentedCdp {
augmentedCdp := AugmentedCdp{
Cdp: Cdp{
Id: cdp.Id,
Owner: cdp.Owner,
Type: cdp.Type,
Collateral: cdp.Collateral,
Principal: cdp.Principal,
AccumulatedFees: cdp.AccumulatedFees,
FeesUpdated: cdp.FeesUpdated,
InterestFactor: cdp.InterestFactor,
},
CollateralValue: collateralValue,
CollateralizationRatio: collateralizationRatio,
}
return augmentedCdp
}
// AugmentedCdps a collection of AugmentedCdp objects
type AugmentedCdps []AugmentedCdp
// String implements stringer
func (augcdps AugmentedCdps) String() string {
out := ""
for _, augcdp := range augcdps {
out += augcdp.String() + "\n"
}
return out
}