-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeper.go
82 lines (69 loc) · 2.69 KB
/
keeper.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
package nameservice
import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/bank"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Keeper maintains the link to data storage and exposes getter/setter methods for the various parts of the state machine
type Keeper struct {
coinKeeper bank.Keeper
namesStoreKey sdk.StoreKey // Unexposed key to access name store from sdk.Context
ownersStoreKey sdk.StoreKey // Unexposed key to access owners store from sdk.Context
pricesStoreKey sdk.StoreKey // Unexposed key to access prices store from sdk.Context
cdc *codec.Codec // The wire codec for binary encoding/decoding.
}
// NewKeeper creates new instances of the nameservice Keeper
func NewKeeper(coinKeeper bank.Keeper, namesStoreKey sdk.StoreKey, ownersStoreKey sdk.StoreKey, priceStoreKey sdk.StoreKey, cdc *codec.Codec) Keeper {
return Keeper{
coinKeeper: coinKeeper,
namesStoreKey: namesStoreKey,
ownersStoreKey: ownersStoreKey,
pricesStoreKey: priceStoreKey,
cdc: cdc,
}
}
// ResolveName - returns the string that the name resolves to
func (k Keeper) ResolveName(ctx sdk.Context, name string) string {
store := ctx.KVStore(k.namesStoreKey)
bz := store.Get([]byte(name))
return string(bz)
}
// SetName - sets the value string that a name resolves to
func (k Keeper) SetName(ctx sdk.Context, name string, value string) {
store := ctx.KVStore(k.namesStoreKey)
store.Set([]byte(name), []byte(value))
}
// HasOwner - returns whether or not the name already has an owner
func (k Keeper) HasOwner(ctx sdk.Context, name string) bool {
store := ctx.KVStore(k.ownersStoreKey)
bz := store.Get([]byte(name))
return bz != nil
}
// GetOwner - get the current owner of a name
func (k Keeper) GetOwner(ctx sdk.Context, name string) sdk.AccAddress {
store := ctx.KVStore(k.ownersStoreKey)
bz := store.Get([]byte(name))
return bz
}
// SetOwner - sets the current owner of a name
func (k Keeper) SetOwner(ctx sdk.Context, name string, owner sdk.AccAddress) {
store := ctx.KVStore(k.ownersStoreKey)
store.Set([]byte(name), owner)
}
// GetPrice - gets the current price of a name. If price doesn't exist yet, set to 1mycoin.
func (k Keeper) GetPrice(ctx sdk.Context, name string) sdk.Coins {
if !k.HasOwner(ctx, name) {
return sdk.Coins{sdk.NewInt64Coin("mycoin", 1)}
}
store := ctx.KVStore(k.pricesStoreKey)
bz := store.Get([]byte(name))
var price sdk.Coins
// k.cdc.MustUnmarshalBinary(bz, &price)
k.cdc.MustUnmarshalBinaryBare(bz, &price)
return price
}
// SetPrice - sets the current price of a name
func (k Keeper) SetPrice(ctx sdk.Context, name string, price sdk.Coins) {
store := ctx.KVStore(k.pricesStoreKey)
store.Set([]byte(name), k.cdc.MustMarshalBinaryBare(price))
}