/
testapp.go
168 lines (141 loc) · 5.14 KB
/
testapp.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
package testapp
import (
"encoding/json"
"time"
tmdb "github.com/cometbft/cometbft-db"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/libs/log"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/NibiruChain/nibiru/app"
"github.com/NibiruChain/nibiru/x/common/asset"
"github.com/NibiruChain/nibiru/x/common/denoms"
"github.com/NibiruChain/nibiru/x/common/testutil"
epochstypes "github.com/NibiruChain/nibiru/x/epochs/types"
sudotypes "github.com/NibiruChain/nibiru/x/sudo/types"
tokenfactorytypes "github.com/NibiruChain/nibiru/x/tokenfactory/types"
)
func init() {
EnsureNibiruPrefix()
}
// NewNibiruTestAppAndContext creates an 'app.NibiruApp' instance with an
// in-memory 'tmdb.MemDB' and fresh 'sdk.Context'.
func NewNibiruTestAppAndContext() (*app.NibiruApp, sdk.Context) {
encoding := app.MakeEncodingConfig()
appGenesis := app.NewDefaultGenesisState(encoding.Marshaler)
genModEpochs := epochstypes.DefaultGenesisFromTime(time.Now().UTC())
// Set happy genesis: epochs
appGenesis[epochstypes.ModuleName] = encoding.Marshaler.MustMarshalJSON(
genModEpochs,
)
// Set happy genesis: sudo
sudoGenesis := new(sudotypes.GenesisState)
sudoGenesis.Sudoers = DefaultSudoers()
appGenesis[sudotypes.ModuleName] = encoding.Marshaler.MustMarshalJSON(sudoGenesis)
app := NewNibiruTestApp(appGenesis)
ctx := NewContext(app)
app.OracleKeeper.SetPrice(ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD), sdk.NewDec(20000))
app.OracleKeeper.SetPrice(ctx, "xxx:yyy", sdk.NewDec(20000))
return app, ctx
}
func NewContext(nibiru *app.NibiruApp) sdk.Context {
return nibiru.NewContext(false, tmproto.Header{
Height: 1,
Time: time.Now().UTC(),
})
}
// DefaultSudoers: State for the x/sudo module for the default test app.
func DefaultSudoers() sudotypes.Sudoers {
addr := DefaultSudoRoot().String()
return sudotypes.Sudoers{
Root: addr,
Contracts: []string{addr},
}
}
func DefaultSudoRoot() sdk.AccAddress {
return sdk.MustAccAddressFromBech32(testutil.ADDR_SUDO_ROOT)
}
// SetDefaultSudoGenesis: Sets the sudo module genesis state to a valid
// default. See "DefaultSudoers".
func SetDefaultSudoGenesis(gen app.GenesisState) {
sudoGen := new(sudotypes.GenesisState)
encoding := app.MakeEncodingConfig()
encoding.Marshaler.MustUnmarshalJSON(gen[sudotypes.ModuleName], sudoGen)
if err := sudoGen.Validate(); err != nil {
sudoGen.Sudoers = DefaultSudoers()
gen[sudotypes.ModuleName] = encoding.Marshaler.MustMarshalJSON(sudoGen)
}
}
// NewNibiruTestAppAndZeroTimeCtx: Runs NewNibiruTestAppAndZeroTimeCtx with the
// block time set to time zero.
func NewNibiruTestAppAndContextAtTime(startTime time.Time) (*app.NibiruApp, sdk.Context) {
app, _ := NewNibiruTestAppAndContext()
ctx := NewContext(app).WithBlockTime(startTime)
return app, ctx
}
// NewNibiruTestApp initializes a chain with the given genesis state to
// creates an application instance ('app.NibiruApp'). This app uses an
// in-memory database ('tmdb.MemDB') and has logging disabled.
func NewNibiruTestApp(gen app.GenesisState) *app.NibiruApp {
db := tmdb.NewMemDB()
logger := log.NewNopLogger()
encoding := app.MakeEncodingConfig()
SetDefaultSudoGenesis(gen)
app := app.NewNibiruApp(
logger,
db,
/*traceStore=*/ nil,
/*loadLatest=*/ true,
encoding,
/*appOpts=*/ sims.EmptyAppOptions{},
)
gen, err := GenesisStateWithSingleValidator(encoding.Marshaler, gen)
if err != nil {
panic(err)
}
stateBytes, err := json.MarshalIndent(gen, "", " ")
if err != nil {
panic(err)
}
app.InitChain(abci.RequestInitChain{
ConsensusParams: sims.DefaultConsensusParams,
AppStateBytes: stateBytes,
})
return app
}
// FundAccount is a utility function that funds an account by minting and
// sending the coins to the address. This should be used for testing purposes
// only!
func FundAccount(
bankKeeper bankkeeper.Keeper, ctx sdk.Context, addr sdk.AccAddress,
amounts sdk.Coins,
) error {
if err := bankKeeper.MintCoins(ctx, tokenfactorytypes.ModuleName, amounts); err != nil {
return err
}
return bankKeeper.SendCoinsFromModuleToAccount(ctx, tokenfactorytypes.ModuleName, addr, amounts)
}
// FundModuleAccount is a utility function that funds a module account by
// minting and sending the coins to the address. This should be used for testing
// purposes only!
func FundModuleAccount(
bankKeeper bankkeeper.Keeper, ctx sdk.Context,
recipientMod string, amounts sdk.Coins,
) error {
if err := bankKeeper.MintCoins(ctx, tokenfactorytypes.ModuleName, amounts); err != nil {
return err
}
return bankKeeper.SendCoinsFromModuleToModule(ctx, tokenfactorytypes.ModuleName, recipientMod, amounts)
}
// EnsureNibiruPrefix sets the account address prefix to Nibiru's rather than
// the default from the Cosmos-SDK, guaranteeing that tests will work with nibi
// addresses rather than cosmos ones (for Gaia).
func EnsureNibiruPrefix() {
csdkConfig := sdk.GetConfig()
nibiruPrefix := app.AccountAddressPrefix
if csdkConfig.GetBech32AccountAddrPrefix() != nibiruPrefix {
app.SetPrefixes(nibiruPrefix)
}
}