Skip to content

Commit

Permalink
checkout x/perp from master
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine committed Sep 5, 2023
1 parent 3ca259a commit b9862f4
Show file tree
Hide file tree
Showing 11 changed files with 1,650 additions and 1,680 deletions.
28 changes: 14 additions & 14 deletions x/perp/v2/keeper/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

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

tutilaction "github.com/NibiruChain/nibiru/x/common/testutil/action"
tutilmock "github.com/NibiruChain/nibiru/x/common/testutil/mock"
perpaction "github.com/NibiruChain/nibiru/x/perp/v2/integration/action"
perpassert "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion"
. "github.com/NibiruChain/nibiru/x/common/testutil/action"
"github.com/NibiruChain/nibiru/x/common/testutil/mock"
. "github.com/NibiruChain/nibiru/x/perp/v2/integration/action"
. "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion"
"github.com/NibiruChain/nibiru/x/perp/v2/keeper"

"github.com/NibiruChain/nibiru/app"
Expand Down Expand Up @@ -91,28 +91,28 @@ func TestAdmin_WithdrawFromInsuranceFund(t *testing.T) {
func TestEnableMarket(t *testing.T) {
pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD)

tests := tutilaction.TestCases{
tutilaction.TC("true -> false").
tests := TestCases{
TC("true -> false").
Given(
perpaction.CreateCustomMarket(pair),
perpassert.MarketShouldBeEqual(pair, perpassert.Market_EnableShouldBeEqualTo(true)),
CreateCustomMarket(pair),
MarketShouldBeEqual(pair, Market_EnableShouldBeEqualTo(true)),
).
When(
perpaction.SetMarketEnabled(pair, false),
perpassert.MarketShouldBeEqual(pair, perpassert.Market_EnableShouldBeEqualTo(false)),
perpaction.SetMarketEnabled(pair, true),
SetMarketEnabled(pair, false),
MarketShouldBeEqual(pair, Market_EnableShouldBeEqualTo(false)),
SetMarketEnabled(pair, true),
).
Then(
perpassert.MarketShouldBeEqual(pair, perpassert.Market_EnableShouldBeEqualTo(true)),
MarketShouldBeEqual(pair, Market_EnableShouldBeEqualTo(true)),
),
}

tutilaction.NewTestSuite(t).WithTestCases(tests...).Run()
NewTestSuite(t).WithTestCases(tests...).Run()
}

func TestCreateMarketFail(t *testing.T) {
pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD)
amm := *tutilmock.TestAMMDefault()
amm := *mock.TestAMMDefault()
app, ctx := testapp.NewNibiruTestAppAndContext()

// Error because of invalid market
Expand Down
316 changes: 158 additions & 158 deletions x/perp/v2/keeper/amm_test.go

Large diffs are not rendered by default.

84 changes: 42 additions & 42 deletions x/perp/v2/keeper/calc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
"github.com/NibiruChain/nibiru/x/perp/v2/keeper"
types "github.com/NibiruChain/nibiru/x/perp/v2/types"

tutilaction "github.com/NibiruChain/nibiru/x/common/testutil/action"
perpaction "github.com/NibiruChain/nibiru/x/perp/v2/integration/action"
perpassert "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion"
. "github.com/NibiruChain/nibiru/x/common/testutil/action"
. "github.com/NibiruChain/nibiru/x/perp/v2/integration/action"
. "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion"
)

