@@ -227,31 +227,20 @@ func (k BaseSendKeeper) addCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.C
227
227
return nil
228
228
}
229
229
230
- // clearBalances removes all balances for a given account by address.
231
- func (k BaseSendKeeper ) clearBalances (ctx sdk.Context , addr sdk.AccAddress ) {
232
- keys := [][]byte {}
233
- k .IterateAccountBalances (ctx , addr , func (balance sdk.Coin ) bool {
234
- keys = append (keys , []byte (balance .Denom ))
235
- return false
236
- })
237
-
230
+ // initBalances sets the balance (multiple coins) for an account by address.
231
+ // An error is returned upon failure.
232
+ func (k BaseSendKeeper ) initBalances (ctx sdk.Context , addr sdk.AccAddress , balances sdk.Coins ) error {
238
233
accountStore := k .getAccountStore (ctx , addr )
234
+ for i := range balances {
235
+ balance := balances [i ]
236
+ if ! balance .IsValid () {
237
+ return sdkerrors .Wrap (sdkerrors .ErrInvalidCoins , balance .String ())
238
+ }
239
239
240
- for _ , key := range keys {
241
- accountStore .Delete (key )
242
- }
243
- }
244
-
245
- // setBalances sets the balance (multiple coins) for an account by address. It will
246
- // clear out all balances prior to setting the new coins as to set existing balances
247
- // to zero if they don't exist in amt. An error is returned upon failure.
248
- func (k BaseSendKeeper ) setBalances (ctx sdk.Context , addr sdk.AccAddress , balances sdk.Coins ) error {
249
- k .clearBalances (ctx , addr )
250
-
251
- for _ , balance := range balances {
252
- err := k .setBalance (ctx , addr , balance )
253
- if err != nil {
254
- return err
240
+ // Bank invariants require to not store zero balances.
241
+ if ! balance .IsZero () {
242
+ bz := k .cdc .MustMarshal (& balance )
243
+ accountStore .Set ([]byte (balance .Denom ), bz )
255
244
}
256
245
}
257
246
0 commit comments