Skip to content

Commit

Permalink
use OIMF to evaluate end state; need to fix e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyding committed Mar 19, 2024
1 parent a550d20 commit 30bd1de
Show file tree
Hide file tree
Showing 19 changed files with 1,172 additions and 52 deletions.
18 changes: 18 additions & 0 deletions protocol/mocks/PerpetualsKeeper.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions protocol/testutil/constants/perpetuals.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package constants

import (
"math/big"

"github.com/dydxprotocol/v4-chain/protocol/dtypes"
perptypes "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/types"
)
Expand Down Expand Up @@ -81,6 +83,15 @@ var LiquidityTiers = []perptypes.LiquidityTier{
MaintenanceFractionPpm: 1_000_000,
ImpactNotional: 50_454_000_000,
},
{
Id: 9,
Name: "9",
InitialMarginPpm: 200_000, // 20%
MaintenanceFractionPpm: 500_000, // 20% * 0.5 = 10%
ImpactNotional: 2_500_000_000,
OpenInterestUpperCap: 50_000_000_000_000, // 50mm USDC
OpenInterestLowerCap: 25_000_000_000_000, // 25mm USDC
},
{
Id: 101,
Name: "101",
Expand All @@ -90,6 +101,22 @@ var LiquidityTiers = []perptypes.LiquidityTier{
},
}

// Perpetual OI setup in tests
var (
BtcUsd_OpenInterest1_AtomicRes8 = perptypes.OpenInterestDelta{
PerpetualId: 0,
BaseQuantumsDelta: big.NewInt(100_000_000),
}
EthUsd_OpenInterest1_AtomicRes9 = perptypes.OpenInterestDelta{
PerpetualId: 1,
BaseQuantumsDelta: big.NewInt(1_000_000_000),
}
DefaultTestPerpOIs = []perptypes.OpenInterestDelta{
BtcUsd_OpenInterest1_AtomicRes8,
EthUsd_OpenInterest1_AtomicRes9,
}
)

