-
Notifications
You must be signed in to change notification settings - Fork 168
/
supply.go
76 lines (62 loc) · 2.4 KB
/
supply.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/umee-network/umee/v6/util/coin"
"github.com/umee-network/umee/v6/x/leverage/types"
)
// GetSupplied returns an sdk.Coin representing how much of a given denom a
// user has supplied, including interest accrued.
func (k Keeper) GetSupplied(ctx sdk.Context, supplierAddr sdk.AccAddress, denom string) (sdk.Coin, error) {
if coin.HasUTokenPrefix(denom) {
return sdk.Coin{}, types.ErrUToken.Wrap(denom)
}
// sum wallet-held and collateral-enabled uTokens in the associated uToken denom
uDenom := coin.ToUTokenDenom(denom)
balance := k.bankKeeper.GetBalance(ctx, supplierAddr, uDenom)
collateral := k.GetCollateral(ctx, supplierAddr, uDenom)
// convert uTokens to tokens
return k.ToToken(ctx, balance.Add(collateral))
}
// GetAllSupplied returns the total tokens supplied by a user, including
// any interest accrued.
func (k Keeper) GetAllSupplied(ctx sdk.Context, supplierAddr sdk.AccAddress) (sdk.Coins, error) {
// get all uTokens set as collateral
collateral := k.GetBorrowerCollateral(ctx, supplierAddr)
// get all uTokens not set as collateral by filtering non-uTokens from supplier balance
uTokens := sdk.Coins{}
balance := k.bankKeeper.GetAllBalances(ctx, supplierAddr)
for _, c := range balance {
if coin.HasUTokenPrefix(c.Denom) {
uTokens = uTokens.Add(c)
}
}
// convert the sum of found uTokens to base tokens
return k.ToTokens(ctx, collateral.Add(uTokens...))
}
// GetTotalSupply returns the total supplied by all suppliers in a given denom,
// including any interest accrued.
func (k Keeper) GetTotalSupply(ctx sdk.Context, denom string) (sdk.Coin, error) {
if coin.HasUTokenPrefix(denom) {
return sdk.Coin{}, types.ErrUToken.Wrap(denom)
}
// convert associated uToken's total supply to base tokens
uTokenDenom := coin.ToUTokenDenom(denom)
return k.ToToken(ctx, k.GetUTokenSupply(ctx, uTokenDenom))
}
// checkMaxSupply returns the appropriate error if a token denom's
// supply has exceeded MaxSupply
func (k Keeper) checkMaxSupply(ctx sdk.Context, denom string) error {
token, err := k.GetTokenSettings(ctx, denom)
if err != nil {
return err
}
total, err := k.GetTotalSupply(ctx, denom)
if err != nil {
return err
}
if token.MaxSupply.IsPositive() && total.Amount.GTE(token.MaxSupply) {
return types.ErrMaxSupply.Wrapf("current supply: %s, max supply: %s",
total.Amount, token.MaxSupply)
}
return nil
}