-
Notifications
You must be signed in to change notification settings - Fork 182
/
test_common.go
109 lines (93 loc) · 3.14 KB
/
test_common.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package token
import (
"testing"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/supply"
"github.com/okex/okexchain/x/params"
"github.com/okex/okexchain/x/token/types"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
dbm "github.com/tendermint/tm-db"
)
// CreateParam create okexchain parm for test
func CreateParam(t *testing.T, isCheckTx bool) (sdk.Context, Keeper, *sdk.KVStoreKey, []byte) {
keyAcc := sdk.NewKVStoreKey(auth.StoreKey)
keyParams := sdk.NewKVStoreKey(params.StoreKey)
tkeyParams := sdk.NewTransientStoreKey(params.TStoreKey)
keySupply := sdk.NewKVStoreKey(supply.StoreKey)
keyToken := sdk.NewKVStoreKey("token")
keyLock := sdk.NewKVStoreKey("lock")
db := dbm.NewMemDB()
ms := store.NewCommitMultiStore(db)
ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db)
ms.MountStoreWithDB(keyToken, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keyLock, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keySupply, sdk.StoreTypeIAVL, db)
err := ms.LoadLatestVersion()
require.Nil(t, err)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "foochainid"}, isCheckTx, nil)
cdc := codec.New()
RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
pk := params.NewKeeper(cdc, keyParams, tkeyParams)
accountKeeper := auth.NewAccountKeeper(
cdc, // amino codec
keyAcc, // target store
pk.Subspace(auth.DefaultParamspace),
auth.ProtoBaseAccount, // prototype
)
blacklistedAddrs := make(map[string]bool)
bk := bank.NewBaseKeeper(
accountKeeper,
pk.Subspace(bank.DefaultParamspace),
blacklistedAddrs,
)
maccPerms := map[string][]string{
auth.FeeCollectorName: nil,
types.ModuleName: nil,
}
supplyKeeper := supply.NewKeeper(cdc, keySupply, accountKeeper, bk, maccPerms)
tk := NewKeeper(bk,
pk.Subspace(DefaultParamspace),
auth.FeeCollectorName,
supplyKeeper,
keyToken,
keyLock,
cdc,
true)
tk.SetParams(ctx, types.DefaultParams())
return ctx, tk, keyParams, []byte("testToken")
}
func NewTestToken(t *testing.T, ctx sdk.Context, keeper Keeper, bankKeeper bank.Keeper, tokenName string, addrList []sdk.AccAddress) {
require.NotEqual(t, 0 ,len(addrList))
tokenObject := InitTestTokenWithOwner(tokenName, addrList[0])
keeper.NewToken(ctx, tokenObject)
initCoins := sdk.NewCoins(sdk.NewCoin(tokenName, sdk.NewInt(100000)))
for _, addr := range addrList {
_, err := bankKeeper.AddCoins(ctx, addr, initCoins)
if err != nil {
panic(err)
}
}
}
func InitTestToken(name string) types.Token {
return InitTestTokenWithOwner(name, supply.NewModuleAddress(ModuleName))
}
func InitTestTokenWithOwner(name string, owner sdk.AccAddress) types.Token {
return types.Token{
Description: name,
Symbol: name,
OriginalSymbol: name,
WholeName: name,
OriginalTotalSupply: sdk.NewDec(0),
Owner: owner,
Type: 1,
Mintable: true,
}
}