-
Notifications
You must be signed in to change notification settings - Fork 352
/
denom.go
96 lines (77 loc) · 2.89 KB
/
denom.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
// Copyright (c) 2016-2021 Shanghai Bianjie AI Technology Inc. (licensed under the Apache License, Version 2.0)
// Modifications Copyright (c) 2021-present Crypto.org (licensed under the Apache License, Version 2.0)
package keeper
import (
sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/crypto-org-chain/chain-main/v4/x/nft/types"
)
// HasDenomID returns whether the specified denom ID exists
func (k Keeper) HasDenomID(ctx sdk.Context, id string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.KeyDenomID(id))
}
// HasDenomNm returns whether the specified denom name exists
func (k Keeper) HasDenomNm(ctx sdk.Context, name string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.KeyDenomName(name))
}
// SetDenom is responsible for saving the definition of denom
func (k Keeper) SetDenom(ctx sdk.Context, denom types.Denom) error {
if k.HasDenomID(ctx, denom.Id) {
return sdkerrors.Wrapf(types.ErrInvalidDenom, "denomID %s has already exists", denom.Id)
}
if k.HasDenomNm(ctx, denom.Name) {
return sdkerrors.Wrapf(types.ErrInvalidDenom, "denomName %s has already exists", denom.Name)
}
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&denom)
store.Set(types.KeyDenomID(denom.Id), bz)
store.Set(types.KeyDenomName(denom.Name), []byte(denom.Id))
return nil
}
// GetDenom returns the denom by id
func (k Keeper) GetDenom(ctx sdk.Context, id string) (denom types.Denom, err error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.KeyDenomID(id))
if len(bz) == 0 {
return denom, sdkerrors.Wrapf(types.ErrInvalidDenom, "not found denomID: %s", id)
}
k.cdc.MustUnmarshal(bz, &denom)
return denom, nil
}
// GetDenom returns the denom by name
func (k Keeper) GetDenomByName(ctx sdk.Context, name string) (denom types.Denom, err error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.KeyDenomName(name))
denomID := string(bz)
return k.GetDenom(ctx, denomID)
}
// GetDenoms returns all the denoms
func (k Keeper) GetDenoms(ctx sdk.Context) (denoms []types.Denom) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyDenomID(""))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var denom types.Denom
k.cdc.MustUnmarshal(iterator.Value(), &denom)
denoms = append(denoms, denom)
}
return denoms
}
// IsDenomCreator checks if address is the creator of Denom
// Return the Denom if true, an error otherwise
func (k Keeper) IsDenomCreator(ctx sdk.Context, denomID string, address sdk.AccAddress) (types.Denom, error) {
denom, err := k.GetDenom(ctx, denomID)
if err != nil {
return types.Denom{}, err
}
creator, err := sdk.AccAddressFromBech32(denom.Creator)
if err != nil {
panic(err)
}
if !creator.Equals(address) {
return types.Denom{}, sdkerrors.Wrapf(types.ErrUnauthorized, "%s is not the creator of %s", address, denomID)
}
return denom, nil
}