-
Notifications
You must be signed in to change notification settings - Fork 364
/
deposit.go
151 lines (130 loc) · 3.65 KB
/
deposit.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
package types
import (
"fmt"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Deposit defines an amount of coins deposited into a hard module account
type Deposit struct {
Depositor sdk.AccAddress `json:"depositor" yaml:"depositor"`
Amount sdk.Coins `json:"amount" yaml:"amount"`
Index SupplyInterestFactors `json:"index" yaml:"index"`
}
// NewDeposit returns a new deposit
func NewDeposit(depositor sdk.AccAddress, amount sdk.Coins, indexes SupplyInterestFactors) Deposit {
return Deposit{
Depositor: depositor,
Amount: amount,
Index: indexes,
}
}
// Validate deposit validation
func (d Deposit) Validate() error {
if d.Depositor.Empty() {
return fmt.Errorf("Depositor cannot be empty")
}
if !d.Amount.IsValid() {
return fmt.Errorf("Invalid deposit coins: %s", d.Amount)
}
if err := d.Index.Validate(); err != nil {
return err
}
return nil
}
func (d Deposit) String() string {
return fmt.Sprintf(`Deposit:
Depositor: %s
Amount: %s
Index: %s
`, d.Depositor, d.Amount, d.Index)
}
// Deposits is a slice of Deposit
type Deposits []Deposit
// Validate validates Deposits
func (ds Deposits) Validate() error {
depositDupMap := make(map[string]Deposit)
for _, d := range ds {
if err := d.Validate(); err != nil {
return err
}
dup, ok := depositDupMap[d.Depositor.String()]
if ok {
return fmt.Errorf("duplicate depositor: %s\n%s", d, dup)
}
depositDupMap[d.Depositor.String()] = d
}
return nil
}
// SupplyInterestFactor defines an individual borrow interest factor
type SupplyInterestFactor struct {
Denom string `json:"denom" yaml:"denom"`
Value sdk.Dec `json:"value" yaml:"value"`
}
// NewSupplyInterestFactor returns a new SupplyInterestFactor instance
func NewSupplyInterestFactor(denom string, value sdk.Dec) SupplyInterestFactor {
return SupplyInterestFactor{
Denom: denom,
Value: value,
}
}
// Validate validates SupplyInterestFactor values
func (sif SupplyInterestFactor) Validate() error {
if strings.TrimSpace(sif.Denom) == "" {
return fmt.Errorf("supply interest factor denom cannot be empty")
}
if sif.Value.IsNegative() {
return fmt.Errorf("supply interest factor value cannot be negative: %s", sif)
}
return nil
}
func (sif SupplyInterestFactor) String() string {
return fmt.Sprintf(`[%s,%s]
`, sif.Denom, sif.Value)
}
// SupplyInterestFactors is a slice of SupplyInterestFactor, because Amino won't marshal maps
type SupplyInterestFactors []SupplyInterestFactor
// GetInterestFactor returns a denom's interest factor value
func (sifs SupplyInterestFactors) GetInterestFactor(denom string) (sdk.Dec, bool) {
for _, sif := range sifs {
if sif.Denom == denom {
return sif.Value, true
}
}
return sdk.ZeroDec(), false
}
// SetInterestFactor sets a denom's interest factor value
func (sifs SupplyInterestFactors) SetInterestFactor(denom string, factor sdk.Dec) SupplyInterestFactors {
for i, sif := range sifs {
if sif.Denom == denom {
sif.Value = factor
sifs[i] = sif
return sifs
}
}
return append(sifs, NewSupplyInterestFactor(denom, factor))
}
// RemoveInterestFactor removes a denom's interest factor value
func (sifs SupplyInterestFactors) RemoveInterestFactor(denom string) (SupplyInterestFactors, bool) {
for i, sif := range sifs {
if sif.Denom == denom {
return append(sifs[:i], sifs[i+1:]...), true
}
}
return sifs, false
}
// Validate validates SupplyInterestFactors
func (sifs SupplyInterestFactors) Validate() error {
for _, sif := range sifs {
if err := sif.Validate(); err != nil {
return err
}
}
return nil
}
func (sifs SupplyInterestFactors) String() string {
out := ""
for _, sif := range sifs {
out += sif.String()
}
return out
}