/
keeper.go
85 lines (67 loc) · 2.09 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
83
84
85
package keeper
import (
"fmt"
"github.com/gogo/protobuf/proto"
"github.com/Carina-hackatom/nova/x/oracle/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/tendermint/tendermint/libs/log"
)
type Keeper struct {
cdc codec.BinaryCodec
storeKey sdk.StoreKey
paramSpace paramtypes.Subspace
}
func NewKeeper(cdc codec.BinaryCodec, key sdk.StoreKey, paramSpace paramtypes.Subspace) Keeper {
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}
return Keeper{
cdc: cdc, storeKey: key, paramSpace: paramSpace,
}
}
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}
func (k Keeper) UpdateChainState(ctx sdk.Context, chainInfo *types.ChainInfo) error {
if !k.IsValidOperator(ctx, chainInfo.OperatorAddress) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, chainInfo.OperatorAddress)
}
store := ctx.KVStore(k.storeKey)
bz, err := chainInfo.Marshal()
if err != nil {
return err
}
store.Set([]byte(chainInfo.Coin.Denom), bz)
return nil
}
func (k Keeper) GetChainState(ctx sdk.Context, chainDenom string) (*types.ChainInfo, error) {
chainInfo := types.ChainInfo{}
store := ctx.KVStore(k.storeKey)
if !store.Has([]byte(chainDenom)) {
return nil, sdkerrors.Wrap(types.ErrNoSupportChain, fmt.Sprintf("chain %s", chainDenom))
}
data := store.Get([]byte(chainDenom))
if err := proto.Unmarshal(data, &chainInfo); err != nil {
return nil, err
}
return &chainInfo, nil
}
func (k Keeper) IsValidOperator(ctx sdk.Context, operatorAddress string) bool {
params := k.GetParams(ctx)
for i := range params.OracleOperators {
if params.OracleOperators[i] == operatorAddress {
return true
}
}
return false
}
func (k Keeper) GetOracleVersion(ctx sdk.Context, chainDenom string) (int64, error) {
chainInfo, err := k.GetChainState(ctx, chainDenom)
if err != nil {
return 0, err
}
return chainInfo.LastBlockHeight, nil
}