@@ -8,13 +8,14 @@ import (
8
8
"github.com/golang/mock/gomock"
9
9
"github.com/stretchr/testify/suite"
10
10
11
+ "cosmossdk.io/core/address"
11
12
"cosmossdk.io/core/header"
12
13
coretesting "cosmossdk.io/core/testing"
13
14
"cosmossdk.io/math"
14
15
storetypes "cosmossdk.io/store/types"
15
- banktypes "cosmossdk.io/x/bank/types"
16
16
"cosmossdk.io/x/bank/v2/keeper"
17
17
banktestutil "cosmossdk.io/x/bank/v2/testutil"
18
+ banktypes "cosmossdk.io/x/bank/v2/types"
18
19
19
20
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
20
21
"github.com/cosmos/cosmos-sdk/runtime"
@@ -73,24 +74,20 @@ func (suite *KeeperTestSuite) mockMintCoins(moduleAcc *authtypes.ModuleAccount)
73
74
74
75
func (suite * KeeperTestSuite ) mockSendCoinsFromModuleToAccount (moduleAcc * authtypes.ModuleAccount , _ sdk.AccAddress ) {
75
76
suite .authKeeper .EXPECT ().GetModuleAddress (moduleAcc .Name ).Return (moduleAcc .GetAddress ())
76
- suite .authKeeper .EXPECT ().GetAccount (suite .ctx , moduleAcc .GetAddress ()).Return (moduleAcc )
77
77
}
78
78
79
79
func (suite * KeeperTestSuite ) mockFundAccount (receiver sdk.AccAddress ) {
80
80
suite .mockMintCoins (mintAcc )
81
81
suite .mockSendCoinsFromModuleToAccount (mintAcc , receiver )
82
82
}
83
83
84
- func (suite * KeeperTestSuite ) mockSendCoins (ctx context.Context , sender sdk.AccountI , _ sdk.AccAddress ) {
85
- suite .authKeeper .EXPECT ().GetAccount (ctx , sender .GetAddress ()).Return (sender )
86
- }
87
-
88
84
type KeeperTestSuite struct {
89
85
suite.Suite
90
86
91
- ctx context.Context
92
- bankKeeper keeper.Keeper
93
- authKeeper * banktestutil.MockAccountKeeper
87
+ ctx context.Context
88
+ bankKeeper keeper.Keeper
89
+ authKeeper * banktestutil.MockAccountKeeper
90
+ addressCodec address.Codec
94
91
}
95
92
96
93
func TestKeeperTestSuite (t * testing.T ) {
@@ -121,26 +118,26 @@ func (suite *KeeperTestSuite) SetupTest() {
121
118
encCfg .Codec ,
122
119
authKeeper ,
123
120
)
121
+ suite .addressCodec = ac
124
122
}
125
123
126
- func (suite * KeeperTestSuite ) TestSendCoins () {
124
+ func (suite * KeeperTestSuite ) TestSendCoins_Acount_To_Account () {
127
125
ctx := suite .ctx
128
126
require := suite .Require ()
129
127
balances := sdk .NewCoins (newFooCoin (100 ), newBarCoin (50 ))
130
128
sendAmt := sdk .NewCoins (newFooCoin (10 ), newBarCoin (10 ))
131
129
132
- acc0 := authtypes .NewBaseAccountWithAddress (accAddrs [0 ])
130
+ acc0Str , _ := suite .addressCodec .BytesToString (accAddrs [0 ])
131
+ acc1Str , _ := suite .addressCodec .BytesToString (accAddrs [1 ])
133
132
134
133
// Try send with empty balances
135
- suite .authKeeper .EXPECT ().GetAccount (suite .ctx , accAddrs [0 ]).Return (acc0 )
136
- err := suite .bankKeeper .SendCoins (ctx , accAddrs [0 ], accAddrs [1 ], sendAmt )
134
+ err := suite .bankKeeper .SendCoins (ctx , acc0Str , acc1Str , sendAmt )
137
135
require .Error (err )
138
136
139
137
// Set balances for acc0 and then try send to acc1
140
138
suite .mockFundAccount (accAddrs [0 ])
141
- require .NoError (banktestutil .FundAccount (ctx , suite .bankKeeper , accAddrs [0 ], balances ))
142
- suite .mockSendCoins (ctx , acc0 , accAddrs [1 ])
143
- require .NoError (suite .bankKeeper .SendCoins (ctx , accAddrs [0 ], accAddrs [1 ], sendAmt ))
139
+ require .NoError (banktestutil .FundAccount (ctx , suite .bankKeeper , acc0Str , balances ))
140
+ require .NoError (suite .bankKeeper .SendCoins (ctx , acc0Str , acc1Str , sendAmt ))
144
141
145
142
// Check balances
146
143
acc0FooBalance := suite .bankKeeper .GetBalance (ctx , accAddrs [0 ], fooDenom )
@@ -151,5 +148,66 @@ func (suite *KeeperTestSuite) TestSendCoins() {
151
148
require .Equal (acc1FooBalance .Amount , math .NewInt (10 ))
152
149
acc1BarBalance := suite .bankKeeper .GetBalance (ctx , accAddrs [1 ], barDenom )
153
150
require .Equal (acc1BarBalance .Amount , math .NewInt (10 ))
151
+ }
152
+
153
+ func (suite * KeeperTestSuite ) TestSendCoins_Module_To_Account () {
154
+ ctx := suite .ctx
155
+ require := suite .Require ()
156
+ balances := sdk .NewCoins (newFooCoin (100 ), newBarCoin (50 ))
157
+
158
+ acc4Str , _ := suite .addressCodec .BytesToString (accAddrs [4 ])
159
+
160
+ suite .mockMintCoins (mintAcc )
161
+ require .NoError (suite .bankKeeper .MintCoins (ctx , banktypes .MintModuleName , balances ))
162
+
163
+ // Try send from invalid module
164
+ suite .authKeeper .EXPECT ().GetModuleAddress ("invalid" ).Return (nil )
165
+ err := suite .bankKeeper .SendCoins (ctx , "invalid" , acc4Str , balances )
166
+ require .Error (err )
167
+
168
+ // Send from mint module
169
+ suite .authKeeper .EXPECT ().GetModuleAddress (mintAcc .Name ).Return (mintAcc .GetAddress ())
170
+ err = suite .bankKeeper .SendCoins (ctx , banktypes .MintModuleName , acc4Str , balances )
171
+ require .NoError (err )
154
172
173
+ // Check balances
174
+ acc4FooBalance := suite .bankKeeper .GetBalance (ctx , accAddrs [4 ], fooDenom )
175
+ require .Equal (acc4FooBalance .Amount , math .NewInt (100 ))
176
+ acc4BarBalance := suite .bankKeeper .GetBalance (ctx , accAddrs [4 ], barDenom )
177
+ require .Equal (acc4BarBalance .Amount , math .NewInt (50 ))
178
+ mintFooBalance := suite .bankKeeper .GetBalance (ctx , mintAcc .GetAddress (), fooDenom )
179
+ require .Equal (mintFooBalance .Amount , math .NewInt (0 ))
180
+ mintBarBalance := suite .bankKeeper .GetBalance (ctx , mintAcc .GetAddress (), barDenom )
181
+ require .Equal (mintBarBalance .Amount , math .NewInt (0 ))
182
+ }
183
+
184
+ func (suite * KeeperTestSuite ) TestSendCoins_Module_To_Module () {
185
+ ctx := suite .ctx
186
+ require := suite .Require ()
187
+ balances := sdk .NewCoins (newFooCoin (100 ), newBarCoin (50 ))
188
+
189
+ suite .mockMintCoins (mintAcc )
190
+ require .NoError (suite .bankKeeper .MintCoins (ctx , banktypes .MintModuleName , balances ))
191
+
192
+ // Try send to invalid module
193
+ suite .authKeeper .EXPECT ().GetModuleAddress (mintAcc .Name ).Return (mintAcc .GetAddress ())
194
+ suite .authKeeper .EXPECT ().GetModuleAddress ("invalid" ).Return (nil )
195
+ err := suite .bankKeeper .SendCoins (ctx , mintAcc .Name , "invalid" , balances )
196
+ require .Error (err )
197
+
198
+ // Send from mint module to burn module
199
+ suite .authKeeper .EXPECT ().GetModuleAddress (mintAcc .Name ).Return (mintAcc .GetAddress ())
200
+ suite .authKeeper .EXPECT ().GetModuleAddress (burnerAcc .Name ).Return (burnerAcc .GetAddress ())
201
+ err = suite .bankKeeper .SendCoins (ctx , mintAcc .Name , burnerAcc .Name , balances )
202
+ require .NoError (err )
203
+
204
+ // Check balances
205
+ burnerFooBalance := suite .bankKeeper .GetBalance (ctx , burnerAcc .GetAddress (), fooDenom )
206
+ require .Equal (burnerFooBalance .Amount , math .NewInt (100 ))
207
+ burnerBarBalance := suite .bankKeeper .GetBalance (ctx , burnerAcc .GetAddress (), barDenom )
208
+ require .Equal (burnerBarBalance .Amount , math .NewInt (50 ))
209
+ mintFooBalance := suite .bankKeeper .GetBalance (ctx , mintAcc .GetAddress (), fooDenom )
210
+ require .Equal (mintFooBalance .Amount , math .NewInt (0 ))
211
+ mintBarBalance := suite .bankKeeper .GetBalance (ctx , mintAcc .GetAddress (), barDenom )
212
+ require .Equal (mintBarBalance .Amount , math .NewInt (0 ))
155
213
}
0 commit comments