-
Notifications
You must be signed in to change notification settings - Fork 368
/
vault.go
171 lines (135 loc) · 3.97 KB
/
vault.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
package types
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// NewVaultRecord returns a new VaultRecord with 0 supply.
func NewVaultRecord(vaultDenom string, amount sdk.Dec) VaultRecord {
return VaultRecord{
TotalShares: NewVaultShare(vaultDenom, amount),
}
}
// Validate returns an error if a VaultRecord is invalid.
func (vr *VaultRecord) Validate() error {
return vr.TotalShares.Validate()
}
// VaultRecords is a slice of VaultRecord.
type VaultRecords []VaultRecord
// Validate returns an error if a slice of VaultRecords is invalid.
func (vrs VaultRecords) Validate() error {
denoms := make(map[string]bool)
for _, vr := range vrs {
if err := vr.Validate(); err != nil {
return err
}
if denoms[vr.TotalShares.Denom] {
return fmt.Errorf("duplicate vault denom %s", vr.TotalShares.Denom)
}
denoms[vr.TotalShares.Denom] = true
}
return nil
}
// NewVaultShareRecord returns a new VaultShareRecord with the provided supplied
// coins.
func NewVaultShareRecord(depositor sdk.AccAddress, shares VaultShares) VaultShareRecord {
return VaultShareRecord{
Depositor: depositor,
Shares: shares,
}
}
// Validate returns an error if an VaultShareRecord is invalid.
func (vsr VaultShareRecord) Validate() error {
if vsr.Depositor.Empty() {
return fmt.Errorf("depositor is empty")
}
if err := vsr.Shares.Validate(); err != nil {
return fmt.Errorf("invalid vault share record shares: %w", err)
}
return nil
}
// VaultShareRecords is a slice of VaultShareRecord.
type VaultShareRecords []VaultShareRecord
// Validate returns an error if a slice of VaultRecords is invalid.
func (vsrs VaultShareRecords) Validate() error {
addrs := make(map[string]bool)
for _, vr := range vsrs {
if err := vr.Validate(); err != nil {
return err
}
if _, found := addrs[vr.Depositor.String()]; found {
return fmt.Errorf("duplicate address %s", vr.Depositor.String())
}
addrs[vr.Depositor.String()] = true
}
return nil
}
// NewAllowedVault returns a new AllowedVault with the given values.
func NewAllowedVault(
denom string,
strategyTypes StrategyTypes,
isPrivateVault bool,
allowedDepositors []sdk.AccAddress,
) AllowedVault {
return AllowedVault{
Denom: denom,
Strategies: strategyTypes,
IsPrivateVault: isPrivateVault,
AllowedDepositors: allowedDepositors,
}
}
// Validate returns an error if the AllowedVault is invalid
func (a *AllowedVault) Validate() error {
if err := sdk.ValidateDenom(a.Denom); err != nil {
return sdkerrors.Wrap(ErrInvalidVaultDenom, err.Error())
}
// Private -> 1+ allowed depositors
// Non-private -> 0 allowed depositors
if a.IsPrivateVault && len(a.AllowedDepositors) == 0 {
return fmt.Errorf("private vaults require non-empty AllowedDepositors")
}
if !a.IsPrivateVault && len(a.AllowedDepositors) > 0 {
return fmt.Errorf("non-private vaults cannot have any AllowedDepositors")
}
return a.Strategies.Validate()
}
// IsStrategyAllowed returns true if the given strategy type is allowed for the
// vault.
func (a *AllowedVault) IsStrategyAllowed(strategy StrategyType) bool {
for _, s := range a.Strategies {
if s == strategy {
return true
}
}
return false
}
// IsAccountAllowed returns true if the given account is allowed to deposit into
// the vault.
func (a *AllowedVault) IsAccountAllowed(account sdk.AccAddress) bool {
// Anyone can deposit to non-private vaults
if !a.IsPrivateVault {
return true
}
for _, addr := range a.AllowedDepositors {
if addr.Equals(account) {
return true
}
}
return false
}
// AllowedVaults is a slice of AllowedVault.
type AllowedVaults []AllowedVault
// Validate returns an error if the AllowedVaults is invalid.
func (a AllowedVaults) Validate() error {
denoms := make(map[string]bool)
for _, v := range a {
if err := v.Validate(); err != nil {
return err
}
if denoms[v.Denom] {
return fmt.Errorf("duplicate vault denom %s", v.Denom)
}
denoms[v.Denom] = true
}
return nil
}