From d2ff8f7253c78642d812e97ab0811203d316bf5b Mon Sep 17 00:00:00 2001 From: ghost Date: Wed, 2 Oct 2024 17:18:40 +0100 Subject: [PATCH] fix: add old supply offset functions --- x/bank/keeper/supply_offset_old.go | 69 ++++++++++++++++++++++++++++++ x/bank/types/keys.go | 5 +++ 2 files changed, 74 insertions(+) create mode 100644 x/bank/keeper/supply_offset_old.go diff --git a/x/bank/keeper/supply_offset_old.go b/x/bank/keeper/supply_offset_old.go new file mode 100644 index 000000000000..e5fc90439ebd --- /dev/null +++ b/x/bank/keeper/supply_offset_old.go @@ -0,0 +1,69 @@ +package keeper + +import ( + "context" + "fmt" + + "cosmossdk.io/math" + "cosmossdk.io/store/prefix" + + "github.com/cosmos/cosmos-sdk/runtime" + "github.com/cosmos/cosmos-sdk/x/bank/types" +) + +// NOTE: All these functions should only be used in the v27 migration +// this file should be removed completely after the migration + +// GetSupplyOffset retrieves the SupplyOffset from store for a specific denom using the pre v26 (old) key +// TODO: Remove after v27 migration +func (k BaseViewKeeper) GetSupplyOffsetOld(ctx context.Context, denom string) math.Int { + store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) + supplyOffsetStore := prefix.NewStore(store, types.SupplyOffsetKeyOld) + + bz := supplyOffsetStore.Get([]byte(denom)) + if bz == nil { + return math.NewInt(0) + } + + var amount math.Int + err := amount.Unmarshal(bz) + if err != nil { + panic(fmt.Errorf("unable to unmarshal supply offset value %v", err)) + } + + return amount +} + +// RemoveOldSupplyOffset removes the old supply offset key +// TODO: Remove after v27 migration +func (k BaseViewKeeper) RemoveOldSupplyOffset(ctx context.Context, denom string) { + store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) + supplyOffsetStore := prefix.NewStore(store, types.SupplyOffsetKeyOld) + + supplyOffsetStore.Delete([]byte(denom)) +} + +// setSupplyOffsetOld sets the supply offset for the given denom using the pre v26 (old) key +// TODO: Remove after v27 migration +func (k BaseKeeper) setSupplyOffsetOld(ctx context.Context, denom string, offsetAmount math.Int) { + intBytes, err := offsetAmount.Marshal() + if err != nil { + panic(fmt.Errorf("unable to marshal amount value %v", err)) + } + + store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) + supplyOffsetStore := prefix.NewStore(store, types.SupplyOffsetKeyOld) + + // Bank invariants and IBC requires to remove zero coins. + if offsetAmount.IsZero() { + supplyOffsetStore.Delete([]byte(denom)) + } else { + supplyOffsetStore.Set([]byte(denom), intBytes) + } +} + +// AddSupplyOffset adjusts the current supply offset of a denom by the inputted offsetAmount using the pre v26 (old) key +// TODO: Remove after v27 migration +func (k BaseKeeper) AddSupplyOffsetOld(ctx context.Context, denom string, offsetAmount math.Int) { + k.setSupplyOffsetOld(ctx, denom, k.GetSupplyOffsetOld(ctx, denom).Add(offsetAmount)) +} diff --git a/x/bank/types/keys.go b/x/bank/types/keys.go index 68eb56a0538e..5134a0c581ce 100644 --- a/x/bank/types/keys.go +++ b/x/bank/types/keys.go @@ -33,7 +33,12 @@ var ( // ParamsKey is the prefix for x/bank parameters ParamsKey = collections.NewPrefix(5) + // SupplyOffKey is a Osmosis specific key that handles supply offsets SupplyOffsetKey = collections.NewPrefix(88) + + // SupplyOffKeyOld is a Osmosis specific key that handles supply offsets pre v0.50 + // TODO: Remove in the v28 upgrade + SupplyOffsetKeyOld = []byte{0x88} ) // BalanceValueCodec is a codec for encoding bank balances in a backwards compatible way.