/
negative_tnc_subaccount.go
63 lines (54 loc) · 1.85 KB
/
negative_tnc_subaccount.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
package keeper
import (
"fmt"
gogotypes "github.com/cosmos/gogoproto/types"
"github.com/jinxprotocol/v4-chain/protocol/x/subaccounts/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// GetNegativeTncSubaccountSeenAtBlock gets the last block height a negative TNC subaccount was
// seen in state. Note this defaults to 0 if it has not been set.
func (k Keeper) GetNegativeTncSubaccountSeenAtBlock(
ctx sdk.Context,
) uint32 {
store := ctx.KVStore(k.storeKey)
return k.getNegativeTncSubaccountSeenAtBlock(store)
}
// getNegativeTncSubaccountSeenAtBlock is a helper function that takes a store and returns the last
// block height a negative TNC subaccount was seen in state.
func (k Keeper) getNegativeTncSubaccountSeenAtBlock(
store sdk.KVStore,
) uint32 {
b := store.Get(
[]byte(types.NegativeTncSubaccountSeenAtBlockKey),
)
blockHeight := gogotypes.UInt32Value{Value: 0}
if b != nil {
k.cdc.MustUnmarshal(b, &blockHeight)
}
return blockHeight.Value
}
// SetNegativeTncSubaccountSeenAtBlock sets a block number in state where a negative TNC subaccount
// was seen. This function will overwrite previous values at this key.
// This function will panic if the old block height is greater than the new block height.
func (k Keeper) SetNegativeTncSubaccountSeenAtBlock(
ctx sdk.Context,
blockHeight uint32,
) {
store := ctx.KVStore(k.storeKey)
// Panic if the new block height is less than the current block height.
currentValue := k.getNegativeTncSubaccountSeenAtBlock(store)
if blockHeight < currentValue {
panic(
fmt.Sprintf(
"SetNegativeTncSubaccountSeenAtBlock: new block height (%d) is less than the current block height (%d)",
blockHeight,
currentValue,
),
)
}
blockHeightValue := gogotypes.UInt32Value{Value: blockHeight}
store.Set(
[]byte(types.NegativeTncSubaccountSeenAtBlockKey),
k.cdc.MustMarshal(&blockHeightValue),
)
}