Skip to content

Commit

Permalink
read and write into the SequenceFee by Sequence value.
Browse files Browse the repository at this point in the history
  • Loading branch information
RustNinja committed Apr 25, 2024
1 parent 1dceede commit e659cf7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
16 changes: 16 additions & 0 deletions x/ibctransfermiddleware/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ func (k Keeper) GetParams(ctx sdk.Context) (p types.Params) {
return p
}

func (k Keeper) GetSequenceFee(ctx sdk.Context, sequence uint64) (coin sdk.Coin, found bool) {
store := ctx.KVStore(k.storeKey)

value := store.Get(types.GetSequenceKey(sequence))
if value == nil {
return sdk.Coin{}, false
}

fee := types.MustUnmarshalCoin(k.cdc, value)
return fee, true
}
func (k Keeper) SetSequenceFee(ctx sdk.Context, sequence uint64, coin sdk.Coin) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetSequenceKey(sequence), types.MustMarshalCoin(k.cdc, &coin))
}

func (k Keeper) GetCoin(ctx sdk.Context, targetChannelID, denom string) *types.CoinItem {
params := k.GetParams(ctx)
channelFee := findChannelParams(params.ChannelFees, targetChannelID)
Expand Down
34 changes: 34 additions & 0 deletions x/ibctransfermiddleware/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package types

import (
"strconv"

"github.com/cosmos/cosmos-sdk/codec"
types "github.com/cosmos/cosmos-sdk/types"
)

// ParamsKey is the key to use for the keeper store.
var (
ParamsKey = []byte{0x01} // key for global staking middleware params in the keeper store

SequenceFeeKey = []byte{0x21} // prefix for sequence fee
)

const (
Expand All @@ -15,3 +24,28 @@ const (

RouterKey = ModuleName
)

// GetSequenceKey returns a key prefix for indexing HistoricalInfo objects.
func GetSequenceKey(sequence uint64) []byte {
return append(SequenceFeeKey, []byte(strconv.FormatUint(sequence, 10))...)
}

func MustMarshalCoin(cdc codec.BinaryCodec, validator *types.Coin) []byte {
return cdc.MustMarshal(validator)
}

// unmarshal a redelegation from a store value
func MustUnmarshalCoin(cdc codec.BinaryCodec, value []byte) types.Coin {
validator, err := UnmarshalCoin(cdc, value)
if err != nil {
panic(err)
}

return validator
}

// unmarshal a redelegation from a store value
func UnmarshalCoin(cdc codec.BinaryCodec, value []byte) (v types.Coin, err error) {
err = cdc.Unmarshal(value, &v)
return v, err
}

0 comments on commit e659cf7

Please sign in to comment.