/
token.go
259 lines (225 loc) · 8.05 KB
/
token.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package types
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
appparams "github.com/umee-network/umee/v6/app/params"
"github.com/umee-network/umee/v6/util/checkers"
"github.com/umee-network/umee/v6/util/coin"
)
var (
halfDec = sdk.MustNewDecFromStr("0.5")
one = sdk.OneDec()
)
// ValidateBaseDenom validates a denom and ensures it is not a uToken.
func ValidateBaseDenom(denom string) error {
if err := sdk.ValidateDenom(denom); err != nil {
return err
}
if coin.HasUTokenPrefix(denom) {
return ErrUToken.Wrap(denom)
}
return nil
}
// Validate performs validation on an Token type returning an error if the Token
// is invalid.
func (t Token) Validate() error {
if err := validateBaseDenoms(t.BaseDenom); err != nil {
return fmt.Errorf("base_denom: %v", err)
}
if err := validateBaseDenoms(t.SymbolDenom); err != nil {
return fmt.Errorf("symbol_denom: %v", err)
}
if err := checkers.DecInZeroOne(t.ReserveFactor, "reserve factor", false); err != nil {
return err
}
if err := checkers.DecInZeroOne(t.CollateralWeight, "collateral weight", false); err != nil {
return err
}
if err := checkers.DecInZeroOne(t.ReserveFactor, "reserve factor", false); err != nil {
return err
}
if t.LiquidationThreshold.LT(t.CollateralWeight) || t.LiquidationThreshold.GTE(one) {
return fmt.Errorf(
"liquidation threshold must be bigger or equal than collateral weight, got: %s",
t.LiquidationThreshold,
)
}
// Kink utilization rate ranges between 0 and 1, inclusive.
if t.KinkUtilization.IsNegative() || t.KinkUtilization.GT(one) {
return fmt.Errorf("invalid kink utilization rate: %s", t.KinkUtilization)
}
// The following rules ensure the utilization:APY graph is continuous
if t.KinkUtilization.GT(t.MaxSupplyUtilization) {
return fmt.Errorf("kink utilization (%s) cannot be greater than than max supply utilization (%s)",
t.KinkUtilization, t.MaxSupplyUtilization)
}
if t.KinkUtilization.Equal(t.MaxSupplyUtilization) && !t.MaxBorrowRate.Equal(t.KinkBorrowRate) {
return fmt.Errorf(
"since kink utilization equals max supply utilization, kink borrow rate must equal max borrow rate (%s)",
t.MaxBorrowRate,
)
}
if t.KinkUtilization.IsZero() && !t.KinkBorrowRate.Equal(t.BaseBorrowRate) {
return fmt.Errorf(
"since kink utilization equals zero, kink borrow rate must equal base borrow rate (%s)",
t.BaseBorrowRate,
)
}
if t.MaxSupplyUtilization.IsZero() && !t.MaxBorrowRate.Equal(t.BaseBorrowRate) {
return fmt.Errorf(
"since max supply utilization equals zero, max borrow rate must equal base borrow rate (%s)",
t.BaseBorrowRate,
)
}
// interest rates are non-negative; they do not need to have a maximum value
if t.BaseBorrowRate.IsNegative() {
return fmt.Errorf("invalid base borrow rate: %s", t.BaseBorrowRate)
}
if t.KinkBorrowRate.IsNegative() {
return fmt.Errorf("invalid kink borrow rate: %s", t.KinkBorrowRate)
}
if t.MaxBorrowRate.IsNegative() {
return fmt.Errorf("invalid max borrow rate: %s", t.MaxBorrowRate)
}
// Liquidation incentive is non-negative
if t.LiquidationIncentive.IsNegative() {
return fmt.Errorf("invalid liquidation incentive: %s", t.LiquidationIncentive)
}
// Blacklisted assets cannot have borrow or supply enabled
if t.Blacklist {
if t.EnableMsgBorrow {
return fmt.Errorf("blacklisted assets cannot have borrowing enabled")
}
if t.EnableMsgSupply {
return fmt.Errorf("blacklisted assets cannot have supplying enabled")
}
}
if t.MaxCollateralShare.IsNegative() || t.MaxCollateralShare.GT(one) {
return sdkerrors.ErrInvalidRequest.Wrap("Token.MaxCollateralShare must be between 0 and 1")
}
if t.MaxSupplyUtilization.IsNegative() || t.MaxSupplyUtilization.GT(one) {
return sdkerrors.ErrInvalidRequest.Wrap("Token.MaxSupplyUtilization must be between 0 and 1")
}
if t.MinCollateralLiquidity.IsNegative() || t.MinCollateralLiquidity.GT(one) {
return sdkerrors.ErrInvalidRequest.Wrap("Token.MinCollateralLiquidity be between 0 and 1")
}
if t.MaxSupply.IsNegative() {
return sdkerrors.ErrInvalidRequest.Wrap("Token.MaxSupply must not be negative")
}
return nil
}
// AssertSupplyEnabled returns an error if a Token cannot be supplied.
func (t Token) AssertSupplyEnabled() error {
if !t.EnableMsgSupply {
return ErrSupplyNotAllowed.Wrap(t.BaseDenom)
}
return nil
}
// AssertBorrowEnabled returns an error if a Token cannot be borrowed.
func (t Token) AssertBorrowEnabled() error {
if !t.EnableMsgBorrow {
return ErrBorrowNotAllowed.Wrap(t.BaseDenom)
}
return nil
}
// AssertNotBlacklisted returns an error if a Token is blacklisted.
func (t Token) AssertNotBlacklisted() error {
if t.Blacklist {
return ErrBlacklisted.Wrap(t.BaseDenom)
}
return nil
}
// BorrowFactor returns the minimum of 2.0 or 1 / collateralWeight.
func (t Token) BorrowFactor() sdk.Dec {
if t.CollateralWeight.LTE(halfDec) {
return sdk.MustNewDecFromStr("2.0")
}
return sdk.OneDec().Quo(t.CollateralWeight)
}
func defaultUmeeToken() Token {
tm := appparams.UmeeTokenMetadata()
return Token{
BaseDenom: tm.Base,
SymbolDenom: tm.Symbol, // SymbolDenom should not change
Exponent: 6,
EnableMsgSupply: true,
EnableMsgBorrow: true,
Blacklist: false,
// Reserves
ReserveFactor: sdk.MustNewDecFromStr("0.10"),
// Interest rate model
BaseBorrowRate: sdk.MustNewDecFromStr("0.05"),
KinkBorrowRate: sdk.MustNewDecFromStr("0.10"),
MaxBorrowRate: sdk.MustNewDecFromStr("0.80"),
KinkUtilization: sdk.MustNewDecFromStr("0.50"),
// Collateral
CollateralWeight: sdk.MustNewDecFromStr("0.35"),
LiquidationThreshold: sdk.MustNewDecFromStr("0.50"),
// Liquidation
LiquidationIncentive: sdk.MustNewDecFromStr("0.10"),
// Market limits
MaxCollateralShare: sdk.MustNewDecFromStr("1.00"),
MaxSupplyUtilization: sdk.MustNewDecFromStr("0.90"),
MinCollateralLiquidity: sdk.MustNewDecFromStr("0.3"),
MaxSupply: sdk.NewInt(1000_000000_000000),
}
}
func DefaultRegistry() []Token {
return []Token{
defaultUmeeToken(),
}
}
// Validate performs validation on an SpecialAssetPair type
func (p SpecialAssetPair) Validate() error {
if err := validateBaseDenoms(p.Collateral, p.Borrow); err != nil {
return err
}
if p.CollateralWeight.IsNil() || p.LiquidationThreshold.IsNil() {
return fmt.Errorf("nil collateral weight or liquidation threshold for asset pair (%s,%s)",
p.Borrow, p.Collateral)
}
// Collateral Weight is non-negative and less than 1.
if p.CollateralWeight.IsNegative() || p.CollateralWeight.GTE(sdk.OneDec()) {
return fmt.Errorf("invalid collateral rate: %s", p.CollateralWeight)
}
// Liquidation Threshold ranges between collateral weight and 1.
if p.LiquidationThreshold.LT(p.CollateralWeight) || p.LiquidationThreshold.GTE(sdk.OneDec()) {
return fmt.Errorf("invalid liquidation threshold: %s", p.LiquidationThreshold)
}
return nil
}
// Validate performs validation on an SpecialAssetSet type
func (s SpecialAssetSet) Validate() error {
if err := validateBaseDenoms(s.Assets...); err != nil {
return err
}
denoms := map[string]bool{}
for _, a := range s.Assets {
if _, ok := denoms[a]; ok {
return fmt.Errorf("duplicate special asset pair: %s", a)
}
denoms[a] = true
}
if s.CollateralWeight.IsNil() || s.LiquidationThreshold.IsNil() {
return fmt.Errorf("nil collateral weight or liquidation threshold for asset set %s)", s.Assets)
}
// Collateral Weight is non-negative and less than 1.
if s.CollateralWeight.IsNegative() || s.CollateralWeight.GTE(sdk.OneDec()) {
return fmt.Errorf("invalid collateral rate: %s", s.CollateralWeight)
}
// Liquidation Threshold ranges between collateral weight and 1.
if s.LiquidationThreshold.LT(s.CollateralWeight) || s.LiquidationThreshold.GTE(sdk.OneDec()) {
return fmt.Errorf("invalid liquidation threshold: %s", s.LiquidationThreshold)
}
return nil
}
// validateBaseDenoms ensures that one or more strings are valid token denoms without the uToken prefix
func validateBaseDenoms(denoms ...string) error {
for _, s := range denoms {
if err := ValidateBaseDenom(s); err != nil {
return err
}
}
return nil
}