func TestPositionNotionalSpot(t *testing.T) {
Expand Down Expand Up @@ -68,74 +68,74 @@ func TestPositionNotionalTWAP(t *testing.T) {
pair := asset.Registry.Pair(denoms.BTC, denoms.USDC)
startTime := time.Now()

tc := tutilaction.TestCases{
tutilaction.TC("long position").
tc := TestCases{
TC("long position").
Given(
tutilaction.SetBlockTime(startTime),
tutilaction.SetBlockNumber(1),
perpaction.CreateCustomMarket(pair),
perpaction.InsertPosition(perpaction.WithSize(sdk.NewDec(10)), perpaction.WithTrader(alice)),
perpaction.InsertReserveSnapshot(pair, startTime, perpaction.WithPriceMultiplier(sdk.NewDec(9))),
perpaction.InsertReserveSnapshot(pair, startTime.Add(10*time.Second), perpaction.WithPriceMultiplier(sdk.MustNewDecFromStr("8.5"))),
perpaction.InsertReserveSnapshot(pair, startTime.Add(20*time.Second), perpaction.WithPriceMultiplier(sdk.MustNewDecFromStr("9.5"))),
SetBlockTime(startTime),
SetBlockNumber(1),
CreateCustomMarket(pair),
InsertPosition(WithSize(sdk.NewDec(10)), WithTrader(alice)),
InsertReserveSnapshot(pair, startTime, WithPriceMultiplier(sdk.NewDec(9))),
InsertReserveSnapshot(pair, startTime.Add(10*time.Second), WithPriceMultiplier(sdk.MustNewDecFromStr("8.5"))),
InsertReserveSnapshot(pair, startTime.Add(20*time.Second), WithPriceMultiplier(sdk.MustNewDecFromStr("9.5"))),
).
When(
tutilaction.MoveToNextBlockWithDuration(30 * time.Second),
MoveToNextBlockWithDuration(30 * time.Second),
).
Then(
perpassert.PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.MustNewDecFromStr("89.999999999100000000")),
PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.MustNewDecFromStr("89.999999999100000000")),
),

tutilaction.TC("short position").
TC("short position").
Given(
tutilaction.SetBlockTime(startTime),
tutilaction.SetBlockNumber(1),
perpaction.CreateCustomMarket(pair),
perpaction.InsertPosition(perpaction.WithSize(sdk.NewDec(-10)), perpaction.WithTrader(alice)),
perpaction.InsertReserveSnapshot(pair, startTime, perpaction.WithPriceMultiplier(sdk.NewDec(9))),
perpaction.InsertReserveSnapshot(pair, startTime.Add(10*time.Second), perpaction.WithPriceMultiplier(sdk.MustNewDecFromStr("8.5"))),
perpaction.InsertReserveSnapshot(pair, startTime.Add(20*time.Second), perpaction.WithPriceMultiplier(sdk.MustNewDecFromStr("9.5"))),
SetBlockTime(startTime),
SetBlockNumber(1),
CreateCustomMarket(pair),
InsertPosition(WithSize(sdk.NewDec(-10)), WithTrader(alice)),
InsertReserveSnapshot(pair, startTime, WithPriceMultiplier(sdk.NewDec(9))),
InsertReserveSnapshot(pair, startTime.Add(10*time.Second), WithPriceMultiplier(sdk.MustNewDecFromStr("8.5"))),
InsertReserveSnapshot(pair, startTime.Add(20*time.Second), WithPriceMultiplier(sdk.MustNewDecFromStr("9.5"))),
).
When(
tutilaction.MoveToNextBlockWithDuration(30 * time.Second),
MoveToNextBlockWithDuration(30 * time.Second),
).
Then(
perpassert.PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.MustNewDecFromStr("90.000000000900000000")),
PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.MustNewDecFromStr("90.000000000900000000")),
),

tutilaction.TC("zero position").
TC("zero position").
Given(
tutilaction.SetBlockTime(startTime),
tutilaction.SetBlockNumber(1),
perpaction.CreateCustomMarket(pair),
perpaction.InsertPosition(perpaction.WithSize(sdk.ZeroDec()), perpaction.WithTrader(alice)),
perpaction.InsertReserveSnapshot(pair, startTime, perpaction.WithPriceMultiplier(sdk.NewDec(9))),
perpaction.InsertReserveSnapshot(pair, startTime.Add(10*time.Second), perpaction.WithPriceMultiplier(sdk.MustNewDecFromStr("8.5"))),
perpaction.InsertReserveSnapshot(pair, startTime.Add(20*time.Second), perpaction.WithPriceMultiplier(sdk.MustNewDecFromStr("9.5"))),
SetBlockTime(startTime),
SetBlockNumber(1),
CreateCustomMarket(pair),
InsertPosition(WithSize(sdk.ZeroDec()), WithTrader(alice)),
InsertReserveSnapshot(pair, startTime, WithPriceMultiplier(sdk.NewDec(9))),
InsertReserveSnapshot(pair, startTime.Add(10*time.Second), WithPriceMultiplier(sdk.MustNewDecFromStr("8.5"))),
InsertReserveSnapshot(pair, startTime.Add(20*time.Second), WithPriceMultiplier(sdk.MustNewDecFromStr("9.5"))),
).
When(
tutilaction.MoveToNextBlockWithDuration(30 * time.Second),
MoveToNextBlockWithDuration(30 * time.Second),
).
Then(
perpassert.PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.ZeroDec()),
PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.ZeroDec()),
),

tutilaction.TC("single snapshot").
TC("single snapshot").
Given(
tutilaction.SetBlockTime(startTime),
tutilaction.SetBlockNumber(1),
perpaction.CreateCustomMarket(pair),
SetBlockTime(startTime),
SetBlockNumber(1),
CreateCustomMarket(pair),
).
When(
perpaction.InsertPosition(perpaction.WithSize(sdk.NewDec(100)), perpaction.WithTrader(alice)),
perpaction.InsertReserveSnapshot(pair, startTime, perpaction.WithPriceMultiplier(sdk.NewDec(9))),
InsertPosition(WithSize(sdk.NewDec(100)), WithTrader(alice)),
InsertReserveSnapshot(pair, startTime, WithPriceMultiplier(sdk.NewDec(9))),
).
Then(
perpassert.PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.MustNewDecFromStr("899.999999910000000009")),
PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.MustNewDecFromStr("899.999999910000000009")),
),
}

tutilaction.NewTestSuite(t).WithTestCases(tc...).Run()
NewTestSuite(t).WithTestCases(tc...).Run()
}

func TestUnrealizedPnl(t *testing.T) {
Expand Down
Loading

0 comments on commit b9862f4

Please sign in to comment.