Skip to content

Commit

Permalink
save price flip
Browse files Browse the repository at this point in the history
  • Loading branch information
jcompagni10 committed Jul 11, 2024
1 parent ff4692e commit c136fd8
Show file tree
Hide file tree
Showing 20 changed files with 589 additions and 544 deletions.
6 changes: 6 additions & 0 deletions proto/neutron/dex/limit_order_tranche.proto
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,10 @@ message LimitOrderTranche {
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "price_taker_to_maker"
];
string maker_price = 8 [
(gogoproto.moretags) = "yaml:\"maker_price\"",
(gogoproto.customtype) = "github.com/neutron-org/neutron/v4/utils/math.PrecDec",
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "maker_price"
];
}
12 changes: 6 additions & 6 deletions proto/neutron/dex/pool_reserves.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ message PoolReserves {
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "price_taker_to_maker"
];
string price_opposite_taker_to_maker = 4 [
(gogoproto.moretags) = "yaml:\"price_opposite_taker_to_maker\"",
(gogoproto.customtype) = "github.com/neutron-org/neutron/v4/utils/math.PrecDec",
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "price_opposite_taker_to_maker"
];
string maker_price = 4 [
(gogoproto.moretags) = "yaml:\"maker_price\"",
(gogoproto.customtype) = "github.com/neutron-org/neutron/v4/utils/math.PrecDec",
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "maker_price"
];
}
4 changes: 4 additions & 0 deletions x/dex/keeper/integration_multihopswap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,7 @@ func (s *DexTestSuite) TestMultiHopSwapEventsEmitted() {
// 8 tickUpdateEvents are emitted 4x for pool setup 4x for two swaps
s.AssertNEventValuesEmitted(types.TickUpdateEventKey, 8)
}

func (s *DexTestSuite) TestGP() {
types.WritePrecomputedPricesToFile()

Check failure on line 507 in x/dex/keeper/integration_multihopswap_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `types.WritePrecomputedPricesToFile` is not checked (errcheck)
}
4 changes: 2 additions & 2 deletions x/dex/keeper/limit_order_tranche.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewLimitOrderTranche(
limitOrderTrancheKey *types.LimitOrderTrancheKey,
goodTil *time.Time,
) (*types.LimitOrderTranche, error) {
priceTakerToMaker, err := limitOrderTrancheKey.PriceTakerToMaker()
price, err := limitOrderTrancheKey.Price()
if err != nil {
return nil, err
}
Expand All @@ -29,7 +29,7 @@ func NewLimitOrderTranche(
TotalMakerDenom: math.ZeroInt(),
TotalTakerDenom: math.ZeroInt(),
ExpirationTime: goodTil,
PriceTakerToMaker: priceTakerToMaker,
MakerPrice: price,
}, nil
}

Expand Down
23 changes: 8 additions & 15 deletions x/dex/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"

v3 "github.com/neutron-org/neutron/v4/x/dex/migrations/v3"
v4 "github.com/neutron-org/neutron/v4/x/dex/migrations/v4"
)

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
keeper Keeper
Expand All @@ -17,12 +10,12 @@ func NewMigrator(keeper Keeper) Migrator {
return Migrator{keeper: keeper}
}

// Migrate2to3 migrates from version 2 to 3.
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
return v3.MigrateStore(ctx, m.keeper.cdc, m.keeper.storeKey)
}
// // Migrate2to3 migrates from version 2 to 3.
// func (m Migrator) Migrate2to3(ctx sdk.Context) error {
// return v3.MigrateStore(ctx, m.keeper.cdc, m.keeper.storeKey)
// }

// Migrate2to3 migrates from version 3 to 4.
func (m Migrator) Migrate3to4(ctx sdk.Context) error {
return v4.MigrateStore(ctx, m.keeper.cdc, m.keeper.storeKey)
}
// // Migrate2to3 migrates from version 3 to 4.
// func (m Migrator) Migrate3to4(ctx sdk.Context) error {
// return v4.MigrateStore(ctx, m.keeper.cdc, m.keeper.storeKey)
// }
192 changes: 96 additions & 96 deletions x/dex/migrations/v3/store.go
Original file line number Diff line number Diff line change
@@ -1,98 +1,98 @@
package v3

import (
"errors"

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/store/prefix"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/neutron-org/neutron/v4/x/dex/types"
v2types "github.com/neutron-org/neutron/v4/x/dex/types/v2"
)

// MigrateStore performs in-place store migrations.
// The migration adds new dex params -- GoodTilPurgeAllowance & MaxJITsPerBlock// for handling JIT orders.
func MigrateStore(ctx sdk.Context, cdc codec.BinaryCodec, storeKey storetypes.StoreKey) error {
if err := migrateParams(ctx, cdc, storeKey); err != nil {
return err
}

if err := migrateLimitOrderExpirations(ctx, cdc, storeKey); err != nil {
return err
}
return nil
}

func migrateParams(ctx sdk.Context, cdc codec.BinaryCodec, storeKey storetypes.StoreKey) error {
ctx.Logger().Info("Migrating dex params...")

// fetch old params
store := ctx.KVStore(storeKey)
bz := store.Get(types.KeyPrefix(types.ParamsKey))
if bz == nil {
return errors.New("cannot fetch dex params from KV store")
}
var oldParams v2types.Params
cdc.MustUnmarshal(bz, &oldParams)

// add new param values
newParams := types.Params{
Paused: types.DefaultPaused,
FeeTiers: oldParams.FeeTiers,
GoodTilPurgeAllowance: types.DefaultGoodTilPurgeAllowance,
MaxJitsPerBlock: types.DefaultMaxJITsPerBlock,
}

// set params
bz, err := cdc.Marshal(&newParams)
if err != nil {
return err
}
store.Set(types.KeyPrefix(types.ParamsKey), bz)

ctx.Logger().Info("Finished migrating dex params")

return nil
}

