-
Notifications
You must be signed in to change notification settings - Fork 352
/
borrow.go
181 lines (155 loc) · 4.55 KB
/
borrow.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package types
import (
"fmt"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Borrow defines an amount of coins borrowed from a hard module account
type Borrow struct {
Borrower sdk.AccAddress `json:"borrower" yaml:"borrower"`
Amount sdk.Coins `json:"amount" yaml:"amount"`
Index BorrowInterestFactors `json:"index" yaml:"index"`
}
// NewBorrow returns a new Borrow instance
func NewBorrow(borrower sdk.AccAddress, amount sdk.Coins, index BorrowInterestFactors) Borrow {
return Borrow{
Borrower: borrower,
Amount: amount,
Index: index,
}
}
// NormalizedBorrow is the borrow amounts divided by the interest factors.
//
// Multiplying the normalized borrow by the current global factors gives the current borrow (ie including all interest, ie a synced borrow).
// The normalized borrow is effectively how big the borrow would have been if it had been borrowed at time 0 and not touched since.
//
// An error is returned if the borrow is in an invalid state.
func (b Borrow) NormalizedBorrow() (sdk.DecCoins, error) {
normalized := sdk.NewDecCoins()
for _, coin := range b.Amount {
factor, found := b.Index.GetInterestFactor(coin.Denom)
if !found {
return nil, fmt.Errorf("borrowed amount '%s' missing interest factor", coin.Denom)
}
if factor.LT(sdk.OneDec()) {
return nil, fmt.Errorf("interest factor '%s' < 1", coin.Denom)
}
normalized = normalized.Add(
sdk.NewDecCoinFromDec(
coin.Denom,
coin.Amount.ToDec().Quo(factor),
),
)
}
return normalized, nil
}
// Validate deposit validation
func (b Borrow) Validate() error {
if b.Borrower.Empty() {
return fmt.Errorf("Borrower cannot be empty")
}
if !b.Amount.IsValid() {
return fmt.Errorf("Invalid borrow coins: %s", b.Amount)
}
if err := b.Index.Validate(); err != nil {
return err
}
return nil
}
func (b Borrow) String() string {
return fmt.Sprintf(`Borrow:
Borrower: %s
Amount: %s
Index: %s
`, b.Borrower, b.Amount, b.Index)
}
// Borrows is a slice of Borrow
type Borrows []Borrow
// Validate validates Borrows
func (bs Borrows) Validate() error {
borrowDupMap := make(map[string]Borrow)
for _, b := range bs {
if err := b.Validate(); err != nil {
return err
}
dup, ok := borrowDupMap[b.Borrower.String()]
if ok {
return fmt.Errorf("duplicate borrower: %s\n%s", b, dup)
}
borrowDupMap[b.Borrower.String()] = b
}
return nil
}
// BorrowInterestFactor defines an individual borrow interest factor
type BorrowInterestFactor struct {
Denom string `json:"denom" yaml:"denom"`
Value sdk.Dec `json:"value" yaml:"value"`
}
// NewBorrowInterestFactor returns a new BorrowInterestFactor instance
func NewBorrowInterestFactor(denom string, value sdk.Dec) BorrowInterestFactor {
return BorrowInterestFactor{
Denom: denom,
Value: value,
}
}
// Validate validates BorrowInterestFactor values
func (bif BorrowInterestFactor) Validate() error {
if strings.TrimSpace(bif.Denom) == "" {
return fmt.Errorf("borrow interest factor denom cannot be empty")
}
if bif.Value.IsNegative() {
return fmt.Errorf("borrow interest factor value cannot be negative: %s", bif)
}
return nil
}
func (bif BorrowInterestFactor) String() string {
return fmt.Sprintf(`[%s,%s]
`, bif.Denom, bif.Value)
}
// BorrowInterestFactors is a slice of BorrowInterestFactor, because Amino won't marshal maps
type BorrowInterestFactors []BorrowInterestFactor
// GetInterestFactor returns a denom's interest factor value
func (bifs BorrowInterestFactors) GetInterestFactor(denom string) (sdk.Dec, bool) {
for _, bif := range bifs {
if bif.Denom == denom {
return bif.Value, true
}
}
return sdk.ZeroDec(), false
}
// SetInterestFactor sets a denom's interest factor value
func (bifs BorrowInterestFactors) SetInterestFactor(denom string, factor sdk.Dec) BorrowInterestFactors {
for i, bif := range bifs {
if bif.Denom == denom {
bif.Value = factor
bifs[i] = bif
return bifs
}
}
return append(bifs, NewBorrowInterestFactor(denom, factor))
}
// RemoveInterestFactor removes a denom's interest factor value
func (bifs BorrowInterestFactors) RemoveInterestFactor(denom string) (BorrowInterestFactors, bool) {
for i, bif := range bifs {
if bif.Denom == denom {
return append(bifs[:i], bifs[i+1:]...), true
}
}
return bifs, false
}
// Validate validates BorrowInterestFactors
func (bifs BorrowInterestFactors) Validate() error {
for _, bif := range bifs {
if err := bif.Validate(); err != nil {
return err
}
}
return nil
}
func (bifs BorrowInterestFactors) String() string {
out := ""
for _, bif := range bifs {
out += bif.String()
}
return out
}