-
Notifications
You must be signed in to change notification settings - Fork 0
/
farm_info.go
53 lines (45 loc) · 1.55 KB
/
farm_info.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/furynet/furymod/modules/farm/types"
)
// GetFarmer return the specified farmer
func (k Keeper) GetFarmInfo(ctx sdk.Context, poolId, address string) (info types.FarmInfo, exist bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.KeyFarmInfo(address, poolId))
if len(bz) == 0 {
return info, false
}
k.cdc.MustUnmarshal(bz, &info)
return info, true
}
func (k Keeper) IteratorFarmInfo(ctx sdk.Context, address string, fun func(farmer types.FarmInfo)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.PrefixFarmInfo(address))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var farmer types.FarmInfo
k.cdc.MustUnmarshal(iterator.Value(), &farmer)
fun(farmer)
}
}
func (k Keeper) IteratorAllFarmInfo(ctx sdk.Context, fun func(farmer types.FarmInfo)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.FarmerKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var farmer types.FarmInfo
k.cdc.MustUnmarshal(iterator.Value(), &farmer)
fun(farmer)
}
}
// SetFarmer save the farmer information
func (k Keeper) SetFarmInfo(ctx sdk.Context, farmer types.FarmInfo) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&farmer)
store.Set(types.KeyFarmInfo(farmer.Address, farmer.PoolId), bz)
}
func (k Keeper) DeleteFarmInfo(ctx sdk.Context, poolId, address string) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.KeyFarmInfo(address, poolId))
}