Skip to content

Commit

Permalink
feeburn util
Browse files Browse the repository at this point in the history
  • Loading branch information
expertdicer committed Nov 2, 2023
1 parent 646270c commit e9592d3
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
47 changes: 47 additions & 0 deletions testutil/keeper/feeburn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package keeper

import (
"testing"

"github.com/White-Whale-Defi-Platform/migaloo-chain/v3/x/feeburn/keeper"
"github.com/White-Whale-Defi-Platform/migaloo-chain/v3/x/feeburn/types"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmdb "github.com/tendermint/tm-db"
)

func FeeburnKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
storeKey := sdk.NewKVStoreKey(types.StoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)

db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db)
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil)
require.NoError(t, stateStore.LoadLatestVersion())

registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)

k := keeper.NewKeeper(
cdc,
storeKey,
memStoreKey,
authtypes.NewModuleAddress(govtypes.ModuleName),
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())

// Initialize params
_ = k.SetParams(ctx, types.DefaultParams())

return k, ctx
}
57 changes: 57 additions & 0 deletions testutil/nullify/nullify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Package nullify provides methods to init nil values structs for test assertion.
package nullify

import (
"reflect"
"unsafe"

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

var (
coinType = reflect.TypeOf(sdk.Coin{})
coinsType = reflect.TypeOf(sdk.Coins{})
)

// Fill analyze all struct fields and slices with
// reflection and initialize the nil and empty slices,
// structs, and pointers.
func Fill(x interface{}) interface{} {
v := reflect.Indirect(reflect.ValueOf(x))
switch v.Kind() {
case reflect.Slice:
for i := 0; i < v.Len(); i++ {
obj := v.Index(i)
objPt := reflect.NewAt(obj.Type(), unsafe.Pointer(obj.UnsafeAddr())).Interface()
objPt = Fill(objPt)
obj.Set(reflect.ValueOf(objPt))
}
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
f := reflect.Indirect(v.Field(i))
if !f.CanSet() {
continue
}
switch f.Kind() {
case reflect.Slice:
f.Set(reflect.MakeSlice(f.Type(), 0, 0))
case reflect.Struct:
switch f.Type() {
case coinType:
coin := reflect.New(coinType).Interface()
s := reflect.ValueOf(coin).Elem()
f.Set(s)
case coinsType:
coins := reflect.New(coinsType).Interface()
s := reflect.ValueOf(coins).Elem()
f.Set(s)
default:
objPt := reflect.NewAt(f.Type(), unsafe.Pointer(f.UnsafeAddr())).Interface()
s := Fill(objPt)
f.Set(reflect.ValueOf(s))
}
}
}
}
return reflect.Indirect(v).Interface()
}
13 changes: 13 additions & 0 deletions testutil/sample/sample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package sample

import (
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// AccAddress returns a sample account address
func AccAddress() string {
pk := ed25519.GenPrivKey().PubKey()
addr := pk.Address()
return sdk.AccAddress(addr).String()
}

0 comments on commit e9592d3

Please sign in to comment.