Skip to content

Commit

Permalink
refactor: remove SetBalance and SetSupply (#629)
Browse files Browse the repository at this point in the history
* feat: remove SetBalance and SetSupply

* docs: add changelog

* test: restore TestHandleStoreAndInstantiate test func
  • Loading branch information
dudong2 authored Aug 10, 2022
1 parent ddc1d4e commit ab4739e
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 61 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (global) [\#611](https://github.com/line/lbm-sdk/pull/611) bump github.com/cosmos/cosmos-sdk from v0.45.1 to v0.45.6
* (simapp) [\#620](https://github.com/line/lbm-sdk/pull/620) chore: add iterator feature for simapp
* (x/collection) [\#622](https://github.com/line/lbm-sdk/pull/622) add Query/TokenClassTypeName
* (x/bank) [\#629](https://github.com/line/lbm-sdk/pull/629) remove unsafe balance changing methods from bank keeper such as `SetBalance` and `SetSupply`.

### Improvements

Expand Down
2 changes: 0 additions & 2 deletions store/listenkv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ func (li *listenIterator) Domain() (start []byte, end []byte) {
return li.parent.Domain()
}

// TODO(dudong2): remove Domain() func (removed in other stores by woosang). Add it later, if it's needed.

// Valid implements the Iterator interface.
func (li *listenIterator) Valid() bool {
return li.parent.Valid()
Expand Down
19 changes: 0 additions & 19 deletions x/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type Keeper interface {
HasSupply(ctx sdk.Context, denom string) bool
GetPaginatedTotalSupply(ctx sdk.Context, pagination *query.PageRequest) (sdk.Coins, *query.PageResponse, error)
IterateTotalSupply(ctx sdk.Context, cb func(sdk.Coin) bool)
SetSupply(ctx sdk.Context, coin sdk.Coin) // TODO(dudong2): remove after x/wasm version up(>= v0.22.0)

GetDenomMetaData(ctx sdk.Context, denom string) (types.Metadata, bool)
SetDenomMetaData(ctx sdk.Context, denomMetaData types.Metadata)
Expand Down Expand Up @@ -487,24 +486,6 @@ func (k BaseKeeper) setSupply(ctx sdk.Context, coin sdk.Coin) {
}
}

// SetSupply sets the supply for the given coin
func (k BaseKeeper) SetSupply(ctx sdk.Context, coin sdk.Coin) {
intBytes, err := coin.Amount.Marshal()
if err != nil {
panic(fmt.Errorf("unable to marshal amount value %v", err))
}

store := ctx.KVStore(k.storeKey)
supplyStore := prefix.NewStore(store, types.SupplyKey)

// Bank invariants and IBC requires to remove zero coins.
if coin.IsZero() {
supplyStore.Delete(conv.UnsafeStrToBytes(coin.GetDenom()))
} else {
supplyStore.Set([]byte(coin.GetDenom()), intBytes)
}
}

// trackDelegation tracks the delegation of the given account if it is a vesting account
func (k BaseKeeper) trackDelegation(ctx sdk.Context, addr sdk.AccAddress, balance, amt sdk.Coins) error {
acc := k.ak.GetAccount(ctx, addr)
Expand Down
24 changes: 0 additions & 24 deletions x/bank/keeper/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ type SendKeeper interface {
InputOutputCoins(ctx sdk.Context, inputs []types.Input, outputs []types.Output) error
SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error

SetBalance(ctx sdk.Context, addr sdk.AccAddress, balance sdk.Coin) error // TODO(dudong2): remove after x/wasm version up(>= v0.22.0)

GetParams(ctx sdk.Context) types.Params
SetParams(ctx sdk.Context, params types.Params)

Expand Down Expand Up @@ -268,28 +266,6 @@ func (k BaseSendKeeper) setBalance(ctx sdk.Context, addr sdk.AccAddress, balance
return nil
}

// SetBalance sets the coin balance for an account by address.
func (k BaseSendKeeper) SetBalance(ctx sdk.Context, addr sdk.AccAddress, balance sdk.Coin) error {
if !balance.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, balance.String())
}

accountStore := k.getAccountStore(ctx, addr)

// Bank invariants require to not store zero balances.
if balance.IsZero() {
accountStore.Delete([]byte(balance.Denom))
} else {
bz, err := balance.Marshal()
if err != nil {
return err
}
accountStore.Set([]byte(balance.Denom), bz)
}

return nil
}

// IsSendEnabledCoins checks the coins provide and returns an ErrSendDisabled if
// any of the coins are not configured for sending. Returns nil if sending is enabled
// for all provided coin
Expand Down
3 changes: 0 additions & 3 deletions x/wasm/keeper/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,9 +714,6 @@ func (m BurnerExampleInitMsg) GetBytes(t testing.TB) []byte {
func fundAccounts(t testing.TB, ctx sdk.Context, am authkeeper.AccountKeeper, bank bankkeeper.Keeper, addr sdk.AccAddress, coins sdk.Coins) {
acc := am.NewAccountWithAddress(ctx, addr)
am.SetAccount(ctx, acc)
for _, coin := range coins {
require.NoError(t, bank.SetBalance(ctx, addr, coin))
}
NewTestFaucet(t, ctx, bank, minttypes.ModuleName, coins...).Fund(ctx, addr, coins...)
}

Expand Down
14 changes: 1 addition & 13 deletions x/wasm/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,7 @@ func TestHandleInstantiate(t *testing.T) {

func TestHandleStoreAndInstantiate(t *testing.T) {
data := setupTest(t)

deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
creator := createFakeFundedAccount(t, data.ctx, data.acctKeeper, data.bankKeeper, deposit)
creator := data.faucet.NewFundedAccount(data.ctx, sdk.NewInt64Coin("denom", 100000))

h := data.module.Route().Handler()
q := data.module.LegacyQuerierHandler(nil)
Expand Down Expand Up @@ -761,13 +759,3 @@ func assertContractInfo(t *testing.T, q sdk.Querier, ctx sdk.Context, contractBe
assert.Equal(t, codeID, res.CodeID)
assert.Equal(t, creator.String(), res.Creator)
}
func createFakeFundedAccount(t *testing.T, ctx sdk.Context, am authkeeper.AccountKeeper, bankKeeper bankkeeper.Keeper, coins sdk.Coins) sdk.AccAddress {
t.Helper()
_, _, addr := keyPubAddr()
acc := am.NewAccountWithAddress(ctx, addr)
am.SetAccount(ctx, acc)
for _, coin := range coins {
require.NoError(t, bankKeeper.SetBalance(ctx, addr, coin))
}
return addr
}

0 comments on commit ab4739e

Please sign in to comment.