-
Notifications
You must be signed in to change notification settings - Fork 1
/
keeper.go
executable file
·50 lines (37 loc) · 1.35 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
package keeper
import (
"fmt"
cTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/commitHub/commitBlockchain/codec"
"github.com/commitHub/commitBlockchain/types"
fiatFactoryTypes "github.com/commitHub/commitBlockchain/modules/fiatFactory/internal/types"
)
type Keeper struct {
storeKey cTypes.StoreKey
accountKeeper fiatFactoryTypes.AccountKeeper
cdc *codec.Codec
}
func NewKeeper(storeKey cTypes.StoreKey, accountKeeper fiatFactoryTypes.AccountKeeper, cdc *codec.Codec) Keeper {
return Keeper{
storeKey: storeKey,
accountKeeper: accountKeeper,
cdc: cdc,
}
}
func (k Keeper) SetFiatPeg(ctx cTypes.Context, fiatPeg types.FiatPeg) {
store := ctx.KVStore(k.storeKey)
fiatPegKey := fiatFactoryTypes.FiatPegHashStoreKey(fiatPeg.GetPegHash())
bytes := k.cdc.MustMarshalBinaryLengthPrefixed(fiatPeg)
store.Set(fiatPegKey, bytes)
}
func (k Keeper) GetFiatPeg(ctx cTypes.Context, pegHash types.PegHash) (types.FiatPeg, cTypes.Error) {
store := ctx.KVStore(k.storeKey)
assetKey := fiatFactoryTypes.FiatPegHashStoreKey(pegHash)
data := store.Get(assetKey)
if data == nil {
return nil, fiatFactoryTypes.ErrInvalidString(fiatFactoryTypes.DefaultCodeSpace, fmt.Sprintf("Fiat with pegHash %s not found", pegHash))
}
var fiatPeg types.FiatPeg
k.cdc.MustUnmarshalBinaryLengthPrefixed(data, &fiatPeg)
return fiatPeg, nil
}