diff --git a/CHANGELOG.md b/CHANGELOG.md index e6bb897a7..22b971361 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,6 +59,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * [#1536](https://github.com/NibiruChain/nibiru/pull/1536) - test(perp): add more tests to perp module and cli * [#1533](https://github.com/NibiruChain/nibiru/pull/1533) - feat(perp): add differential fields to PositionChangedEvent * [#1541](https://github.com/NibiruChain/nibiru/pull/1541) - feat(perp): add clamp to premium fractions +* [#1556](https://github.com/NibiruChain/nibiru/pull/1556) - refactor(perp): remove usage of unnamed dot imports * [#1555](https://github.com/NibiruChain/nibiru/pull/1555) - feat(devgas): Convert legacy ABCI events to typed proto events * [#1558](https://github.com/NibiruChain/nibiru/pull/1558) - feat(perp): paginated query to read the position store * [#1554](https://github.com/NibiruChain/nibiru/pull/1554) - refactor: runs gofumpt formatter, which has nice conventions: go install mvdan.cc/gofumpt@latest diff --git a/x/oracle/keeper/ballot.go b/x/oracle/keeper/ballot.go index 1c7da53ad..ee5aa0d3b 100644 --- a/x/oracle/keeper/ballot.go +++ b/x/oracle/keeper/ballot.go @@ -137,11 +137,17 @@ func (k Keeper) removeInvalidBallots( return pairBallotsMap, whitelistedPairs } -// Tally calculates the median and returns it. Sets the set of voters to be rewarded, i.e. voted within -// a reasonable spread from the weighted median to the store +// Tally calculates the median and returns it. Sets the set of voters to be +// rewarded, i.e. voted within a reasonable spread from the weighted median to +// the store. // -// ALERT: This function mutates validatorPerformances slice based on the votes made by the validators. -func Tally(ballots types.ExchangeRateBallots, rewardBand sdk.Dec, validatorPerformances types.ValidatorPerformances) sdk.Dec { +// ALERT: This function mutates validatorPerformances slice based on the votes +// made by the validators. +func Tally( + ballots types.ExchangeRateBallots, + rewardBand sdk.Dec, + validatorPerformances types.ValidatorPerformances, +) sdk.Dec { weightedMedian := ballots.WeightedMedianWithAssertion() standardDeviation := ballots.StandardDeviation(weightedMedian) rewardSpread := weightedMedian.Mul(rewardBand.QuoInt64(2)) @@ -155,15 +161,20 @@ func Tally(ballots types.ExchangeRateBallots, rewardBand sdk.Dec, validatorPerfo voteInsideSpread := ballot.ExchangeRate.GTE(weightedMedian.Sub(rewardSpread)) && ballot.ExchangeRate.LTE(weightedMedian.Add(rewardSpread)) isAbstainVote := !ballot.ExchangeRate.IsPositive() - - if voteInsideSpread || isAbstainVote { - voterAddr := ballot.Voter.String() - - validatorPerformance := validatorPerformances[voterAddr] + isMiss := !(voteInsideSpread || isAbstainVote) + + voterAddr := ballot.Voter.String() + validatorPerformance := validatorPerformances[voterAddr] + switch { + case isAbstainVote: + validatorPerformance.AbstainCount++ + case voteInsideSpread: validatorPerformance.RewardWeight += ballot.Power validatorPerformance.WinCount++ - validatorPerformances[voterAddr] = validatorPerformance + case isMiss: + validatorPerformance.MissCount++ } + validatorPerformances[voterAddr] = validatorPerformance } return weightedMedian diff --git a/x/oracle/keeper/slash_test.go b/x/oracle/keeper/slash_test.go index b4bd13077..edf6e54eb 100644 --- a/x/oracle/keeper/slash_test.go +++ b/x/oracle/keeper/slash_test.go @@ -170,7 +170,10 @@ func TestWhitelistSlashing(t *testing.T) { slashFraction := input.OracleKeeper.SlashFraction(input.Ctx) minValidPerWindow := input.OracleKeeper.MinValidPerWindow(input.Ctx) - for i := uint64(0); i < uint64(sdk.OneDec().Sub(minValidPerWindow).MulInt64(votePeriodsPerWindow).TruncateInt64()); i++ { + allowedMissPct := sdk.OneDec().Sub(minValidPerWindow) + allowedMissPeriods := allowedMissPct.MulInt64(votePeriodsPerWindow). + TruncateInt64() + for idxMissPeriod := uint64(0); idxMissPeriod < uint64(allowedMissPeriods); idxMissPeriod++ { input.Ctx = input.Ctx.WithBlockHeight(input.Ctx.BlockHeight() + 1) // Account 2, govstable @@ -181,7 +184,7 @@ func TestWhitelistSlashing(t *testing.T) { input.OracleKeeper.UpdateExchangeRates(input.Ctx) // input.OracleKeeper.SlashAndResetMissCounters(input.Ctx) // input.OracleKeeper.UpdateExchangeRates(input.Ctx) - require.Equal(t, i+1, input.OracleKeeper.MissCounters.GetOr(input.Ctx, ValAddrs[0], 0)) + require.Equal(t, idxMissPeriod+1, input.OracleKeeper.MissCounters.GetOr(input.Ctx, ValAddrs[0], 0)) } validator := input.StakingKeeper.Validator(input.Ctx, ValAddrs[0]) diff --git a/x/oracle/keeper/update_exchange_rates.go b/x/oracle/keeper/update_exchange_rates.go index a8fd8f9d1..369c3487b 100644 --- a/x/oracle/keeper/update_exchange_rates.go +++ b/x/oracle/keeper/update_exchange_rates.go @@ -29,19 +29,25 @@ func (k Keeper) UpdateExchangeRates(ctx sdk.Context) { k.updateWhitelist(ctx, params.Whitelist, whitelistedPairs) } -// registerMissedVotes it parses all validators performance and increases the missed vote of those that did not vote. -func (k Keeper) registerMissedVotes(ctx sdk.Context, whitelistedPairs set.Set[asset.Pair], validatorPerformances types.ValidatorPerformances) { +// registerMissedVotes it parses all validators performance and increases the +// missed vote of those that did not vote. +func (k Keeper) registerMissedVotes( + ctx sdk.Context, + whitelistedPairs set.Set[asset.Pair], + validatorPerformances types.ValidatorPerformances, +) { for _, validatorPerformance := range validatorPerformances { - if int(validatorPerformance.WinCount) == len(whitelistedPairs) { - continue + if int(validatorPerformance.MissCount) > 0 { + k.MissCounters.Insert( + ctx, validatorPerformance.ValAddress, + k.MissCounters.GetOr(ctx, validatorPerformance.ValAddress, 0)+1) + k.Logger(ctx).Info("vote miss", "validator", validatorPerformance.ValAddress.String()) } - - k.MissCounters.Insert(ctx, validatorPerformance.ValAddress, k.MissCounters.GetOr(ctx, validatorPerformance.ValAddress, 0)+1) - k.Logger(ctx).Info("vote miss", "validator", validatorPerformance.ValAddress.String()) } } -// countVotesAndUpdateExchangeRates processes the votes and updates the ExchangeRates based on the results. +// countVotesAndUpdateExchangeRates processes the votes and updates the +// ExchangeRates based on the results. func (k Keeper) countVotesAndUpdateExchangeRates( ctx sdk.Context, pairBallotsMap map[asset.Pair]types.ExchangeRateBallots, diff --git a/x/oracle/keeper/update_exchange_rates_test.go b/x/oracle/keeper/update_exchange_rates_test.go index b2660addb..2f169f1b4 100644 --- a/x/oracle/keeper/update_exchange_rates_test.go +++ b/x/oracle/keeper/update_exchange_rates_test.go @@ -170,12 +170,10 @@ func TestOracleTally(t *testing.T) { validatorClaimMap := make(types.ValidatorPerformances) for _, valAddr := range valAddrs { - validatorClaimMap[valAddr.String()] = types.ValidatorPerformance{ - Power: stakingKeeper.Validator(fixture.Ctx, valAddr).GetConsensusPower(sdk.DefaultPowerReduction), - RewardWeight: int64(0), - WinCount: int64(0), - ValAddress: valAddr, - } + validatorClaimMap[valAddr.String()] = types.NewValidatorPerformance( + stakingKeeper.Validator(fixture.Ctx, valAddr).GetConsensusPower(sdk.DefaultPowerReduction), + valAddr, + ) } sort.Sort(ballot) weightedMedian := ballot.WeightedMedianWithAssertion() @@ -188,27 +186,29 @@ func TestOracleTally(t *testing.T) { expectedValidatorClaimMap := make(types.ValidatorPerformances) for _, valAddr := range valAddrs { - expectedValidatorClaimMap[valAddr.String()] = types.ValidatorPerformance{ - Power: stakingKeeper.Validator(fixture.Ctx, valAddr).GetConsensusPower(sdk.DefaultPowerReduction), - RewardWeight: int64(0), - WinCount: int64(0), - ValAddress: valAddr, - } + expectedValidatorClaimMap[valAddr.String()] = types.NewValidatorPerformance( + stakingKeeper.Validator(fixture.Ctx, valAddr).GetConsensusPower(sdk.DefaultPowerReduction), + valAddr, + ) } for _, vote := range ballot { - if (vote.ExchangeRate.GTE(weightedMedian.Sub(maxSpread)) && - vote.ExchangeRate.LTE(weightedMedian.Add(maxSpread))) || - !vote.ExchangeRate.IsPositive() { - key := vote.Voter.String() - claim := expectedValidatorClaimMap[key] + key := vote.Voter.String() + claim := expectedValidatorClaimMap[key] + if vote.ExchangeRate.GTE(weightedMedian.Sub(maxSpread)) && + vote.ExchangeRate.LTE(weightedMedian.Add(maxSpread)) { claim.RewardWeight += vote.Power claim.WinCount++ - expectedValidatorClaimMap[key] = claim + } else if !vote.ExchangeRate.IsPositive() { + claim.AbstainCount++ + } else { + claim.MissCount++ } + expectedValidatorClaimMap[key] = claim } - tallyMedian := Tally(ballot, fixture.OracleKeeper.RewardBand(fixture.Ctx), validatorClaimMap) + tallyMedian := Tally( + ballot, fixture.OracleKeeper.RewardBand(fixture.Ctx), validatorClaimMap) assert.Equal(t, expectedValidatorClaimMap, validatorClaimMap) assert.Equal(t, tallyMedian.MulInt64(100).TruncateInt(), weightedMedian.MulInt64(100).TruncateInt()) diff --git a/x/oracle/types/ballot.go b/x/oracle/types/ballot.go index f4ed8b953..3851db302 100644 --- a/x/oracle/types/ballot.go +++ b/x/oracle/types/ballot.go @@ -173,9 +173,14 @@ func (pb ExchangeRateBallots) Swap(i, j int) { // ValidatorPerformance keeps track of a validator performance in the voting period. type ValidatorPerformance struct { - Power int64 // tendermint consensus power - RewardWeight int64 // how much of the rewards this validator should receive, units of consensus power - WinCount int64 + // Tendermint consensus voting power + Power int64 + // RewardWeight: Weight of rewards the validator should receive in units of + // consensus power. + RewardWeight int64 + WinCount int64 // Number of valid votes for which the validator will be rewarded + AbstainCount int64 // Number of abstained votes for which there will be no reward or punishment + MissCount int64 // Number of invalid/punishable votes ValAddress sdk.ValAddress } @@ -185,6 +190,8 @@ func NewValidatorPerformance(power int64, recipient sdk.ValAddress) ValidatorPer Power: power, RewardWeight: 0, WinCount: 0, + AbstainCount: 0, + MissCount: 0, ValAddress: recipient, } } diff --git a/x/perp/v2/keeper/admin_test.go b/x/perp/v2/keeper/admin_test.go index b372edb12..017e53981 100644 --- a/x/perp/v2/keeper/admin_test.go +++ b/x/perp/v2/keeper/admin_test.go @@ -9,10 +9,10 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" - . "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" + 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/perp/v2/keeper" "github.com/NibiruChain/nibiru/app" @@ -91,28 +91,28 @@ func TestAdmin_WithdrawFromInsuranceFund(t *testing.T) { func TestEnableMarket(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) - tests := TestCases{ - TC("true -> false"). + tests := tutilaction.TestCases{ + tutilaction.TC("true -> false"). Given( - CreateCustomMarket(pair), - MarketShouldBeEqual(pair, Market_EnableShouldBeEqualTo(true)), + perpaction.CreateCustomMarket(pair), + perpassert.MarketShouldBeEqual(pair, perpassert.Market_EnableShouldBeEqualTo(true)), ). When( - SetMarketEnabled(pair, false), - MarketShouldBeEqual(pair, Market_EnableShouldBeEqualTo(false)), - SetMarketEnabled(pair, true), + perpaction.SetMarketEnabled(pair, false), + perpassert.MarketShouldBeEqual(pair, perpassert.Market_EnableShouldBeEqualTo(false)), + perpaction.SetMarketEnabled(pair, true), ). Then( - MarketShouldBeEqual(pair, Market_EnableShouldBeEqualTo(true)), + perpassert.MarketShouldBeEqual(pair, perpassert.Market_EnableShouldBeEqualTo(true)), ), } - NewTestSuite(t).WithTestCases(tests...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tests...).Run() } func TestCreateMarketFail(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) - amm := *mock.TestAMMDefault() + amm := *tutilmock.TestAMMDefault() app, ctx := testapp.NewNibiruTestAppAndContext() // Error because of invalid market diff --git a/x/perp/v2/keeper/amm_test.go b/x/perp/v2/keeper/amm_test.go index 3c31475bf..2252a387c 100644 --- a/x/perp/v2/keeper/amm_test.go +++ b/x/perp/v2/keeper/amm_test.go @@ -10,11 +10,11 @@ import ( "github.com/NibiruChain/nibiru/x/common/asset" "github.com/NibiruChain/nibiru/x/common/denoms" - . "github.com/NibiruChain/nibiru/x/common/testutil/action" - . "github.com/NibiruChain/nibiru/x/common/testutil/assertion" + tutilaction "github.com/NibiruChain/nibiru/x/common/testutil/action" + tutilassertion "github.com/NibiruChain/nibiru/x/common/testutil/assertion" "github.com/NibiruChain/nibiru/x/common/testutil/testapp" - . "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" - . "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" + perpaction "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" + perpassertion "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" "github.com/NibiruChain/nibiru/x/perp/v2/keeper" types "github.com/NibiruChain/nibiru/x/perp/v2/types" ) @@ -22,129 +22,129 @@ import ( func TestEditPriceMultipler(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) - tests := TestCases{ - TC("same price multiplier"). + tests := tutilaction.TestCases{ + tutilaction.TC("same price multiplier"). Given( - CreateCustomMarket(pair, WithTotalLong(sdk.NewDec(1000)), WithTotalShort(sdk.NewDec(500))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + perpaction.CreateCustomMarket(pair, perpaction.WithTotalLong(sdk.NewDec(1000)), perpaction.WithTotalShort(sdk.NewDec(500))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - EditPriceMultiplier(pair, sdk.OneDec()), + perpaction.EditPriceMultiplier(pair, sdk.OneDec()), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1e6)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(1e6)), - AMMShouldBeEqual(pair, - AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e12)), - AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e12)), - AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e12)), - AMM_PriceMultiplierShouldBeEqual(sdk.OneDec()), + tutilassertion.ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1e6)), + tutilassertion.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(1e6)), + perpassertion.AMMShouldBeEqual(pair, + perpassertion.AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e12)), + perpassertion.AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e12)), + perpassertion.AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e12)), + perpassertion.AMM_PriceMultiplierShouldBeEqual(sdk.OneDec()), ), ), - TC("net bias zero"). + tutilaction.TC("net bias zero"). Given( - CreateCustomMarket(pair, WithTotalLong(sdk.NewDec(1000)), WithTotalShort(sdk.NewDec(1000))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + perpaction.CreateCustomMarket(pair, perpaction.WithTotalLong(sdk.NewDec(1000)), perpaction.WithTotalShort(sdk.NewDec(1000))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - EditPriceMultiplier(pair, sdk.NewDec(10)), + perpaction.EditPriceMultiplier(pair, sdk.NewDec(10)), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1e6)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(1e6)), - AMMShouldBeEqual(pair, - AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e12)), - AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e12)), - AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e12)), - AMM_PriceMultiplierShouldBeEqual(sdk.NewDec(10)), + tutilassertion.ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1e6)), + tutilassertion.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(1e6)), + perpassertion.AMMShouldBeEqual(pair, + perpassertion.AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e12)), + perpassertion.AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e12)), + perpassertion.AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e12)), + perpassertion.AMM_PriceMultiplierShouldBeEqual(sdk.NewDec(10)), ), ), - TC("long bias, increase price multiplier"). + tutilaction.TC("long bias, increase price multiplier"). Given( - CreateCustomMarket(pair, WithTotalLong(sdk.NewDec(1000)), WithTotalShort(sdk.NewDec(500))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + perpaction.CreateCustomMarket(pair, perpaction.WithTotalLong(sdk.NewDec(1000)), perpaction.WithTotalShort(sdk.NewDec(500))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - EditPriceMultiplier(pair, sdk.NewDec(10)), + perpaction.EditPriceMultiplier(pair, sdk.NewDec(10)), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1004500)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(995500)), - AMMShouldBeEqual(pair, - AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e12)), - AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e12)), - AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e12)), - AMM_PriceMultiplierShouldBeEqual(sdk.NewDec(10)), + tutilassertion.ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1004500)), + tutilassertion.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(995500)), + perpassertion.AMMShouldBeEqual(pair, + perpassertion.AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e12)), + perpassertion.AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e12)), + perpassertion.AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e12)), + perpassertion.AMM_PriceMultiplierShouldBeEqual(sdk.NewDec(10)), ), ), - TC("long bias, decrease price multiplier"). + tutilaction.TC("long bias, decrease price multiplier"). Given( - CreateCustomMarket(pair, WithTotalLong(sdk.NewDec(1000)), WithTotalShort(sdk.NewDec(500))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + perpaction.CreateCustomMarket(pair, perpaction.WithTotalLong(sdk.NewDec(1000)), perpaction.WithTotalShort(sdk.NewDec(500))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - EditPriceMultiplier(pair, sdk.MustNewDecFromStr("0.25")), + perpaction.EditPriceMultiplier(pair, sdk.MustNewDecFromStr("0.25")), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(999626)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(1000374)), - AMMShouldBeEqual(pair, - AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e12)), - AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e12)), - AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e12)), - AMM_PriceMultiplierShouldBeEqual(sdk.MustNewDecFromStr("0.25")), + tutilassertion.ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(999626)), + tutilassertion.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(1000374)), + perpassertion.AMMShouldBeEqual(pair, + perpassertion.AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e12)), + perpassertion.AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e12)), + perpassertion.AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e12)), + perpassertion.AMM_PriceMultiplierShouldBeEqual(sdk.MustNewDecFromStr("0.25")), ), ), - TC("short bias, increase price multiplier"). + tutilaction.TC("short bias, increase price multiplier"). Given( - CreateCustomMarket(pair, WithTotalLong(sdk.NewDec(500)), WithTotalShort(sdk.NewDec(1000))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + perpaction.CreateCustomMarket(pair, perpaction.WithTotalLong(sdk.NewDec(500)), perpaction.WithTotalShort(sdk.NewDec(1000))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - EditPriceMultiplier(pair, sdk.NewDec(10)), + perpaction.EditPriceMultiplier(pair, sdk.NewDec(10)), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(995500)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(1004500)), - AMMShouldBeEqual(pair, - AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e12)), - AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e12)), - AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e12)), - AMM_PriceMultiplierShouldBeEqual(sdk.NewDec(10)), + tutilassertion.ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(995500)), + tutilassertion.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(1004500)), + perpassertion.AMMShouldBeEqual(pair, + perpassertion.AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e12)), + perpassertion.AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e12)), + perpassertion.AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e12)), + perpassertion.AMM_PriceMultiplierShouldBeEqual(sdk.NewDec(10)), ), ), - TC("short bias, decrease price multiplier"). + tutilaction.TC("short bias, decrease price multiplier"). Given( - CreateCustomMarket(pair, WithTotalLong(sdk.NewDec(500)), WithTotalShort(sdk.NewDec(1000))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + perpaction.CreateCustomMarket(pair, perpaction.WithTotalLong(sdk.NewDec(500)), perpaction.WithTotalShort(sdk.NewDec(1000))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - EditPriceMultiplier(pair, sdk.MustNewDecFromStr("0.25")), + perpaction.EditPriceMultiplier(pair, sdk.MustNewDecFromStr("0.25")), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1000376)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(999624)), - AMMShouldBeEqual(pair, - AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e12)), - AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e12)), - AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e12)), - AMM_PriceMultiplierShouldBeEqual(sdk.MustNewDecFromStr("0.25")), + tutilassertion.ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1000376)), + tutilassertion.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(999624)), + perpassertion.AMMShouldBeEqual(pair, + perpassertion.AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e12)), + perpassertion.AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e12)), + perpassertion.AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e12)), + perpassertion.AMM_PriceMultiplierShouldBeEqual(sdk.MustNewDecFromStr("0.25")), ), ), } - NewTestSuite(t).WithTestCases(tests...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tests...).Run() } func TestEditPriceMultiplerFail(t *testing.T) { @@ -284,135 +284,135 @@ func TestEditSwapInvariantFail(t *testing.T) { func TestEditSwapInvariant(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) - tests := TestCases{ - TC("same swap invariant"). + tests := tutilaction.TestCases{ + tutilaction.TC("same swap invariant"). Given( - CreateCustomMarket(pair, - WithTotalLong(sdk.NewDec(1000)), - WithTotalShort(sdk.NewDec(500)), - WithSqrtDepth(sdk.NewDec(1e6)), + perpaction.CreateCustomMarket(pair, + perpaction.WithTotalLong(sdk.NewDec(1000)), + perpaction.WithTotalShort(sdk.NewDec(500)), + perpaction.WithSqrtDepth(sdk.NewDec(1e6)), ), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - EditSwapInvariant(pair, sdk.NewDec(1e12)), + perpaction.EditSwapInvariant(pair, sdk.NewDec(1e12)), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1e6)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(1e6)), - AMMShouldBeEqual(pair, - AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e6)), - AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e6)), - AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e6)), - AMM_PriceMultiplierShouldBeEqual(sdk.OneDec()), + tutilassertion.ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1e6)), + tutilassertion.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(1e6)), + perpassertion.AMMShouldBeEqual(pair, + perpassertion.AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e6)), + perpassertion.AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e6)), + perpassertion.AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e6)), + perpassertion.AMM_PriceMultiplierShouldBeEqual(sdk.OneDec()), ), ), - TC("net bias zero"). + tutilaction.TC("net bias zero"). Given( - CreateCustomMarket(pair, - WithTotalLong(sdk.NewDec(1000)), - WithTotalShort(sdk.NewDec(1000)), - WithSqrtDepth(sdk.NewDec(1e6)), + perpaction.CreateCustomMarket(pair, + perpaction.WithTotalLong(sdk.NewDec(1000)), + perpaction.WithTotalShort(sdk.NewDec(1000)), + perpaction.WithSqrtDepth(sdk.NewDec(1e6)), ), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - EditSwapInvariant(pair, sdk.NewDec(1e18)), + perpaction.EditSwapInvariant(pair, sdk.NewDec(1e18)), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1e6)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(1e6)), - AMMShouldBeEqual(pair, - AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e9)), - AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e9)), - AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e9)), - AMM_PriceMultiplierShouldBeEqual(sdk.OneDec()), + tutilassertion.ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1e6)), + tutilassertion.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(1e6)), + perpassertion.AMMShouldBeEqual(pair, + perpassertion.AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e9)), + perpassertion.AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e9)), + perpassertion.AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e9)), + perpassertion.AMM_PriceMultiplierShouldBeEqual(sdk.OneDec()), ), ), - TC("long bias, increase swap invariant"). + tutilaction.TC("long bias, increase swap invariant"). Given( - CreateCustomMarket(pair, WithTotalLong(sdk.NewDec(1e5)), WithSqrtDepth(sdk.NewDec(1e6))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + perpaction.CreateCustomMarket(pair, perpaction.WithTotalLong(sdk.NewDec(1e5)), perpaction.WithSqrtDepth(sdk.NewDec(1e6))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - EditSwapInvariant(pair, sdk.NewDec(1e14)), + perpaction.EditSwapInvariant(pair, sdk.NewDec(1e14)), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1008101)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(991899)), - AMMShouldBeEqual(pair, - AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e7)), - AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e7)), - AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e7)), - AMM_PriceMultiplierShouldBeEqual(sdk.OneDec()), + tutilassertion.ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1008101)), + tutilassertion.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(991899)), + perpassertion.AMMShouldBeEqual(pair, + perpassertion.AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e7)), + perpassertion.AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e7)), + perpassertion.AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e7)), + perpassertion.AMM_PriceMultiplierShouldBeEqual(sdk.OneDec()), ), ), - TC("long bias, decrease swap invariant"). + tutilaction.TC("long bias, decrease swap invariant"). Given( - CreateCustomMarket(pair, WithTotalLong(sdk.NewDec(1e2)), WithSqrtDepth(sdk.NewDec(1e6))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + perpaction.CreateCustomMarket(pair, perpaction.WithTotalLong(sdk.NewDec(1e2)), perpaction.WithSqrtDepth(sdk.NewDec(1e6))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - EditSwapInvariant(pair, sdk.NewDec(1e6)), + perpaction.EditSwapInvariant(pair, sdk.NewDec(1e6)), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(999991)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(1000009)), - AMMShouldBeEqual(pair, - AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e3)), - AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e3)), - AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e3)), - AMM_PriceMultiplierShouldBeEqual(sdk.OneDec()), + tutilassertion.ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(999991)), + tutilassertion.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(1000009)), + perpassertion.AMMShouldBeEqual(pair, + perpassertion.AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e3)), + perpassertion.AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e3)), + perpassertion.AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e3)), + perpassertion.AMM_PriceMultiplierShouldBeEqual(sdk.OneDec()), ), ), - TC("short bias, increase swap invariant"). + tutilaction.TC("short bias, increase swap invariant"). Given( - CreateCustomMarket(pair, WithTotalShort(sdk.NewDec(1e5)), WithSqrtDepth(sdk.NewDec(1e6))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + perpaction.CreateCustomMarket(pair, perpaction.WithTotalShort(sdk.NewDec(1e5)), perpaction.WithSqrtDepth(sdk.NewDec(1e6))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - EditSwapInvariant(pair, sdk.NewDec(1e14)), + perpaction.EditSwapInvariant(pair, sdk.NewDec(1e14)), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1010102)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(989898)), - AMMShouldBeEqual(pair, - AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e7)), - AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e7)), - AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e7)), - AMM_PriceMultiplierShouldBeEqual(sdk.OneDec()), + tutilassertion.ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1010102)), + tutilassertion.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(989898)), + perpassertion.AMMShouldBeEqual(pair, + perpassertion.AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e7)), + perpassertion.AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e7)), + perpassertion.AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e7)), + perpassertion.AMM_PriceMultiplierShouldBeEqual(sdk.OneDec()), ), ), - TC("short bias, decrease swap invariant"). + tutilaction.TC("short bias, decrease swap invariant"). Given( - CreateCustomMarket(pair, WithTotalShort(sdk.NewDec(1e2)), WithSqrtDepth(sdk.NewDec(1e6))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + perpaction.CreateCustomMarket(pair, perpaction.WithTotalShort(sdk.NewDec(1e2)), perpaction.WithSqrtDepth(sdk.NewDec(1e6))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - EditSwapInvariant(pair, sdk.NewDec(1e6)), + perpaction.EditSwapInvariant(pair, sdk.NewDec(1e6)), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(999989)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(1000011)), - AMMShouldBeEqual(pair, - AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e3)), - AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e3)), - AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e3)), - AMM_PriceMultiplierShouldBeEqual(sdk.OneDec()), + tutilassertion.ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(999989)), + tutilassertion.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(1000011)), + perpassertion.AMMShouldBeEqual(pair, + perpassertion.AMM_SqrtDepthShouldBeEqual(sdk.NewDec(1e3)), + perpassertion.AMM_BaseReserveShouldBeEqual(sdk.NewDec(1e3)), + perpassertion.AMM_QuoteReserveShouldBeEqual(sdk.NewDec(1e3)), + perpassertion.AMM_PriceMultiplierShouldBeEqual(sdk.OneDec()), ), ), } - NewTestSuite(t).WithTestCases(tests...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tests...).Run() } diff --git a/x/perp/v2/keeper/calc_test.go b/x/perp/v2/keeper/calc_test.go index dade53d60..dee020800 100644 --- a/x/perp/v2/keeper/calc_test.go +++ b/x/perp/v2/keeper/calc_test.go @@ -15,9 +15,9 @@ import ( "github.com/NibiruChain/nibiru/x/perp/v2/keeper" types "github.com/NibiruChain/nibiru/x/perp/v2/types" - . "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" + 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" ) func TestPositionNotionalSpot(t *testing.T) { @@ -68,74 +68,74 @@ func TestPositionNotionalTWAP(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.USDC) startTime := time.Now() - tc := TestCases{ - TC("long position"). + tc := tutilaction.TestCases{ + tutilaction.TC("long position"). Given( - 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"))), + 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"))), ). When( - MoveToNextBlockWithDuration(30 * time.Second), + tutilaction.MoveToNextBlockWithDuration(30 * time.Second), ). Then( - PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.MustNewDecFromStr("89.999999999100000000")), + perpassert.PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.MustNewDecFromStr("89.999999999100000000")), ), - TC("short position"). + tutilaction.TC("short position"). Given( - 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"))), + 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"))), ). When( - MoveToNextBlockWithDuration(30 * time.Second), + tutilaction.MoveToNextBlockWithDuration(30 * time.Second), ). Then( - PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.MustNewDecFromStr("90.000000000900000000")), + perpassert.PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.MustNewDecFromStr("90.000000000900000000")), ), - TC("zero position"). + tutilaction.TC("zero position"). Given( - 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"))), + 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"))), ). When( - MoveToNextBlockWithDuration(30 * time.Second), + tutilaction.MoveToNextBlockWithDuration(30 * time.Second), ). Then( - PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.ZeroDec()), + perpassert.PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.ZeroDec()), ), - TC("single snapshot"). + tutilaction.TC("single snapshot"). Given( - SetBlockTime(startTime), - SetBlockNumber(1), - CreateCustomMarket(pair), + tutilaction.SetBlockTime(startTime), + tutilaction.SetBlockNumber(1), + perpaction.CreateCustomMarket(pair), ). When( - InsertPosition(WithSize(sdk.NewDec(100)), WithTrader(alice)), - InsertReserveSnapshot(pair, startTime, WithPriceMultiplier(sdk.NewDec(9))), + perpaction.InsertPosition(perpaction.WithSize(sdk.NewDec(100)), perpaction.WithTrader(alice)), + perpaction.InsertReserveSnapshot(pair, startTime, perpaction.WithPriceMultiplier(sdk.NewDec(9))), ). Then( - PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.MustNewDecFromStr("899.999999910000000009")), + perpassert.PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.MustNewDecFromStr("899.999999910000000009")), ), } - NewTestSuite(t).WithTestCases(tc...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() } func TestUnrealizedPnl(t *testing.T) { diff --git a/x/perp/v2/keeper/clearing_house_test.go b/x/perp/v2/keeper/clearing_house_test.go index f7a33c531..ecc370adf 100644 --- a/x/perp/v2/keeper/clearing_house_test.go +++ b/x/perp/v2/keeper/clearing_house_test.go @@ -13,12 +13,12 @@ import ( "github.com/NibiruChain/nibiru/x/common/asset" "github.com/NibiruChain/nibiru/x/common/denoms" "github.com/NibiruChain/nibiru/x/common/testutil" - . "github.com/NibiruChain/nibiru/x/common/testutil/action" - . "github.com/NibiruChain/nibiru/x/common/testutil/assertion" + tutilaction "github.com/NibiruChain/nibiru/x/common/testutil/action" + tutilassert "github.com/NibiruChain/nibiru/x/common/testutil/assertion" "github.com/NibiruChain/nibiru/x/common/testutil/mock" "github.com/NibiruChain/nibiru/x/common/testutil/testapp" - . "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" - . "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" + perpaction "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" + perpassert "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" types "github.com/NibiruChain/nibiru/x/perp/v2/types" ) @@ -29,44 +29,44 @@ func TestMarketOrder(t *testing.T) { pairBtcNusd := asset.Registry.Pair(denoms.BTC, denoms.NUSD) startBlockTime := time.Now() - tc := TestCases{ - TC("open big short position and then close after reducing swap invariant"). + tc := tutilaction.TestCases{ + tutilaction.TC("open big short position and then close after reducing swap invariant"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pairBtcNusd, - WithPricePeg(sdk.OneDec()), - WithSqrtDepth(sdk.NewDec(100_000)), + perpaction.WithPricePeg(sdk.OneDec()), + perpaction.WithSqrtDepth(sdk.NewDec(100_000)), ), - SetBlockNumber(1), - SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200)))), - FundAccount(bob, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200)))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100_000_000)))), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200)))), + tutilaction.FundAccount(bob, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100_000_000)))), - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000), sdk.OneDec(), sdk.ZeroDec()), - MarketOrder(bob, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000), sdk.OneDec(), sdk.ZeroDec()), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000), sdk.OneDec(), sdk.ZeroDec()), + perpaction.MarketOrder(bob, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000), sdk.OneDec(), sdk.ZeroDec()), - EditSwapInvariant(pairBtcNusd, sdk.OneDec()), + perpaction.EditSwapInvariant(pairBtcNusd, sdk.OneDec()), ). When( - PartialCloseFails(alice, pairBtcNusd, sdk.NewDec(5_000), types.ErrBaseReserveAtZero), + perpaction.PartialCloseFails(alice, pairBtcNusd, sdk.NewDec(5_000), types.ErrBaseReserveAtZero), ). Then( - ClosePosition(bob, pairBtcNusd), - PartialClose(alice, pairBtcNusd, sdk.NewDec(5_000)), + perpaction.ClosePosition(bob, pairBtcNusd), + perpaction.PartialClose(alice, pairBtcNusd, sdk.NewDec(5_000)), ), - TC("new long position"). + tutilaction.TC("new long position"). Given( - CreateCustomMarket(pairBtcNusd), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1020)))), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1020)))), ). When( - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec(), - MarketOrderResp_PositionShouldBeEqual( + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec(), + perpaction.MarketOrderResp_PositionShouldBeEqual( types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -76,18 +76,18 @@ func TestMarketOrder(t *testing.T) { LastUpdatedBlockNumber: 1, LatestCumulativePremiumFraction: sdk.ZeroDec(), }), - MarketOrderResp_ExchangeNotionalValueShouldBeEqual(sdk.NewDec(10_000)), // margin * leverage - MarketOrderResp_ExchangedPositionSizeShouldBeEqual(sdk.MustNewDecFromStr("9999.999900000001")), - MarketOrderResp_BadDebtShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_FundingPaymentShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_RealizedPnlShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_UnrealizedPnlAfterShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_MarginToVaultShouldBeEqual(sdk.NewDec(1000)), - MarketOrderResp_PositionNotionalShouldBeEqual(sdk.NewDec(10_000)), + perpaction.MarketOrderResp_ExchangeNotionalValueShouldBeEqual(sdk.NewDec(10_000)), // margin * leverage + perpaction.MarketOrderResp_ExchangedPositionSizeShouldBeEqual(sdk.MustNewDecFromStr("9999.999900000001")), + perpaction.MarketOrderResp_BadDebtShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_FundingPaymentShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_RealizedPnlShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_UnrealizedPnlAfterShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_MarginToVaultShouldBeEqual(sdk.NewDec(1000)), + perpaction.MarketOrderResp_PositionNotionalShouldBeEqual(sdk.NewDec(10_000)), ), ). Then( - PositionShouldBeEqual(alice, pairBtcNusd, Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), Margin: sdk.NewDec(1000), @@ -96,7 +96,7 @@ func TestMarketOrder(t *testing.T) { LastUpdatedBlockNumber: 1, LatestCumulativePremiumFraction: sdk.ZeroDec(), })), - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -120,18 +120,18 @@ func TestMarketOrder(t *testing.T) { }), ), - TC("existing long position, go more long"). + tutilaction.TC("existing long position, go more long"). Given( - SetBlockNumber(1), - SetBlockTime(startBlockTime), - CreateCustomMarket(pairBtcNusd), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(2040)))), - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(2040)))), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), ). When( - MoveToNextBlock(), - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec(), - MarketOrderResp_PositionShouldBeEqual( + tutilaction.MoveToNextBlock(), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec(), + perpaction.MarketOrderResp_PositionShouldBeEqual( types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -141,18 +141,18 @@ func TestMarketOrder(t *testing.T) { LastUpdatedBlockNumber: 2, LatestCumulativePremiumFraction: sdk.ZeroDec(), }), - MarketOrderResp_ExchangeNotionalValueShouldBeEqual(sdk.NewDec(10_000)), // margin * leverage - MarketOrderResp_ExchangedPositionSizeShouldBeEqual(sdk.MustNewDecFromStr("9999.999700000007000000")), - MarketOrderResp_BadDebtShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_FundingPaymentShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_RealizedPnlShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_UnrealizedPnlAfterShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_MarginToVaultShouldBeEqual(sdk.NewDec(1000)), - MarketOrderResp_PositionNotionalShouldBeEqual(sdk.NewDec(20_000)), + perpaction.MarketOrderResp_ExchangeNotionalValueShouldBeEqual(sdk.NewDec(10_000)), // margin * leverage + perpaction.MarketOrderResp_ExchangedPositionSizeShouldBeEqual(sdk.MustNewDecFromStr("9999.999700000007000000")), + perpaction.MarketOrderResp_BadDebtShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_FundingPaymentShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_RealizedPnlShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_UnrealizedPnlAfterShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_MarginToVaultShouldBeEqual(sdk.NewDec(1000)), + perpaction.MarketOrderResp_PositionNotionalShouldBeEqual(sdk.NewDec(20_000)), ), ). Then( - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -176,32 +176,32 @@ func TestMarketOrder(t *testing.T) { }), ), - TC("existing long position, go more long but there's bad debt"). + tutilaction.TC("existing long position, go more long but there's bad debt"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pairBtcNusd, - WithPricePeg(sdk.MustNewDecFromStr("0.89")), - WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.89")), + perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(18)))), - InsertPosition( - WithPair(pairBtcNusd), - WithTrader(alice), - WithMargin(sdk.NewDec(1_000)), - WithSize(sdk.NewDec(10_000)), - WithOpenNotional(sdk.NewDec(10_000)), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(18)))), + perpaction.InsertPosition( + perpaction.WithPair(pairBtcNusd), + perpaction.WithTrader(alice), + perpaction.WithMargin(sdk.NewDec(1_000)), + perpaction.WithSize(sdk.NewDec(10_000)), + perpaction.WithOpenNotional(sdk.NewDec(10_000)), ), ). When( - MoveToNextBlock(), - MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), + tutilaction.MoveToNextBlock(), + perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), types.ErrMarginRatioTooLow, ), ). Then( - PositionShouldBeEqual(alice, pairBtcNusd, Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ TraderAddress: alice.String(), Pair: pairBtcNusd, Size_: sdk.NewDec(10_000), @@ -212,32 +212,32 @@ func TestMarketOrder(t *testing.T) { })), ), - TC("existing long position, close a bit but there's bad debt"). + tutilaction.TC("existing long position, close a bit but there's bad debt"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pairBtcNusd, - WithPricePeg(sdk.MustNewDecFromStr("0.89")), - WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.89")), + perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(18)))), - InsertPosition( - WithPair(pairBtcNusd), - WithTrader(alice), - WithMargin(sdk.NewDec(1_000)), - WithSize(sdk.NewDec(10_000)), - WithOpenNotional(sdk.NewDec(10_000)), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(18)))), + perpaction.InsertPosition( + perpaction.WithPair(pairBtcNusd), + perpaction.WithTrader(alice), + perpaction.WithMargin(sdk.NewDec(1_000)), + perpaction.WithSize(sdk.NewDec(10_000)), + perpaction.WithOpenNotional(sdk.NewDec(10_000)), ), ). When( - MoveToNextBlock(), - MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), + tutilaction.MoveToNextBlock(), + perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), types.ErrMarginRatioTooLow, ), ). Then( - PositionShouldBeEqual(alice, pairBtcNusd, Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ TraderAddress: alice.String(), Pair: pairBtcNusd, Size_: sdk.NewDec(10_000), @@ -248,39 +248,39 @@ func TestMarketOrder(t *testing.T) { })), ), - TC("open big long position and then close after reducing swap invariant"). + tutilaction.TC("open big long position and then close after reducing swap invariant"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pairBtcNusd, - WithPricePeg(sdk.OneDec()), - WithSqrtDepth(sdk.NewDec(100_000)), + perpaction.WithPricePeg(sdk.OneDec()), + perpaction.WithSqrtDepth(sdk.NewDec(100_000)), ), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_000)))), - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(9_000), sdk.NewDec(10), sdk.ZeroDec()), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100_000_000)))), - EditSwapInvariant(pairBtcNusd, sdk.OneDec()), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_000)))), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(9_000), sdk.NewDec(10), sdk.ZeroDec()), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100_000_000)))), + perpaction.EditSwapInvariant(pairBtcNusd, sdk.OneDec()), ). When( - ClosePosition(alice, pairBtcNusd), + perpaction.ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), ), - TC("existing long position, decrease a bit"). + tutilaction.TC("existing long position, decrease a bit"). Given( - CreateCustomMarket(pairBtcNusd), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1030)))), - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1030)))), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), ). When( - MoveToNextBlock(), - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(500), sdk.NewDec(10), sdk.ZeroDec(), - MarketOrderResp_PositionShouldBeEqual( + tutilaction.MoveToNextBlock(), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(500), sdk.NewDec(10), sdk.ZeroDec(), + perpaction.MarketOrderResp_PositionShouldBeEqual( types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -291,18 +291,18 @@ func TestMarketOrder(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), }, ), - MarketOrderResp_ExchangeNotionalValueShouldBeEqual(sdk.NewDec(5000)), // margin * leverage - MarketOrderResp_ExchangedPositionSizeShouldBeEqual(sdk.MustNewDecFromStr("-4999.999925000000875000")), - MarketOrderResp_BadDebtShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_FundingPaymentShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_RealizedPnlShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_UnrealizedPnlAfterShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_MarginToVaultShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_PositionNotionalShouldBeEqual(sdk.NewDec(5000)), + perpaction.MarketOrderResp_ExchangeNotionalValueShouldBeEqual(sdk.NewDec(5000)), // margin * leverage + perpaction.MarketOrderResp_ExchangedPositionSizeShouldBeEqual(sdk.MustNewDecFromStr("-4999.999925000000875000")), + perpaction.MarketOrderResp_BadDebtShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_FundingPaymentShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_RealizedPnlShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_UnrealizedPnlAfterShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_MarginToVaultShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_PositionNotionalShouldBeEqual(sdk.NewDec(5000)), ), ). Then( - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -326,32 +326,32 @@ func TestMarketOrder(t *testing.T) { }), ), - TC("existing long position, decrease a bit but there's bad debt"). + tutilaction.TC("existing long position, decrease a bit but there's bad debt"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pairBtcNusd, - WithPricePeg(sdk.MustNewDecFromStr("0.89")), - WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.89")), + perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(18)))), - InsertPosition( - WithPair(pairBtcNusd), - WithTrader(alice), - WithMargin(sdk.NewDec(1_000)), - WithSize(sdk.NewDec(10_000)), - WithOpenNotional(sdk.NewDec(10_000)), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(18)))), + perpaction.InsertPosition( + perpaction.WithPair(pairBtcNusd), + perpaction.WithTrader(alice), + perpaction.WithMargin(sdk.NewDec(1_000)), + perpaction.WithSize(sdk.NewDec(10_000)), + perpaction.WithOpenNotional(sdk.NewDec(10_000)), ), ). When( - MoveToNextBlock(), - MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), + tutilaction.MoveToNextBlock(), + perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), types.ErrMarginRatioTooLow, ), ). Then( - PositionShouldBeEqual(alice, pairBtcNusd, Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ TraderAddress: alice.String(), Pair: pairBtcNusd, Size_: sdk.NewDec(10_000), @@ -362,18 +362,18 @@ func TestMarketOrder(t *testing.T) { })), ), - TC("existing long position, decrease a lot"). + tutilaction.TC("existing long position, decrease a lot"). Given( - CreateCustomMarket(pairBtcNusd), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(4080)))), - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(4080)))), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), ). When( - MoveToNextBlock(), - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), - MarketOrderResp_PositionShouldBeEqual( + tutilaction.MoveToNextBlock(), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), + perpaction.MarketOrderResp_PositionShouldBeEqual( types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -384,18 +384,18 @@ func TestMarketOrder(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), }, ), - MarketOrderResp_ExchangeNotionalValueShouldBeEqual(sdk.NewDec(30000)), // margin * leverage - MarketOrderResp_ExchangedPositionSizeShouldBeEqual(sdk.MustNewDecFromStr("-30000.000300000009000000")), - MarketOrderResp_BadDebtShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_FundingPaymentShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_RealizedPnlShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_UnrealizedPnlAfterShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_MarginToVaultShouldBeEqual(sdk.NewDec(1000)), - MarketOrderResp_PositionNotionalShouldBeEqual(sdk.NewDec(20000)), + perpaction.MarketOrderResp_ExchangeNotionalValueShouldBeEqual(sdk.NewDec(30000)), // margin * leverage + perpaction.MarketOrderResp_ExchangedPositionSizeShouldBeEqual(sdk.MustNewDecFromStr("-30000.000300000009000000")), + perpaction.MarketOrderResp_BadDebtShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_FundingPaymentShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_RealizedPnlShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_UnrealizedPnlAfterShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_MarginToVaultShouldBeEqual(sdk.NewDec(1000)), + perpaction.MarketOrderResp_PositionNotionalShouldBeEqual(sdk.NewDec(20000)), ), ). Then( - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -419,28 +419,28 @@ func TestMarketOrder(t *testing.T) { }), ), - TC("existing long position, decrease a lot but there's bad debt"). + tutilaction.TC("existing long position, decrease a lot but there's bad debt"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pairBtcNusd, - WithPricePeg(sdk.MustNewDecFromStr("0.89")), - WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.89")), + perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(18)))), - InsertPosition( - WithPair(pairBtcNusd), - WithTrader(alice), - WithMargin(sdk.NewDec(1_000)), - WithSize(sdk.NewDec(10_000)), - WithOpenNotional(sdk.NewDec(10_000)), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(18)))), + perpaction.InsertPosition( + perpaction.WithPair(pairBtcNusd), + perpaction.WithTrader(alice), + perpaction.WithMargin(sdk.NewDec(1_000)), + perpaction.WithSize(sdk.NewDec(10_000)), + perpaction.WithOpenNotional(sdk.NewDec(10_000)), ), ). When( - MoveToNextBlock(), - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), - MarketOrderResp_PositionShouldBeEqual( + tutilaction.MoveToNextBlock(), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), + perpaction.MarketOrderResp_PositionShouldBeEqual( types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -451,18 +451,18 @@ func TestMarketOrder(t *testing.T) { LatestCumulativePremiumFraction: sdk.MustNewDecFromStr("0.0002"), }, ), - MarketOrderResp_ExchangeNotionalValueShouldBeEqual(sdk.MustNewDecFromStr("8899.999911000000890000")), - MarketOrderResp_ExchangedPositionSizeShouldBeEqual(sdk.MustNewDecFromStr("-10000")), - MarketOrderResp_BadDebtShouldBeEqual(sdk.MustNewDecFromStr("102.000088999999110000")), - MarketOrderResp_FundingPaymentShouldBeEqual(sdk.NewDec(2)), - MarketOrderResp_RealizedPnlShouldBeEqual(sdk.MustNewDecFromStr("-1100.000088999999110000")), - MarketOrderResp_UnrealizedPnlAfterShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_MarginToVaultShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_PositionNotionalShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_ExchangeNotionalValueShouldBeEqual(sdk.MustNewDecFromStr("8899.999911000000890000")), + perpaction.MarketOrderResp_ExchangedPositionSizeShouldBeEqual(sdk.MustNewDecFromStr("-10000")), + perpaction.MarketOrderResp_BadDebtShouldBeEqual(sdk.MustNewDecFromStr("102.000088999999110000")), + perpaction.MarketOrderResp_FundingPaymentShouldBeEqual(sdk.NewDec(2)), + perpaction.MarketOrderResp_RealizedPnlShouldBeEqual(sdk.MustNewDecFromStr("-1100.000088999999110000")), + perpaction.MarketOrderResp_UnrealizedPnlAfterShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_MarginToVaultShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_PositionNotionalShouldBeEqual(sdk.ZeroDec()), ), ). Then( - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -485,16 +485,16 @@ func TestMarketOrder(t *testing.T) { }), ), - TC("new short position"). + tutilaction.TC("new short position"). Given( - CreateCustomMarket(pairBtcNusd), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1020)))), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1020)))), ). When( - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec(), - MarketOrderResp_PositionShouldBeEqual( + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec(), + perpaction.MarketOrderResp_PositionShouldBeEqual( types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -504,19 +504,19 @@ func TestMarketOrder(t *testing.T) { LastUpdatedBlockNumber: 1, LatestCumulativePremiumFraction: sdk.ZeroDec(), }), - MarketOrderResp_ExchangeNotionalValueShouldBeEqual(sdk.NewDec(10_000)), // margin * leverage - MarketOrderResp_ExchangedPositionSizeShouldBeEqual(sdk.MustNewDecFromStr("-10000.000100000001000000")), - MarketOrderResp_BadDebtShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_FundingPaymentShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_RealizedPnlShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_UnrealizedPnlAfterShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_MarginToVaultShouldBeEqual(sdk.NewDec(1000)), - MarketOrderResp_PositionNotionalShouldBeEqual(sdk.NewDec(10_000)), + perpaction.MarketOrderResp_ExchangeNotionalValueShouldBeEqual(sdk.NewDec(10_000)), // margin * leverage + perpaction.MarketOrderResp_ExchangedPositionSizeShouldBeEqual(sdk.MustNewDecFromStr("-10000.000100000001000000")), + perpaction.MarketOrderResp_BadDebtShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_FundingPaymentShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_RealizedPnlShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_UnrealizedPnlAfterShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_MarginToVaultShouldBeEqual(sdk.NewDec(1000)), + perpaction.MarketOrderResp_PositionNotionalShouldBeEqual(sdk.NewDec(10_000)), ), ). Then( - PositionShouldBeEqual(alice, pairBtcNusd, - Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pairBtcNusd, + perpassert.Position_PositionShouldBeEqualTo(types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), Margin: sdk.NewDec(1000), @@ -526,7 +526,7 @@ func TestMarketOrder(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), }), ), - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -550,18 +550,18 @@ func TestMarketOrder(t *testing.T) { }), ), - TC("existing short position, go more short"). + tutilaction.TC("existing short position, go more short"). Given( - CreateCustomMarket(pairBtcNusd), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(2040)))), - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(2040)))), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), ). When( - MoveToNextBlock(), - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec(), - MarketOrderResp_PositionShouldBeEqual( + tutilaction.MoveToNextBlock(), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec(), + perpaction.MarketOrderResp_PositionShouldBeEqual( types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -572,18 +572,18 @@ func TestMarketOrder(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), }, ), - MarketOrderResp_ExchangeNotionalValueShouldBeEqual(sdk.NewDec(10_000)), // margin * leverage - MarketOrderResp_ExchangedPositionSizeShouldBeEqual(sdk.MustNewDecFromStr("-10000.000300000007000000")), - MarketOrderResp_BadDebtShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_FundingPaymentShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_RealizedPnlShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_UnrealizedPnlAfterShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_MarginToVaultShouldBeEqual(sdk.NewDec(1000)), - MarketOrderResp_PositionNotionalShouldBeEqual(sdk.NewDec(20_000)), + perpaction.MarketOrderResp_ExchangeNotionalValueShouldBeEqual(sdk.NewDec(10_000)), // margin * leverage + perpaction.MarketOrderResp_ExchangedPositionSizeShouldBeEqual(sdk.MustNewDecFromStr("-10000.000300000007000000")), + perpaction.MarketOrderResp_BadDebtShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_FundingPaymentShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_RealizedPnlShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_UnrealizedPnlAfterShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_MarginToVaultShouldBeEqual(sdk.NewDec(1000)), + perpaction.MarketOrderResp_PositionNotionalShouldBeEqual(sdk.NewDec(20_000)), ), ). Then( - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -607,31 +607,31 @@ func TestMarketOrder(t *testing.T) { }), ), - TC("existing short position, go more short but there's bad debt"). + tutilaction.TC("existing short position, go more short but there's bad debt"). Given( - CreateCustomMarket(pairBtcNusd, - WithPricePeg(sdk.MustNewDecFromStr("1.11")), - WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + perpaction.CreateCustomMarket(pairBtcNusd, + perpaction.WithPricePeg(sdk.MustNewDecFromStr("1.11")), + perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(22)))), - InsertPosition( - WithPair(pairBtcNusd), - WithTrader(alice), - WithMargin(sdk.NewDec(1_000)), - WithSize(sdk.NewDec(-10_000)), - WithOpenNotional(sdk.NewDec(10_000)), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(22)))), + perpaction.InsertPosition( + perpaction.WithPair(pairBtcNusd), + perpaction.WithTrader(alice), + perpaction.WithMargin(sdk.NewDec(1_000)), + perpaction.WithSize(sdk.NewDec(-10_000)), + perpaction.WithOpenNotional(sdk.NewDec(10_000)), ), ). When( - MoveToNextBlock(), - MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), + tutilaction.MoveToNextBlock(), + perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), types.ErrMarginRatioTooLow, ), ). Then( - PositionShouldBeEqual(alice, pairBtcNusd, Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ TraderAddress: alice.String(), Pair: pairBtcNusd, Size_: sdk.NewDec(-10_000), @@ -642,18 +642,18 @@ func TestMarketOrder(t *testing.T) { })), ), - TC("existing short position, decrease a bit"). + tutilaction.TC("existing short position, decrease a bit"). Given( - CreateCustomMarket(pairBtcNusd), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1030)))), - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1030)))), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), ). When( - MoveToNextBlock(), - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(500), sdk.NewDec(10), sdk.ZeroDec(), - MarketOrderResp_PositionShouldBeEqual( + tutilaction.MoveToNextBlock(), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(500), sdk.NewDec(10), sdk.ZeroDec(), + perpaction.MarketOrderResp_PositionShouldBeEqual( types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -664,18 +664,18 @@ func TestMarketOrder(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), }, ), - MarketOrderResp_ExchangeNotionalValueShouldBeEqual(sdk.NewDec(5000)), // margin * leverage - MarketOrderResp_ExchangedPositionSizeShouldBeEqual(sdk.MustNewDecFromStr("5000.000075000000875000")), - MarketOrderResp_BadDebtShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_FundingPaymentShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_RealizedPnlShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_UnrealizedPnlAfterShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_MarginToVaultShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_PositionNotionalShouldBeEqual(sdk.NewDec(5000)), + perpaction.MarketOrderResp_ExchangeNotionalValueShouldBeEqual(sdk.NewDec(5000)), // margin * leverage + perpaction.MarketOrderResp_ExchangedPositionSizeShouldBeEqual(sdk.MustNewDecFromStr("5000.000075000000875000")), + perpaction.MarketOrderResp_BadDebtShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_FundingPaymentShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_RealizedPnlShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_UnrealizedPnlAfterShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_MarginToVaultShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_PositionNotionalShouldBeEqual(sdk.NewDec(5000)), ), ). Then( - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -699,31 +699,31 @@ func TestMarketOrder(t *testing.T) { }), ), - TC("existing short position, decrease a bit but there's bad debt"). + tutilaction.TC("existing short position, decrease a bit but there's bad debt"). Given( - CreateCustomMarket(pairBtcNusd, - WithPricePeg(sdk.MustNewDecFromStr("1.11")), - WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + perpaction.CreateCustomMarket(pairBtcNusd, + perpaction.WithPricePeg(sdk.MustNewDecFromStr("1.11")), + perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(22)))), - InsertPosition( - WithPair(pairBtcNusd), - WithTrader(alice), - WithMargin(sdk.NewDec(1_000)), - WithSize(sdk.NewDec(-10_000)), - WithOpenNotional(sdk.NewDec(10_000)), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(22)))), + perpaction.InsertPosition( + perpaction.WithPair(pairBtcNusd), + perpaction.WithTrader(alice), + perpaction.WithMargin(sdk.NewDec(1_000)), + perpaction.WithSize(sdk.NewDec(-10_000)), + perpaction.WithOpenNotional(sdk.NewDec(10_000)), ), ). When( - MoveToNextBlock(), - MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), + tutilaction.MoveToNextBlock(), + perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), types.ErrMarginRatioTooLow, ), ). Then( - PositionShouldBeEqual(alice, pairBtcNusd, Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ TraderAddress: alice.String(), Pair: pairBtcNusd, Size_: sdk.NewDec(-10_000), @@ -734,18 +734,18 @@ func TestMarketOrder(t *testing.T) { })), ), - TC("existing short position, decrease a lot"). + tutilaction.TC("existing short position, decrease a lot"). Given( - CreateCustomMarket(pairBtcNusd), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(4080)))), - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(4080)))), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), ). When( - MoveToNextBlock(), - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), - MarketOrderResp_PositionShouldBeEqual( + tutilaction.MoveToNextBlock(), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), + perpaction.MarketOrderResp_PositionShouldBeEqual( types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -756,18 +756,18 @@ func TestMarketOrder(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), }, ), - MarketOrderResp_ExchangeNotionalValueShouldBeEqual(sdk.NewDec(30000)), // margin * leverage - MarketOrderResp_ExchangedPositionSizeShouldBeEqual(sdk.MustNewDecFromStr("29999.999700000009000000")), - MarketOrderResp_BadDebtShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_FundingPaymentShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_RealizedPnlShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_UnrealizedPnlAfterShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_MarginToVaultShouldBeEqual(sdk.NewDec(1000)), - MarketOrderResp_PositionNotionalShouldBeEqual(sdk.NewDec(20000)), + perpaction.MarketOrderResp_ExchangeNotionalValueShouldBeEqual(sdk.NewDec(30000)), // margin * leverage + perpaction.MarketOrderResp_ExchangedPositionSizeShouldBeEqual(sdk.MustNewDecFromStr("29999.999700000009000000")), + perpaction.MarketOrderResp_BadDebtShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_FundingPaymentShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_RealizedPnlShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_UnrealizedPnlAfterShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_MarginToVaultShouldBeEqual(sdk.NewDec(1000)), + perpaction.MarketOrderResp_PositionNotionalShouldBeEqual(sdk.NewDec(20000)), ), ). Then( - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -791,27 +791,27 @@ func TestMarketOrder(t *testing.T) { }), ), - TC("existing short position, decrease a lot but there's bad debt"). + tutilaction.TC("existing short position, decrease a lot but there's bad debt"). Given( - CreateCustomMarket(pairBtcNusd, - WithPricePeg(sdk.MustNewDecFromStr("1.11")), - WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + perpaction.CreateCustomMarket(pairBtcNusd, + perpaction.WithPricePeg(sdk.MustNewDecFromStr("1.11")), + perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(22)))), - InsertPosition( - WithPair(pairBtcNusd), - WithTrader(alice), - WithMargin(sdk.NewDec(1_000)), - WithSize(sdk.NewDec(-10_000)), - WithOpenNotional(sdk.NewDec(10_000)), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(22)))), + perpaction.InsertPosition( + perpaction.WithPair(pairBtcNusd), + perpaction.WithTrader(alice), + perpaction.WithMargin(sdk.NewDec(1_000)), + perpaction.WithSize(sdk.NewDec(-10_000)), + perpaction.WithOpenNotional(sdk.NewDec(10_000)), ), ). When( - MoveToNextBlock(), - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), - MarketOrderResp_PositionShouldBeEqual( + tutilaction.MoveToNextBlock(), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), + perpaction.MarketOrderResp_PositionShouldBeEqual( types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -822,18 +822,18 @@ func TestMarketOrder(t *testing.T) { LatestCumulativePremiumFraction: sdk.MustNewDecFromStr("0.0002"), }, ), - MarketOrderResp_ExchangeNotionalValueShouldBeEqual(sdk.MustNewDecFromStr("11100.000111000001110000")), - MarketOrderResp_ExchangedPositionSizeShouldBeEqual(sdk.MustNewDecFromStr("10000")), - MarketOrderResp_BadDebtShouldBeEqual(sdk.MustNewDecFromStr("98.000111000001110000")), - MarketOrderResp_FundingPaymentShouldBeEqual(sdk.NewDec(-2)), - MarketOrderResp_RealizedPnlShouldBeEqual(sdk.MustNewDecFromStr("-1100.000111000001110000")), - MarketOrderResp_UnrealizedPnlAfterShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_MarginToVaultShouldBeEqual(sdk.ZeroDec()), - MarketOrderResp_PositionNotionalShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_ExchangeNotionalValueShouldBeEqual(sdk.MustNewDecFromStr("11100.000111000001110000")), + perpaction.MarketOrderResp_ExchangedPositionSizeShouldBeEqual(sdk.MustNewDecFromStr("10000")), + perpaction.MarketOrderResp_BadDebtShouldBeEqual(sdk.MustNewDecFromStr("98.000111000001110000")), + perpaction.MarketOrderResp_FundingPaymentShouldBeEqual(sdk.NewDec(-2)), + perpaction.MarketOrderResp_RealizedPnlShouldBeEqual(sdk.MustNewDecFromStr("-1100.000111000001110000")), + perpaction.MarketOrderResp_UnrealizedPnlAfterShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_MarginToVaultShouldBeEqual(sdk.ZeroDec()), + perpaction.MarketOrderResp_PositionNotionalShouldBeEqual(sdk.ZeroDec()), ), ). Then( - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -857,208 +857,208 @@ func TestMarketOrder(t *testing.T) { }), ), - TC("user has insufficient funds"). + tutilaction.TC("user has insufficient funds"). Given( - CreateCustomMarket(pairBtcNusd), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(99)))), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(99)))), ). When( - MarketOrderFails( + perpaction.MarketOrderFails( alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(100), sdk.OneDec(), sdk.ZeroDec(), sdkerrors.ErrInsufficientFunds), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), ), - TC("new long position, can close position after market is not enabled"). + tutilaction.TC("new long position, can close position after market is not enabled"). Given( - CreateCustomMarket(pairBtcNusd), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(47_714_285_715)))), - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(47_619_047_619), sdk.OneDec(), sdk.ZeroDec()), - SetMarketEnabled(pairBtcNusd, false), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(47_714_285_715)))), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(47_619_047_619), sdk.OneDec(), sdk.ZeroDec()), + perpaction.SetMarketEnabled(pairBtcNusd, false), ). When( - ClosePosition(alice, pairBtcNusd), + perpaction.ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), ), - TC("new long position, can not open new position after market is not enabled"). + tutilaction.TC("new long position, can not open new position after market is not enabled"). Given( - CreateCustomMarket(pairBtcNusd), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(47_714_285_715)))), - SetMarketEnabled(pairBtcNusd, false), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(47_714_285_715)))), + perpaction.SetMarketEnabled(pairBtcNusd, false), ). When( - MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(47_619_047_619), sdk.OneDec(), sdk.ZeroDec(), + perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(47_619_047_619), sdk.OneDec(), sdk.ZeroDec(), types.ErrMarketNotEnabled), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), ), - TC("existing long position, can not open new one but can close"). + tutilaction.TC("existing long position, can not open new one but can close"). Given( - CreateCustomMarket(pairBtcNusd), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(47_714_285_715)))), - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(50_000), sdk.OneDec(), sdk.ZeroDec()), - SetMarketEnabled(pairBtcNusd, false), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(47_714_285_715)))), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(50_000), sdk.OneDec(), sdk.ZeroDec()), + perpaction.SetMarketEnabled(pairBtcNusd, false), ). When( - MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(47_619_047_619), sdk.OneDec(), sdk.ZeroDec(), + perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(47_619_047_619), sdk.OneDec(), sdk.ZeroDec(), types.ErrMarketNotEnabled), - ClosePosition(alice, pairBtcNusd), + perpaction.ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), ), - TC("market doesn't exist"). + tutilaction.TC("market doesn't exist"). Given( - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(47_714_285_715)))), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(47_714_285_715)))), ). When( - MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(47_619_047_619), sdk.OneDec(), sdk.ZeroDec(), + perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(47_619_047_619), sdk.OneDec(), sdk.ZeroDec(), types.ErrPairNotFound), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), ), - TC("zero quote asset amount"). + tutilaction.TC("zero quote asset amount"). Given( - SetBlockTime(startBlockTime), - SetBlockNumber(1), - CreateCustomMarket(pairBtcNusd), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(47_714_285_715)))), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(47_714_285_715)))), ). When( - MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.ZeroInt(), sdk.OneDec(), sdk.ZeroDec(), + perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.ZeroInt(), sdk.OneDec(), sdk.ZeroDec(), types.ErrInputQuoteAmtNegative), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), ), - TC("zero leverage"). + tutilaction.TC("zero leverage"). Given( - SetBlockTime(startBlockTime), - SetBlockNumber(1), - CreateCustomMarket(pairBtcNusd), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.ZeroDec(), sdk.ZeroDec(), + perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.ZeroDec(), sdk.ZeroDec(), types.ErrUserLeverageNegative), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), ), - TC("user leverage greater than market max leverage"). + tutilaction.TC("user leverage greater than market max leverage"). Given( - SetBlockTime(startBlockTime), - SetBlockNumber(1), - CreateCustomMarket(pairBtcNusd), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.NewDec(11), sdk.ZeroDec(), + perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.NewDec(11), sdk.ZeroDec(), types.ErrLeverageIsTooHigh), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), ), - TC("position should not exist after opening a closing manually"). + tutilaction.TC("position should not exist after opening a closing manually"). Given( - SetBlockTime(startBlockTime), - SetBlockNumber(1), - CreateCustomMarket(pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("25001.0112"))), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(20_000_000_000+20_000_000)))), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + perpaction.CreateCustomMarket(pairBtcNusd, perpaction.WithPricePeg(sdk.MustNewDecFromStr("25001.0112"))), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(20_000_000_000+20_000_000)))), ). When( - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), ), - TC("position should not exist after opening a closing manually - reverse with leverage"). + tutilaction.TC("position should not exist after opening a closing manually - reverse with leverage"). Given( - SetBlockTime(startBlockTime), - SetBlockNumber(1), - CreateCustomMarket(pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("25001.0112"))), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + perpaction.CreateCustomMarket(pairBtcNusd, perpaction.WithPricePeg(sdk.MustNewDecFromStr("25001.0112"))), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(100_000), sdk.OneDec(), sdk.ZeroDec()), - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(50_000), sdk.NewDec(2), sdk.ZeroDec()), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(100_000), sdk.OneDec(), sdk.ZeroDec()), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(50_000), sdk.NewDec(2), sdk.ZeroDec()), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), ), - TC("position should not exist after opening a closing manually - open with leverage"). + tutilaction.TC("position should not exist after opening a closing manually - open with leverage"). Given( - SetBlockTime(startBlockTime), - SetBlockNumber(1), - CreateCustomMarket(pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("25001.0112"))), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + perpaction.CreateCustomMarket(pairBtcNusd, perpaction.WithPricePeg(sdk.MustNewDecFromStr("25001.0112"))), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(50_000), sdk.NewDec(2), sdk.ZeroDec()), - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(100_000), sdk.OneDec(), sdk.ZeroDec()), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(50_000), sdk.NewDec(2), sdk.ZeroDec()), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(100_000), sdk.OneDec(), sdk.ZeroDec()), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), ), - TC("position should not exist after opening a closing manually - reverse with leverage"). + tutilaction.TC("position should not exist after opening a closing manually - reverse with leverage"). Given( - SetBlockTime(startBlockTime), - SetBlockNumber(1), - CreateCustomMarket(pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("25001.0112"))), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + perpaction.CreateCustomMarket(pairBtcNusd, perpaction.WithPricePeg(sdk.MustNewDecFromStr("25001.0112"))), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(100_000), sdk.OneDec(), sdk.ZeroDec()), - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(50_000), sdk.NewDec(2), sdk.ZeroDec()), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(100_000), sdk.OneDec(), sdk.ZeroDec()), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(50_000), sdk.NewDec(2), sdk.ZeroDec()), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), ), - TC("position should not exist after opening a closing manually - reverse with leverage - more steps"). + tutilaction.TC("position should not exist after opening a closing manually - reverse with leverage - more steps"). Given( - SetBlockTime(startBlockTime), - SetBlockNumber(1), - CreateCustomMarket(pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("25000"))), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + perpaction.CreateCustomMarket(pairBtcNusd, perpaction.WithPricePeg(sdk.MustNewDecFromStr("25000"))), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(100_000), sdk.OneDec(), sdk.ZeroDec()), - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(50_000), sdk.NewDec(4), sdk.ZeroDec()), - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(50_000), sdk.NewDec(2), sdk.ZeroDec()), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(100_000), sdk.OneDec(), sdk.ZeroDec()), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(50_000), sdk.NewDec(4), sdk.ZeroDec()), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(50_000), sdk.NewDec(2), sdk.ZeroDec()), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), ), } - NewTestSuite(t).WithTestCases(tc...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() } func TestMarketOrderError(t *testing.T) { @@ -1199,30 +1199,30 @@ func TestPartialClose(t *testing.T) { pairBtcNusd := asset.Registry.Pair(denoms.BTC, denoms.NUSD) startBlockTime := time.Now() - tc := TestCases{ - TC("partial close long position with positive PnL"). + tc := tutilaction.TestCases{ + tutilaction.TC("partial close long position with positive PnL"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pairBtcNusd, - WithPricePeg(sdk.NewDec(2)), - WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + perpaction.WithPricePeg(sdk.NewDec(2)), + perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10)))), - InsertPosition( - WithPair(pairBtcNusd), - WithTrader(alice), - WithSize(sdk.NewDec(10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10)))), + perpaction.InsertPosition( + perpaction.WithPair(pairBtcNusd), + perpaction.WithTrader(alice), + perpaction.WithSize(sdk.NewDec(10_000)), + perpaction.WithMargin(sdk.NewDec(1_000)), + perpaction.WithOpenNotional(sdk.NewDec(10_000)), ), ). When( - PartialClose(alice, pairBtcNusd, sdk.NewDec(2_500)), + perpaction.PartialClose(alice, pairBtcNusd, sdk.NewDec(2_500)), ). Then( - PositionShouldBeEqual(alice, pairBtcNusd, Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), Margin: sdk.MustNewDecFromStr("3497.999950000000500000"), @@ -1231,7 +1231,7 @@ func TestPartialClose(t *testing.T) { LastUpdatedBlockNumber: 1, LatestCumulativePremiumFraction: sdk.MustNewDecFromStr("0.0002"), })), - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -1254,29 +1254,29 @@ func TestPartialClose(t *testing.T) { }), ), - TC("partial close long position with negative PnL"). + tutilaction.TC("partial close long position with negative PnL"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pairBtcNusd, - WithPricePeg(sdk.MustNewDecFromStr("0.95")), - WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.95")), + perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(4)))), - InsertPosition( - WithPair(pairBtcNusd), - WithTrader(alice), - WithSize(sdk.NewDec(10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(4)))), + perpaction.InsertPosition( + perpaction.WithPair(pairBtcNusd), + perpaction.WithTrader(alice), + perpaction.WithSize(sdk.NewDec(10_000)), + perpaction.WithMargin(sdk.NewDec(1_000)), + perpaction.WithOpenNotional(sdk.NewDec(10_000)), ), ). When( - PartialClose(alice, pairBtcNusd, sdk.NewDec(2_500)), + perpaction.PartialClose(alice, pairBtcNusd, sdk.NewDec(2_500)), ). Then( - PositionShouldBeEqual(alice, pairBtcNusd, Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), Margin: sdk.MustNewDecFromStr("872.999976250000237500"), @@ -1285,7 +1285,7 @@ func TestPartialClose(t *testing.T) { LastUpdatedBlockNumber: 1, LatestCumulativePremiumFraction: sdk.MustNewDecFromStr("0.0002"), })), - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -1308,29 +1308,29 @@ func TestPartialClose(t *testing.T) { }), ), - TC("partial close long position without bad debt but below maintenance margin ratio"). + tutilaction.TC("partial close long position without bad debt but below maintenance margin ratio"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pairBtcNusd, - WithPricePeg(sdk.MustNewDecFromStr("0.94")), - WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.94")), + perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(4)))), - InsertPosition( - WithPair(pairBtcNusd), - WithTrader(alice), - WithSize(sdk.NewDec(10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(4)))), + perpaction.InsertPosition( + perpaction.WithPair(pairBtcNusd), + perpaction.WithTrader(alice), + perpaction.WithSize(sdk.NewDec(10_000)), + perpaction.WithMargin(sdk.NewDec(1_000)), + perpaction.WithOpenNotional(sdk.NewDec(10_000)), ), ). When( - PartialClose(alice, pairBtcNusd, sdk.NewDec(2_500)), + perpaction.PartialClose(alice, pairBtcNusd, sdk.NewDec(2_500)), ). Then( - PositionShouldBeEqual(alice, pairBtcNusd, Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), Margin: sdk.MustNewDecFromStr("847.999976500000235000"), @@ -1339,7 +1339,7 @@ func TestPartialClose(t *testing.T) { LastUpdatedBlockNumber: 1, LatestCumulativePremiumFraction: sdk.MustNewDecFromStr("0.0002"), })), - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -1361,30 +1361,30 @@ func TestPartialClose(t *testing.T) { ExchangedSize: sdk.MustNewDecFromStr("-2500.000000000000000001"), })), - TC("partial close long position with bad debt"). + tutilaction.TC("partial close long position with bad debt"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pairBtcNusd, - WithPricePeg(sdk.MustNewDecFromStr("0.59")), - WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.59")), + perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 2))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 27))), - InsertPosition( - WithPair(pairBtcNusd), - WithTrader(alice), - WithSize(sdk.NewDec(10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 2))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 27))), + perpaction.InsertPosition( + perpaction.WithPair(pairBtcNusd), + perpaction.WithTrader(alice), + perpaction.WithSize(sdk.NewDec(10_000)), + perpaction.WithMargin(sdk.NewDec(1_000)), + perpaction.WithOpenNotional(sdk.NewDec(10_000)), ), ). When( - PartialClose(alice, pairBtcNusd, sdk.NewDec(2_500)), + perpaction.PartialClose(alice, pairBtcNusd, sdk.NewDec(2_500)), ). Then( - PositionShouldBeEqual(alice, pairBtcNusd, Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), Margin: sdk.ZeroDec(), @@ -1393,7 +1393,7 @@ func TestPartialClose(t *testing.T) { LastUpdatedBlockNumber: 1, LatestCumulativePremiumFraction: sdk.MustNewDecFromStr("0.0002"), })), - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -1415,29 +1415,29 @@ func TestPartialClose(t *testing.T) { ExchangedSize: sdk.MustNewDecFromStr("-2500.000000000000000000"), })), - TC("partial close short position with positive PnL"). + tutilaction.TC("partial close short position with positive PnL"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pairBtcNusd, - WithPricePeg(sdk.MustNewDecFromStr("0.10")), - WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.10")), + perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(2)))), - InsertPosition( - WithPair(pairBtcNusd), - WithTrader(alice), - WithSize(sdk.NewDec(-10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(2)))), + perpaction.InsertPosition( + perpaction.WithPair(pairBtcNusd), + perpaction.WithTrader(alice), + perpaction.WithSize(sdk.NewDec(-10_000)), + perpaction.WithMargin(sdk.NewDec(1_000)), + perpaction.WithOpenNotional(sdk.NewDec(10_000)), ), ). When( - PartialClose(alice, pairBtcNusd, sdk.NewDec(7_500)), + perpaction.PartialClose(alice, pairBtcNusd, sdk.NewDec(7_500)), ). Then( - PositionShouldBeEqual(alice, pairBtcNusd, Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), Margin: sdk.MustNewDecFromStr("7751.999992499999925000"), @@ -1446,7 +1446,7 @@ func TestPartialClose(t *testing.T) { LastUpdatedBlockNumber: 1, LatestCumulativePremiumFraction: sdk.MustNewDecFromStr("0.0002"), })), - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -1469,29 +1469,29 @@ func TestPartialClose(t *testing.T) { }), ), - TC("partial close short position with negative PnL"). + tutilaction.TC("partial close short position with negative PnL"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pairBtcNusd, - WithPricePeg(sdk.MustNewDecFromStr("1.05")), - WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + perpaction.WithPricePeg(sdk.MustNewDecFromStr("1.05")), + perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(16)))), - InsertPosition( - WithPair(pairBtcNusd), - WithTrader(alice), - WithSize(sdk.NewDec(-10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(16)))), + perpaction.InsertPosition( + perpaction.WithPair(pairBtcNusd), + perpaction.WithTrader(alice), + perpaction.WithSize(sdk.NewDec(-10_000)), + perpaction.WithMargin(sdk.NewDec(1_000)), + perpaction.WithOpenNotional(sdk.NewDec(10_000)), ), ). When( - PartialClose(alice, pairBtcNusd, sdk.NewDec(7_500)), + perpaction.PartialClose(alice, pairBtcNusd, sdk.NewDec(7_500)), ). Then( - PositionShouldBeEqual(alice, pairBtcNusd, Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), Margin: sdk.MustNewDecFromStr("626.999921249999212500"), @@ -1500,7 +1500,7 @@ func TestPartialClose(t *testing.T) { LastUpdatedBlockNumber: 1, LatestCumulativePremiumFraction: sdk.MustNewDecFromStr("0.0002"), })), - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -1523,29 +1523,29 @@ func TestPartialClose(t *testing.T) { }), ), - TC("partial close short position with no bad debt but below maintenance margin ratio"). + tutilaction.TC("partial close short position with no bad debt but below maintenance margin ratio"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pairBtcNusd, - WithPricePeg(sdk.MustNewDecFromStr("1.09")), - WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + perpaction.WithPricePeg(sdk.MustNewDecFromStr("1.09")), + perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(16)))), - InsertPosition( - WithPair(pairBtcNusd), - WithTrader(alice), - WithSize(sdk.NewDec(-10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(16)))), + perpaction.InsertPosition( + perpaction.WithPair(pairBtcNusd), + perpaction.WithTrader(alice), + perpaction.WithSize(sdk.NewDec(-10_000)), + perpaction.WithMargin(sdk.NewDec(1_000)), + perpaction.WithOpenNotional(sdk.NewDec(10_000)), ), ). When( - PartialClose(alice, pairBtcNusd, sdk.NewDec(7_500)), + perpaction.PartialClose(alice, pairBtcNusd, sdk.NewDec(7_500)), ). Then( - PositionShouldBeEqual(alice, pairBtcNusd, Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), Margin: sdk.MustNewDecFromStr("326.999918249999182500"), @@ -1554,7 +1554,7 @@ func TestPartialClose(t *testing.T) { LastUpdatedBlockNumber: 1, LatestCumulativePremiumFraction: sdk.MustNewDecFromStr("0.0002"), })), - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -1577,30 +1577,30 @@ func TestPartialClose(t *testing.T) { }), ), - TC("partial close short position with bad debt"). + tutilaction.TC("partial close short position with bad debt"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pairBtcNusd, - WithPricePeg(sdk.MustNewDecFromStr("1.14")), - WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + perpaction.WithPricePeg(sdk.MustNewDecFromStr("1.14")), + perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(18)))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 48))), - InsertPosition( - WithPair(pairBtcNusd), - WithTrader(alice), - WithSize(sdk.NewDec(-10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(18)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 48))), + perpaction.InsertPosition( + perpaction.WithPair(pairBtcNusd), + perpaction.WithTrader(alice), + perpaction.WithSize(sdk.NewDec(-10_000)), + perpaction.WithMargin(sdk.NewDec(1_000)), + perpaction.WithOpenNotional(sdk.NewDec(10_000)), ), ). When( - PartialClose(alice, pairBtcNusd, sdk.NewDec(7_500)), + perpaction.PartialClose(alice, pairBtcNusd, sdk.NewDec(7_500)), ). Then( - PositionShouldBeEqual(alice, pairBtcNusd, Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), Margin: sdk.ZeroDec(), @@ -1609,7 +1609,7 @@ func TestPartialClose(t *testing.T) { LastUpdatedBlockNumber: 1, LatestCumulativePremiumFraction: sdk.MustNewDecFromStr("0.0002"), })), - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -1631,27 +1631,27 @@ func TestPartialClose(t *testing.T) { ExchangedSize: sdk.MustNewDecFromStr("7500.000000000000000000"), }), ), - TC("test partial closes fail"). + tutilaction.TC("test partial closes fail"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pairBtcNusd, - WithPricePeg(sdk.OneDec()), - WithSqrtDepth(sdk.NewDec(10_000)), + perpaction.WithPricePeg(sdk.OneDec()), + perpaction.WithSqrtDepth(sdk.NewDec(10_000)), ), - SetBlockNumber(1), - SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200)))), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200)))), - PartialCloseFails(alice, pairBtcNusd, sdk.NewDec(5_000), collections.ErrNotFound), - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(9_000), sdk.OneDec(), sdk.ZeroDec()), + perpaction.PartialCloseFails(alice, pairBtcNusd, sdk.NewDec(5_000), collections.ErrNotFound), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(9_000), sdk.OneDec(), sdk.ZeroDec()), ). When( - PartialCloseFails(alice, asset.MustNewPair("luna:usdt"), sdk.NewDec(5_000), types.ErrPairNotFound), + perpaction.PartialCloseFails(alice, asset.MustNewPair("luna:usdt"), sdk.NewDec(5_000), types.ErrPairNotFound), ), } - NewTestSuite(t).WithTestCases(tc...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() } func TestClosePosition(t *testing.T) { @@ -1659,31 +1659,31 @@ func TestClosePosition(t *testing.T) { pairBtcNusd := asset.Registry.Pair(denoms.BTC, denoms.NUSD) startBlockTime := time.Now() - tc := TestCases{ - TC("close long position with positive PnL"). + tc := tutilaction.TestCases{ + tutilaction.TC("close long position with positive PnL"). Given( - CreateCustomMarket(pairBtcNusd, - WithPricePeg(sdk.NewDec(2)), - WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + perpaction.CreateCustomMarket(pairBtcNusd, + perpaction.WithPricePeg(sdk.NewDec(2)), + perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(40)))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 10_998))), - InsertPosition( - WithTrader(alice), - WithPair(pairBtcNusd), - WithSize(sdk.NewDec(10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(40)))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 10_998))), + perpaction.InsertPosition( + perpaction.WithTrader(alice), + perpaction.WithPair(pairBtcNusd), + perpaction.WithSize(sdk.NewDec(10_000)), + perpaction.WithMargin(sdk.NewDec(1_000)), + perpaction.WithOpenNotional(sdk.NewDec(10_000)), ), ). When( - ClosePosition(alice, pairBtcNusd), + perpaction.ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -1706,30 +1706,30 @@ func TestClosePosition(t *testing.T) { }), ), - TC("close long position with negative PnL"). + tutilaction.TC("close long position with negative PnL"). Given( - CreateCustomMarket(pairBtcNusd, - WithPricePeg(sdk.MustNewDecFromStr("0.99")), - WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + perpaction.CreateCustomMarket(pairBtcNusd, + perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.99")), + perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(20)))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 898))), - InsertPosition( - WithTrader(alice), - WithPair(pairBtcNusd), - WithSize(sdk.NewDec(10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(20)))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 898))), + perpaction.InsertPosition( + perpaction.WithTrader(alice), + perpaction.WithPair(pairBtcNusd), + perpaction.WithSize(sdk.NewDec(10_000)), + perpaction.WithMargin(sdk.NewDec(1_000)), + perpaction.WithOpenNotional(sdk.NewDec(10_000)), ), ). When( - ClosePosition(alice, pairBtcNusd), + perpaction.ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -1752,32 +1752,32 @@ func TestClosePosition(t *testing.T) { }), ), - TC("close long position with bad debt"). + tutilaction.TC("close long position with bad debt"). Given( - CreateCustomMarket(pairBtcNusd, - WithPricePeg(sdk.MustNewDecFromStr("0.89")), - WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + perpaction.CreateCustomMarket(pairBtcNusd, + perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.89")), + perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - InsertPosition( - WithTrader(alice), - WithPair(pairBtcNusd), - WithSize(sdk.NewDec(10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + perpaction.InsertPosition( + perpaction.WithTrader(alice), + perpaction.WithPair(pairBtcNusd), + perpaction.WithSize(sdk.NewDec(10_000)), + perpaction.WithMargin(sdk.NewDec(1_000)), + perpaction.WithOpenNotional(sdk.NewDec(10_000)), ), - FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 18))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 1000))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 102))), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 18))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 1000))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 102))), ). When( - MoveToNextBlock(), - ClosePosition(alice, pairBtcNusd), + tutilaction.MoveToNextBlock(), + perpaction.ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ TraderAddress: alice.String(), Pair: pairBtcNusd, @@ -1798,34 +1798,34 @@ func TestClosePosition(t *testing.T) { ExchangedNotional: sdk.MustNewDecFromStr("-10000.000000000000000000"), ExchangedSize: sdk.MustNewDecFromStr("-10000.000000000000000000"), }), - ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1102)), // 1000 + 102 from perp ef - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(9)), + tutilassert.ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1102)), // 1000 + 102 from perp ef + tutilassert.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(9)), ), - TC("close short position with positive PnL"). + tutilaction.TC("close short position with positive PnL"). Given( - CreateCustomMarket(pairBtcNusd, - WithPricePeg(sdk.MustNewDecFromStr("0.10")), - WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + perpaction.CreateCustomMarket(pairBtcNusd, + perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.10")), + perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(2)))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 10_002))), - InsertPosition( - WithTrader(alice), - WithPair(pairBtcNusd), - WithSize(sdk.NewDec(-10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(2)))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 10_002))), + perpaction.InsertPosition( + perpaction.WithTrader(alice), + perpaction.WithPair(pairBtcNusd), + perpaction.WithSize(sdk.NewDec(-10_000)), + perpaction.WithMargin(sdk.NewDec(1_000)), + perpaction.WithOpenNotional(sdk.NewDec(10_000)), ), ). When( - ClosePosition(alice, pairBtcNusd), + perpaction.ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -1848,30 +1848,30 @@ func TestClosePosition(t *testing.T) { }), ), - TC("close short position with negative PnL"). + tutilaction.TC("close short position with negative PnL"). Given( - CreateCustomMarket(pairBtcNusd, - WithPricePeg(sdk.MustNewDecFromStr("1.01")), - WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + perpaction.CreateCustomMarket(pairBtcNusd, + perpaction.WithPricePeg(sdk.MustNewDecFromStr("1.01")), + perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(20)))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 902))), - InsertPosition( - WithTrader(alice), - WithPair(pairBtcNusd), - WithSize(sdk.NewDec(-10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(20)))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 902))), + perpaction.InsertPosition( + perpaction.WithTrader(alice), + perpaction.WithPair(pairBtcNusd), + perpaction.WithSize(sdk.NewDec(-10_000)), + perpaction.WithMargin(sdk.NewDec(1_000)), + perpaction.WithOpenNotional(sdk.NewDec(10_000)), ), ). When( - ClosePosition(alice, pairBtcNusd), + perpaction.ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -1894,32 +1894,32 @@ func TestClosePosition(t *testing.T) { }), ), - TC("close short position with bad debt"). + tutilaction.TC("close short position with bad debt"). Given( - CreateCustomMarket(pairBtcNusd, - WithPricePeg(sdk.MustNewDecFromStr("1.11")), - WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + perpaction.CreateCustomMarket(pairBtcNusd, + perpaction.WithPricePeg(sdk.MustNewDecFromStr("1.11")), + perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - InsertPosition( - WithTrader(alice), - WithPair(pairBtcNusd), - WithSize(sdk.NewDec(-10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + perpaction.InsertPosition( + perpaction.WithTrader(alice), + perpaction.WithPair(pairBtcNusd), + perpaction.WithSize(sdk.NewDec(-10_000)), + perpaction.WithMargin(sdk.NewDec(1_000)), + perpaction.WithOpenNotional(sdk.NewDec(10_000)), ), - FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 22))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 1000))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 98))), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 22))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 1000))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 98))), ). When( - MoveToNextBlock(), - ClosePosition(alice, pairBtcNusd), + tutilaction.MoveToNextBlock(), + perpaction.ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ TraderAddress: alice.String(), Pair: pairBtcNusd, @@ -1940,12 +1940,12 @@ func TestClosePosition(t *testing.T) { ExchangedNotional: sdk.MustNewDecFromStr("-10000.000000000000000000"), ExchangedSize: sdk.MustNewDecFromStr("10000.000000000000000000"), }), - ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1098)), // 1000 + 98 from perp ef - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(11)), + tutilassert.ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1098)), // 1000 + 98 from perp ef + tutilassert.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(11)), ), } - NewTestSuite(t).WithTestCases(tc...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() } func TestUpdateSwapInvariant(t *testing.T) { @@ -1956,182 +1956,182 @@ func TestUpdateSwapInvariant(t *testing.T) { startingSwapInvariant := sdk.NewDec(1_000_000_000_000).Mul(sdk.NewDec(1_000_000_000_000)) - tc := TestCases{ - TC("only long position - no change to swap invariant"). + tc := tutilaction.TestCases{ + tutilaction.TC("only long position - no change to swap invariant"). Given( - CreateCustomMarket(pairBtcNusd), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100000)))), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100000)))), ). When( - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), - ClosePosition(alice, pairBtcNusd), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), + perpaction.ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), ), - TC("only short position - no change to swap invariant"). + tutilaction.TC("only short position - no change to swap invariant"). Given( - CreateCustomMarket(pairBtcNusd), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100000)))), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100000)))), ). When( - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.NewDec(10_000_000_000_000)), - ClosePosition(alice, pairBtcNusd), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.NewDec(10_000_000_000_000)), + perpaction.ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), ), - TC("only long position - increasing k"). + tutilaction.TC("only long position - increasing k"). Given( - CreateCustomMarket(pairBtcNusd), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100_000_000)))), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100_000_000)))), ). When( - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), - EditSwapInvariant(pairBtcNusd, startingSwapInvariant.MulInt64(100)), - AMMShouldBeEqual( + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), + perpaction.EditSwapInvariant(pairBtcNusd, startingSwapInvariant.MulInt64(100)), + perpassert.AMMShouldBeEqual( pairBtcNusd, - AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999999999999.999999000000000000"))), - ClosePosition(alice, pairBtcNusd), + perpassert.AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999999999999.999999000000000000"))), + perpaction.ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), - ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), + perpassert.ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), ), - TC("only short position - increasing k"). + tutilaction.TC("only short position - increasing k"). Given( - CreateCustomMarket(pairBtcNusd), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100_000_000)))), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100_000_000)))), ). When( - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), - EditSwapInvariant(pairBtcNusd, startingSwapInvariant.MulInt64(100)), - AMMShouldBeEqual( + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), + perpaction.EditSwapInvariant(pairBtcNusd, startingSwapInvariant.MulInt64(100)), + perpassert.AMMShouldBeEqual( pairBtcNusd, - AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999999999999.999999000000000000"))), - ClosePosition(alice, pairBtcNusd), + perpassert.AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999999999999.999999000000000000"))), + perpaction.ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), - ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.OneInt()))), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), + perpassert.ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.OneInt()))), ), - TC("only long position - decreasing k"). + tutilaction.TC("only long position - decreasing k"). Given( - CreateCustomMarket(pairBtcNusd), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100_000_000)))), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100_000_000)))), ). When( - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), - EditSwapInvariant(pairBtcNusd, startingSwapInvariant.Mul(sdk.MustNewDecFromStr("0.1"))), - AMMShouldBeEqual( + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), + perpaction.EditSwapInvariant(pairBtcNusd, startingSwapInvariant.Mul(sdk.MustNewDecFromStr("0.1"))), + perpassert.AMMShouldBeEqual( pairBtcNusd, - AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999873578.871987715651277660"))), - ClosePosition(alice, pairBtcNusd), + perpassert.AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999873578.871987715651277660"))), + perpaction.ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), - ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), + perpassert.ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), ), - TC("only short position - decreasing k"). + tutilaction.TC("only short position - decreasing k"). Given( - CreateCustomMarket(pairBtcNusd), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100_000_000)))), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100_000_000)))), ). When( - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), - EditSwapInvariant(pairBtcNusd, startingSwapInvariant.Mul(sdk.MustNewDecFromStr("0.1"))), - AMMShouldBeEqual( + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), + perpaction.EditSwapInvariant(pairBtcNusd, startingSwapInvariant.Mul(sdk.MustNewDecFromStr("0.1"))), + perpassert.AMMShouldBeEqual( pairBtcNusd, - AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999873578.871987801032774485"))), - ClosePosition(alice, pairBtcNusd), + perpassert.AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999873578.871987801032774485"))), + perpaction.ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), - ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), + perpassert.ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), ), - TC("long and short position - increasing k"). + tutilaction.TC("long and short position - increasing k"). Given( - CreateCustomMarket(pairBtcNusd), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))), - FundAccount(bob, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))), + tutilaction.FundAccount(bob, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))), ). When( - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), - MarketOrder(bob, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.NewDec(10_000_000_000_000)), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), + perpaction.MarketOrder(bob, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.NewDec(10_000_000_000_000)), - EditSwapInvariant(pairBtcNusd, startingSwapInvariant.MulInt64(100)), - AMMShouldBeEqual( + perpaction.EditSwapInvariant(pairBtcNusd, startingSwapInvariant.MulInt64(100)), + perpassert.AMMShouldBeEqual( pairBtcNusd, - AMM_BiasShouldBeEqual(sdk.ZeroDec()), - AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("100000000000000000000000000.000000000000000000"))), + perpassert.AMM_BiasShouldBeEqual(sdk.ZeroDec()), + perpassert.AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("100000000000000000000000000.000000000000000000"))), - ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(20_000_000_000)))), - ModuleBalanceShouldBeEqualTo(types.FeePoolModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(20_000_000)))), // Fees are 10_000_000_000 * 0.0010 * 2 + perpassert.ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(20_000_000_000)))), + perpassert.ModuleBalanceShouldBeEqualTo(types.FeePoolModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(20_000_000)))), // Fees are 10_000_000_000 * 0.0010 * 2 - ClosePosition(alice, pairBtcNusd), - ClosePosition(bob, pairBtcNusd), + perpaction.ClosePosition(alice, pairBtcNusd), + perpaction.ClosePosition(bob, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), - PositionShouldNotExist(bob, pairBtcNusd), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionShouldNotExist(bob, pairBtcNusd), - ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), - ModuleBalanceShouldBeEqualTo(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(39_782_394)))), + perpassert.ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), + perpassert.ModuleBalanceShouldBeEqualTo(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(39_782_394)))), ), - TC("long and short position - reducing k"). + tutilaction.TC("long and short position - reducing k"). Given( - CreateCustomMarket(pairBtcNusd), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))), - FundAccount(bob, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))), + perpaction.CreateCustomMarket(pairBtcNusd), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))), + tutilaction.FundAccount(bob, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200_000_000)))), ). When( - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), - MarketOrder(bob, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.NewDec(10_000_000_000_000)), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), + perpaction.MarketOrder(bob, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.NewDec(10_000_000_000_000)), - EditSwapInvariant(pairBtcNusd, startingSwapInvariant.Mul(sdk.MustNewDecFromStr("0.1"))), - AMMShouldBeEqual( + perpaction.EditSwapInvariant(pairBtcNusd, startingSwapInvariant.Mul(sdk.MustNewDecFromStr("0.1"))), + perpassert.AMMShouldBeEqual( pairBtcNusd, - AMM_BiasShouldBeEqual(sdk.ZeroDec()), - AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999873578.871987712489000000"))), + perpassert.AMM_BiasShouldBeEqual(sdk.ZeroDec()), + perpassert.AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999873578.871987712489000000"))), - ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(20_000_000_000)))), - ModuleBalanceShouldBeEqualTo(types.FeePoolModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(20_000_000)))), // Fees are 10_000_000_000 * 0.0010 * 2 + perpassert.ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(20_000_000_000)))), + perpassert.ModuleBalanceShouldBeEqualTo(types.FeePoolModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(20_000_000)))), // Fees are 10_000_000_000 * 0.0010 * 2 - ClosePosition(alice, pairBtcNusd), - ClosePosition(bob, pairBtcNusd), + perpaction.ClosePosition(alice, pairBtcNusd), + perpaction.ClosePosition(bob, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), - PositionShouldNotExist(bob, pairBtcNusd), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionShouldNotExist(bob, pairBtcNusd), - ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), - ModuleBalanceShouldBeEqualTo(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(39_200_810)))), + perpassert.ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), + perpassert.ModuleBalanceShouldBeEqualTo(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(39_200_810)))), ), } - NewTestSuite(t).WithTestCases(tc...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() } diff --git a/x/perp/v2/keeper/grpc_query_test.go b/x/perp/v2/keeper/grpc_query_test.go index d01280814..86d9f894d 100644 --- a/x/perp/v2/keeper/grpc_query_test.go +++ b/x/perp/v2/keeper/grpc_query_test.go @@ -10,8 +10,8 @@ import ( "github.com/NibiruChain/nibiru/x/common/asset" "github.com/NibiruChain/nibiru/x/common/denoms" "github.com/NibiruChain/nibiru/x/common/testutil" - . "github.com/NibiruChain/nibiru/x/common/testutil/action" - . "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" + tutilaction "github.com/NibiruChain/nibiru/x/common/testutil/action" + perpaction "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" types "github.com/NibiruChain/nibiru/x/perp/v2/types" ) @@ -20,38 +20,38 @@ func TestQueryPositions(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) pair2 := asset.Registry.Pair(denoms.ETH, denoms.NUSD) - tc := TestCases{ - TC("positive PnL"). + tc := tutilaction.TestCases{ + tutilaction.TC("positive PnL"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pair, - WithPricePeg(sdk.NewDec(2)), + perpaction.WithPricePeg(sdk.NewDec(2)), ), - CreateCustomMarket( + perpaction.CreateCustomMarket( pair2, - WithPricePeg(sdk.NewDec(3)), + perpaction.WithPricePeg(sdk.NewDec(3)), ), ). When( - InsertPosition( - WithPair(pair), - WithTrader(alice), - WithMargin(sdk.OneDec()), - WithSize(sdk.NewDec(10)), - WithOpenNotional(sdk.NewDec(10)), + perpaction.InsertPosition( + perpaction.WithPair(pair), + perpaction.WithTrader(alice), + perpaction.WithMargin(sdk.OneDec()), + perpaction.WithSize(sdk.NewDec(10)), + perpaction.WithOpenNotional(sdk.NewDec(10)), ), - InsertPosition( - WithPair(pair2), - WithTrader(alice), - WithMargin(sdk.OneDec()), - WithSize(sdk.NewDec(10)), - WithOpenNotional(sdk.NewDec(10)), + perpaction.InsertPosition( + perpaction.WithPair(pair2), + perpaction.WithTrader(alice), + perpaction.WithMargin(sdk.OneDec()), + perpaction.WithSize(sdk.NewDec(10)), + perpaction.WithOpenNotional(sdk.NewDec(10)), ), ). Then( - QueryPositions(alice, - []QueryPositionChecker{ - QueryPosition_PositionEquals(types.Position{ + perpaction.QueryPositions(alice, + []perpaction.QueryPositionChecker{ + perpaction.QueryPosition_PositionEquals(types.Position{ Pair: pair, TraderAddress: alice.String(), Size_: sdk.NewDec(10), @@ -60,12 +60,12 @@ func TestQueryPositions(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), LastUpdatedBlockNumber: 0, }), - QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("19.9999999998")), - QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("9.9999999998")), - QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.5499999999955")), + perpaction.QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("19.9999999998")), + perpaction.QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("9.9999999998")), + perpaction.QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.5499999999955")), }, - []QueryPositionChecker{ - QueryPosition_PositionEquals(types.Position{ + []perpaction.QueryPositionChecker{ + perpaction.QueryPosition_PositionEquals(types.Position{ Pair: pair2, TraderAddress: alice.String(), Size_: sdk.NewDec(10), @@ -74,44 +74,44 @@ func TestQueryPositions(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), LastUpdatedBlockNumber: 0, }), - QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("29.9999999997")), - QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("19.9999999997")), - QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.699999999997")), + perpaction.QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("29.9999999997")), + perpaction.QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("19.9999999997")), + perpaction.QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.699999999997")), }, ), ), - TC("negative PnL, positive margin ratio"). + tutilaction.TC("negative PnL, positive margin ratio"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pair, - WithPricePeg(sdk.OneDec()), + perpaction.WithPricePeg(sdk.OneDec()), ), - CreateCustomMarket( + perpaction.CreateCustomMarket( pair2, - WithPricePeg(sdk.MustNewDecFromStr("0.95")), + perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.95")), ), ). When( - InsertPosition( - WithPair(pair), - WithTrader(alice), - WithMargin(sdk.OneDec()), - WithSize(sdk.NewDec(10)), - WithOpenNotional(sdk.NewDec(10)), + perpaction.InsertPosition( + perpaction.WithPair(pair), + perpaction.WithTrader(alice), + perpaction.WithMargin(sdk.OneDec()), + perpaction.WithSize(sdk.NewDec(10)), + perpaction.WithOpenNotional(sdk.NewDec(10)), ), - InsertPosition( - WithPair(pair2), - WithTrader(alice), - WithMargin(sdk.OneDec()), - WithSize(sdk.NewDec(10)), - WithOpenNotional(sdk.NewDec(10)), + perpaction.InsertPosition( + perpaction.WithPair(pair2), + perpaction.WithTrader(alice), + perpaction.WithMargin(sdk.OneDec()), + perpaction.WithSize(sdk.NewDec(10)), + perpaction.WithOpenNotional(sdk.NewDec(10)), ), ). Then( - QueryPositions(alice, - []QueryPositionChecker{ - QueryPosition_PositionEquals(types.Position{ + perpaction.QueryPositions(alice, + []perpaction.QueryPositionChecker{ + perpaction.QueryPosition_PositionEquals(types.Position{ Pair: pair, TraderAddress: alice.String(), Size_: sdk.NewDec(10), @@ -120,12 +120,12 @@ func TestQueryPositions(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), LastUpdatedBlockNumber: 0, }), - QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("9.9999999999")), - QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-0.0000000001")), - QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.099999999991")), + perpaction.QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("9.9999999999")), + perpaction.QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-0.0000000001")), + perpaction.QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.099999999991")), }, - []QueryPositionChecker{ - QueryPosition_PositionEquals(types.Position{ + []perpaction.QueryPositionChecker{ + perpaction.QueryPosition_PositionEquals(types.Position{ Pair: pair2, TraderAddress: alice.String(), Size_: sdk.NewDec(10), @@ -134,44 +134,44 @@ func TestQueryPositions(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), LastUpdatedBlockNumber: 0, }), - QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("9.499999999905")), - QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-0.500000000095")), - QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.052631578937894737")), + perpaction.QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("9.499999999905")), + perpaction.QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-0.500000000095")), + perpaction.QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.052631578937894737")), }, ), ), - TC("negative PnL, negative margin ratio"). + tutilaction.TC("negative PnL, negative margin ratio"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pair, - WithPricePeg(sdk.MustNewDecFromStr("0.5")), + perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.5")), ), - CreateCustomMarket( + perpaction.CreateCustomMarket( pair2, - WithPricePeg(sdk.MustNewDecFromStr("0.9")), + perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.9")), ), ). When( - InsertPosition( - WithPair(pair), - WithTrader(alice), - WithMargin(sdk.OneDec()), - WithSize(sdk.NewDec(10)), - WithOpenNotional(sdk.NewDec(10)), + perpaction.InsertPosition( + perpaction.WithPair(pair), + perpaction.WithTrader(alice), + perpaction.WithMargin(sdk.OneDec()), + perpaction.WithSize(sdk.NewDec(10)), + perpaction.WithOpenNotional(sdk.NewDec(10)), ), - InsertPosition( - WithPair(pair2), - WithTrader(alice), - WithMargin(sdk.OneDec()), - WithSize(sdk.NewDec(10)), - WithOpenNotional(sdk.NewDec(10)), + perpaction.InsertPosition( + perpaction.WithPair(pair2), + perpaction.WithTrader(alice), + perpaction.WithMargin(sdk.OneDec()), + perpaction.WithSize(sdk.NewDec(10)), + perpaction.WithOpenNotional(sdk.NewDec(10)), ), ). Then( - QueryPositions(alice, - []QueryPositionChecker{ - QueryPosition_PositionEquals(types.Position{ + perpaction.QueryPositions(alice, + []perpaction.QueryPositionChecker{ + perpaction.QueryPosition_PositionEquals(types.Position{ Pair: pair, TraderAddress: alice.String(), Size_: sdk.NewDec(10), @@ -180,12 +180,12 @@ func TestQueryPositions(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), LastUpdatedBlockNumber: 0, }), - QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("4.99999999995")), - QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-5.00000000005")), - QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("-0.800000000018")), + perpaction.QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("4.99999999995")), + perpaction.QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-5.00000000005")), + perpaction.QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("-0.800000000018")), }, - []QueryPositionChecker{ - QueryPosition_PositionEquals(types.Position{ + []perpaction.QueryPositionChecker{ + perpaction.QueryPosition_PositionEquals(types.Position{ Pair: pair2, TraderAddress: alice.String(), Size_: sdk.NewDec(10), @@ -194,41 +194,41 @@ func TestQueryPositions(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), LastUpdatedBlockNumber: 0, }), - QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("8.99999999991")), - QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-1.00000000009")), - QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("-0.00000000001")), + perpaction.QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("8.99999999991")), + perpaction.QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-1.00000000009")), + perpaction.QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("-0.00000000001")), }, ), ), } - NewTestSuite(t).WithTestCases(tc...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() } func TestQueryPosition(t *testing.T) { alice := testutil.AccAddress() pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) - tc := TestCases{ - TC("positive PnL"). + tc := tutilaction.TestCases{ + tutilaction.TC("positive PnL"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pair, - WithPricePeg(sdk.NewDec(2)), + perpaction.WithPricePeg(sdk.NewDec(2)), ), ). When( - InsertPosition( - WithPair(pair), - WithTrader(alice), - WithMargin(sdk.OneDec()), - WithSize(sdk.NewDec(10)), - WithOpenNotional(sdk.NewDec(10)), + perpaction.InsertPosition( + perpaction.WithPair(pair), + perpaction.WithTrader(alice), + perpaction.WithMargin(sdk.OneDec()), + perpaction.WithSize(sdk.NewDec(10)), + perpaction.WithOpenNotional(sdk.NewDec(10)), ), ). Then( - QueryPosition(pair, alice, - QueryPosition_PositionEquals(types.Position{ + perpaction.QueryPosition(pair, alice, + perpaction.QueryPosition_PositionEquals(types.Position{ Pair: pair, TraderAddress: alice.String(), Size_: sdk.NewDec(10), @@ -237,31 +237,31 @@ func TestQueryPosition(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), LastUpdatedBlockNumber: 0, }), - QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("19.9999999998")), - QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("9.9999999998")), - QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.5499999999955")), + perpaction.QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("19.9999999998")), + perpaction.QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("9.9999999998")), + perpaction.QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.5499999999955")), ), ), - TC("negative PnL, positive margin ratio"). + tutilaction.TC("negative PnL, positive margin ratio"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pair, - WithPricePeg(sdk.OneDec()), + perpaction.WithPricePeg(sdk.OneDec()), ), ). When( - InsertPosition( - WithPair(pair), - WithTrader(alice), - WithMargin(sdk.OneDec()), - WithSize(sdk.NewDec(10)), - WithOpenNotional(sdk.NewDec(10)), + perpaction.InsertPosition( + perpaction.WithPair(pair), + perpaction.WithTrader(alice), + perpaction.WithMargin(sdk.OneDec()), + perpaction.WithSize(sdk.NewDec(10)), + perpaction.WithOpenNotional(sdk.NewDec(10)), ), ). Then( - QueryPosition(pair, alice, - QueryPosition_PositionEquals(types.Position{ + perpaction.QueryPosition(pair, alice, + perpaction.QueryPosition_PositionEquals(types.Position{ Pair: pair, TraderAddress: alice.String(), Size_: sdk.NewDec(10), @@ -270,31 +270,31 @@ func TestQueryPosition(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), LastUpdatedBlockNumber: 0, }), - QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("9.9999999999")), - QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-0.0000000001")), - QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.099999999991")), + perpaction.QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("9.9999999999")), + perpaction.QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-0.0000000001")), + perpaction.QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.099999999991")), ), ), - TC("negative PnL, negative margin ratio"). + tutilaction.TC("negative PnL, negative margin ratio"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pair, - WithPricePeg(sdk.MustNewDecFromStr("0.5")), + perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.5")), ), ). When( - InsertPosition( - WithPair(pair), - WithTrader(alice), - WithMargin(sdk.OneDec()), - WithSize(sdk.NewDec(10)), - WithOpenNotional(sdk.NewDec(10)), + perpaction.InsertPosition( + perpaction.WithPair(pair), + perpaction.WithTrader(alice), + perpaction.WithMargin(sdk.OneDec()), + perpaction.WithSize(sdk.NewDec(10)), + perpaction.WithOpenNotional(sdk.NewDec(10)), ), ). Then( - QueryPosition(pair, alice, - QueryPosition_PositionEquals(types.Position{ + perpaction.QueryPosition(pair, alice, + perpaction.QueryPosition_PositionEquals(types.Position{ Pair: pair, TraderAddress: alice.String(), Size_: sdk.NewDec(10), @@ -303,53 +303,55 @@ func TestQueryPosition(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), LastUpdatedBlockNumber: 0, }), - QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("4.99999999995")), - QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-5.00000000005")), - QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("-0.800000000018")), + perpaction.QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("4.99999999995")), + perpaction.QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-5.00000000005")), + perpaction.QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("-0.800000000018")), ), ), - TC("non existent position"). + tutilaction.TC("non existent position"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pair, - WithPricePeg(sdk.NewDec(2)), + perpaction.WithPricePeg(sdk.NewDec(2)), ), ). When(). Then( - QueryPositionNotFound(pair, alice), + perpaction.QueryPositionNotFound(pair, alice), ), } - NewTestSuite(t).WithTestCases(tc...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() } func TestQueryMarkets(t *testing.T) { alice := testutil.AccAddress() pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) - tc := TestCases{ - TC("positive PnL"). + tc := tutilaction.TestCases{ + tutilaction.TC("positive PnL"). Given( - CreateCustomMarket( + perpaction.CreateCustomMarket( pair, - WithPricePeg(sdk.NewDec(2)), + perpaction.WithPricePeg(sdk.NewDec(2)), + ), + tutilaction.FundModule( + "perp_ef", sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10))), ), - FundModule("perp_ef", sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10)))), ). When( - InsertPosition( - WithPair(pair), - WithTrader(alice), - WithMargin(sdk.OneDec()), - WithSize(sdk.NewDec(10)), - WithOpenNotional(sdk.NewDec(10)), + perpaction.InsertPosition( + perpaction.WithPair(pair), + perpaction.WithTrader(alice), + perpaction.WithMargin(sdk.OneDec()), + perpaction.WithSize(sdk.NewDec(10)), + perpaction.WithOpenNotional(sdk.NewDec(10)), ), ). Then( - QueryMarkets(QueryMarkets_MarketsShouldContain(*types.DefaultMarket(pair))), - QueryModuleAccounts(QueryModuleAccounts_ModulesBalanceShouldBe( + perpaction.QueryMarkets(perpaction.QueryMarkets_MarketsShouldContain(*types.DefaultMarket(pair))), + perpaction.QueryModuleAccounts(perpaction.QueryModuleAccounts_ModulesBalanceShouldBe( map[string]sdk.Coins{ "perp_ef": sdk.NewCoins( sdk.NewCoin(denoms.BTC, sdk.ZeroInt()), @@ -360,7 +362,7 @@ func TestQueryMarkets(t *testing.T) { ), } - NewTestSuite(t).WithTestCases(tc...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() } func TestQueryPositionStore(t *testing.T) { diff --git a/x/perp/v2/keeper/hooks_test.go b/x/perp/v2/keeper/hooks_test.go index fd4555cd0..53a554281 100644 --- a/x/perp/v2/keeper/hooks_test.go +++ b/x/perp/v2/keeper/hooks_test.go @@ -8,146 +8,146 @@ import ( "github.com/NibiruChain/nibiru/x/common/asset" "github.com/NibiruChain/nibiru/x/common/denoms" - . "github.com/NibiruChain/nibiru/x/common/testutil/action" - . "github.com/NibiruChain/nibiru/x/epochs/integration/action" + tutilaction "github.com/NibiruChain/nibiru/x/common/testutil/action" + epochsaction "github.com/NibiruChain/nibiru/x/epochs/integration/action" epochtypes "github.com/NibiruChain/nibiru/x/epochs/types" - . "github.com/NibiruChain/nibiru/x/oracle/integration/action" - . "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" - . "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" + oracleaction "github.com/NibiruChain/nibiru/x/oracle/integration/action" + perpaction "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" + perpassert "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" ) func TestAfterEpochEnd(t *testing.T) { pairBtcUsdc := asset.Registry.Pair(denoms.BTC, denoms.USDC) startTime := time.Now() - tc := TestCases{ - TC("index > mark"). + tc := tutilaction.TestCases{ + tutilaction.TC("index > mark"). Given( - CreateCustomMarket(pairBtcUsdc), - SetBlockTime(startTime), - InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.MustNewDecFromStr("5.8")), - StartEpoch(epochtypes.ThirtyMinuteEpochID), + perpaction.CreateCustomMarket(pairBtcUsdc), + tutilaction.SetBlockTime(startTime), + oracleaction.InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.MustNewDecFromStr("5.8")), + epochsaction.StartEpoch(epochtypes.ThirtyMinuteEpochID), ). When( - MoveToNextBlockWithDuration(30 * time.Minute), + tutilaction.MoveToNextBlockWithDuration(30 * time.Minute), ). Then( - MarketShouldBeEqual(pairBtcUsdc, Market_LatestCPFShouldBeEqualTo(sdk.MustNewDecFromStr("-0.099999999999999999"))), + perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_LatestCPFShouldBeEqualTo(sdk.MustNewDecFromStr("-0.099999999999999999"))), ), - TC("index < mark"). + tutilaction.TC("index < mark"). Given( - CreateCustomMarket(pairBtcUsdc), - SetBlockTime(startTime), - InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.MustNewDecFromStr("0.52")), - StartEpoch(epochtypes.ThirtyMinuteEpochID), + perpaction.CreateCustomMarket(pairBtcUsdc), + tutilaction.SetBlockTime(startTime), + oracleaction.InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.MustNewDecFromStr("0.52")), + epochsaction.StartEpoch(epochtypes.ThirtyMinuteEpochID), ). When( - MoveToNextBlockWithDuration(30 * time.Minute), + tutilaction.MoveToNextBlockWithDuration(30 * time.Minute), ). Then( - MarketShouldBeEqual(pairBtcUsdc, Market_LatestCPFShouldBeEqualTo(sdk.MustNewDecFromStr("0.01"))), + perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_LatestCPFShouldBeEqualTo(sdk.MustNewDecFromStr("0.01"))), ), - TC("index > mark - max funding rate"). + tutilaction.TC("index > mark - max funding rate"). Given( - CreateCustomMarket(pairBtcUsdc, WithMaxFundingRate(sdk.MustNewDecFromStr("0.001"))), - SetBlockTime(startTime), - InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.MustNewDecFromStr("5.8")), - StartEpoch(epochtypes.ThirtyMinuteEpochID), + perpaction.CreateCustomMarket(pairBtcUsdc, perpaction.WithMaxFundingRate(sdk.MustNewDecFromStr("0.001"))), + tutilaction.SetBlockTime(startTime), + oracleaction.InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.MustNewDecFromStr("5.8")), + epochsaction.StartEpoch(epochtypes.ThirtyMinuteEpochID), ). When( - MoveToNextBlockWithDuration(30 * time.Minute), + tutilaction.MoveToNextBlockWithDuration(30 * time.Minute), ). Then( - MarketShouldBeEqual(pairBtcUsdc, Market_LatestCPFShouldBeEqualTo(sdk.MustNewDecFromStr("-0.000120833333333333"))), + perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_LatestCPFShouldBeEqualTo(sdk.MustNewDecFromStr("-0.000120833333333333"))), ), - TC("index < mark - max funding rate"). + tutilaction.TC("index < mark - max funding rate"). Given( - CreateCustomMarket(pairBtcUsdc, WithMaxFundingRate(sdk.MustNewDecFromStr("0.001"))), - SetBlockTime(startTime), - InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.MustNewDecFromStr("0.52")), - StartEpoch(epochtypes.ThirtyMinuteEpochID), + perpaction.CreateCustomMarket(pairBtcUsdc, perpaction.WithMaxFundingRate(sdk.MustNewDecFromStr("0.001"))), + tutilaction.SetBlockTime(startTime), + oracleaction.InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.MustNewDecFromStr("0.52")), + epochsaction.StartEpoch(epochtypes.ThirtyMinuteEpochID), ). When( - MoveToNextBlockWithDuration(30 * time.Minute), + tutilaction.MoveToNextBlockWithDuration(30 * time.Minute), ). Then( - MarketShouldBeEqual(pairBtcUsdc, Market_LatestCPFShouldBeEqualTo(sdk.MustNewDecFromStr("0.000010833333333333"))), + perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_LatestCPFShouldBeEqualTo(sdk.MustNewDecFromStr("0.000010833333333333"))), ), - TC("index == mark"). + tutilaction.TC("index == mark"). Given( - CreateCustomMarket(pairBtcUsdc), - SetBlockTime(startTime), - InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.OneDec()), - StartEpoch(epochtypes.ThirtyMinuteEpochID), + perpaction.CreateCustomMarket(pairBtcUsdc), + tutilaction.SetBlockTime(startTime), + oracleaction.InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.OneDec()), + epochsaction.StartEpoch(epochtypes.ThirtyMinuteEpochID), ). When( - MoveToNextBlockWithDuration(30 * time.Minute), + tutilaction.MoveToNextBlockWithDuration(30 * time.Minute), ). Then( - MarketShouldBeEqual(pairBtcUsdc, Market_LatestCPFShouldBeEqualTo(sdk.ZeroDec())), + perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_LatestCPFShouldBeEqualTo(sdk.ZeroDec())), ), - TC("missing twap"). + tutilaction.TC("missing twap"). Given( - CreateCustomMarket(pairBtcUsdc), - SetBlockTime(startTime), - StartEpoch(epochtypes.ThirtyMinuteEpochID), + perpaction.CreateCustomMarket(pairBtcUsdc), + tutilaction.SetBlockTime(startTime), + epochsaction.StartEpoch(epochtypes.ThirtyMinuteEpochID), ). When( - MoveToNextBlockWithDuration(30 * time.Minute), + tutilaction.MoveToNextBlockWithDuration(30 * time.Minute), ). Then( - MarketShouldBeEqual(pairBtcUsdc, Market_LatestCPFShouldBeEqualTo(sdk.ZeroDec())), + perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_LatestCPFShouldBeEqualTo(sdk.ZeroDec())), ), - TC("0 price mark"). + tutilaction.TC("0 price mark"). Given( - CreateCustomMarket(pairBtcUsdc), - SetBlockTime(startTime), - StartEpoch(epochtypes.ThirtyMinuteEpochID), - InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.ZeroDec()), + perpaction.CreateCustomMarket(pairBtcUsdc), + tutilaction.SetBlockTime(startTime), + epochsaction.StartEpoch(epochtypes.ThirtyMinuteEpochID), + oracleaction.InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.ZeroDec()), ). When( - MoveToNextBlockWithDuration(30 * time.Minute), + tutilaction.MoveToNextBlockWithDuration(30 * time.Minute), ). Then( - MarketShouldBeEqual(pairBtcUsdc, Market_LatestCPFShouldBeEqualTo(sdk.ZeroDec())), + perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_LatestCPFShouldBeEqualTo(sdk.ZeroDec())), ), - TC("market not enabled"). + tutilaction.TC("market not enabled"). Given( - CreateCustomMarket(pairBtcUsdc), - SetMarketEnabled(pairBtcUsdc, false), - SetBlockTime(startTime), - StartEpoch(epochtypes.ThirtyMinuteEpochID), - InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.NewDec(2)), + perpaction.CreateCustomMarket(pairBtcUsdc), + perpaction.SetMarketEnabled(pairBtcUsdc, false), + tutilaction.SetBlockTime(startTime), + epochsaction.StartEpoch(epochtypes.ThirtyMinuteEpochID), + oracleaction.InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.NewDec(2)), ). When( - MoveToNextBlockWithDuration(30 * time.Minute), + tutilaction.MoveToNextBlockWithDuration(30 * time.Minute), ). Then( - MarketShouldBeEqual(pairBtcUsdc, Market_LatestCPFShouldBeEqualTo(sdk.ZeroDec())), + perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_LatestCPFShouldBeEqualTo(sdk.ZeroDec())), ), - TC("not correct epoch id"). + tutilaction.TC("not correct epoch id"). Given( - CreateCustomMarket(pairBtcUsdc), - SetMarketEnabled(pairBtcUsdc, false), - SetBlockTime(startTime), - StartEpoch(epochtypes.FifteenMinuteEpochID), - InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.NewDec(2)), + perpaction.CreateCustomMarket(pairBtcUsdc), + perpaction.SetMarketEnabled(pairBtcUsdc, false), + tutilaction.SetBlockTime(startTime), + epochsaction.StartEpoch(epochtypes.FifteenMinuteEpochID), + oracleaction.InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.NewDec(2)), ). When( - MoveToNextBlockWithDuration(30 * time.Minute), + tutilaction.MoveToNextBlockWithDuration(30 * time.Minute), ). Then( - MarketShouldBeEqual(pairBtcUsdc, Market_LatestCPFShouldBeEqualTo(sdk.ZeroDec())), + perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_LatestCPFShouldBeEqualTo(sdk.ZeroDec())), ), } - NewTestSuite(t).WithTestCases(tc...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() } diff --git a/x/perp/v2/keeper/liquidate_test.go b/x/perp/v2/keeper/liquidate_test.go index d0a408d9f..dde25da7c 100644 --- a/x/perp/v2/keeper/liquidate_test.go +++ b/x/perp/v2/keeper/liquidate_test.go @@ -10,10 +10,10 @@ import ( "github.com/NibiruChain/nibiru/x/common/asset" "github.com/NibiruChain/nibiru/x/common/denoms" "github.com/NibiruChain/nibiru/x/common/testutil" - . "github.com/NibiruChain/nibiru/x/common/testutil/action" - . "github.com/NibiruChain/nibiru/x/common/testutil/assertion" - . "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" - . "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" + tutilaction "github.com/NibiruChain/nibiru/x/common/testutil/action" + assertion "github.com/NibiruChain/nibiru/x/common/testutil/assertion" + perpaction "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" + perpassertion "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" "github.com/NibiruChain/nibiru/x/perp/v2/keeper" types "github.com/NibiruChain/nibiru/x/perp/v2/types" @@ -30,27 +30,34 @@ func TestMultiLiquidate(t *testing.T) { liquidator := testutil.AccAddress() startTime := time.Now() - tc := TestCases{ - TC("partial liquidation"). + tc := tutilaction.TestCases{ + tutilaction.TC("partial liquidation"). Given( - SetBlockNumber(1), - SetBlockTime(startTime), - CreateCustomMarket(pairBtcUsdc), - InsertPosition(WithTrader(alice), WithPair(pairBtcUsdc), WithSize(sdk.NewDec(10000)), WithMargin(sdk.NewDec(1000)), WithOpenNotional(sdk.NewDec(10400))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startTime), + perpaction.CreateCustomMarket(pairBtcUsdc), + perpaction.InsertPosition( + perpaction.WithTrader(alice), + perpaction.WithPair(pairBtcUsdc), + perpaction.WithSize(sdk.NewDec(10000)), + perpaction.WithMargin(sdk.NewDec(1000)), + perpaction.WithOpenNotional(sdk.NewDec(10400))), + tutilaction.FundModule( + types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000)), + ), ). When( - MoveToNextBlock(), - MultiLiquidate(liquidator, false, - PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + tutilaction.MoveToNextBlock(), + perpaction.MultiLiquidate(liquidator, false, + perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, ), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(750)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(125)), - BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(125)), - PositionShouldBeEqual(alice, pairBtcUsdc, - Position_PositionShouldBeEqualTo( + assertion.ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(750)), + assertion.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(125)), + assertion.BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(125)), + perpassertion.PositionShouldBeEqual(alice, pairBtcUsdc, + perpassertion.Position_PositionShouldBeEqualTo( types.Position{ Pair: pairBtcUsdc, TraderAddress: alice.String(), @@ -64,157 +71,178 @@ func TestMultiLiquidate(t *testing.T) { ), ), - TC("full liquidation"). + tutilaction.TC("full liquidation"). Given( - SetBlockNumber(1), - SetBlockTime(startTime), - CreateCustomMarket(pairBtcUsdc), - InsertPosition(WithTrader(alice), WithPair(pairBtcUsdc), WithSize(sdk.NewDec(10000)), WithMargin(sdk.NewDec(1000)), WithOpenNotional(sdk.NewDec(10600))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startTime), + perpaction.CreateCustomMarket(pairBtcUsdc), + perpaction.InsertPosition( + perpaction.WithTrader(alice), + perpaction.WithPair(pairBtcUsdc), + perpaction.WithSize(sdk.NewDec(10000)), + perpaction.WithMargin(sdk.NewDec(1000)), + perpaction.WithOpenNotional(sdk.NewDec(10600))), + tutilaction.FundModule( + types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000)), + ), ). When( - MoveToNextBlock(), - MultiLiquidate(liquidator, false, - PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, - PairTraderTuple{Pair: pairAtomUsdc, Trader: alice, Successful: false}, + tutilaction.MoveToNextBlock(), + + perpaction.MultiLiquidate(liquidator, false, + perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + perpaction.PairTraderTuple{Pair: pairAtomUsdc, Trader: alice, Successful: false}, ), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(600)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(150)), - BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(250)), - PositionShouldNotExist(alice, pairBtcUsdc), + assertion.ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(600)), + assertion.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(150)), + assertion.BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(250)), + perpassertion.PositionShouldNotExist(alice, pairBtcUsdc), ), - TC("full liquidation"). + tutilaction.TC("full liquidation"). Given( - SetBlockNumber(1), - SetBlockTime(startTime), - CreateCustomMarket(pairBtcUsdc), - InsertPosition(WithTrader(alice), WithPair(pairBtcUsdc), WithSize(sdk.NewDec(10000)), WithMargin(sdk.NewDec(1000)), WithOpenNotional(sdk.NewDec(10600))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startTime), + perpaction.CreateCustomMarket(pairBtcUsdc), + perpaction.InsertPosition(perpaction.WithTrader(alice), perpaction.WithPair(pairBtcUsdc), perpaction.WithSize(sdk.NewDec(10000)), perpaction.WithMargin(sdk.NewDec(1000)), perpaction.WithOpenNotional(sdk.NewDec(10600))), + tutilaction.FundModule( + types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000)), + ), ). When( - MoveToNextBlock(), - MultiLiquidate(liquidator, false, - PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + tutilaction.MoveToNextBlock(), + perpaction.MultiLiquidate(liquidator, false, + perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, ), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(600)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(150)), - BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(250)), - PositionShouldNotExist(alice, pairBtcUsdc), + assertion.ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(600)), + assertion.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(150)), + assertion.BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(250)), + perpassertion.PositionShouldNotExist(alice, pairBtcUsdc), ), - TC("one fail liquidation - one correct"). + tutilaction.TC("one fail liquidation - one correct"). Given( - SetBlockNumber(1), - SetBlockTime(startTime), - CreateCustomMarket(pairBtcUsdc), - InsertPosition(WithTrader(alice), WithPair(pairBtcUsdc), WithSize(sdk.NewDec(10000)), WithMargin(sdk.NewDec(1000)), WithOpenNotional(sdk.NewDec(10600))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startTime), + perpaction.CreateCustomMarket(pairBtcUsdc), + perpaction.InsertPosition(perpaction.WithTrader(alice), perpaction.WithPair(pairBtcUsdc), perpaction.WithSize(sdk.NewDec(10000)), perpaction.WithMargin(sdk.NewDec(1000)), perpaction.WithOpenNotional(sdk.NewDec(10600))), + tutilaction.FundModule( + types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000)), + ), ). When( - MoveToNextBlock(), - MultiLiquidate(liquidator, false, - PairTraderTuple{Pair: pairBtcUsdc, Successful: false}, - PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + tutilaction.MoveToNextBlock(), + perpaction.MultiLiquidate(liquidator, false, + perpaction.PairTraderTuple{Pair: pairBtcUsdc, Successful: false}, + perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, ), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(600)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(150)), - BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(250)), - PositionShouldNotExist(alice, pairBtcUsdc), + assertion.ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(600)), + assertion.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(150)), + assertion.BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(250)), + perpassertion.PositionShouldNotExist(alice, pairBtcUsdc), ), - TC("one fail liquidation because market does not exists- one correct"). + tutilaction.TC("one fail liquidation because market does not exists- one correct"). Given( - SetBlockNumber(1), - SetBlockTime(startTime), - CreateCustomMarket(pairBtcUsdc), - InsertPosition(WithTrader(alice), WithPair(pairBtcUsdc), WithSize(sdk.NewDec(10000)), WithMargin(sdk.NewDec(1000)), WithOpenNotional(sdk.NewDec(10600))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startTime), + perpaction.CreateCustomMarket(pairBtcUsdc), + perpaction.InsertPosition(perpaction.WithTrader(alice), perpaction.WithPair(pairBtcUsdc), perpaction.WithSize(sdk.NewDec(10000)), perpaction.WithMargin(sdk.NewDec(1000)), perpaction.WithOpenNotional(sdk.NewDec(10600))), + tutilaction.FundModule( + types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000)), + ), ). When( - MoveToNextBlock(), - MultiLiquidate(liquidator, false, - PairTraderTuple{Pair: asset.MustNewPair("luna:usdt"), Successful: false}, - PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + tutilaction.MoveToNextBlock(), + perpaction.MultiLiquidate(liquidator, false, + perpaction.PairTraderTuple{Pair: asset.MustNewPair("luna:usdt"), Successful: false}, + perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, ), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(600)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(150)), - BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(250)), - PositionShouldNotExist(alice, pairBtcUsdc), + assertion.ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(600)), + assertion.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(150)), + assertion.BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(250)), + perpassertion.PositionShouldNotExist(alice, pairBtcUsdc), ), - TC("realizes bad debt"). + tutilaction.TC("realizes bad debt"). Given( - SetBlockNumber(1), - SetBlockTime(startTime), - CreateCustomMarket(pairBtcUsdc), - InsertPosition(WithTrader(alice), WithPair(pairBtcUsdc), WithSize(sdk.NewDec(10000)), WithMargin(sdk.NewDec(1000)), WithOpenNotional(sdk.NewDec(10800))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 50))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startTime), + perpaction.CreateCustomMarket(pairBtcUsdc), + perpaction.InsertPosition(perpaction.WithTrader(alice), perpaction.WithPair(pairBtcUsdc), perpaction.WithSize(sdk.NewDec(10000)), perpaction.WithMargin(sdk.NewDec(1000)), perpaction.WithOpenNotional(sdk.NewDec(10800))), + tutilaction.FundModule( + types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000)), + ), + tutilaction.FundModule( + types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 50)), + ), ). When( - MoveToNextBlock(), - MultiLiquidate(liquidator, false, - PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + tutilaction.MoveToNextBlock(), + perpaction.MultiLiquidate(liquidator, false, + perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, ), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(800)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.ZeroInt()), - BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(250)), - PositionShouldNotExist(alice, pairBtcUsdc), + assertion.ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(800)), + assertion.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.ZeroInt()), + assertion.BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(250)), + perpassertion.PositionShouldNotExist(alice, pairBtcUsdc), ), - TC("uses prepaid bad debt"). + tutilaction.TC("uses prepaid bad debt"). Given( - SetBlockNumber(1), - SetBlockTime(startTime), - CreateCustomMarket(pairBtcUsdc, WithPrepaidBadDebt(sdk.NewInt(50))), - InsertPosition(WithTrader(alice), WithPair(pairBtcUsdc), WithSize(sdk.NewDec(10000)), WithMargin(sdk.NewDec(1000)), WithOpenNotional(sdk.NewDec(10800))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startTime), + perpaction.CreateCustomMarket(pairBtcUsdc, perpaction.WithPrepaidBadDebt(sdk.NewInt(50))), + perpaction.InsertPosition(perpaction.WithTrader(alice), perpaction.WithPair(pairBtcUsdc), perpaction.WithSize(sdk.NewDec(10000)), perpaction.WithMargin(sdk.NewDec(1000)), perpaction.WithOpenNotional(sdk.NewDec(10800))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000))), ). When( - MoveToNextBlock(), - MultiLiquidate(liquidator, false, - PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + tutilaction.MoveToNextBlock(), + perpaction.MultiLiquidate(liquidator, false, + perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, ), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(750)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.ZeroInt()), - BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(250)), - PositionShouldNotExist(alice, pairBtcUsdc), - MarketShouldBeEqual(pairBtcUsdc, Market_PrepaidBadDebtShouldBeEqualTo(sdk.ZeroInt())), + assertion.ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(750)), + assertion.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.ZeroInt()), + assertion.BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(250)), + perpassertion.PositionShouldNotExist(alice, pairBtcUsdc), + perpassertion.MarketShouldBeEqual( + pairBtcUsdc, + perpassertion.Market_PrepaidBadDebtShouldBeEqualTo(sdk.ZeroInt()), + ), ), - TC("healthy position"). + tutilaction.TC("healthy position"). Given( - SetBlockNumber(1), - SetBlockTime(startTime), - CreateCustomMarket(pairBtcUsdc), - InsertPosition(WithTrader(alice), WithPair(pairBtcUsdc), WithSize(sdk.NewDec(100)), WithMargin(sdk.NewDec(10)), WithOpenNotional(sdk.NewDec(100))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 10))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startTime), + perpaction.CreateCustomMarket(pairBtcUsdc), + perpaction.InsertPosition(perpaction.WithTrader(alice), perpaction.WithPair(pairBtcUsdc), perpaction.WithSize(sdk.NewDec(100)), perpaction.WithMargin(sdk.NewDec(10)), perpaction.WithOpenNotional(sdk.NewDec(100))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 10))), ). When( - MoveToNextBlock(), - MultiLiquidate(liquidator, true, - PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: false}, + tutilaction.MoveToNextBlock(), + perpaction.MultiLiquidate(liquidator, true, + perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: false}, ), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(10)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.ZeroInt()), - BalanceEqual(liquidator, denoms.USDC, sdk.ZeroInt()), - PositionShouldBeEqual(alice, pairBtcUsdc, - Position_PositionShouldBeEqualTo( + assertion.ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(10)), + assertion.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.ZeroInt()), + assertion.BalanceEqual(liquidator, denoms.USDC, sdk.ZeroInt()), + perpassertion.PositionShouldBeEqual(alice, pairBtcUsdc, + perpassertion.Position_PositionShouldBeEqualTo( types.Position{ Pair: pairBtcUsdc, TraderAddress: alice.String(), @@ -226,7 +254,7 @@ func TestMultiLiquidate(t *testing.T) { }, ), ), - ContainsLiquidateEvent(&types.LiquidationFailedEvent{ + perpassertion.ContainsLiquidateEvent(&types.LiquidationFailedEvent{ Pair: pairBtcUsdc, Trader: alice.String(), Liquidator: liquidator.String(), @@ -234,34 +262,34 @@ func TestMultiLiquidate(t *testing.T) { }), ), - TC("mixed bag"). + tutilaction.TC("mixed bag"). Given( - SetBlockNumber(1), - SetBlockTime(startTime), - CreateCustomMarket(pairBtcUsdc), - CreateCustomMarket(pairEthUsdc), - CreateCustomMarket(pairAtomUsdc), - InsertPosition(WithTrader(alice), WithPair(pairBtcUsdc), WithSize(sdk.NewDec(10000)), WithMargin(sdk.NewDec(1000)), WithOpenNotional(sdk.NewDec(10400))), // partial - InsertPosition(WithTrader(alice), WithPair(pairEthUsdc), WithSize(sdk.NewDec(10000)), WithMargin(sdk.NewDec(1000)), WithOpenNotional(sdk.NewDec(10600))), // full - InsertPosition(WithTrader(alice), WithPair(pairAtomUsdc), WithSize(sdk.NewDec(10000)), WithMargin(sdk.NewDec(1000)), WithOpenNotional(sdk.NewDec(10000))), // healthy - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 3000))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startTime), + perpaction.CreateCustomMarket(pairBtcUsdc), + perpaction.CreateCustomMarket(pairEthUsdc), + perpaction.CreateCustomMarket(pairAtomUsdc), + perpaction.InsertPosition(perpaction.WithTrader(alice), perpaction.WithPair(pairBtcUsdc), perpaction.WithSize(sdk.NewDec(10000)), perpaction.WithMargin(sdk.NewDec(1000)), perpaction.WithOpenNotional(sdk.NewDec(10400))), // partial + perpaction.InsertPosition(perpaction.WithTrader(alice), perpaction.WithPair(pairEthUsdc), perpaction.WithSize(sdk.NewDec(10000)), perpaction.WithMargin(sdk.NewDec(1000)), perpaction.WithOpenNotional(sdk.NewDec(10600))), // full + perpaction.InsertPosition(perpaction.WithTrader(alice), perpaction.WithPair(pairAtomUsdc), perpaction.WithSize(sdk.NewDec(10000)), perpaction.WithMargin(sdk.NewDec(1000)), perpaction.WithOpenNotional(sdk.NewDec(10000))), // healthy + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 3000))), ). When( - MoveToNextBlock(), - MultiLiquidate(liquidator, false, - PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, - PairTraderTuple{Pair: pairEthUsdc, Trader: alice, Successful: true}, - PairTraderTuple{Pair: pairAtomUsdc, Trader: alice, Successful: false}, - PairTraderTuple{Pair: pairSolUsdc, Trader: alice, Successful: false}, // non-existent market - PairTraderTuple{Pair: pairBtcUsdc, Trader: bob, Successful: false}, // non-existent position + tutilaction.MoveToNextBlock(), + perpaction.MultiLiquidate(liquidator, false, + perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + perpaction.PairTraderTuple{Pair: pairEthUsdc, Trader: alice, Successful: true}, + perpaction.PairTraderTuple{Pair: pairAtomUsdc, Trader: alice, Successful: false}, + perpaction.PairTraderTuple{Pair: pairSolUsdc, Trader: alice, Successful: false}, // non-existent market + perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: bob, Successful: false}, // non-existent position ), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(2350)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(275)), - BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(375)), - PositionShouldBeEqual(alice, pairBtcUsdc, - Position_PositionShouldBeEqualTo( + assertion.ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(2350)), + assertion.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(275)), + assertion.BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(375)), + perpassertion.PositionShouldBeEqual(alice, pairBtcUsdc, + perpassertion.Position_PositionShouldBeEqualTo( types.Position{ Pair: pairBtcUsdc, TraderAddress: alice.String(), @@ -273,9 +301,9 @@ func TestMultiLiquidate(t *testing.T) { }, ), ), - PositionShouldNotExist(alice, pairEthUsdc), - PositionShouldBeEqual(alice, pairAtomUsdc, - Position_PositionShouldBeEqualTo( + perpassertion.PositionShouldNotExist(alice, pairEthUsdc), + perpassertion.PositionShouldBeEqual(alice, pairAtomUsdc, + perpassertion.Position_PositionShouldBeEqualTo( types.Position{ Pair: pairAtomUsdc, TraderAddress: alice.String(), @@ -288,19 +316,19 @@ func TestMultiLiquidate(t *testing.T) { ), ), - ContainsLiquidateEvent(&types.LiquidationFailedEvent{ + perpassertion.ContainsLiquidateEvent(&types.LiquidationFailedEvent{ Pair: pairAtomUsdc, Trader: alice.String(), Liquidator: liquidator.String(), Reason: types.LiquidationFailedEvent_POSITION_HEALTHY, }), - ContainsLiquidateEvent(&types.LiquidationFailedEvent{ + perpassertion.ContainsLiquidateEvent(&types.LiquidationFailedEvent{ Pair: pairSolUsdc, Trader: alice.String(), Liquidator: liquidator.String(), Reason: types.LiquidationFailedEvent_NONEXISTENT_PAIR, }), - ContainsLiquidateEvent(&types.LiquidationFailedEvent{ + perpassertion.ContainsLiquidateEvent(&types.LiquidationFailedEvent{ Pair: pairBtcUsdc, Trader: bob.String(), Liquidator: liquidator.String(), @@ -309,7 +337,7 @@ func TestMultiLiquidate(t *testing.T) { ), } - NewTestSuite(t).WithTestCases(tc...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() } func TestPrettyLiquidateResponse(t *testing.T) { diff --git a/x/perp/v2/keeper/margin_test.go b/x/perp/v2/keeper/margin_test.go index b996a02d9..7c2714412 100644 --- a/x/perp/v2/keeper/margin_test.go +++ b/x/perp/v2/keeper/margin_test.go @@ -12,10 +12,10 @@ import ( "github.com/NibiruChain/nibiru/x/common/asset" "github.com/NibiruChain/nibiru/x/common/denoms" "github.com/NibiruChain/nibiru/x/common/testutil" - . "github.com/NibiruChain/nibiru/x/common/testutil/action" - . "github.com/NibiruChain/nibiru/x/common/testutil/assertion" - . "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" - . "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" + tutilaction "github.com/NibiruChain/nibiru/x/common/testutil/action" + tutilassert "github.com/NibiruChain/nibiru/x/common/testutil/assertion" + perpaction "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" + perpassert "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" types "github.com/NibiruChain/nibiru/x/perp/v2/types" ) @@ -25,21 +25,21 @@ func TestAddMargin(t *testing.T) { pairEthUsdc := asset.Registry.Pair(denoms.ETH, denoms.USDC) startBlockTime := time.Now() - tc := TestCases{ - TC("existing long position, add margin"). + tc := tutilaction.TestCases{ + tutilaction.TC("existing long position, add margin"). Given( - CreateCustomMarket(pairBtcUsdc), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(2020)))), - MarketOrder(alice, pairBtcUsdc, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), + perpaction.CreateCustomMarket(pairBtcUsdc), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(2020)))), + perpaction.MarketOrder(alice, pairBtcUsdc, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), ). When( - MoveToNextBlock(), - AddMargin(alice, pairBtcUsdc, sdk.NewInt(1000)), + tutilaction.MoveToNextBlock(), + perpaction.AddMargin(alice, pairBtcUsdc, sdk.NewInt(1000)), ). Then( - PositionShouldBeEqual(alice, pairBtcUsdc, Position_PositionShouldBeEqualTo( + perpassert.PositionShouldBeEqual(alice, pairBtcUsdc, perpassert.Position_PositionShouldBeEqualTo( types.Position{ Pair: pairBtcUsdc, TraderAddress: alice.String(), @@ -50,7 +50,7 @@ func TestAddMargin(t *testing.T) { LastUpdatedBlockNumber: 2, }, )), - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcUsdc, TraderAddress: alice.String(), @@ -71,25 +71,25 @@ func TestAddMargin(t *testing.T) { ExchangedNotional: sdk.MustNewDecFromStr("0"), ExchangedSize: sdk.MustNewDecFromStr("0"), }), - BalanceEqual(alice, denoms.USDC, sdk.ZeroInt()), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(10)), - ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.NewInt(10)), + tutilassert.BalanceEqual(alice, denoms.USDC, sdk.ZeroInt()), + tutilassert.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(10)), + tutilassert.ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.NewInt(10)), ), - TC("existing short position, add margin"). + tutilaction.TC("existing short position, add margin"). Given( - CreateCustomMarket(pairBtcUsdc), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(2020)))), - MarketOrder(alice, pairBtcUsdc, types.Direction_SHORT, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), + perpaction.CreateCustomMarket(pairBtcUsdc), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(2020)))), + perpaction.MarketOrder(alice, pairBtcUsdc, types.Direction_SHORT, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), ). When( - MoveToNextBlock(), - AddMargin(alice, pairBtcUsdc, sdk.NewInt(1000)), + tutilaction.MoveToNextBlock(), + perpaction.AddMargin(alice, pairBtcUsdc, sdk.NewInt(1000)), ). Then( - PositionShouldBeEqual(alice, pairBtcUsdc, Position_PositionShouldBeEqualTo( + perpassert.PositionShouldBeEqual(alice, pairBtcUsdc, perpassert.Position_PositionShouldBeEqualTo( types.Position{ Pair: pairBtcUsdc, TraderAddress: alice.String(), @@ -100,7 +100,7 @@ func TestAddMargin(t *testing.T) { LastUpdatedBlockNumber: 2, }, )), - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcUsdc, TraderAddress: alice.String(), @@ -121,35 +121,35 @@ func TestAddMargin(t *testing.T) { ExchangedNotional: sdk.MustNewDecFromStr("0"), ExchangedSize: sdk.MustNewDecFromStr("0"), }), - BalanceEqual(alice, denoms.USDC, sdk.ZeroInt()), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(10)), - ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.NewInt(10)), + tutilassert.BalanceEqual(alice, denoms.USDC, sdk.ZeroInt()), + tutilassert.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(10)), + tutilassert.ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.NewInt(10)), ), - TC("Testing fails"). + tutilaction.TC("Testing fails"). Given( - CreateCustomMarket(pairBtcUsdc), - CreateCustomMarket(pairEthUsdc), + perpaction.CreateCustomMarket(pairBtcUsdc), + perpaction.CreateCustomMarket(pairEthUsdc), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1020)))), - MarketOrder(alice, pairBtcUsdc, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1020)))), + perpaction.MarketOrder(alice, pairBtcUsdc, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), ). When( - MoveToNextBlock(), - AddMarginFail(alice, asset.MustNewPair("luna:usdt"), sdk.NewInt(1000), types.ErrPairNotFound), - AddMarginFail(alice, pairEthUsdc, sdk.NewInt(1000), collections.ErrNotFound), - AddMarginFail(alice, pairBtcUsdc, sdk.NewInt(1000), sdkerrors.ErrInsufficientFunds), + tutilaction.MoveToNextBlock(), + perpaction.AddMarginFail(alice, asset.MustNewPair("luna:usdt"), sdk.NewInt(1000), types.ErrPairNotFound), + perpaction.AddMarginFail(alice, pairEthUsdc, sdk.NewInt(1000), collections.ErrNotFound), + perpaction.AddMarginFail(alice, pairBtcUsdc, sdk.NewInt(1000), sdkerrors.ErrInsufficientFunds), - RemoveMarginFail(alice, asset.MustNewPair("luna:usdt"), sdk.NewInt(1000), types.ErrPairNotFound), - RemoveMarginFail(alice, pairEthUsdc, sdk.NewInt(1000), collections.ErrNotFound), - RemoveMarginFail(alice, pairBtcUsdc, sdk.NewInt(2000), types.ErrBadDebt), - RemoveMarginFail(alice, pairBtcUsdc, sdk.NewInt(900), types.ErrMarginRatioTooLow), + perpaction.RemoveMarginFail(alice, asset.MustNewPair("luna:usdt"), sdk.NewInt(1000), types.ErrPairNotFound), + perpaction.RemoveMarginFail(alice, pairEthUsdc, sdk.NewInt(1000), collections.ErrNotFound), + perpaction.RemoveMarginFail(alice, pairBtcUsdc, sdk.NewInt(2000), types.ErrBadDebt), + perpaction.RemoveMarginFail(alice, pairBtcUsdc, sdk.NewInt(900), types.ErrMarginRatioTooLow), ), } - NewTestSuite(t).WithTestCases(tc...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() } func TestRemoveMargin(t *testing.T) { @@ -157,21 +157,21 @@ func TestRemoveMargin(t *testing.T) { pairBtcUsdc := asset.Registry.Pair(denoms.BTC, denoms.USDC) startBlockTime := time.Now() - tc := TestCases{ - TC("existing long position, remove margin"). + tc := tutilaction.TestCases{ + tutilaction.TC("existing long position, remove margin"). Given( - CreateCustomMarket(pairBtcUsdc), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1002)))), - MarketOrder(alice, pairBtcUsdc, types.Direction_LONG, sdk.NewInt(1000), sdk.OneDec(), sdk.ZeroDec()), + perpaction.CreateCustomMarket(pairBtcUsdc), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1002)))), + perpaction.MarketOrder(alice, pairBtcUsdc, types.Direction_LONG, sdk.NewInt(1000), sdk.OneDec(), sdk.ZeroDec()), ). When( - MoveToNextBlock(), - RemoveMargin(alice, pairBtcUsdc, sdk.NewInt(500)), + tutilaction.MoveToNextBlock(), + perpaction.RemoveMargin(alice, pairBtcUsdc, sdk.NewInt(500)), ). Then( - PositionShouldBeEqual(alice, pairBtcUsdc, Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pairBtcUsdc, perpassert.Position_PositionShouldBeEqualTo(types.Position{ Pair: pairBtcUsdc, TraderAddress: alice.String(), Size_: sdk.MustNewDecFromStr("999.999999000000001000"), @@ -180,7 +180,7 @@ func TestRemoveMargin(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), LastUpdatedBlockNumber: 2, })), - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcUsdc, TraderAddress: alice.String(), @@ -201,25 +201,25 @@ func TestRemoveMargin(t *testing.T) { ExchangedNotional: sdk.MustNewDecFromStr("0"), ExchangedSize: sdk.MustNewDecFromStr("0"), }), - BalanceEqual(alice, denoms.USDC, sdk.NewInt(500)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.OneInt()), - ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.OneInt()), + tutilassert.BalanceEqual(alice, denoms.USDC, sdk.NewInt(500)), + tutilassert.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.OneInt()), + tutilassert.ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.OneInt()), ), - TC("existing long position, remove almost all margin fails"). + tutilaction.TC("existing long position, remove almost all margin fails"). Given( - CreateCustomMarket(pairBtcUsdc), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1002)))), - MarketOrder(alice, pairBtcUsdc, types.Direction_LONG, sdk.NewInt(1000), sdk.OneDec(), sdk.ZeroDec()), - MoveToNextBlock(), + perpaction.CreateCustomMarket(pairBtcUsdc), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1002)))), + perpaction.MarketOrder(alice, pairBtcUsdc, types.Direction_LONG, sdk.NewInt(1000), sdk.OneDec(), sdk.ZeroDec()), + tutilaction.MoveToNextBlock(), ). When( - RemoveMarginFail(alice, pairBtcUsdc, sdk.NewInt(999), types.ErrMarginRatioTooLow), + perpaction.RemoveMarginFail(alice, pairBtcUsdc, sdk.NewInt(999), types.ErrMarginRatioTooLow), ). Then( - PositionShouldBeEqual(alice, pairBtcUsdc, Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pairBtcUsdc, perpassert.Position_PositionShouldBeEqualTo(types.Position{ Pair: pairBtcUsdc, TraderAddress: alice.String(), Size_: sdk.MustNewDecFromStr("999.999999000000001000"), @@ -228,25 +228,25 @@ func TestRemoveMargin(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), LastUpdatedBlockNumber: 1, })), - BalanceEqual(alice, denoms.USDC, sdk.ZeroInt()), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.OneInt()), - ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.OneInt()), + tutilassert.BalanceEqual(alice, denoms.USDC, sdk.ZeroInt()), + tutilassert.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.OneInt()), + tutilassert.ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.OneInt()), ), - TC("existing short position, remove margin"). + tutilaction.TC("existing short position, remove margin"). Given( - CreateCustomMarket(pairBtcUsdc), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1002)))), - MarketOrder(alice, pairBtcUsdc, types.Direction_SHORT, sdk.NewInt(1000), sdk.OneDec(), sdk.ZeroDec()), + perpaction.CreateCustomMarket(pairBtcUsdc), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1002)))), + perpaction.MarketOrder(alice, pairBtcUsdc, types.Direction_SHORT, sdk.NewInt(1000), sdk.OneDec(), sdk.ZeroDec()), ). When( - MoveToNextBlock(), - RemoveMargin(alice, pairBtcUsdc, sdk.NewInt(500)), + tutilaction.MoveToNextBlock(), + perpaction.RemoveMargin(alice, pairBtcUsdc, sdk.NewInt(500)), ). Then( - PositionShouldBeEqual(alice, pairBtcUsdc, Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pairBtcUsdc, perpassert.Position_PositionShouldBeEqualTo(types.Position{ Pair: pairBtcUsdc, TraderAddress: alice.String(), Size_: sdk.MustNewDecFromStr("-1000.000001000000001000"), @@ -255,7 +255,7 @@ func TestRemoveMargin(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), LastUpdatedBlockNumber: 2, })), - PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcUsdc, TraderAddress: alice.String(), @@ -276,25 +276,25 @@ func TestRemoveMargin(t *testing.T) { ExchangedNotional: sdk.MustNewDecFromStr("0"), ExchangedSize: sdk.MustNewDecFromStr("0"), }), - BalanceEqual(alice, denoms.USDC, sdk.NewInt(500)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.OneInt()), - ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.OneInt()), + tutilassert.BalanceEqual(alice, denoms.USDC, sdk.NewInt(500)), + tutilassert.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.OneInt()), + tutilassert.ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.OneInt()), ), - TC("existing short position, remove almost all margin fails"). + tutilaction.TC("existing short position, remove almost all margin fails"). Given( - CreateCustomMarket(pairBtcUsdc), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1002)))), - MarketOrder(alice, pairBtcUsdc, types.Direction_SHORT, sdk.NewInt(1000), sdk.OneDec(), sdk.ZeroDec()), - MoveToNextBlock(), + perpaction.CreateCustomMarket(pairBtcUsdc), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1002)))), + perpaction.MarketOrder(alice, pairBtcUsdc, types.Direction_SHORT, sdk.NewInt(1000), sdk.OneDec(), sdk.ZeroDec()), + tutilaction.MoveToNextBlock(), ). When( - RemoveMarginFail(alice, pairBtcUsdc, sdk.NewInt(999), types.ErrMarginRatioTooLow), + perpaction.RemoveMarginFail(alice, pairBtcUsdc, sdk.NewInt(999), types.ErrMarginRatioTooLow), ). Then( - PositionShouldBeEqual(alice, pairBtcUsdc, Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pairBtcUsdc, perpassert.Position_PositionShouldBeEqualTo(types.Position{ Pair: pairBtcUsdc, TraderAddress: alice.String(), Size_: sdk.MustNewDecFromStr("-1000.000001000000001000"), @@ -303,11 +303,11 @@ func TestRemoveMargin(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), LastUpdatedBlockNumber: 1, })), - BalanceEqual(alice, denoms.USDC, sdk.ZeroInt()), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.OneInt()), - ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.OneInt()), + tutilassert.BalanceEqual(alice, denoms.USDC, sdk.ZeroInt()), + tutilassert.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.OneInt()), + tutilassert.ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.OneInt()), ), } - NewTestSuite(t).WithTestCases(tc...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() } diff --git a/x/perp/v2/keeper/msg_server_test.go b/x/perp/v2/keeper/msg_server_test.go index 4ec0e9bec..cd9ec1ccf 100644 --- a/x/perp/v2/keeper/msg_server_test.go +++ b/x/perp/v2/keeper/msg_server_test.go @@ -10,11 +10,11 @@ import ( "github.com/NibiruChain/nibiru/x/common/asset" "github.com/NibiruChain/nibiru/x/common/denoms" "github.com/NibiruChain/nibiru/x/common/testutil" - . "github.com/NibiruChain/nibiru/x/common/testutil/action" - . "github.com/NibiruChain/nibiru/x/common/testutil/assertion" + tutilaction "github.com/NibiruChain/nibiru/x/common/testutil/action" + tutilassert "github.com/NibiruChain/nibiru/x/common/testutil/assertion" "github.com/NibiruChain/nibiru/x/common/testutil/testapp" - . "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" - . "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" + 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/perp/v2/keeper" "github.com/NibiruChain/nibiru/x/perp/v2/types" ) @@ -23,18 +23,18 @@ func TestMsgServerMarketOrder(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) alice := testutil.AccAddress() - tests := TestCases{ - TC("open long position"). + tests := tutilaction.TestCases{ + tutilaction.TC("open long position"). Given( - CreateCustomMarket(pair), - FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), + perpaction.CreateCustomMarket(pair), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), ). When( - MsgServerMarketOrder(alice, pair, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroInt()), + perpaction.MsgServerMarketOrder(alice, pair, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroInt()), ). Then( - PositionShouldBeEqual(alice, pair, - Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pair, + perpassert.Position_PositionShouldBeEqualTo(types.Position{ TraderAddress: alice.String(), Pair: pair, Size_: sdk.MustNewDecFromStr("0.999999999999"), @@ -44,20 +44,20 @@ func TestMsgServerMarketOrder(t *testing.T) { LastUpdatedBlockNumber: 1, }), ), - BalanceEqual(alice, denoms.NUSD, sdk.NewInt(99)), + tutilassert.BalanceEqual(alice, denoms.NUSD, sdk.NewInt(99)), ), - TC("open short position"). + tutilaction.TC("open short position"). Given( - CreateCustomMarket(pair), - FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), + perpaction.CreateCustomMarket(pair), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), ). When( - MsgServerMarketOrder(alice, pair, types.Direction_SHORT, sdk.OneInt(), sdk.OneDec(), sdk.ZeroInt()), + perpaction.MsgServerMarketOrder(alice, pair, types.Direction_SHORT, sdk.OneInt(), sdk.OneDec(), sdk.ZeroInt()), ). Then( - PositionShouldBeEqual(alice, pair, - Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pair, + perpassert.Position_PositionShouldBeEqualTo(types.Position{ TraderAddress: alice.String(), Pair: pair, Size_: sdk.MustNewDecFromStr("-1.000000000001"), @@ -67,70 +67,70 @@ func TestMsgServerMarketOrder(t *testing.T) { LastUpdatedBlockNumber: 1, }), ), - BalanceEqual(alice, denoms.NUSD, sdk.NewInt(99)), + tutilassert.BalanceEqual(alice, denoms.NUSD, sdk.NewInt(99)), ), } - NewTestSuite(t).WithTestCases(tests...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tests...).Run() } func TestMsgServerClosePosition(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) alice := testutil.AccAddress() - tests := TestCases{ - TC("close long position"). + tests := tutilaction.TestCases{ + tutilaction.TC("close long position"). Given( - CreateCustomMarket(pair), - FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), - MarketOrder(alice, pair, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec()), - MoveToNextBlock(), + perpaction.CreateCustomMarket(pair), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), + perpaction.MarketOrder(alice, pair, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec()), + tutilaction.MoveToNextBlock(), ). When( - MsgServerClosePosition(alice, pair), + perpaction.MsgServerClosePosition(alice, pair), ). Then( - PositionShouldNotExist(alice, pair), - BalanceEqual(alice, denoms.NUSD, sdk.NewInt(100)), + perpassert.PositionShouldNotExist(alice, pair), + tutilassert.BalanceEqual(alice, denoms.NUSD, sdk.NewInt(100)), ), - TC("close short position"). + tutilaction.TC("close short position"). Given( - CreateCustomMarket(pair), - FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), - MarketOrder(alice, pair, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec()), - MoveToNextBlock(), + perpaction.CreateCustomMarket(pair), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), + perpaction.MarketOrder(alice, pair, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec()), + tutilaction.MoveToNextBlock(), ). When( - MsgServerClosePosition(alice, pair), + perpaction.MsgServerClosePosition(alice, pair), ). Then( - PositionShouldNotExist(alice, pair), - BalanceEqual(alice, denoms.NUSD, sdk.NewInt(100)), + perpassert.PositionShouldNotExist(alice, pair), + tutilassert.BalanceEqual(alice, denoms.NUSD, sdk.NewInt(100)), ), } - NewTestSuite(t).WithTestCases(tests...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tests...).Run() } func TestMsgServerAddMargin(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) alice := testutil.AccAddress() - tests := TestCases{ - TC("add margin"). + tests := tutilaction.TestCases{ + tutilaction.TC("add margin"). Given( - CreateCustomMarket(pair), - FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), - MarketOrder(alice, pair, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec()), - MoveToNextBlock(), + perpaction.CreateCustomMarket(pair), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), + perpaction.MarketOrder(alice, pair, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec()), + tutilaction.MoveToNextBlock(), ). When( - MsgServerAddMargin(alice, pair, sdk.OneInt()), + perpaction.MsgServerAddMargin(alice, pair, sdk.OneInt()), ). Then( - PositionShouldBeEqual(alice, pair, - Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pair, + perpassert.Position_PositionShouldBeEqualTo(types.Position{ TraderAddress: alice.String(), Pair: pair, Size_: sdk.MustNewDecFromStr("0.999999999999"), @@ -140,21 +140,21 @@ func TestMsgServerAddMargin(t *testing.T) { LastUpdatedBlockNumber: 2, }), ), - BalanceEqual(alice, denoms.NUSD, sdk.NewInt(98)), + tutilassert.BalanceEqual(alice, denoms.NUSD, sdk.NewInt(98)), ), - TC("partial close"). + tutilaction.TC("partial close"). Given( - CreateCustomMarket(pair), - FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), - MarketOrder(alice, pair, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec()), - MoveToNextBlock(), + perpaction.CreateCustomMarket(pair), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), + perpaction.MarketOrder(alice, pair, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec()), + tutilaction.MoveToNextBlock(), ). When( - MsgServerAddMargin(alice, pair, sdk.OneInt()), + perpaction.MsgServerAddMargin(alice, pair, sdk.OneInt()), ). Then( - PositionShouldBeEqual(alice, pair, - Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pair, + perpassert.Position_PositionShouldBeEqualTo(types.Position{ TraderAddress: alice.String(), Pair: pair, Size_: sdk.MustNewDecFromStr("0.999999999999"), @@ -164,31 +164,31 @@ func TestMsgServerAddMargin(t *testing.T) { LastUpdatedBlockNumber: 2, }), ), - BalanceEqual(alice, denoms.NUSD, sdk.NewInt(98)), + tutilassert.BalanceEqual(alice, denoms.NUSD, sdk.NewInt(98)), ), } - NewTestSuite(t).WithTestCases(tests...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tests...).Run() } func TestMsgServerRemoveMargin(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) alice := testutil.AccAddress() - tests := TestCases{ - TC("add margin"). + tests := tutilaction.TestCases{ + tutilaction.TC("add margin"). Given( - CreateCustomMarket(pair), - FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), - MarketOrder(alice, pair, types.Direction_LONG, sdk.NewInt(2), sdk.OneDec(), sdk.ZeroDec()), - MoveToNextBlock(), + perpaction.CreateCustomMarket(pair), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), + perpaction.MarketOrder(alice, pair, types.Direction_LONG, sdk.NewInt(2), sdk.OneDec(), sdk.ZeroDec()), + tutilaction.MoveToNextBlock(), ). When( - MsgServerRemoveMargin(alice, pair, sdk.OneInt()), + perpaction.MsgServerRemoveMargin(alice, pair, sdk.OneInt()), ). Then( - PositionShouldBeEqual(alice, pair, - Position_PositionShouldBeEqualTo(types.Position{ + perpassert.PositionShouldBeEqual(alice, pair, + perpassert.Position_PositionShouldBeEqualTo(types.Position{ TraderAddress: alice.String(), Pair: pair, Size_: sdk.MustNewDecFromStr("1.999999999996"), @@ -198,31 +198,31 @@ func TestMsgServerRemoveMargin(t *testing.T) { LastUpdatedBlockNumber: 2, }), ), - BalanceEqual(alice, denoms.NUSD, sdk.NewInt(99)), + tutilassert.BalanceEqual(alice, denoms.NUSD, sdk.NewInt(99)), ), } - NewTestSuite(t).WithTestCases(tests...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tests...).Run() } func TestMsgServerDonateToPerpEf(t *testing.T) { alice := testutil.AccAddress() - tests := TestCases{ - TC("success"). + tests := tutilaction.TestCases{ + tutilaction.TC("success"). Given( - FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), ). When( - MsgServerDonateToPerpEf(alice, sdk.NewInt(50)), + perpaction.MsgServerDonateToPerpEf(alice, sdk.NewInt(50)), ). Then( - BalanceEqual(alice, denoms.NUSD, sdk.NewInt(50)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(50)), + tutilassert.BalanceEqual(alice, denoms.NUSD, sdk.NewInt(50)), + tutilassert.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(50)), ), } - NewTestSuite(t).WithTestCases(tests...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tests...).Run() } func TestMsgServerMultiLiquidate(t *testing.T) { @@ -231,27 +231,27 @@ func TestMsgServerMultiLiquidate(t *testing.T) { liquidator := testutil.AccAddress() startTime := time.Now() - tests := TestCases{ - TC("partial liquidation"). + tests := tutilaction.TestCases{ + tutilaction.TC("partial liquidation"). Given( - SetBlockNumber(1), - SetBlockTime(startTime), - CreateCustomMarket(pairBtcUsdc), - InsertPosition(WithTrader(alice), WithPair(pairBtcUsdc), WithSize(sdk.NewDec(10000)), WithMargin(sdk.NewDec(1000)), WithOpenNotional(sdk.NewDec(10400))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startTime), + perpaction.CreateCustomMarket(pairBtcUsdc), + perpaction.InsertPosition(perpaction.WithTrader(alice), perpaction.WithPair(pairBtcUsdc), perpaction.WithSize(sdk.NewDec(10000)), perpaction.WithMargin(sdk.NewDec(1000)), perpaction.WithOpenNotional(sdk.NewDec(10400))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000))), ). When( - MoveToNextBlock(), - MsgServerMultiLiquidate(liquidator, false, - PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + tutilaction.MoveToNextBlock(), + perpaction.MsgServerMultiLiquidate(liquidator, false, + perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, ), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(750)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(125)), - BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(125)), - PositionShouldBeEqual(alice, pairBtcUsdc, - Position_PositionShouldBeEqualTo( + tutilassert.ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(750)), + tutilassert.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(125)), + tutilassert.BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(125)), + perpassert.PositionShouldBeEqual(alice, pairBtcUsdc, + perpassert.Position_PositionShouldBeEqualTo( types.Position{ Pair: pairBtcUsdc, TraderAddress: alice.String(), @@ -265,29 +265,29 @@ func TestMsgServerMultiLiquidate(t *testing.T) { ), ), - TC("full liquidation"). + tutilaction.TC("full liquidation"). Given( - SetBlockNumber(1), - SetBlockTime(startTime), - CreateCustomMarket(pairBtcUsdc), - InsertPosition(WithTrader(alice), WithPair(pairBtcUsdc), WithSize(sdk.NewDec(10000)), WithMargin(sdk.NewDec(1000)), WithOpenNotional(sdk.NewDec(10600))), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startTime), + perpaction.CreateCustomMarket(pairBtcUsdc), + perpaction.InsertPosition(perpaction.WithTrader(alice), perpaction.WithPair(pairBtcUsdc), perpaction.WithSize(sdk.NewDec(10000)), perpaction.WithMargin(sdk.NewDec(1000)), perpaction.WithOpenNotional(sdk.NewDec(10600))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000))), ). When( - MoveToNextBlock(), - MsgServerMultiLiquidate(liquidator, false, - PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + tutilaction.MoveToNextBlock(), + perpaction.MsgServerMultiLiquidate(liquidator, false, + perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, ), ). Then( - ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(600)), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(150)), - BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(250)), - PositionShouldNotExist(alice, pairBtcUsdc), + tutilassert.ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(600)), + tutilassert.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(150)), + tutilassert.BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(250)), + perpassert.PositionShouldNotExist(alice, pairBtcUsdc), ), } - NewTestSuite(t).WithTestCases(tests...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tests...).Run() } func TestFailMsgServer(t *testing.T) { diff --git a/x/perp/v2/keeper/twap_test.go b/x/perp/v2/keeper/twap_test.go index 608af5580..cf5d94575 100644 --- a/x/perp/v2/keeper/twap_test.go +++ b/x/perp/v2/keeper/twap_test.go @@ -11,11 +11,11 @@ import ( "github.com/NibiruChain/nibiru/x/common/asset" "github.com/NibiruChain/nibiru/x/common/denoms" - . "github.com/NibiruChain/nibiru/x/common/testutil/action" + tutilaction "github.com/NibiruChain/nibiru/x/common/testutil/action" "github.com/NibiruChain/nibiru/x/common/testutil/mock" "github.com/NibiruChain/nibiru/x/common/testutil/testapp" - . "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" - . "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" + perpaction "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" + perpassert "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" types "github.com/NibiruChain/nibiru/x/perp/v2/types" tmproto "github.com/cometbft/cometbft/proto/tendermint/types" @@ -25,84 +25,84 @@ func TestCalcTwap(t *testing.T) { pairBtcUsdc := asset.Registry.Pair(denoms.BTC, denoms.USDC) startTime := time.Now() - tc := TestCases{ - TC("spot twap"). + tc := tutilaction.TestCases{ + tutilaction.TC("spot twap"). Given( - CreateCustomMarket(pairBtcUsdc), - SetBlockTime(startTime), - InsertReserveSnapshot(pairBtcUsdc, startTime, WithPriceMultiplier(sdk.NewDec(9))), - InsertReserveSnapshot(pairBtcUsdc, startTime.Add(10*time.Second), WithPriceMultiplier(sdk.NewDec(10))), - InsertReserveSnapshot(pairBtcUsdc, startTime.Add(20*time.Second), WithPriceMultiplier(sdk.NewDec(11))), + perpaction.CreateCustomMarket(pairBtcUsdc), + tutilaction.SetBlockTime(startTime), + perpaction.InsertReserveSnapshot(pairBtcUsdc, startTime, perpaction.WithPriceMultiplier(sdk.NewDec(9))), + perpaction.InsertReserveSnapshot(pairBtcUsdc, startTime.Add(10*time.Second), perpaction.WithPriceMultiplier(sdk.NewDec(10))), + perpaction.InsertReserveSnapshot(pairBtcUsdc, startTime.Add(20*time.Second), perpaction.WithPriceMultiplier(sdk.NewDec(11))), ). When( - MoveToNextBlockWithDuration(30 * time.Second), + tutilaction.MoveToNextBlockWithDuration(30 * time.Second), ). Then( - TwapShouldBe(pairBtcUsdc, types.TwapCalcOption_SPOT, types.Direction_DIRECTION_UNSPECIFIED, sdk.ZeroDec(), 30*time.Second, sdk.NewDec(10)), + perpassert.TwapShouldBe(pairBtcUsdc, types.TwapCalcOption_SPOT, types.Direction_DIRECTION_UNSPECIFIED, sdk.ZeroDec(), 30*time.Second, sdk.NewDec(10)), ), - TC("base asset twap, long"). + tutilaction.TC("base asset twap, long"). Given( - CreateCustomMarket(pairBtcUsdc), - SetBlockTime(startTime), - InsertReserveSnapshot(pairBtcUsdc, startTime, WithPriceMultiplier(sdk.NewDec(9))), - InsertReserveSnapshot(pairBtcUsdc, startTime.Add(10*time.Second), WithPriceMultiplier(sdk.NewDec(10))), - InsertReserveSnapshot(pairBtcUsdc, startTime.Add(20*time.Second), WithPriceMultiplier(sdk.NewDec(11))), + perpaction.CreateCustomMarket(pairBtcUsdc), + tutilaction.SetBlockTime(startTime), + perpaction.InsertReserveSnapshot(pairBtcUsdc, startTime, perpaction.WithPriceMultiplier(sdk.NewDec(9))), + perpaction.InsertReserveSnapshot(pairBtcUsdc, startTime.Add(10*time.Second), perpaction.WithPriceMultiplier(sdk.NewDec(10))), + perpaction.InsertReserveSnapshot(pairBtcUsdc, startTime.Add(20*time.Second), perpaction.WithPriceMultiplier(sdk.NewDec(11))), ). When( - MoveToNextBlockWithDuration(30 * time.Second), + tutilaction.MoveToNextBlockWithDuration(30 * time.Second), ). Then( - TwapShouldBe(pairBtcUsdc, types.TwapCalcOption_BASE_ASSET_SWAP, types.Direction_LONG, sdk.NewDec(5), 30*time.Second, sdk.MustNewDecFromStr("50.000000000250000000")), + perpassert.TwapShouldBe(pairBtcUsdc, types.TwapCalcOption_BASE_ASSET_SWAP, types.Direction_LONG, sdk.NewDec(5), 30*time.Second, sdk.MustNewDecFromStr("50.000000000250000000")), ), - TC("base asset twap, short"). + tutilaction.TC("base asset twap, short"). Given( - CreateCustomMarket(pairBtcUsdc), - SetBlockTime(startTime), - InsertReserveSnapshot(pairBtcUsdc, startTime, WithPriceMultiplier(sdk.NewDec(9))), - InsertReserveSnapshot(pairBtcUsdc, startTime.Add(10*time.Second), WithPriceMultiplier(sdk.NewDec(10))), - InsertReserveSnapshot(pairBtcUsdc, startTime.Add(20*time.Second), WithPriceMultiplier(sdk.NewDec(11))), + perpaction.CreateCustomMarket(pairBtcUsdc), + tutilaction.SetBlockTime(startTime), + perpaction.InsertReserveSnapshot(pairBtcUsdc, startTime, perpaction.WithPriceMultiplier(sdk.NewDec(9))), + perpaction.InsertReserveSnapshot(pairBtcUsdc, startTime.Add(10*time.Second), perpaction.WithPriceMultiplier(sdk.NewDec(10))), + perpaction.InsertReserveSnapshot(pairBtcUsdc, startTime.Add(20*time.Second), perpaction.WithPriceMultiplier(sdk.NewDec(11))), ). When( - MoveToNextBlockWithDuration(30 * time.Second), + tutilaction.MoveToNextBlockWithDuration(30 * time.Second), ). Then( - TwapShouldBe(pairBtcUsdc, types.TwapCalcOption_BASE_ASSET_SWAP, types.Direction_SHORT, sdk.NewDec(5), 30*time.Second, sdk.MustNewDecFromStr("49.999999999750000000")), + perpassert.TwapShouldBe(pairBtcUsdc, types.TwapCalcOption_BASE_ASSET_SWAP, types.Direction_SHORT, sdk.NewDec(5), 30*time.Second, sdk.MustNewDecFromStr("49.999999999750000000")), ), - TC("quote asset twap, long"). + tutilaction.TC("quote asset twap, long"). Given( - CreateCustomMarket(pairBtcUsdc), - SetBlockTime(startTime), - InsertReserveSnapshot(pairBtcUsdc, startTime, WithPriceMultiplier(sdk.NewDec(9))), - InsertReserveSnapshot(pairBtcUsdc, startTime.Add(10*time.Second), WithPriceMultiplier(sdk.NewDec(10))), - InsertReserveSnapshot(pairBtcUsdc, startTime.Add(20*time.Second), WithPriceMultiplier(sdk.NewDec(11))), + perpaction.CreateCustomMarket(pairBtcUsdc), + tutilaction.SetBlockTime(startTime), + perpaction.InsertReserveSnapshot(pairBtcUsdc, startTime, perpaction.WithPriceMultiplier(sdk.NewDec(9))), + perpaction.InsertReserveSnapshot(pairBtcUsdc, startTime.Add(10*time.Second), perpaction.WithPriceMultiplier(sdk.NewDec(10))), + perpaction.InsertReserveSnapshot(pairBtcUsdc, startTime.Add(20*time.Second), perpaction.WithPriceMultiplier(sdk.NewDec(11))), ). When( - MoveToNextBlockWithDuration(30 * time.Second), + tutilaction.MoveToNextBlockWithDuration(30 * time.Second), ). Then( - TwapShouldBe(pairBtcUsdc, types.TwapCalcOption_QUOTE_ASSET_SWAP, types.Direction_LONG, sdk.NewDec(5), 30*time.Second, sdk.MustNewDecFromStr("0.503367003366748282")), + perpassert.TwapShouldBe(pairBtcUsdc, types.TwapCalcOption_QUOTE_ASSET_SWAP, types.Direction_LONG, sdk.NewDec(5), 30*time.Second, sdk.MustNewDecFromStr("0.503367003366748282")), ), - TC("quote asset twap, short"). + tutilaction.TC("quote asset twap, short"). Given( - CreateCustomMarket(pairBtcUsdc), - SetBlockTime(startTime), - InsertReserveSnapshot(pairBtcUsdc, startTime, WithPriceMultiplier(sdk.NewDec(9))), - InsertReserveSnapshot(pairBtcUsdc, startTime.Add(10*time.Second), WithPriceMultiplier(sdk.NewDec(10))), - InsertReserveSnapshot(pairBtcUsdc, startTime.Add(20*time.Second), WithPriceMultiplier(sdk.NewDec(11))), + perpaction.CreateCustomMarket(pairBtcUsdc), + tutilaction.SetBlockTime(startTime), + perpaction.InsertReserveSnapshot(pairBtcUsdc, startTime, perpaction.WithPriceMultiplier(sdk.NewDec(9))), + perpaction.InsertReserveSnapshot(pairBtcUsdc, startTime.Add(10*time.Second), perpaction.WithPriceMultiplier(sdk.NewDec(10))), + perpaction.InsertReserveSnapshot(pairBtcUsdc, startTime.Add(20*time.Second), perpaction.WithPriceMultiplier(sdk.NewDec(11))), ). When( - MoveToNextBlockWithDuration(30 * time.Second), + tutilaction.MoveToNextBlockWithDuration(30 * time.Second), ). Then( - TwapShouldBe(pairBtcUsdc, types.TwapCalcOption_QUOTE_ASSET_SWAP, types.Direction_SHORT, sdk.NewDec(5), 30*time.Second, sdk.MustNewDecFromStr("0.503367003367258451")), + perpassert.TwapShouldBe(pairBtcUsdc, types.TwapCalcOption_QUOTE_ASSET_SWAP, types.Direction_SHORT, sdk.NewDec(5), 30*time.Second, sdk.MustNewDecFromStr("0.503367003367258451")), ), } - NewTestSuite(t).WithTestCases(tc...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() } func TestInvalidTwap(t *testing.T) { diff --git a/x/perp/v2/keeper/withdraw_test.go b/x/perp/v2/keeper/withdraw_test.go index 5f0d60a7f..bc17860b2 100644 --- a/x/perp/v2/keeper/withdraw_test.go +++ b/x/perp/v2/keeper/withdraw_test.go @@ -9,10 +9,10 @@ import ( "github.com/NibiruChain/nibiru/x/common/asset" "github.com/NibiruChain/nibiru/x/common/denoms" "github.com/NibiruChain/nibiru/x/common/testutil" - . "github.com/NibiruChain/nibiru/x/common/testutil/action" - . "github.com/NibiruChain/nibiru/x/common/testutil/assertion" - . "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" - . "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" + tutilaction "github.com/NibiruChain/nibiru/x/common/testutil/action" + tutilassert "github.com/NibiruChain/nibiru/x/common/testutil/assertion" + 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/perp/v2/types" ) @@ -21,75 +21,75 @@ func TestWithdrawFromVault(t *testing.T) { pairBtcUsdc := asset.Registry.Pair(denoms.BTC, denoms.USDC) startBlockTime := time.Now() - tc := TestCases{ - TC("successful withdraw, no bad debt"). + tc := tutilaction.TestCases{ + tutilaction.TC("successful withdraw, no bad debt"). Given( - SetBlockNumber(1), - SetBlockTime(startBlockTime), - CreateCustomMarket(pairBtcUsdc), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1000)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + perpaction.CreateCustomMarket(pairBtcUsdc), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1000)))), ). When( - WithdrawFromVault(pairBtcUsdc, alice, sdk.NewInt(1000)), + perpaction.WithdrawFromVault(pairBtcUsdc, alice, sdk.NewInt(1000)), ). Then( - BalanceEqual(alice, denoms.USDC, sdk.NewInt(1000)), - ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.ZeroInt()), - MarketShouldBeEqual(pairBtcUsdc, Market_PrepaidBadDebtShouldBeEqualTo(sdk.ZeroInt())), + tutilassert.BalanceEqual(alice, denoms.USDC, sdk.NewInt(1000)), + tutilassert.ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.ZeroInt()), + perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_PrepaidBadDebtShouldBeEqualTo(sdk.ZeroInt())), ), - TC("successful withdraw, some bad debt"). + tutilaction.TC("successful withdraw, some bad debt"). Given( - SetBlockNumber(1), - SetBlockTime(startBlockTime), - CreateCustomMarket(pairBtcUsdc), - FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(500)))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(500)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + perpaction.CreateCustomMarket(pairBtcUsdc), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(500)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(500)))), ). When( - WithdrawFromVault(pairBtcUsdc, alice, sdk.NewInt(1000)), + perpaction.WithdrawFromVault(pairBtcUsdc, alice, sdk.NewInt(1000)), ). Then( - BalanceEqual(alice, denoms.USDC, sdk.NewInt(1000)), - ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.ZeroInt()), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.ZeroInt()), - MarketShouldBeEqual(pairBtcUsdc, Market_PrepaidBadDebtShouldBeEqualTo(sdk.NewInt(500))), + tutilassert.BalanceEqual(alice, denoms.USDC, sdk.NewInt(1000)), + tutilassert.ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.ZeroInt()), + tutilassert.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.ZeroInt()), + perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_PrepaidBadDebtShouldBeEqualTo(sdk.NewInt(500))), ), - TC("successful withdraw, all bad debt"). + tutilaction.TC("successful withdraw, all bad debt"). Given( - SetBlockNumber(1), - SetBlockTime(startBlockTime), - CreateCustomMarket(pairBtcUsdc), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1000)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + perpaction.CreateCustomMarket(pairBtcUsdc), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1000)))), ). When( - WithdrawFromVault(pairBtcUsdc, alice, sdk.NewInt(1000)), + perpaction.WithdrawFromVault(pairBtcUsdc, alice, sdk.NewInt(1000)), ). Then( - BalanceEqual(alice, denoms.USDC, sdk.NewInt(1000)), - ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.ZeroInt()), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.ZeroInt()), - MarketShouldBeEqual(pairBtcUsdc, Market_PrepaidBadDebtShouldBeEqualTo(sdk.NewInt(1000))), + tutilassert.BalanceEqual(alice, denoms.USDC, sdk.NewInt(1000)), + tutilassert.ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.ZeroInt()), + tutilassert.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.ZeroInt()), + perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_PrepaidBadDebtShouldBeEqualTo(sdk.NewInt(1000))), ), - TC("successful withdraw, existing bad debt"). + tutilaction.TC("successful withdraw, existing bad debt"). Given( - SetBlockNumber(1), - SetBlockTime(startBlockTime), - CreateCustomMarket(pairBtcUsdc, WithPrepaidBadDebt(sdk.NewInt(1000))), - FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1000)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + perpaction.CreateCustomMarket(pairBtcUsdc, perpaction.WithPrepaidBadDebt(sdk.NewInt(1000))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1000)))), ). When( - WithdrawFromVault(pairBtcUsdc, alice, sdk.NewInt(1000)), + perpaction.WithdrawFromVault(pairBtcUsdc, alice, sdk.NewInt(1000)), ). Then( - BalanceEqual(alice, denoms.USDC, sdk.NewInt(1000)), - ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.ZeroInt()), - ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.ZeroInt()), - MarketShouldBeEqual(pairBtcUsdc, Market_PrepaidBadDebtShouldBeEqualTo(sdk.NewInt(2000))), + tutilassert.BalanceEqual(alice, denoms.USDC, sdk.NewInt(1000)), + tutilassert.ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.ZeroInt()), + tutilassert.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.ZeroInt()), + perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_PrepaidBadDebtShouldBeEqualTo(sdk.NewInt(2000))), ), } - NewTestSuite(t).WithTestCases(tc...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() }