diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b52fc9d7d..de27ae0b7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#1278](https://github.com/regen-network/regen-ledger/pull/1278) Fix `open` not set in `Msg/CreateBatch` - [#1281](https://github.com/regen-network/regen-ledger/pull/1281) Fix curator address in state migrations +- [#1284](https://github.com/regen-network/regen-ledger/pull/1284) Fix basket state migration ## [v4.0.0-rc1](https://github.com/regen-network/regen-ledger/releases/tag/v4.0.0-rc1) - 2022-07-15 diff --git a/x/ecocredit/migrations/v3/state.go b/x/ecocredit/migrations/v3/state.go index 94d9403af5..5333dab299 100644 --- a/x/ecocredit/migrations/v3/state.go +++ b/x/ecocredit/migrations/v3/state.go @@ -324,6 +324,11 @@ func MigrateState(sdkCtx sdk.Context, storeKey storetypes.StoreKey, return err } + // migrate basket state + if err = migrateBasketState(basketStore, ctx, oldBatchDenomToNewDenomMap); err != nil { + return err + } + if err := patchMigrate(ctx, sdkCtx, ss, basketStore, oldBatchDenomToNewDenomMap); err != nil { return err } @@ -331,6 +336,41 @@ func MigrateState(sdkCtx sdk.Context, storeKey storetypes.StoreKey, return nil } +// migrateBasketState updates basket balance old batch denom to new batch denom. +func migrateBasketState(basketStore basketapi.StateStore, ctx context.Context, oldBatchDenomToNewDenomMap map[string]string) error { + var balances []*basketapi.BasketBalance + itr, err := basketStore.BasketBalanceTable().List(ctx, basketapi.BasketBalancePrimaryKey{}) + if err != nil { + return err + } + + for itr.Next() { + bBalance, err := itr.Value() + if err != nil { + return err + } + + balances = append(balances, bBalance) + } + itr.Close() + + for i := 0; i < len(balances); i++ { + if err := basketStore.BasketBalanceTable().Delete(ctx, balances[i]); err != nil { + return err + } + } + + for i := 0; i < len(balances); i++ { + update := balances[i] + update.BatchDenom = oldBatchDenomToNewDenomMap[update.BatchDenom] + if err := basketStore.BasketBalanceTable().Insert(ctx, update); err != nil { + return err + } + } + + return nil +} + // migrateBalances migrates ecocredit tradable and retired balances to orm v1 func migrateBalances(store storetypes.KVStore, ss api.StateStore, ctx context.Context, batchDenomToBatchMap map[string]batchMapT) error { // migrate tradable balances to ORM v1 diff --git a/x/ecocredit/migrations/v3/state_test.go b/x/ecocredit/migrations/v3/state_test.go index b56dc0758d..4142284089 100644 --- a/x/ecocredit/migrations/v3/state_test.go +++ b/x/ecocredit/migrations/v3/state_test.go @@ -176,6 +176,19 @@ func TestMigrations(t *testing.T) { basketStore, err := basketapi.NewStateStore(ormdb) require.Nil(t, err) + require.NoError(t, basketStore.BasketBalanceTable().Insert(ormCtx, &basketapi.BasketBalance{ + BasketId: 1, + BatchDenom: bd1, + Balance: "100", + BatchStartDate: nil, + })) + require.NoError(t, basketStore.BasketBalanceTable().Insert(ormCtx, &basketapi.BasketBalance{ + BasketId: 2, + BatchDenom: bd2, + Balance: "100", + BatchStartDate: nil, + })) + err = v3.MigrateState(sdkCtx, ecocreditKey, encCfg.Marshaler, ss, basketStore, paramStore) require.NoError(t, err) @@ -330,6 +343,18 @@ func TestMigrations(t *testing.T) { bz = store.Get(tradableSKey1) require.Nil(t, bz) + // verify basket denom migration + bBalance, err := basketStore.BasketBalanceTable().Get(ormCtx, 1, expbd1) + require.NoError(t, err) + require.Equal(t, bBalance.BatchDenom, expbd1) + require.Equal(t, bBalance.Balance, "100") + require.Equal(t, bBalance.BasketId, uint64(1)) + + bBalance, err = basketStore.BasketBalanceTable().Get(ormCtx, 2, expbd2) + require.NoError(t, err) + require.Equal(t, bBalance.BatchDenom, expbd2) + require.Equal(t, bBalance.Balance, "100") + require.Equal(t, bBalance.BasketId, uint64(2)) } func formatBatchDenom(classId string, batchSeqNo uint64, startDate *time.Time, endDate *time.Time) string {