-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_common.go
192 lines (154 loc) · 6.04 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package keeper
import (
"fmt"
"os"
"testing"
"time"
"github.com/evoblockchain/evochain/libs/cosmos-sdk/store/mpt"
"github.com/evoblockchain/evochain/x/staking"
"github.com/evoblockchain/evochain/libs/cosmos-sdk/codec"
"github.com/evoblockchain/evochain/libs/cosmos-sdk/store"
sdk "github.com/evoblockchain/evochain/libs/cosmos-sdk/types"
"github.com/evoblockchain/evochain/libs/cosmos-sdk/x/auth"
"github.com/evoblockchain/evochain/libs/cosmos-sdk/x/bank"
"github.com/evoblockchain/evochain/libs/cosmos-sdk/x/supply"
abci "github.com/evoblockchain/evochain/libs/tendermint/abci/types"
"github.com/evoblockchain/evochain/libs/tendermint/crypto/ed25519"
"github.com/evoblockchain/evochain/libs/tendermint/libs/log"
dbm "github.com/evoblockchain/evochain/libs/tm-db"
"github.com/evoblockchain/evochain/x/params"
"github.com/stretchr/testify/require"
"github.com/evoblockchain/evochain/x/common"
"github.com/evoblockchain/evochain/x/dex/types"
"github.com/evoblockchain/evochain/x/gov"
"github.com/evoblockchain/evochain/x/token"
)
type testInput struct {
Ctx sdk.Context
Cdc *codec.Codec
TestAddrs []sdk.AccAddress
DexKeeper Keeper
}
// create a codec used only for testing
func makeTestCodec() *codec.Codec {
var cdc = codec.New()
bank.RegisterCodec(cdc)
auth.RegisterCodec(cdc)
supply.RegisterCodec(cdc)
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
types.RegisterCodec(cdc) // dex
return cdc
}
func createTestInputWithBalance(t *testing.T, numAddrs, initQuantity int64) testInput {
db := dbm.NewMemDB()
keyAcc := sdk.NewKVStoreKey(auth.StoreKey)
keyMpt := sdk.NewKVStoreKey(mpt.StoreKey)
keySupply := sdk.NewKVStoreKey(supply.StoreKey)
keyParams := sdk.NewKVStoreKey(params.StoreKey)
tkeyParams := sdk.NewTransientStoreKey(params.TStoreKey)
// token module
keyToken := sdk.NewKVStoreKey(token.StoreKey)
keyLock := sdk.NewKVStoreKey(token.KeyLock)
//keyTokenPair := sdk.NewKVStoreKey(token.KeyTokenPair)
keyStaking := sdk.NewKVStoreKey(staking.StoreKey)
// dex module
storeKey := sdk.NewKVStoreKey(types.StoreKey)
keyTokenPair := sdk.NewKVStoreKey(types.TokenPairStoreKey)
ms := store.NewCommitMultiStore(db)
ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keyMpt, sdk.StoreTypeMPT, db)
ms.MountStoreWithDB(keySupply, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db)
ms.MountStoreWithDB(keyStaking, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keyToken, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keyLock, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(storeKey, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keyTokenPair, sdk.StoreTypeIAVL, db)
err := ms.LoadLatestVersion()
require.Nil(t, err)
ctx := sdk.NewContext(ms, abci.Header{Time: time.Unix(0, 0)}, false, log.NewTMLogger(os.Stdout))
cdc := makeTestCodec()
feeCollectorAcc := supply.NewEmptyModuleAccount(auth.FeeCollectorName)
blacklistedAddrs := make(map[string]bool)
blacklistedAddrs[feeCollectorAcc.String()] = true
paramsKeeper := params.NewKeeper(cdc, keyParams, tkeyParams)
accountKeeper := auth.NewAccountKeeper(cdc, keyAcc, keyMpt,
paramsKeeper.Subspace(auth.DefaultParamspace), auth.ProtoBaseAccount)
bankKeeper := bank.NewBaseKeeper(accountKeeper, paramsKeeper.Subspace(bank.DefaultParamspace),
blacklistedAddrs)
maccPerms := map[string][]string{
auth.FeeCollectorName: nil,
token.ModuleName: {supply.Minter, supply.Burner},
types.ModuleName: nil,
gov.ModuleName: nil,
}
supplyKeeper := supply.NewKeeper(cdc, keySupply, accountKeeper, bankKeeper, maccPerms)
supplyKeeper.SetSupply(ctx, supply.NewSupply(sdk.Coins{}))
// set module accounts
supplyKeeper.SetModuleAccount(ctx, feeCollectorAcc)
// token keeper
tokenKeepr := token.NewKeeper(bankKeeper,
paramsKeeper.Subspace(token.DefaultParamspace), auth.FeeCollectorName, supplyKeeper,
keyToken, keyLock, cdc, true, accountKeeper)
paramsSubspace := paramsKeeper.Subspace(types.DefaultParamspace)
mockStakingKeeper := &mockStakingKeeper{true}
mockBankKeeper := mockBankKeeper{}
// dex keeper
dexKeeper := NewKeeper(auth.FeeCollectorName, supplyKeeper, paramsSubspace, tokenKeepr, mockStakingKeeper, mockBankKeeper, storeKey, keyTokenPair, cdc)
// init account tokens
decCoins, err := sdk.ParseDecCoins(fmt.Sprintf("%d%s,%d%s",
initQuantity, common.NativeToken, initQuantity, common.TestToken))
if err != nil {
panic(err)
}
var testAddrs []sdk.AccAddress
for i := int64(0); i < numAddrs; i++ {
pk := ed25519.GenPrivKey().PubKey()
addr := sdk.AccAddress(pk.Address())
testAddrs = append(testAddrs, addr)
err := dexKeeper.supplyKeeper.MintCoins(ctx, token.ModuleName, decCoins)
require.Nil(t, err)
err = dexKeeper.supplyKeeper.SendCoinsFromModuleToAccount(ctx, token.ModuleName, addr, decCoins)
require.Nil(t, err)
}
return testInput{ctx, cdc, testAddrs, dexKeeper}
}
// nolint
func createTestInput(t *testing.T) testInput {
return createTestInputWithBalance(t, 2, 100)
}
type mockStakingKeeper struct {
getFakeValidator bool
}
func (m *mockStakingKeeper) IsValidator(ctx sdk.Context, addr sdk.AccAddress) bool {
return m.getFakeValidator
}
func (m *mockStakingKeeper) SetFakeValidator(fakeValidator bool) {
m.getFakeValidator = fakeValidator
}
type mockBankKeeper struct{}
// GetCoins returns coins for test
func (keeper mockBankKeeper) GetCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins {
return sdk.NewDecCoinsFromDec(common.NativeToken, sdk.NewDec(2500))
}
// GetBuiltInTokenPair returns built in token pair for test
func GetBuiltInTokenPair() *types.TokenPair {
common.InitConfig()
addr, err := sdk.AccAddressFromBech32(types.TestTokenPairOwner)
if err != nil {
panic(err)
}
return &types.TokenPair{
ID: 1,
BaseAssetSymbol: common.TestToken,
QuoteAssetSymbol: sdk.DefaultBondDenom,
InitPrice: sdk.MustNewDecFromStr("10.0"),
MaxPriceDigit: 8,
MaxQuantityDigit: 8,
MinQuantity: sdk.MustNewDecFromStr("0"),
Owner: addr,
Deposits: sdk.NewDecCoin(sdk.DefaultBondDenom, sdk.NewInt(0)),
}
}