// Perpetual genesis parameters.
const TestFundingRateClampFactorPpm = 6_000_000
const TestPremiumVoteClampFactorPpm = 60_000_000
Expand Down Expand Up @@ -237,6 +264,19 @@ var (
FundingIndex: dtypes.ZeroInt(),
OpenInterest: dtypes.ZeroInt(),
}
BtcUsd_20PercentInitial_10PercentMaintenance_25mmLowerCap_50mmUpperCap = perptypes.Perpetual{
Params: perptypes.PerpetualParams{
Id: 0,
Ticker: "BTC-USD 20/10 margin requirements",
MarketId: uint32(0),
AtomicResolution: int32(-8),
DefaultFundingPpm: int32(0),
LiquidityTier: uint32(9),
MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS,
},
FundingIndex: dtypes.ZeroInt(),
OpenInterest: dtypes.ZeroInt(),
}
BtcUsd_NoMarginRequirement = perptypes.Perpetual{
Params: perptypes.PerpetualParams{
Id: 0,
Expand Down
29 changes: 29 additions & 0 deletions protocol/testutil/perpetuals/perpetuals.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package perpetuals

import (
"math/big"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

"github.com/dydxprotocol/v4-chain/protocol/dtypes"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
perptypes "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/types"
)

Expand Down Expand Up @@ -114,3 +119,27 @@ func abs(n int64) int64 {
}
return n
}

// Helper function to set up default open interest for input perpetuals.
func SetUpDefaultPerpOIsForTest(
t *testing.T,
ctx sdk.Context,
k perptypes.PerpetualsKeeper,
perps []perptypes.Perpetual,
) {
for _, perpOI := range constants.DefaultTestPerpOIs {
for _, perp := range perps {
if perp.Params.Id != perpOI.PerpetualId {
continue
}
// If the perpetual exists in input, set up the open interest.
require.NoError(t,
k.ModifyOpenInterest(
ctx,
perp.Params.Id,
perpOI.BaseQuantumsDelta,
),
)
}
}
}
44 changes: 38 additions & 6 deletions protocol/x/clob/keeper/deleveraging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
clobtest "github.com/dydxprotocol/v4-chain/protocol/testutil/clob"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
keepertest "github.com/dydxprotocol/v4-chain/protocol/testutil/keeper"
perptest "github.com/dydxprotocol/v4-chain/protocol/testutil/perpetuals"
assettypes "github.com/dydxprotocol/v4-chain/protocol/x/assets/types"
blocktimetypes "github.com/dydxprotocol/v4-chain/protocol/x/blocktime/types"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/memclob"
Expand Down Expand Up @@ -765,6 +766,13 @@ func TestOffsetSubaccountPerpetualPosition(t *testing.T) {
require.NoError(t, err)
}

perptest.SetUpDefaultPerpOIsForTest(
t,
ks.Ctx,
ks.PerpetualsKeeper,
perps,
)

clobPairs := []types.ClobPair{
constants.ClobPair_Btc,
constants.ClobPair_Eth,
Expand Down Expand Up @@ -1212,10 +1220,11 @@ func TestProcessDeleveraging(t *testing.T) {
err := keepertest.CreateUsdcAsset(ks.Ctx, ks.AssetsKeeper)
require.NoError(t, err)

for _, p := range []perptypes.Perpetual{
testPerps := []perptypes.Perpetual{
constants.BtcUsd_20PercentInitial_10PercentMaintenance,
constants.EthUsd_20PercentInitial_10PercentMaintenance,
} {
}
for _, p := range testPerps {
_, err := ks.PerpetualsKeeper.CreatePerpetual(
ks.Ctx,
p.Params.Id,
Expand All @@ -1229,6 +1238,13 @@ func TestProcessDeleveraging(t *testing.T) {
require.NoError(t, err)
}

perptest.SetUpDefaultPerpOIsForTest(
t,
ks.Ctx,
ks.PerpetualsKeeper,
testPerps,
)

ks.SubaccountsKeeper.SetSubaccount(ks.Ctx, tc.liquidatedSubaccount)
ks.SubaccountsKeeper.SetSubaccount(ks.Ctx, tc.offsettingSubaccount)

Expand Down Expand Up @@ -1423,10 +1439,11 @@ func TestProcessDeleveragingAtOraclePrice(t *testing.T) {
err := keepertest.CreateUsdcAsset(ks.Ctx, ks.AssetsKeeper)
require.NoError(t, err)

for _, p := range []perptypes.Perpetual{
testPerps := []perptypes.Perpetual{
constants.BtcUsd_20PercentInitial_10PercentMaintenance,
constants.EthUsd_20PercentInitial_10PercentMaintenance,
} {
}
for _, p := range testPerps {
_, err := ks.PerpetualsKeeper.CreatePerpetual(
ks.Ctx,
p.Params.Id,
Expand All @@ -1440,6 +1457,13 @@ func TestProcessDeleveragingAtOraclePrice(t *testing.T) {
require.NoError(t, err)
}

perptest.SetUpDefaultPerpOIsForTest(
t,
ks.Ctx,
ks.PerpetualsKeeper,
testPerps,
)

ks.SubaccountsKeeper.SetSubaccount(ks.Ctx, tc.liquidatedSubaccount)
ks.SubaccountsKeeper.SetSubaccount(ks.Ctx, tc.offsettingSubaccount)

Expand Down Expand Up @@ -1585,10 +1609,11 @@ func TestProcessDeleveraging_Rounding(t *testing.T) {
err := keepertest.CreateUsdcAsset(ks.Ctx, ks.AssetsKeeper)
require.NoError(t, err)

for _, p := range []perptypes.Perpetual{
testPerps := []perptypes.Perpetual{
constants.BtcUsd_20PercentInitial_10PercentMaintenance,
constants.EthUsd_20PercentInitial_10PercentMaintenance,
} {
}
for _, p := range testPerps {
_, err := ks.PerpetualsKeeper.CreatePerpetual(
ks.Ctx,
p.Params.Id,
Expand All @@ -1602,6 +1627,13 @@ func TestProcessDeleveraging_Rounding(t *testing.T) {
require.NoError(t, err)
}

perptest.SetUpDefaultPerpOIsForTest(
t,
ks.Ctx,
ks.PerpetualsKeeper,
testPerps,
)

ks.SubaccountsKeeper.SetSubaccount(ks.Ctx, tc.liquidatedSubaccount)
ks.SubaccountsKeeper.SetSubaccount(ks.Ctx, tc.offsettingSubaccount)
bankruptcyPriceQuoteQuantums, err := ks.ClobKeeper.GetBankruptcyPriceInQuoteQuantums(
Expand Down
43 changes: 40 additions & 3 deletions protocol/x/clob/keeper/liquidations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
clobtest "github.com/dydxprotocol/v4-chain/protocol/testutil/clob"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
keepertest "github.com/dydxprotocol/v4-chain/protocol/testutil/keeper"
perptest "github.com/dydxprotocol/v4-chain/protocol/testutil/perpetuals"
blocktimetypes "github.com/dydxprotocol/v4-chain/protocol/x/blocktime/types"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/memclob"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
Expand Down Expand Up @@ -299,6 +300,13 @@ func TestPlacePerpetualLiquidation(t *testing.T) {
require.NoError(t, err)
}

perptest.SetUpDefaultPerpOIsForTest(
t,
ks.Ctx,
ks.PerpetualsKeeper,
tc.perpetuals,
)

// Create all subaccounts.
for _, subaccount := range tc.subaccounts {
ks.SubaccountsKeeper.SetSubaccount(ctx, subaccount)
Expand Down Expand Up @@ -1139,10 +1147,11 @@ func TestPlacePerpetualLiquidation_PreexistingLiquidation(t *testing.T) {
err := keepertest.CreateUsdcAsset(ctx, ks.AssetsKeeper)
require.NoError(t, err)

for _, perpetual := range []perptypes.Perpetual{
testPerps := []perptypes.Perpetual{
constants.BtcUsd_100PercentMarginRequirement,
constants.EthUsd_100PercentMarginRequirement,
} {
}
for _, perpetual := range testPerps {
_, err = ks.PerpetualsKeeper.CreatePerpetual(
ctx,
perpetual.Params.Id,
Expand All @@ -1156,6 +1165,13 @@ func TestPlacePerpetualLiquidation_PreexistingLiquidation(t *testing.T) {
require.NoError(t, err)
}

perptest.SetUpDefaultPerpOIsForTest(
t,
ks.Ctx,
ks.PerpetualsKeeper,
testPerps,
)

for _, s := range tc.subaccounts {
ks.SubaccountsKeeper.SetSubaccount(ctx, s)
}
Expand Down Expand Up @@ -2045,6 +2061,13 @@ func TestPlacePerpetualLiquidation_Deleveraging(t *testing.T) {
require.NoError(t, err)
}

perptest.SetUpDefaultPerpOIsForTest(
t,
ks.Ctx,
ks.PerpetualsKeeper,
perpetuals,
)

for _, s := range tc.subaccounts {
ks.SubaccountsKeeper.SetSubaccount(ctx, s)
}
Expand Down Expand Up @@ -2756,6 +2779,13 @@ func TestGetBankruptcyPriceInQuoteQuantums(t *testing.T) {
require.NoError(t, err)
}

perptest.SetUpDefaultPerpOIsForTest(
t,
ks.Ctx,
ks.PerpetualsKeeper,
tc.perpetuals,
)

// Create the subaccount.
subaccountId := satypes.SubaccountId{
Owner: "liquidations_test",
Expand Down Expand Up @@ -2796,7 +2826,7 @@ func TestGetBankruptcyPriceInQuoteQuantums(t *testing.T) {
},
},
},
satypes.Match,
satypes.CollatCheck,
)

require.True(t, success)
Expand Down Expand Up @@ -4694,6 +4724,13 @@ func TestMaybeGetLiquidationOrder(t *testing.T) {
require.NoError(t, err)
}

perptest.SetUpDefaultPerpOIsForTest(
t,
ks.Ctx,
ks.PerpetualsKeeper,
tc.perpetuals,
)

// Create all subaccounts.
for _, subaccount := range tc.subaccounts {
ks.SubaccountsKeeper.SetSubaccount(ctx, subaccount)
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/clob/keeper/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,7 @@ func (k Keeper) AddOrderToOrderbookCollatCheck(
success, successPerSubaccountUpdate, err := k.subaccountsKeeper.CanUpdateSubaccounts(
ctx,
updates,
satypes.Match,
satypes.CollatCheck,
)
// TODO(DEC-191): Remove the error case from `CanUpdateSubaccounts`, which can only occur on overflow and specifying
// duplicate accounts.
Expand Down
15 changes: 15 additions & 0 deletions protocol/x/clob/keeper/orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

cmt "github.com/cometbft/cometbft/types"
testapp "github.com/dydxprotocol/v4-chain/protocol/testutil/app"
perptest "github.com/dydxprotocol/v4-chain/protocol/testutil/perpetuals"

"github.com/dydxprotocol/v4-chain/protocol/indexer/indexer_manager"

Expand Down Expand Up @@ -684,6 +685,13 @@ func TestPlaceShortTermOrder(t *testing.T) {
require.NoError(t, err)
}

perptest.SetUpDefaultPerpOIsForTest(
t,
ks.Ctx,
ks.PerpetualsKeeper,
tc.perpetuals,
)

// Create all subaccounts.
for _, subaccount := range tc.subaccounts {
ks.SubaccountsKeeper.SetSubaccount(ctx, subaccount)
Expand Down Expand Up @@ -912,6 +920,13 @@ func TestAddPreexistingStatefulOrder(t *testing.T) {
require.NoError(t, err)
}

perptest.SetUpDefaultPerpOIsForTest(
t,
ks.Ctx,
ks.PerpetualsKeeper,
tc.perpetuals,
)

// Create all subaccounts.
for _, subaccount := range tc.subaccounts {
ks.SubaccountsKeeper.SetSubaccount(ctx, subaccount)
Expand Down
3 changes: 2 additions & 1 deletion protocol/x/perpetuals/keeper/perpetual.go
Original file line number Diff line number Diff line change
Expand Up @@ -1257,8 +1257,9 @@ func (k Keeper) ModifyOpenInterest(
if bigOpenInterest.Sign() < 0 {
return errorsmod.Wrapf(
types.ErrOpenInterestWouldBecomeNegative,
"perpetualId = %d, openInterest = %s",
"perpetualId = %d, openInterest before = %s, after = %s",
perpetualId,
perpetual.OpenInterest.String(),
bigOpenInterest.String(),
)
}
Expand Down
Loading

0 comments on commit 30bd1de

Please sign in to comment.