Skip to content

Commit

Permalink
add convenience method for saving market
Browse files Browse the repository at this point in the history
  • Loading branch information
jgimeno committed Sep 13, 2023
1 parent 03524f2 commit 5f5fdb3
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion x/epochs/types/identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func ValidateEpochIdentifierInterface(i interface{}) error {
return ValidateEpochIdentifierString(v)
}

// ValidateEpochIdentifierInterface performs a stateless
// ValidateEpochIdentifierString performs a stateless
// validation of the epoch ID.
func ValidateEpochIdentifierString(s string) error {
s = strings.TrimSpace(s)
Expand Down
2 changes: 1 addition & 1 deletion x/perp/v2/integration/action/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ type createMarketAction struct {
}

func (c createMarketAction) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
app.PerpKeeperV2.Markets.Insert(ctx, collections.Join(c.Market.Pair, uint64(1)), c.Market)
app.PerpKeeperV2.MarketLastVersion.Insert(ctx, c.Market.Pair, types.MarketLastVersion{Version: 1})
app.PerpKeeperV2.SaveMarket(ctx, c.Market)
app.PerpKeeperV2.AMMs.Insert(ctx, collections.Join(c.AMM.Pair, uint64(1)), c.AMM)

app.PerpKeeperV2.ReserveSnapshots.Insert(ctx, collections.Join(c.AMM.Pair, ctx.BlockTime()), types.ReserveSnapshot{
Expand Down
2 changes: 1 addition & 1 deletion x/perp/v2/keeper/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (k admin) CreateMarket(
return err
}

k.Markets.Insert(ctx, collections.Join(pair, lastVersion.Version), market)
k.SaveMarket(ctx, market)
k.AMMs.Insert(ctx, collections.Join(pair, lastVersion.Version), amm)
k.MarketLastVersion.Insert(ctx, pair, lastVersion)

Expand Down
4 changes: 4 additions & 0 deletions x/perp/v2/keeper/amm.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ func (k Keeper) GetMarket(ctx sdk.Context, pair asset.Pair) (types.Market, error
return market, nil
}

func (k Keeper) SaveMarket(ctx sdk.Context, market types.Market) {
k.Markets.Insert(ctx, collections.Join(market.Pair, market.Version), market)
}

// GetAMM returns the amm with last version.
func (k Keeper) GetAMM(ctx sdk.Context, pair asset.Pair) (types.AMM, error) {
lastVersion, err := k.MarketLastVersion.Get(ctx, pair)
Expand Down
2 changes: 1 addition & 1 deletion x/perp/v2/keeper/clearing_house_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ func TestMarketOrderError(t *testing.T) {
traderAddr := testutil.AccAddress()

market := mock.TestMarket()
app.PerpKeeperV2.Markets.Insert(ctx, collections.Join(asset.Registry.Pair(denoms.BTC, denoms.NUSD), market.Version), *market)
app.PerpKeeperV2.SaveMarket(ctx, *market)
app.PerpKeeperV2.MarketLastVersion.Insert(ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD), types.MarketLastVersion{Version: market.Version})
amm := mock.TestAMMDefault()
app.PerpKeeperV2.AMMs.Insert(ctx, collections.Join(asset.Registry.Pair(denoms.BTC, denoms.NUSD), market.Version), *amm)
Expand Down
2 changes: 1 addition & 1 deletion x/perp/v2/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, _ uint64)
premiumFraction := clampedDivergence.Mul(indexTwap).QuoInt64(int64(intervalsPerDay))

market.LatestCumulativePremiumFraction = market.LatestCumulativePremiumFraction.Add(premiumFraction)
k.Markets.Insert(ctx, collections.Join(market.Pair, market.Version), market)
k.SaveMarket(ctx, market)

_ = ctx.EventManager().EmitTypedEvent(&types.FundingRateChangedEvent{
Pair: market.Pair,
Expand Down
2 changes: 1 addition & 1 deletion x/perp/v2/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,6 @@ func (k Keeper) ChangeMarketEnabledParameter(ctx sdk.Context, pair asset.Pair, e
return
}
market.Enabled = enabled
k.Markets.Insert(ctx, collections.Join(pair, market.Version), market)
k.SaveMarket(ctx, market)
return
}
4 changes: 2 additions & 2 deletions x/perp/v2/keeper/twap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestInvalidTwap(t *testing.T) {
})
startTime := time.UnixMilli(0)

app.PerpKeeperV2.Markets.Insert(ctx, collections.Join(pair, uint64(1)), *mock.TestMarket())
app.PerpKeeperV2.SaveMarket(ctx, *mock.TestMarket())
app.PerpKeeperV2.AMMs.Insert(ctx, collections.Join(pair, uint64(1)), *mock.TestAMMDefault())
app.PerpKeeperV2.ReserveSnapshots.Insert(
ctx, collections.Join(pair, startTime), types.ReserveSnapshot{
Expand Down Expand Up @@ -375,7 +375,7 @@ func TestCalcTwapExtended(t *testing.T) {
})
startTime := time.UnixMilli(0)

app.PerpKeeperV2.Markets.Insert(ctx, collections.Join(pair, uint64(1)), *mock.TestMarket())
app.PerpKeeperV2.SaveMarket(ctx, *mock.TestMarket())
app.PerpKeeperV2.AMMs.Insert(ctx, collections.Join(pair, uint64(1)), *mock.TestAMMDefault())
app.PerpKeeperV2.ReserveSnapshots.Insert(
ctx, collections.Join(pair, startTime), types.ReserveSnapshot{
Expand Down
7 changes: 3 additions & 4 deletions x/perp/v2/keeper/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
sdkmath "cosmossdk.io/math"
"github.com/NibiruChain/collections"
sdk "github.com/cosmos/cosmos-sdk/types"

types "github.com/NibiruChain/nibiru/x/perp/v2/types"
Expand Down Expand Up @@ -82,19 +81,19 @@ func (k Keeper) WithdrawFromVault(
// IncrementPrepaidBadDebt increases the bad debt for the provided denom.
func (k Keeper) IncrementPrepaidBadDebt(ctx sdk.Context, market types.Market, amount sdkmath.Int) {
market.PrepaidBadDebt.Amount = market.PrepaidBadDebt.Amount.Add(amount)
k.Markets.Insert(ctx, collections.Join(market.Pair, market.Version), market)
k.SaveMarket(ctx, market)
}

// ZeroPrepaidBadDebt out the prepaid bad debt
func (k Keeper) ZeroPrepaidBadDebt(ctx sdk.Context, market types.Market) {
market.PrepaidBadDebt.Amount = sdk.ZeroInt()
k.Markets.Insert(ctx, collections.Join(market.Pair, market.Version), market)
k.SaveMarket(ctx, market)
}

// DecrementPrepaidBadDebt decrements the amount of bad debt prepaid by denom.
func (k Keeper) DecrementPrepaidBadDebt(ctx sdk.Context, market types.Market, amount sdkmath.Int) {
market.PrepaidBadDebt.Amount = market.PrepaidBadDebt.Amount.Sub(amount)
k.Markets.Insert(ctx, collections.Join(market.Pair, market.Version), market)
k.SaveMarket(ctx, market)
}

/*
Expand Down
2 changes: 1 addition & 1 deletion x/perp/v2/module/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
}

for _, m := range genState.Markets {
k.Markets.Insert(ctx, collections.Join(m.Pair, m.Version), m)
k.SaveMarket(ctx, m)
}

for _, g := range genState.MarketLastVersions {
Expand Down
2 changes: 1 addition & 1 deletion x/perp/v2/module/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func RunTestGenesis(t *testing.T, tc TestCase) {
pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD)

// create some params
app.PerpKeeperV2.Markets.Insert(ctx, collections.Join(pair, uint64(1)), *mock.TestMarket())
app.PerpKeeperV2.SaveMarket(ctx, *mock.TestMarket())
app.PerpKeeperV2.MarketLastVersion.Insert(ctx, pair, types.MarketLastVersion{Version: 1})
app.PerpKeeperV2.AMMs.Insert(ctx, collections.Join(pair, uint64(1)), *mock.TestAMMDefault())

Expand Down

0 comments on commit 5f5fdb3

Please sign in to comment.