func migrateLimitOrderExpirations(ctx sdk.Context, cdc codec.BinaryCodec, storeKey storetypes.StoreKey) error {
ctx.Logger().Info("Migrating dex LimitOrderExpirations...")

// fetch list of all old limit order expirations
expirationKeys := make([][]byte, 0)
expirationVals := make([]*types.LimitOrderExpiration, 0)
store := prefix.NewStore(ctx.KVStore(storeKey), types.KeyPrefix(types.LimitOrderExpirationKeyPrefix))
iterator := storetypes.KVStorePrefixIterator(store, []byte{})

for ; iterator.Valid(); iterator.Next() {
expirationKeys = append(expirationKeys, iterator.Key())
var expiration types.LimitOrderExpiration
cdc.MustUnmarshal(iterator.Value(), &expiration)
expirationVals = append(expirationVals, &expiration)
}

err := iterator.Close()
if err != nil {
return errorsmod.Wrap(err, "iterator failed to close during migration")
}

for i, key := range expirationKeys {
// re-save expiration with new key

expiration := expirationVals[i]
b := cdc.MustMarshal(expiration)
store.Set(types.LimitOrderExpirationKey(
expiration.ExpirationTime,
expiration.TrancheRef,
), b)
// Delete record with old key
store.Delete(key)
}

ctx.Logger().Info("Finished migrating dex LimitOrderExpirations")

return nil
}
// import (
// "errors"

// errorsmod "cosmossdk.io/errors"
// "cosmossdk.io/store/prefix"
// storetypes "cosmossdk.io/store/types"
// "github.com/cosmos/cosmos-sdk/codec"
// sdk "github.com/cosmos/cosmos-sdk/types"

// "github.com/neutron-org/neutron/v4/x/dex/types"
// v2types "github.com/neutron-org/neutron/v4/x/dex/types/v2"
// )

// // MigrateStore performs in-place store migrations.
// // The migration adds new dex params -- GoodTilPurgeAllowance & MaxJITsPerBlock// for handling JIT orders.
// func MigrateStore(ctx sdk.Context, cdc codec.BinaryCodec, storeKey storetypes.StoreKey) error {
// if err := migrateParams(ctx, cdc, storeKey); err != nil {
// return err
// }

// if err := migrateLimitOrderExpirations(ctx, cdc, storeKey); err != nil {
// return err
// }
// return nil
// }

// func migrateParams(ctx sdk.Context, cdc codec.BinaryCodec, storeKey storetypes.StoreKey) error {
// ctx.Logger().Info("Migrating dex params...")

// // fetch old params
// store := ctx.KVStore(storeKey)
// bz := store.Get(types.KeyPrefix(types.ParamsKey))
// if bz == nil {
// return errors.New("cannot fetch dex params from KV store")
// }
// var oldParams v2types.Params
// cdc.MustUnmarshal(bz, &oldParams)

// // add new param values
// newParams := types.Params{
// Paused: types.DefaultPaused,
// FeeTiers: oldParams.FeeTiers,
// GoodTilPurgeAllowance: types.DefaultGoodTilPurgeAllowance,
// MaxJitsPerBlock: types.DefaultMaxJITsPerBlock,
// }

// // set params
// bz, err := cdc.Marshal(&newParams)
// if err != nil {
// return err
// }
// store.Set(types.KeyPrefix(types.ParamsKey), bz)

// ctx.Logger().Info("Finished migrating dex params")

// return nil
// }

// func migrateLimitOrderExpirations(ctx sdk.Context, cdc codec.BinaryCodec, storeKey storetypes.StoreKey) error {
// ctx.Logger().Info("Migrating dex LimitOrderExpirations...")

// // fetch list of all old limit order expirations
// expirationKeys := make([][]byte, 0)
// expirationVals := make([]*types.LimitOrderExpiration, 0)
// store := prefix.NewStore(ctx.KVStore(storeKey), types.KeyPrefix(types.LimitOrderExpirationKeyPrefix))
// iterator := storetypes.KVStorePrefixIterator(store, []byte{})

// for ; iterator.Valid(); iterator.Next() {
// expirationKeys = append(expirationKeys, iterator.Key())
// var expiration types.LimitOrderExpiration
// cdc.MustUnmarshal(iterator.Value(), &expiration)
// expirationVals = append(expirationVals, &expiration)
// }

// err := iterator.Close()
// if err != nil {
// return errorsmod.Wrap(err, "iterator failed to close during migration")
// }

// for i, key := range expirationKeys {
// // re-save expiration with new key

// expiration := expirationVals[i]
// b := cdc.MustMarshal(expiration)
// store.Set(types.LimitOrderExpirationKey(
// expiration.ExpirationTime,
// expiration.TrancheRef,
// ), b)
// // Delete record with old key
// store.Delete(key)
// }

// ctx.Logger().Info("Finished migrating dex LimitOrderExpirations")

// return nil
// }
Loading

0 comments on commit c136fd8

Please sign in to comment.