Skip to content

Commit

Permalink
fix: add old supply offset functions
Browse files Browse the repository at this point in the history
  • Loading branch information
PaddyMc committed Oct 2, 2024
1 parent d4e20fe commit d2ff8f7
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
69 changes: 69 additions & 0 deletions x/bank/keeper/supply_offset_old.go
Original file line number Diff line number Diff line change
@@ -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))
}
5 changes: 5 additions & 0 deletions x/bank/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit d2ff8f7

Please sign in to comment.