From 2b8cd27608c77802371365afbbacd8b0282053ee Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Fri, 25 Aug 2023 04:38:53 -0500 Subject: [PATCH 01/23] refactor(liquidate_test): use named imports --- x/perp/v2/keeper/liquidate_test.go | 320 ++++++++++++++++------------- 1 file changed, 174 insertions(+), 146 deletions(-) diff --git a/x/perp/v2/keeper/liquidate_test.go b/x/perp/v2/keeper/liquidate_test.go index d0a408d9f..08dbf1449 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" + action "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 := action.TestCases{ + action.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))), + action.SetBlockNumber(1), + action.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))), + action.FundModule( + types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000)), + ), ). When( - MoveToNextBlock(), - MultiLiquidate(liquidator, false, - PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + action.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"). + action.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))), + action.SetBlockNumber(1), + action.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))), + action.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}, + action.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"). + action.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))), + action.SetBlockNumber(1), + action.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))), + action.FundModule( + types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000)), + ), ). When( - MoveToNextBlock(), - MultiLiquidate(liquidator, false, - PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + action.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"). + action.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))), + action.SetBlockNumber(1), + action.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))), + action.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}, + action.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"). + action.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))), + action.SetBlockNumber(1), + action.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))), + action.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}, + action.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"). + action.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))), + action.SetBlockNumber(1), + action.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))), + action.FundModule( + types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000)), + ), + action.FundModule( + types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 50)), + ), ). When( - MoveToNextBlock(), - MultiLiquidate(liquidator, false, - PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + action.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"). + action.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))), + action.SetBlockNumber(1), + action.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))), + action.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000))), ). When( - MoveToNextBlock(), - MultiLiquidate(liquidator, false, - PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + action.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"). + action.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))), + action.SetBlockNumber(1), + action.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))), + action.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 10))), ). When( - MoveToNextBlock(), - MultiLiquidate(liquidator, true, - PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: false}, + action.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"). + action.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))), + action.SetBlockNumber(1), + action.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 + action.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 + action.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() + action.NewTestSuite(t).WithTestCases(tc...).Run() } func TestPrettyLiquidateResponse(t *testing.T) { From 6ad181be60181bb13a0b2eccf2524a723d8b6a6c Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Fri, 25 Aug 2023 04:46:57 -0500 Subject: [PATCH 02/23] refactor(amm_test): use named imports --- x/perp/v2/keeper/amm_test.go | 316 +++++++++++++++++------------------ 1 file changed, 158 insertions(+), 158 deletions(-) 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() } From 765f5a3eda2f14bb045abf6ce0f39c477beed7c5 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Fri, 25 Aug 2023 04:57:08 -0500 Subject: [PATCH 03/23] refactor(admin_test, calc_test, grpc_query_test): use named imports --- x/perp/v2/keeper/admin_test.go | 28 +-- x/perp/v2/keeper/calc_test.go | 74 +++---- x/perp/v2/keeper/grpc_query_test.go | 302 ++++++++++++++-------------- 3 files changed, 203 insertions(+), 201 deletions(-) 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/calc_test.go b/x/perp/v2/keeper/calc_test.go index dade53d60..8d06d37d4 100644 --- a/x/perp/v2/keeper/calc_test.go +++ b/x/perp/v2/keeper/calc_test.go @@ -15,8 +15,8 @@ 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" + tutilaction "github.com/NibiruChain/nibiru/x/common/testutil/action" + perpaction "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" . "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" ) @@ -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")), ), - 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")), ), - 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()), ), - 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")), ), } - NewTestSuite(t).WithTestCases(tc...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() } func TestUnrealizedPnl(t *testing.T) { diff --git a/x/perp/v2/keeper/grpc_query_test.go b/x/perp/v2/keeper/grpc_query_test.go index e425817a6..97fb2f09f 100644 --- a/x/perp/v2/keeper/grpc_query_test.go +++ b/x/perp/v2/keeper/grpc_query_test.go @@ -8,8 +8,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" ) @@ -18,38 +18,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), @@ -58,12 +58,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), @@ -72,44 +72,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), @@ -118,12 +118,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), @@ -132,44 +132,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), @@ -178,12 +178,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), @@ -192,41 +192,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), @@ -235,31 +235,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), @@ -268,31 +268,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), @@ -301,53 +301,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()), @@ -358,5 +360,5 @@ func TestQueryMarkets(t *testing.T) { ), } - NewTestSuite(t).WithTestCases(tc...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() } From c490ffcd6462a3f78625b99c306c63724571a7f7 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Fri, 25 Aug 2023 05:29:57 -0500 Subject: [PATCH 04/23] refactor(perp/keeper): use named imports --- x/perp/v2/keeper/calc_test.go | 10 +- x/perp/v2/keeper/clearing_house_test.go | 514 ++++++++++++------------ x/perp/v2/keeper/hooks_test.go | 142 +++---- x/perp/v2/keeper/liquidate_test.go | 98 ++--- x/perp/v2/keeper/margin_test.go | 80 ++-- x/perp/v2/keeper/msg_server_test.go | 210 +++++----- x/perp/v2/keeper/twap_test.go | 36 +- x/perp/v2/keeper/withdraw_test.go | 92 ++--- 8 files changed, 591 insertions(+), 591 deletions(-) diff --git a/x/perp/v2/keeper/calc_test.go b/x/perp/v2/keeper/calc_test.go index 8d06d37d4..dee020800 100644 --- a/x/perp/v2/keeper/calc_test.go +++ b/x/perp/v2/keeper/calc_test.go @@ -17,7 +17,7 @@ import ( tutilaction "github.com/NibiruChain/nibiru/x/common/testutil/action" perpaction "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" - . "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" + perpassert "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" ) func TestPositionNotionalSpot(t *testing.T) { @@ -83,7 +83,7 @@ func TestPositionNotionalTWAP(t *testing.T) { 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")), ), tutilaction.TC("short position"). @@ -100,7 +100,7 @@ func TestPositionNotionalTWAP(t *testing.T) { 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")), ), tutilaction.TC("zero position"). @@ -117,7 +117,7 @@ func TestPositionNotionalTWAP(t *testing.T) { tutilaction.MoveToNextBlockWithDuration(30 * time.Second), ). Then( - PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.ZeroDec()), + perpassert.PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.ZeroDec()), ), tutilaction.TC("single snapshot"). @@ -131,7 +131,7 @@ func TestPositionNotionalTWAP(t *testing.T) { 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")), ), } diff --git a/x/perp/v2/keeper/clearing_house_test.go b/x/perp/v2/keeper/clearing_house_test.go index f7a33c531..e460c0dcd 100644 --- a/x/perp/v2/keeper/clearing_house_test.go +++ b/x/perp/v2/keeper/clearing_house_test.go @@ -13,7 +13,7 @@ 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" + tutilaction "github.com/NibiruChain/nibiru/x/common/testutil/action" . "github.com/NibiruChain/nibiru/x/common/testutil/assertion" "github.com/NibiruChain/nibiru/x/common/testutil/mock" "github.com/NibiruChain/nibiru/x/common/testutil/testapp" @@ -29,20 +29,20 @@ 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( pairBtcNusd, WithPricePeg(sdk.OneDec()), 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()), @@ -57,12 +57,12 @@ func TestMarketOrder(t *testing.T) { 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)))), + 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(), @@ -120,16 +120,16 @@ 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), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), CreateCustomMarket(pairBtcNusd), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(2040)))), + tutilaction.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()), ). When( - MoveToNextBlock(), + tutilaction.MoveToNextBlock(), MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec(), MarketOrderResp_PositionShouldBeEqual( types.Position{ @@ -176,16 +176,16 @@ 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( pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("0.89")), WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(18)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(18)))), InsertPosition( WithPair(pairBtcNusd), WithTrader(alice), @@ -195,7 +195,7 @@ func TestMarketOrder(t *testing.T) { ), ). When( - MoveToNextBlock(), + tutilaction.MoveToNextBlock(), MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), types.ErrMarginRatioTooLow, ), @@ -212,16 +212,16 @@ 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( pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("0.89")), WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(18)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(18)))), InsertPosition( WithPair(pairBtcNusd), WithTrader(alice), @@ -231,7 +231,7 @@ func TestMarketOrder(t *testing.T) { ), ). When( - MoveToNextBlock(), + tutilaction.MoveToNextBlock(), MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), types.ErrMarginRatioTooLow, ), @@ -248,18 +248,18 @@ 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( pairBtcNusd, WithPricePeg(sdk.OneDec()), WithSqrtDepth(sdk.NewDec(100_000)), ), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_000)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.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)))), + tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(100_000_000)))), EditSwapInvariant(pairBtcNusd, sdk.OneDec()), ). When( @@ -269,16 +269,16 @@ func TestMarketOrder(t *testing.T) { 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)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.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()), ). When( - MoveToNextBlock(), + tutilaction.MoveToNextBlock(), MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(500), sdk.NewDec(10), sdk.ZeroDec(), MarketOrderResp_PositionShouldBeEqual( types.Position{ @@ -326,16 +326,16 @@ 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( pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("0.89")), WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(18)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(18)))), InsertPosition( WithPair(pairBtcNusd), WithTrader(alice), @@ -345,7 +345,7 @@ func TestMarketOrder(t *testing.T) { ), ). When( - MoveToNextBlock(), + tutilaction.MoveToNextBlock(), MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), types.ErrMarginRatioTooLow, ), @@ -362,16 +362,16 @@ 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)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.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()), ). When( - MoveToNextBlock(), + tutilaction.MoveToNextBlock(), MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), MarketOrderResp_PositionShouldBeEqual( types.Position{ @@ -419,16 +419,16 @@ 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( pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("0.89")), WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(18)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(18)))), InsertPosition( WithPair(pairBtcNusd), WithTrader(alice), @@ -438,7 +438,7 @@ func TestMarketOrder(t *testing.T) { ), ). When( - MoveToNextBlock(), + tutilaction.MoveToNextBlock(), MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), MarketOrderResp_PositionShouldBeEqual( types.Position{ @@ -485,12 +485,12 @@ 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)))), + 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(), @@ -550,16 +550,16 @@ 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)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.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()), ). When( - MoveToNextBlock(), + tutilaction.MoveToNextBlock(), MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec(), MarketOrderResp_PositionShouldBeEqual( types.Position{ @@ -607,15 +607,15 @@ 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")), ), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(22)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(22)))), InsertPosition( WithPair(pairBtcNusd), WithTrader(alice), @@ -625,7 +625,7 @@ func TestMarketOrder(t *testing.T) { ), ). When( - MoveToNextBlock(), + tutilaction.MoveToNextBlock(), MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), types.ErrMarginRatioTooLow, ), @@ -642,16 +642,16 @@ 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)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.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()), ). When( - MoveToNextBlock(), + tutilaction.MoveToNextBlock(), MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(500), sdk.NewDec(10), sdk.ZeroDec(), MarketOrderResp_PositionShouldBeEqual( types.Position{ @@ -699,15 +699,15 @@ 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")), ), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(22)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(22)))), InsertPosition( WithPair(pairBtcNusd), WithTrader(alice), @@ -717,7 +717,7 @@ func TestMarketOrder(t *testing.T) { ), ). When( - MoveToNextBlock(), + tutilaction.MoveToNextBlock(), MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), types.ErrMarginRatioTooLow, ), @@ -734,16 +734,16 @@ 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)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.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()), ). When( - MoveToNextBlock(), + tutilaction.MoveToNextBlock(), MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), MarketOrderResp_PositionShouldBeEqual( types.Position{ @@ -791,15 +791,15 @@ 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")), ), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(22)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(22)))), InsertPosition( WithPair(pairBtcNusd), WithTrader(alice), @@ -809,7 +809,7 @@ func TestMarketOrder(t *testing.T) { ), ). When( - MoveToNextBlock(), + tutilaction.MoveToNextBlock(), MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), MarketOrderResp_PositionShouldBeEqual( types.Position{ @@ -857,12 +857,12 @@ 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)))), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(99)))), ). When( MarketOrderFails( @@ -873,12 +873,12 @@ func TestMarketOrder(t *testing.T) { 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)))), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.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), ). @@ -889,12 +889,12 @@ func TestMarketOrder(t *testing.T) { 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)))), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(47_714_285_715)))), SetMarketEnabled(pairBtcNusd, false), ). When( @@ -905,12 +905,12 @@ func TestMarketOrder(t *testing.T) { 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)))), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.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), ). @@ -923,11 +923,11 @@ func TestMarketOrder(t *testing.T) { 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(), @@ -937,12 +937,12 @@ func TestMarketOrder(t *testing.T) { PositionShouldNotExist(alice, pairBtcNusd), ), - TC("zero quote asset amount"). + tutilaction.TC("zero quote asset amount"). Given( - SetBlockTime(startBlockTime), - SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), CreateCustomMarket(pairBtcNusd), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(47_714_285_715)))), + 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(), @@ -952,12 +952,12 @@ func TestMarketOrder(t *testing.T) { PositionShouldNotExist(alice, pairBtcNusd), ), - TC("zero leverage"). + tutilaction.TC("zero leverage"). Given( - SetBlockTime(startBlockTime), - SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), CreateCustomMarket(pairBtcNusd), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + 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(), @@ -967,12 +967,12 @@ func TestMarketOrder(t *testing.T) { 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), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), CreateCustomMarket(pairBtcNusd), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + 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(), @@ -982,12 +982,12 @@ func TestMarketOrder(t *testing.T) { 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), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.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.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()), @@ -997,12 +997,12 @@ func TestMarketOrder(t *testing.T) { 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), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), CreateCustomMarket(pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("25001.0112"))), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + 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()), @@ -1011,12 +1011,12 @@ func TestMarketOrder(t *testing.T) { Then( 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), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), CreateCustomMarket(pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("25001.0112"))), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + 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()), @@ -1026,12 +1026,12 @@ func TestMarketOrder(t *testing.T) { 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), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), CreateCustomMarket(pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("25001.0112"))), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + 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()), @@ -1041,12 +1041,12 @@ func TestMarketOrder(t *testing.T) { 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), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), CreateCustomMarket(pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("25000"))), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + 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()), @@ -1058,7 +1058,7 @@ func TestMarketOrder(t *testing.T) { ), } - NewTestSuite(t).WithTestCases(tc...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() } func TestMarketOrderError(t *testing.T) { @@ -1199,17 +1199,17 @@ 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( pairBtcNusd, WithPricePeg(sdk.NewDec(2)), WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10)))), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10)))), InsertPosition( WithPair(pairBtcNusd), WithTrader(alice), @@ -1254,16 +1254,16 @@ func TestPartialClose(t *testing.T) { }), ), - TC("partial close long position with negative PnL"). + tutilaction.TC("partial close long position with negative PnL"). Given( CreateCustomMarket( pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("0.95")), WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(4)))), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(4)))), InsertPosition( WithPair(pairBtcNusd), WithTrader(alice), @@ -1308,16 +1308,16 @@ 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( pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("0.94")), WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(4)))), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(4)))), InsertPosition( WithPair(pairBtcNusd), WithTrader(alice), @@ -1361,17 +1361,17 @@ 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( pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("0.59")), 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))), + 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))), InsertPosition( WithPair(pairBtcNusd), WithTrader(alice), @@ -1415,16 +1415,16 @@ 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( pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("0.10")), WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(2)))), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(2)))), InsertPosition( WithPair(pairBtcNusd), WithTrader(alice), @@ -1469,16 +1469,16 @@ func TestPartialClose(t *testing.T) { }), ), - TC("partial close short position with negative PnL"). + tutilaction.TC("partial close short position with negative PnL"). Given( CreateCustomMarket( pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("1.05")), WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(16)))), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(16)))), InsertPosition( WithPair(pairBtcNusd), WithTrader(alice), @@ -1523,16 +1523,16 @@ 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( pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("1.09")), WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - SetBlockTime(startBlockTime), - SetBlockNumber(1), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(16)))), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(16)))), InsertPosition( WithPair(pairBtcNusd), WithTrader(alice), @@ -1577,17 +1577,17 @@ func TestPartialClose(t *testing.T) { }), ), - TC("partial close short position with bad debt"). + tutilaction.TC("partial close short position with bad debt"). Given( CreateCustomMarket( pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("1.14")), 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))), + 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))), InsertPosition( WithPair(pairBtcNusd), WithTrader(alice), @@ -1631,17 +1631,17 @@ func TestPartialClose(t *testing.T) { ExchangedSize: sdk.MustNewDecFromStr("7500.000000000000000000"), }), ), - TC("test partial closes fail"). + tutilaction.TC("test partial closes fail"). Given( CreateCustomMarket( pairBtcNusd, WithPricePeg(sdk.OneDec()), 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()), @@ -1651,7 +1651,7 @@ func TestPartialClose(t *testing.T) { ), } - NewTestSuite(t).WithTestCases(tc...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() } func TestClosePosition(t *testing.T) { @@ -1659,17 +1659,17 @@ 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")), ), - 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))), + 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))), InsertPosition( WithTrader(alice), WithPair(pairBtcNusd), @@ -1706,16 +1706,16 @@ 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")), ), - 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))), + 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))), InsertPosition( WithTrader(alice), WithPair(pairBtcNusd), @@ -1752,14 +1752,14 @@ 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")), ), - SetBlockNumber(1), - SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), InsertPosition( WithTrader(alice), WithPair(pairBtcNusd), @@ -1767,12 +1767,12 @@ func TestClosePosition(t *testing.T) { WithMargin(sdk.NewDec(1_000)), 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(), + tutilaction.MoveToNextBlock(), ClosePosition(alice, pairBtcNusd), ). Then( @@ -1802,16 +1802,16 @@ func TestClosePosition(t *testing.T) { 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")), ), - 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))), + 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))), InsertPosition( WithTrader(alice), WithPair(pairBtcNusd), @@ -1848,16 +1848,16 @@ 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")), ), - 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))), + 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))), InsertPosition( WithTrader(alice), WithPair(pairBtcNusd), @@ -1894,14 +1894,14 @@ 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")), ), - SetBlockTime(startBlockTime), - SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.SetBlockNumber(1), InsertPosition( WithTrader(alice), WithPair(pairBtcNusd), @@ -1909,12 +1909,12 @@ func TestClosePosition(t *testing.T) { WithMargin(sdk.NewDec(1_000)), 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(), + tutilaction.MoveToNextBlock(), ClosePosition(alice, pairBtcNusd), ). Then( @@ -1945,7 +1945,7 @@ func TestClosePosition(t *testing.T) { ), } - NewTestSuite(t).WithTestCases(tc...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() } func TestUpdateSwapInvariant(t *testing.T) { @@ -1956,14 +1956,14 @@ 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)))), + 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()), @@ -1972,13 +1972,13 @@ func TestUpdateSwapInvariant(t *testing.T) { Then( 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)))), + 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)), @@ -1987,13 +1987,13 @@ func TestUpdateSwapInvariant(t *testing.T) { Then( 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)))), + 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()), @@ -2007,13 +2007,13 @@ func TestUpdateSwapInvariant(t *testing.T) { PositionShouldNotExist(alice, pairBtcNusd), 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)))), + 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()), @@ -2028,13 +2028,13 @@ func TestUpdateSwapInvariant(t *testing.T) { 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)))), + 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()), @@ -2048,13 +2048,13 @@ func TestUpdateSwapInvariant(t *testing.T) { PositionShouldNotExist(alice, pairBtcNusd), 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)))), + 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()), @@ -2069,13 +2069,13 @@ func TestUpdateSwapInvariant(t *testing.T) { 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)))), + 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()), @@ -2100,13 +2100,13 @@ func TestUpdateSwapInvariant(t *testing.T) { ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), 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)))), + 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()), @@ -2133,5 +2133,5 @@ func TestUpdateSwapInvariant(t *testing.T) { ), } - NewTestSuite(t).WithTestCases(tc...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() } 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 08dbf1449..dde25da7c 100644 --- a/x/perp/v2/keeper/liquidate_test.go +++ b/x/perp/v2/keeper/liquidate_test.go @@ -10,7 +10,7 @@ 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/action" + 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" @@ -30,11 +30,11 @@ func TestMultiLiquidate(t *testing.T) { liquidator := testutil.AccAddress() startTime := time.Now() - tc := action.TestCases{ - action.TC("partial liquidation"). + tc := tutilaction.TestCases{ + tutilaction.TC("partial liquidation"). Given( - action.SetBlockNumber(1), - action.SetBlockTime(startTime), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startTime), perpaction.CreateCustomMarket(pairBtcUsdc), perpaction.InsertPosition( perpaction.WithTrader(alice), @@ -42,12 +42,12 @@ func TestMultiLiquidate(t *testing.T) { perpaction.WithSize(sdk.NewDec(10000)), perpaction.WithMargin(sdk.NewDec(1000)), perpaction.WithOpenNotional(sdk.NewDec(10400))), - action.FundModule( + tutilaction.FundModule( types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000)), ), ). When( - action.MoveToNextBlock(), + tutilaction.MoveToNextBlock(), perpaction.MultiLiquidate(liquidator, false, perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, ), @@ -71,10 +71,10 @@ func TestMultiLiquidate(t *testing.T) { ), ), - action.TC("full liquidation"). + tutilaction.TC("full liquidation"). Given( - action.SetBlockNumber(1), - action.SetBlockTime(startTime), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startTime), perpaction.CreateCustomMarket(pairBtcUsdc), perpaction.InsertPosition( perpaction.WithTrader(alice), @@ -82,12 +82,12 @@ func TestMultiLiquidate(t *testing.T) { perpaction.WithSize(sdk.NewDec(10000)), perpaction.WithMargin(sdk.NewDec(1000)), perpaction.WithOpenNotional(sdk.NewDec(10600))), - action.FundModule( + tutilaction.FundModule( types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000)), ), ). When( - action.MoveToNextBlock(), + tutilaction.MoveToNextBlock(), perpaction.MultiLiquidate(liquidator, false, perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, @@ -101,18 +101,18 @@ func TestMultiLiquidate(t *testing.T) { perpassertion.PositionShouldNotExist(alice, pairBtcUsdc), ), - action.TC("full liquidation"). + tutilaction.TC("full liquidation"). Given( - action.SetBlockNumber(1), - action.SetBlockTime(startTime), + 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))), - action.FundModule( + tutilaction.FundModule( types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000)), ), ). When( - action.MoveToNextBlock(), + tutilaction.MoveToNextBlock(), perpaction.MultiLiquidate(liquidator, false, perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, ), @@ -124,18 +124,18 @@ func TestMultiLiquidate(t *testing.T) { perpassertion.PositionShouldNotExist(alice, pairBtcUsdc), ), - action.TC("one fail liquidation - one correct"). + tutilaction.TC("one fail liquidation - one correct"). Given( - action.SetBlockNumber(1), - action.SetBlockTime(startTime), + 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))), - action.FundModule( + tutilaction.FundModule( types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000)), ), ). When( - action.MoveToNextBlock(), + tutilaction.MoveToNextBlock(), perpaction.MultiLiquidate(liquidator, false, perpaction.PairTraderTuple{Pair: pairBtcUsdc, Successful: false}, perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, @@ -148,18 +148,18 @@ func TestMultiLiquidate(t *testing.T) { perpassertion.PositionShouldNotExist(alice, pairBtcUsdc), ), - action.TC("one fail liquidation because market does not exists- one correct"). + tutilaction.TC("one fail liquidation because market does not exists- one correct"). Given( - action.SetBlockNumber(1), - action.SetBlockTime(startTime), + 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))), - action.FundModule( + tutilaction.FundModule( types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000)), ), ). When( - action.MoveToNextBlock(), + tutilaction.MoveToNextBlock(), perpaction.MultiLiquidate(liquidator, false, perpaction.PairTraderTuple{Pair: asset.MustNewPair("luna:usdt"), Successful: false}, perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, @@ -172,21 +172,21 @@ func TestMultiLiquidate(t *testing.T) { perpassertion.PositionShouldNotExist(alice, pairBtcUsdc), ), - action.TC("realizes bad debt"). + tutilaction.TC("realizes bad debt"). Given( - action.SetBlockNumber(1), - action.SetBlockTime(startTime), + 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))), - action.FundModule( + tutilaction.FundModule( types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000)), ), - action.FundModule( + tutilaction.FundModule( types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 50)), ), ). When( - action.MoveToNextBlock(), + tutilaction.MoveToNextBlock(), perpaction.MultiLiquidate(liquidator, false, perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, ), @@ -198,16 +198,16 @@ func TestMultiLiquidate(t *testing.T) { perpassertion.PositionShouldNotExist(alice, pairBtcUsdc), ), - action.TC("uses prepaid bad debt"). + tutilaction.TC("uses prepaid bad debt"). Given( - action.SetBlockNumber(1), - action.SetBlockTime(startTime), + 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))), - action.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 1000))), ). When( - action.MoveToNextBlock(), + tutilaction.MoveToNextBlock(), perpaction.MultiLiquidate(liquidator, false, perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, ), @@ -223,16 +223,16 @@ func TestMultiLiquidate(t *testing.T) { ), ), - action.TC("healthy position"). + tutilaction.TC("healthy position"). Given( - action.SetBlockNumber(1), - action.SetBlockTime(startTime), + 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))), - action.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 10))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 10))), ). When( - action.MoveToNextBlock(), + tutilaction.MoveToNextBlock(), perpaction.MultiLiquidate(liquidator, true, perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: false}, ), @@ -262,20 +262,20 @@ func TestMultiLiquidate(t *testing.T) { }), ), - action.TC("mixed bag"). + tutilaction.TC("mixed bag"). Given( - action.SetBlockNumber(1), - action.SetBlockTime(startTime), + 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 - action.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 3000))), + tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.USDC, 3000))), ). When( - action.MoveToNextBlock(), + tutilaction.MoveToNextBlock(), perpaction.MultiLiquidate(liquidator, false, perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, perpaction.PairTraderTuple{Pair: pairEthUsdc, Trader: alice, Successful: true}, @@ -337,7 +337,7 @@ func TestMultiLiquidate(t *testing.T) { ), } - action.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..239adc45e 100644 --- a/x/perp/v2/keeper/margin_test.go +++ b/x/perp/v2/keeper/margin_test.go @@ -12,7 +12,7 @@ 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" + tutilaction "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" @@ -25,17 +25,17 @@ 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)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.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()), ). When( - MoveToNextBlock(), + tutilaction.MoveToNextBlock(), AddMargin(alice, pairBtcUsdc, sdk.NewInt(1000)), ). Then( @@ -76,16 +76,16 @@ func TestAddMargin(t *testing.T) { 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)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.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()), ). When( - MoveToNextBlock(), + tutilaction.MoveToNextBlock(), AddMargin(alice, pairBtcUsdc, sdk.NewInt(1000)), ). Then( @@ -126,18 +126,18 @@ func TestAddMargin(t *testing.T) { ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.NewInt(10)), ), - TC("Testing fails"). + tutilaction.TC("Testing fails"). Given( CreateCustomMarket(pairBtcUsdc), CreateCustomMarket(pairEthUsdc), - SetBlockNumber(1), - SetBlockTime(startBlockTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1020)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.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()), ). When( - MoveToNextBlock(), + tutilaction.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), @@ -149,7 +149,7 @@ func TestAddMargin(t *testing.T) { ), } - NewTestSuite(t).WithTestCases(tc...).Run() + tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() } func TestRemoveMargin(t *testing.T) { @@ -157,17 +157,17 @@ 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)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1002)))), MarketOrder(alice, pairBtcUsdc, types.Direction_LONG, sdk.NewInt(1000), sdk.OneDec(), sdk.ZeroDec()), ). When( - MoveToNextBlock(), + tutilaction.MoveToNextBlock(), RemoveMargin(alice, pairBtcUsdc, sdk.NewInt(500)), ). Then( @@ -206,14 +206,14 @@ func TestRemoveMargin(t *testing.T) { 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)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.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(), + tutilaction.MoveToNextBlock(), ). When( RemoveMarginFail(alice, pairBtcUsdc, sdk.NewInt(999), types.ErrMarginRatioTooLow), @@ -233,16 +233,16 @@ func TestRemoveMargin(t *testing.T) { 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)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1002)))), MarketOrder(alice, pairBtcUsdc, types.Direction_SHORT, sdk.NewInt(1000), sdk.OneDec(), sdk.ZeroDec()), ). When( - MoveToNextBlock(), + tutilaction.MoveToNextBlock(), RemoveMargin(alice, pairBtcUsdc, sdk.NewInt(500)), ). Then( @@ -281,14 +281,14 @@ func TestRemoveMargin(t *testing.T) { 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)))), + tutilaction.SetBlockNumber(1), + tutilaction.SetBlockTime(startBlockTime), + tutilaction.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(), + tutilaction.MoveToNextBlock(), ). When( RemoveMarginFail(alice, pairBtcUsdc, sdk.NewInt(999), types.ErrMarginRatioTooLow), @@ -309,5 +309,5 @@ func TestRemoveMargin(t *testing.T) { ), } - 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..f93d4cdd9 100644 --- a/x/perp/v2/keeper/twap_test.go +++ b/x/perp/v2/keeper/twap_test.go @@ -11,7 +11,7 @@ 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" @@ -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), + tutilaction.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))), ). 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)), ), - TC("base asset twap, long"). + tutilaction.TC("base asset twap, long"). Given( CreateCustomMarket(pairBtcUsdc), - SetBlockTime(startTime), + tutilaction.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))), ). 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")), ), - TC("base asset twap, short"). + tutilaction.TC("base asset twap, short"). Given( CreateCustomMarket(pairBtcUsdc), - SetBlockTime(startTime), + tutilaction.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))), ). 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")), ), - TC("quote asset twap, long"). + tutilaction.TC("quote asset twap, long"). Given( CreateCustomMarket(pairBtcUsdc), - SetBlockTime(startTime), + tutilaction.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))), ). 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")), ), - TC("quote asset twap, short"). + tutilaction.TC("quote asset twap, short"). Given( CreateCustomMarket(pairBtcUsdc), - SetBlockTime(startTime), + tutilaction.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))), ). 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")), ), } - 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() } From 02a2e9d1ed07542b826c678db93027956b3ae68f Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Fri, 25 Aug 2023 05:44:51 -0500 Subject: [PATCH 05/23] refactor(perp/keeper): use named imports | Complete --- x/perp/v2/keeper/clearing_house_test.go | 1034 +++++++++++------------ x/perp/v2/keeper/margin_test.go | 118 +-- x/perp/v2/keeper/twap_test.go | 54 +- 3 files changed, 603 insertions(+), 603 deletions(-) diff --git a/x/perp/v2/keeper/clearing_house_test.go b/x/perp/v2/keeper/clearing_house_test.go index e460c0dcd..ecc370adf 100644 --- a/x/perp/v2/keeper/clearing_house_test.go +++ b/x/perp/v2/keeper/clearing_house_test.go @@ -14,11 +14,11 @@ import ( "github.com/NibiruChain/nibiru/x/common/denoms" "github.com/NibiruChain/nibiru/x/common/testutil" tutilaction "github.com/NibiruChain/nibiru/x/common/testutil/action" - . "github.com/NibiruChain/nibiru/x/common/testutil/assertion" + 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" ) @@ -32,10 +32,10 @@ func TestMarketOrder(t *testing.T) { 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)), ), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), @@ -44,29 +44,29 @@ func TestMarketOrder(t *testing.T) { 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)), ), tutilaction.TC("new long position"). Given( - CreateCustomMarket(pairBtcNusd), + 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(), @@ -124,14 +124,14 @@ func TestMarketOrder(t *testing.T) { Given( tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), - CreateCustomMarket(pairBtcNusd), + perpaction.CreateCustomMarket(pairBtcNusd), tutilaction.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()), + perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), ). When( tutilaction.MoveToNextBlock(), - 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(), @@ -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(), @@ -178,30 +178,30 @@ func TestMarketOrder(t *testing.T) { 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")), ), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), tutilaction.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)), + 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( tutilaction.MoveToNextBlock(), - MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), + 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), @@ -214,30 +214,30 @@ func TestMarketOrder(t *testing.T) { 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")), ), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), tutilaction.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)), + 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( tutilaction.MoveToNextBlock(), - MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), + 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), @@ -250,37 +250,37 @@ func TestMarketOrder(t *testing.T) { 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)), ), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), tutilaction.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()), + 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)))), - EditSwapInvariant(pairBtcNusd, sdk.OneDec()), + perpaction.EditSwapInvariant(pairBtcNusd, sdk.OneDec()), ). When( - ClosePosition(alice, pairBtcNusd), + perpaction.ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + perpassert.PositionShouldNotExist(alice, pairBtcNusd), ), tutilaction.TC("existing long position, decrease a bit"). Given( - CreateCustomMarket(pairBtcNusd), + perpaction.CreateCustomMarket(pairBtcNusd), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), tutilaction.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.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), ). When( tutilaction.MoveToNextBlock(), - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(500), sdk.NewDec(10), sdk.ZeroDec(), - MarketOrderResp_PositionShouldBeEqual( + 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(), @@ -328,30 +328,30 @@ func TestMarketOrder(t *testing.T) { 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")), ), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), tutilaction.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)), + 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( tutilaction.MoveToNextBlock(), - MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), + 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), @@ -364,16 +364,16 @@ func TestMarketOrder(t *testing.T) { tutilaction.TC("existing long position, decrease a lot"). Given( - CreateCustomMarket(pairBtcNusd), + perpaction.CreateCustomMarket(pairBtcNusd), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), tutilaction.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.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), ). When( tutilaction.MoveToNextBlock(), - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), - MarketOrderResp_PositionShouldBeEqual( + 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(), @@ -421,26 +421,26 @@ func TestMarketOrder(t *testing.T) { 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")), ), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), tutilaction.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)), + 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( tutilaction.MoveToNextBlock(), - MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), - MarketOrderResp_PositionShouldBeEqual( + 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(), @@ -487,14 +487,14 @@ func TestMarketOrder(t *testing.T) { tutilaction.TC("new short position"). Given( - CreateCustomMarket(pairBtcNusd), + 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(), @@ -552,16 +552,16 @@ func TestMarketOrder(t *testing.T) { tutilaction.TC("existing short position, go more short"). Given( - CreateCustomMarket(pairBtcNusd), + perpaction.CreateCustomMarket(pairBtcNusd), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), tutilaction.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.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), ). When( tutilaction.MoveToNextBlock(), - 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(), @@ -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(), @@ -609,29 +609,29 @@ func TestMarketOrder(t *testing.T) { 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")), ), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), tutilaction.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)), + 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( tutilaction.MoveToNextBlock(), - MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), + 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), @@ -644,16 +644,16 @@ func TestMarketOrder(t *testing.T) { tutilaction.TC("existing short position, decrease a bit"). Given( - CreateCustomMarket(pairBtcNusd), + perpaction.CreateCustomMarket(pairBtcNusd), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), tutilaction.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.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), ). When( tutilaction.MoveToNextBlock(), - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(500), sdk.NewDec(10), sdk.ZeroDec(), - MarketOrderResp_PositionShouldBeEqual( + 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(), @@ -701,29 +701,29 @@ func TestMarketOrder(t *testing.T) { 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")), ), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), tutilaction.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)), + 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( tutilaction.MoveToNextBlock(), - MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), + 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), @@ -736,16 +736,16 @@ func TestMarketOrder(t *testing.T) { tutilaction.TC("existing short position, decrease a lot"). Given( - CreateCustomMarket(pairBtcNusd), + perpaction.CreateCustomMarket(pairBtcNusd), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), tutilaction.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.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), ). When( tutilaction.MoveToNextBlock(), - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), - MarketOrderResp_PositionShouldBeEqual( + 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(), @@ -793,25 +793,25 @@ func TestMarketOrder(t *testing.T) { 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")), ), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), tutilaction.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)), + 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( tutilaction.MoveToNextBlock(), - MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), - MarketOrderResp_PositionShouldBeEqual( + 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(), @@ -859,68 +859,68 @@ func TestMarketOrder(t *testing.T) { tutilaction.TC("user has insufficient funds"). Given( - CreateCustomMarket(pairBtcNusd), + 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), ), tutilaction.TC("new long position, can close position after market is not enabled"). Given( - CreateCustomMarket(pairBtcNusd), + perpaction.CreateCustomMarket(pairBtcNusd), tutilaction.SetBlockTime(startBlockTime), tutilaction.SetBlockNumber(1), tutilaction.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.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), ), tutilaction.TC("new long position, can not open new position after market is not enabled"). Given( - CreateCustomMarket(pairBtcNusd), + perpaction.CreateCustomMarket(pairBtcNusd), tutilaction.SetBlockTime(startBlockTime), tutilaction.SetBlockNumber(1), tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(47_714_285_715)))), - SetMarketEnabled(pairBtcNusd, false), + 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), ), tutilaction.TC("existing long position, can not open new one but can close"). Given( - CreateCustomMarket(pairBtcNusd), + perpaction.CreateCustomMarket(pairBtcNusd), tutilaction.SetBlockTime(startBlockTime), tutilaction.SetBlockNumber(1), tutilaction.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.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), ), tutilaction.TC("market doesn't exist"). @@ -930,131 +930,131 @@ func TestMarketOrder(t *testing.T) { 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), ), tutilaction.TC("zero quote asset amount"). Given( tutilaction.SetBlockTime(startBlockTime), tutilaction.SetBlockNumber(1), - CreateCustomMarket(pairBtcNusd), + 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), ), tutilaction.TC("zero leverage"). Given( tutilaction.SetBlockTime(startBlockTime), tutilaction.SetBlockNumber(1), - CreateCustomMarket(pairBtcNusd), + 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), ), tutilaction.TC("user leverage greater than market max leverage"). Given( tutilaction.SetBlockTime(startBlockTime), tutilaction.SetBlockNumber(1), - CreateCustomMarket(pairBtcNusd), + 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), ), tutilaction.TC("position should not exist after opening a closing manually"). Given( tutilaction.SetBlockTime(startBlockTime), tutilaction.SetBlockNumber(1), - CreateCustomMarket(pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("25001.0112"))), + 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), ), tutilaction.TC("position should not exist after opening a closing manually - reverse with leverage"). Given( tutilaction.SetBlockTime(startBlockTime), tutilaction.SetBlockNumber(1), - CreateCustomMarket(pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("25001.0112"))), + 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), ), tutilaction.TC("position should not exist after opening a closing manually - open with leverage"). Given( tutilaction.SetBlockTime(startBlockTime), tutilaction.SetBlockNumber(1), - CreateCustomMarket(pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("25001.0112"))), + 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), ), tutilaction.TC("position should not exist after opening a closing manually - reverse with leverage"). Given( tutilaction.SetBlockTime(startBlockTime), tutilaction.SetBlockNumber(1), - CreateCustomMarket(pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("25001.0112"))), + 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), ), tutilaction.TC("position should not exist after opening a closing manually - reverse with leverage - more steps"). Given( tutilaction.SetBlockTime(startBlockTime), tutilaction.SetBlockNumber(1), - CreateCustomMarket(pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("25000"))), + 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), ), } @@ -1202,27 +1202,27 @@ func TestPartialClose(t *testing.T) { 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")), ), tutilaction.SetBlockTime(startBlockTime), tutilaction.SetBlockNumber(1), tutilaction.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)), + 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(), @@ -1256,27 +1256,27 @@ func TestPartialClose(t *testing.T) { 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")), ), tutilaction.SetBlockTime(startBlockTime), tutilaction.SetBlockNumber(1), tutilaction.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)), + 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(), @@ -1310,27 +1310,27 @@ func TestPartialClose(t *testing.T) { 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")), ), tutilaction.SetBlockTime(startBlockTime), tutilaction.SetBlockNumber(1), tutilaction.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)), + 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(), @@ -1363,28 +1363,28 @@ func TestPartialClose(t *testing.T) { 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")), ), 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))), - InsertPosition( - WithPair(pairBtcNusd), - WithTrader(alice), - WithSize(sdk.NewDec(10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + 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(), @@ -1417,27 +1417,27 @@ func TestPartialClose(t *testing.T) { 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")), ), tutilaction.SetBlockTime(startBlockTime), tutilaction.SetBlockNumber(1), tutilaction.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)), + 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(), @@ -1471,27 +1471,27 @@ func TestPartialClose(t *testing.T) { 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")), ), tutilaction.SetBlockTime(startBlockTime), tutilaction.SetBlockNumber(1), tutilaction.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)), + 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(), @@ -1525,27 +1525,27 @@ func TestPartialClose(t *testing.T) { 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")), ), tutilaction.SetBlockTime(startBlockTime), tutilaction.SetBlockNumber(1), tutilaction.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)), + 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(), @@ -1579,28 +1579,28 @@ func TestPartialClose(t *testing.T) { 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")), ), 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))), - InsertPosition( - WithPair(pairBtcNusd), - WithTrader(alice), - WithSize(sdk.NewDec(-10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + 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(), @@ -1633,21 +1633,21 @@ func TestPartialClose(t *testing.T) { ), 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)), ), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), 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), ), } @@ -1662,28 +1662,28 @@ func TestClosePosition(t *testing.T) { 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")), ), 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))), - InsertPosition( - WithTrader(alice), - WithPair(pairBtcNusd), - WithSize(sdk.NewDec(10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + 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(), @@ -1708,28 +1708,28 @@ func TestClosePosition(t *testing.T) { 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")), ), 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))), - InsertPosition( - WithTrader(alice), - WithPair(pairBtcNusd), - WithSize(sdk.NewDec(10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + 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(), @@ -1754,18 +1754,18 @@ func TestClosePosition(t *testing.T) { 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")), ), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), - InsertPosition( - WithTrader(alice), - WithPair(pairBtcNusd), - WithSize(sdk.NewDec(10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + 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)), ), tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 18))), tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 1000))), @@ -1773,11 +1773,11 @@ func TestClosePosition(t *testing.T) { ). When( tutilaction.MoveToNextBlock(), - 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{ 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)), ), 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")), ), 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))), - InsertPosition( - WithTrader(alice), - WithPair(pairBtcNusd), - WithSize(sdk.NewDec(-10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + 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(), @@ -1850,28 +1850,28 @@ func TestClosePosition(t *testing.T) { 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")), ), 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))), - InsertPosition( - WithTrader(alice), - WithPair(pairBtcNusd), - WithSize(sdk.NewDec(-10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + 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(), @@ -1896,18 +1896,18 @@ func TestClosePosition(t *testing.T) { 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")), ), tutilaction.SetBlockTime(startBlockTime), tutilaction.SetBlockNumber(1), - InsertPosition( - WithTrader(alice), - WithPair(pairBtcNusd), - WithSize(sdk.NewDec(-10_000)), - WithMargin(sdk.NewDec(1_000)), - WithOpenNotional(sdk.NewDec(10_000)), + 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)), ), tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 22))), tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 1000))), @@ -1915,11 +1915,11 @@ func TestClosePosition(t *testing.T) { ). When( tutilaction.MoveToNextBlock(), - 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{ TraderAddress: alice.String(), Pair: pairBtcNusd, @@ -1940,8 +1940,8 @@ 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)), ), } @@ -1959,177 +1959,177 @@ func TestUpdateSwapInvariant(t *testing.T) { tc := tutilaction.TestCases{ tutilaction.TC("only long position - no change to swap invariant"). Given( - CreateCustomMarket(pairBtcNusd), + 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), ), tutilaction.TC("only short position - no change to swap invariant"). Given( - CreateCustomMarket(pairBtcNusd), + 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), ), tutilaction.TC("only long position - increasing k"). Given( - CreateCustomMarket(pairBtcNusd), + 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()), ), tutilaction.TC("only short position - increasing k"). Given( - CreateCustomMarket(pairBtcNusd), + 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()))), ), tutilaction.TC("only long position - decreasing k"). Given( - CreateCustomMarket(pairBtcNusd), + 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()), ), tutilaction.TC("only short position - decreasing k"). Given( - CreateCustomMarket(pairBtcNusd), + 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()), ), tutilaction.TC("long and short position - increasing k"). Given( - CreateCustomMarket(pairBtcNusd), + 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)))), ), tutilaction.TC("long and short position - reducing k"). Given( - CreateCustomMarket(pairBtcNusd), + 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)))), ), } diff --git a/x/perp/v2/keeper/margin_test.go b/x/perp/v2/keeper/margin_test.go index 239adc45e..7c2714412 100644 --- a/x/perp/v2/keeper/margin_test.go +++ b/x/perp/v2/keeper/margin_test.go @@ -13,9 +13,9 @@ import ( "github.com/NibiruChain/nibiru/x/common/denoms" "github.com/NibiruChain/nibiru/x/common/testutil" tutilaction "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" + 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" ) @@ -28,18 +28,18 @@ func TestAddMargin(t *testing.T) { tc := tutilaction.TestCases{ tutilaction.TC("existing long position, add margin"). Given( - CreateCustomMarket(pairBtcUsdc), + perpaction.CreateCustomMarket(pairBtcUsdc), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), tutilaction.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.MarketOrder(alice, pairBtcUsdc, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), ). When( tutilaction.MoveToNextBlock(), - AddMargin(alice, pairBtcUsdc, sdk.NewInt(1000)), + 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)), ), tutilaction.TC("existing short position, add margin"). Given( - CreateCustomMarket(pairBtcUsdc), + perpaction.CreateCustomMarket(pairBtcUsdc), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), tutilaction.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.MarketOrder(alice, pairBtcUsdc, types.Direction_SHORT, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), ). When( tutilaction.MoveToNextBlock(), - AddMargin(alice, pairBtcUsdc, sdk.NewInt(1000)), + 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,31 +121,31 @@ 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)), ), tutilaction.TC("Testing fails"). Given( - CreateCustomMarket(pairBtcUsdc), - CreateCustomMarket(pairEthUsdc), + perpaction.CreateCustomMarket(pairBtcUsdc), + perpaction.CreateCustomMarket(pairEthUsdc), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), tutilaction.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()), + perpaction.MarketOrder(alice, pairBtcUsdc, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec()), ). When( tutilaction.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), + 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), ), } @@ -160,18 +160,18 @@ func TestRemoveMargin(t *testing.T) { tc := tutilaction.TestCases{ tutilaction.TC("existing long position, remove margin"). Given( - CreateCustomMarket(pairBtcUsdc), + perpaction.CreateCustomMarket(pairBtcUsdc), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), tutilaction.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.MarketOrder(alice, pairBtcUsdc, types.Direction_LONG, sdk.NewInt(1000), sdk.OneDec(), sdk.ZeroDec()), ). When( tutilaction.MoveToNextBlock(), - RemoveMargin(alice, pairBtcUsdc, sdk.NewInt(500)), + 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()), ), tutilaction.TC("existing long position, remove almost all margin fails"). Given( - CreateCustomMarket(pairBtcUsdc), + perpaction.CreateCustomMarket(pairBtcUsdc), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), tutilaction.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.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()), ), tutilaction.TC("existing short position, remove margin"). Given( - CreateCustomMarket(pairBtcUsdc), + perpaction.CreateCustomMarket(pairBtcUsdc), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), tutilaction.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.MarketOrder(alice, pairBtcUsdc, types.Direction_SHORT, sdk.NewInt(1000), sdk.OneDec(), sdk.ZeroDec()), ). When( tutilaction.MoveToNextBlock(), - RemoveMargin(alice, pairBtcUsdc, sdk.NewInt(500)), + 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()), ), tutilaction.TC("existing short position, remove almost all margin fails"). Given( - CreateCustomMarket(pairBtcUsdc), + perpaction.CreateCustomMarket(pairBtcUsdc), tutilaction.SetBlockNumber(1), tutilaction.SetBlockTime(startBlockTime), tutilaction.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.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,9 +303,9 @@ 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()), ), } diff --git a/x/perp/v2/keeper/twap_test.go b/x/perp/v2/keeper/twap_test.go index f93d4cdd9..cf5d94575 100644 --- a/x/perp/v2/keeper/twap_test.go +++ b/x/perp/v2/keeper/twap_test.go @@ -14,8 +14,8 @@ import ( 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" @@ -28,77 +28,77 @@ func TestCalcTwap(t *testing.T) { tc := tutilaction.TestCases{ tutilaction.TC("spot twap"). Given( - CreateCustomMarket(pairBtcUsdc), + perpaction.CreateCustomMarket(pairBtcUsdc), tutilaction.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.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( 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)), ), tutilaction.TC("base asset twap, long"). Given( - CreateCustomMarket(pairBtcUsdc), + perpaction.CreateCustomMarket(pairBtcUsdc), tutilaction.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.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( 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")), ), tutilaction.TC("base asset twap, short"). Given( - CreateCustomMarket(pairBtcUsdc), + perpaction.CreateCustomMarket(pairBtcUsdc), tutilaction.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.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( 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")), ), tutilaction.TC("quote asset twap, long"). Given( - CreateCustomMarket(pairBtcUsdc), + perpaction.CreateCustomMarket(pairBtcUsdc), tutilaction.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.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( 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")), ), tutilaction.TC("quote asset twap, short"). Given( - CreateCustomMarket(pairBtcUsdc), + perpaction.CreateCustomMarket(pairBtcUsdc), tutilaction.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.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( 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")), ), } From 9cbae1f0e74b01e1e7677ef9254a8c6c5e7a5435 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Fri, 25 Aug 2023 05:50:00 -0500 Subject: [PATCH 06/23] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0f3a4943..99e1b225d 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 ### Features From 9adc9e46dd42442fad6ab6a69c9afdcb038eff65 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Tue, 5 Sep 2023 03:37:51 -0500 Subject: [PATCH 07/23] fix(oracle): miss outside reward band but not on abstain --- x/oracle/keeper/ballot.go | 31 ++++++++++----- x/oracle/keeper/slash_test.go | 7 +++- x/oracle/keeper/update_exchange_rates.go | 22 +++++++---- x/oracle/keeper/update_exchange_rates_test.go | 38 +++++++++---------- x/oracle/types/ballot.go | 13 +++++-- 5 files changed, 69 insertions(+), 42 deletions(-) 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, } } From 3ca259a2a231d5c2f0d67951bf76a13f82ce0e52 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Tue, 5 Sep 2023 10:44:58 -0500 Subject: [PATCH 08/23] fix(oracle): #wip checkpoint for omit == abstain --- x/oracle/keeper/ballot.go | 14 +- x/oracle/keeper/msg_server_test.go | 2 +- x/oracle/keeper/reward_test.go | 2 +- x/oracle/keeper/slash_test.go | 101 ++++++++------ x/oracle/keeper/test_utils.go | 23 ++-- x/oracle/keeper/update_exchange_rates.go | 20 ++- x/oracle/keeper/update_exchange_rates_test.go | 130 ++++++++++-------- x/oracle/types/ballot.go | 11 ++ 8 files changed, 189 insertions(+), 114 deletions(-) diff --git a/x/oracle/keeper/ballot.go b/x/oracle/keeper/ballot.go index ee5aa0d3b..1e05387fd 100644 --- a/x/oracle/keeper/ballot.go +++ b/x/oracle/keeper/ballot.go @@ -22,7 +22,7 @@ import ( // asset pair is associated with its collection of ExchangeRateBallots. func (k Keeper) groupBallotsByPair( ctx sdk.Context, - validatorsPerformance types.ValidatorPerformances, + valPerfMap types.ValidatorPerformances, ) (pairBallotsMap map[asset.Pair]types.ExchangeRateBallots) { pairBallotsMap = map[asset.Pair]types.ExchangeRateBallots{} @@ -30,7 +30,7 @@ func (k Keeper) groupBallotsByPair( voterAddr, aggregateVote := value.Key, value.Value // organize ballot only for the active validators - if validatorPerformance, exists := validatorsPerformance[aggregateVote.Voter]; exists { + if validatorPerformance, exists := valPerfMap[aggregateVote.Voter]; exists { for _, exchangeRateTuple := range aggregateVote.ExchangeRateTuples { power := validatorPerformance.Power if !exchangeRateTuple.ExchangeRate.IsPositive() { @@ -147,7 +147,7 @@ func Tally( ballots types.ExchangeRateBallots, rewardBand sdk.Dec, validatorPerformances types.ValidatorPerformances, -) sdk.Dec { +) (sdk.Dec, types.ValidatorPerformances) { weightedMedian := ballots.WeightedMedianWithAssertion() standardDeviation := ballots.StandardDeviation(weightedMedian) rewardSpread := weightedMedian.Mul(rewardBand.QuoInt64(2)) @@ -162,20 +162,18 @@ func Tally( ballot.ExchangeRate.LTE(weightedMedian.Add(rewardSpread)) isAbstainVote := !ballot.ExchangeRate.IsPositive() isMiss := !(voteInsideSpread || isAbstainVote) - voterAddr := ballot.Voter.String() validatorPerformance := validatorPerformances[voterAddr] switch { - case isAbstainVote: - validatorPerformance.AbstainCount++ case voteInsideSpread: validatorPerformance.RewardWeight += ballot.Power validatorPerformance.WinCount++ case isMiss: validatorPerformance.MissCount++ + case isAbstainVote: + validatorPerformance.AbstainCount++ } validatorPerformances[voterAddr] = validatorPerformance } - - return weightedMedian + return weightedMedian, validatorPerformances } diff --git a/x/oracle/keeper/msg_server_test.go b/x/oracle/keeper/msg_server_test.go index 5337cab7a..f76d1deb3 100644 --- a/x/oracle/keeper/msg_server_test.go +++ b/x/oracle/keeper/msg_server_test.go @@ -17,7 +17,7 @@ func TestFeederDelegation(t *testing.T) { exchangeRates := types.ExchangeRateTuples{ { Pair: asset.Registry.Pair(denoms.BTC, denoms.NUSD), - ExchangeRate: randomExchangeRate, + ExchangeRate: TEST_ERATE, }, } diff --git a/x/oracle/keeper/reward_test.go b/x/oracle/keeper/reward_test.go index c0861f6c8..19e0bc85b 100644 --- a/x/oracle/keeper/reward_test.go +++ b/x/oracle/keeper/reward_test.go @@ -40,7 +40,7 @@ func TestKeeperRewardsDistributionMultiVotePeriods(t *testing.T) { MakeAggregatePrevoteAndVote(t, fixture, msgServer, fixture.Ctx.BlockHeight(), types.ExchangeRateTuples{ { Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), - ExchangeRate: randomExchangeRate, + ExchangeRate: TEST_ERATE, }, }, valIndex) } diff --git a/x/oracle/keeper/slash_test.go b/x/oracle/keeper/slash_test.go index edf6e54eb..7c4d6c127 100644 --- a/x/oracle/keeper/slash_test.go +++ b/x/oracle/keeper/slash_test.go @@ -105,22 +105,22 @@ func TestInvalidVotesSlashing(t *testing.T) { // Account 1, govstable MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}, }, 0) // Account 2, govstable, miss vote MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate.Add(sdk.NewDec(100000000000000))}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE.Add(sdk.NewDec(100000000000000))}, }, 1) // Account 3, govstable MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}, }, 2) // Account 4, govstable MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}, }, 3) input.OracleKeeper.UpdateExchangeRates(input.Ctx) @@ -131,27 +131,27 @@ func TestInvalidVotesSlashing(t *testing.T) { } validator := input.StakingKeeper.Validator(input.Ctx, ValAddrs[1]) - require.Equal(t, stakingAmt, validator.GetBondedTokens()) + require.Equal(t, TEST_STAKING_AMT, validator.GetBondedTokens()) // one more miss vote will inccur ValAddrs[1] slashing // Account 1, govstable MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}, }, 0) // Account 2, govstable, miss vote MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate.Add(sdk.NewDec(100000000000000))}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE.Add(sdk.NewDec(100000000000000))}, }, 1) // Account 3, govstable MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}, }, 2) // Account 4, govstable MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}, }, 3) input.Ctx = input.Ctx.WithBlockHeight(votePeriodsPerWindow - 1) @@ -160,49 +160,72 @@ func TestInvalidVotesSlashing(t *testing.T) { // input.OracleKeeper.UpdateExchangeRates(input.Ctx) validator = input.StakingKeeper.Validator(input.Ctx, ValAddrs[1]) - require.Equal(t, sdk.OneDec().Sub(slashFraction).MulInt(stakingAmt).TruncateInt(), validator.GetBondedTokens()) + require.Equal(t, sdk.OneDec().Sub(slashFraction).MulInt(TEST_STAKING_AMT).TruncateInt(), validator.GetBondedTokens()) } +// TestWhitelistSlashing: Creates a scenario where one valoper (valIdx 0) does +// not vote throughout an entire vote window, while valopers 1 and 2 do. func TestWhitelistSlashing(t *testing.T) { - input, h := Setup(t) + input, msgServer := Setup(t) votePeriodsPerWindow := sdk.NewDec(int64(input.OracleKeeper.SlashWindow(input.Ctx))).QuoInt64(int64(input.OracleKeeper.VotePeriod(input.Ctx))).TruncateInt64() - slashFraction := input.OracleKeeper.SlashFraction(input.Ctx) + // slashFraction := input.OracleKeeper.SlashFraction(input.Ctx) minValidPerWindow := input.OracleKeeper.MinValidPerWindow(input.Ctx) + pair := asset.Registry.Pair(denoms.NIBI, denoms.NUSD) + priceVoteFromVal := func(valIdx int, block int64, erate sdk.Dec) { + MakeAggregatePrevoteAndVote(t, input, msgServer, block, + types.ExchangeRateTuples{{Pair: pair, ExchangeRate: erate}}, + valIdx) + } + input.OracleKeeper.WhitelistedPairs.Insert(input.Ctx, pair) + perfs := input.OracleKeeper.UpdateExchangeRates(input.Ctx) + require.EqualValues(t, 0, perfs.GetTotalRewardWeight()) + + block := input.Ctx.BlockHeight() + 50 + priceVoteFromVal(0, block, TEST_ERATE) + priceVoteFromVal(1, block, TEST_ERATE) + input.Ctx = input.Ctx.WithBlockHeight(block + 100) + 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 - MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 1) - // Account 3, govstable - MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 2) + // TODO - input.OracleKeeper.UpdateExchangeRates(input.Ctx) - // input.OracleKeeper.SlashAndResetMissCounters(input.Ctx) - // input.OracleKeeper.UpdateExchangeRates(input.Ctx) - require.Equal(t, idxMissPeriod+1, input.OracleKeeper.MissCounters.GetOr(input.Ctx, ValAddrs[0], 0)) - } + t.Logf("For %v blocks, valoper0 does not vote, while 1 and 2 do.", allowedMissPeriods) + for idxMissPeriod := uint64(0); idxMissPeriod < uint64(allowedMissPeriods); idxMissPeriod++ { + block := input.Ctx.BlockHeight() + 1 + input.Ctx = input.Ctx.WithBlockHeight(block) - validator := input.StakingKeeper.Validator(input.Ctx, ValAddrs[0]) - require.Equal(t, stakingAmt, validator.GetBondedTokens()) + valIdx := 0 + priceVoteFromVal(valIdx+1, block, TEST_ERATE) + priceVoteFromVal(valIdx+2, block, TEST_ERATE) - // one more miss vote will inccur Account 1 slashing + // Valoper 0 votes outside band (miss) - // Account 2, govstable - MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 1) - // Account 3, govstable - MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 2) + perfs := input.OracleKeeper.UpdateExchangeRates(input.Ctx) + missCount := input.OracleKeeper.MissCounters.GetOr(input.Ctx, ValAddrs[0], 0) + require.EqualValues(t, 0, missCount, perfs.String()) + } - input.Ctx = input.Ctx.WithBlockHeight(votePeriodsPerWindow - 1) - input.OracleKeeper.UpdateExchangeRates(input.Ctx) - input.OracleKeeper.SlashAndResetMissCounters(input.Ctx) + t.Log("valoper0 should not be slashed") + validator := input.StakingKeeper.Validator(input.Ctx, ValAddrs[0]) + require.Equal(t, TEST_STAKING_AMT, validator.GetBondedTokens()) + + // // one more miss vote will inccur Account 1 slashing + // + // // Account 2, govstable + // MakeAggregatePrevoteAndVote(t, input, msgServer, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 1) + // // Account 3, govstable + // MakeAggregatePrevoteAndVote(t, input, msgServer, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 2) + // + // input.Ctx = input.Ctx.WithBlockHeight(votePeriodsPerWindow - 1) // input.OracleKeeper.UpdateExchangeRates(input.Ctx) - validator = input.StakingKeeper.Validator(input.Ctx, ValAddrs[0]) - require.Equal(t, sdk.OneDec().Sub(slashFraction).MulInt(stakingAmt).TruncateInt(), validator.GetBondedTokens()) + // input.OracleKeeper.SlashAndResetMissCounters(input.Ctx) + // // input.OracleKeeper.UpdateExchangeRates(input.Ctx) + // validator = input.StakingKeeper.Validator(input.Ctx, ValAddrs[0]) + // require.Equal(t, sdk.OneDec().Sub(slashFraction).MulInt(stakingAmt).TruncateInt(), validator.GetBondedTokens()) } func TestNotPassedBallotSlashing(t *testing.T) { @@ -221,7 +244,7 @@ func TestNotPassedBallotSlashing(t *testing.T) { input.Ctx = input.Ctx.WithBlockHeight(input.Ctx.BlockHeight() + 1) // Account 1, govstable - MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 0) + MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}}, 0) input.OracleKeeper.UpdateExchangeRates(input.Ctx) input.OracleKeeper.SlashAndResetMissCounters(input.Ctx) @@ -251,13 +274,13 @@ func TestAbstainSlashing(t *testing.T) { input.Ctx = input.Ctx.WithBlockHeight(input.Ctx.BlockHeight() + 1) // Account 1, govstable - MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 0) + MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}}, 0) // Account 2, govstable, abstain vote MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: sdk.ZeroDec()}}, 1) // Account 3, govstable - MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 2) + MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}}, 2) input.OracleKeeper.UpdateExchangeRates(input.Ctx) input.OracleKeeper.SlashAndResetMissCounters(input.Ctx) @@ -266,5 +289,5 @@ func TestAbstainSlashing(t *testing.T) { } validator := input.StakingKeeper.Validator(input.Ctx, ValAddrs[1]) - require.Equal(t, stakingAmt, validator.GetBondedTokens()) + require.Equal(t, TEST_STAKING_AMT, validator.GetBondedTokens()) } diff --git a/x/oracle/keeper/test_utils.go b/x/oracle/keeper/test_utils.go index c08bb1797..7e95f40a2 100644 --- a/x/oracle/keeper/test_utils.go +++ b/x/oracle/keeper/test_utils.go @@ -285,8 +285,8 @@ func AllocateRewards(t *testing.T, input TestFixture, rewards sdk.Coins, votePer } var ( - stakingAmt = sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction) - randomExchangeRate = sdk.NewDec(1700) + TEST_STAKING_AMT = sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction) + TEST_ERATE = sdk.NewDec(1700) ) func Setup(t *testing.T) (TestFixture, types.MsgServer) { @@ -304,22 +304,29 @@ func Setup(t *testing.T) (TestFixture, types.MsgServer) { sh := stakingkeeper.NewMsgServerImpl(&fixture.StakingKeeper) // Validator created - _, err := sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[0], ValPubKeys[0], stakingAmt)) + _, err := sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[0], ValPubKeys[0], TEST_STAKING_AMT)) require.NoError(t, err) - _, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[1], ValPubKeys[1], stakingAmt)) + _, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[1], ValPubKeys[1], TEST_STAKING_AMT)) require.NoError(t, err) - _, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[2], ValPubKeys[2], stakingAmt)) + _, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[2], ValPubKeys[2], TEST_STAKING_AMT)) require.NoError(t, err) - _, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[3], ValPubKeys[3], stakingAmt)) + _, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[3], ValPubKeys[3], TEST_STAKING_AMT)) require.NoError(t, err) - _, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[4], ValPubKeys[4], stakingAmt)) + _, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[4], ValPubKeys[4], TEST_STAKING_AMT)) require.NoError(t, err) staking.EndBlocker(fixture.Ctx, &fixture.StakingKeeper) return fixture, h } -func MakeAggregatePrevoteAndVote(t *testing.T, input TestFixture, msgServer types.MsgServer, height int64, rates types.ExchangeRateTuples, valIdx int) { +func MakeAggregatePrevoteAndVote( + t *testing.T, + input TestFixture, + msgServer types.MsgServer, + height int64, + rates types.ExchangeRateTuples, + valIdx int, +) { salt := "1" ratesStr, err := rates.ToString() require.NoError(t, err) diff --git a/x/oracle/keeper/update_exchange_rates.go b/x/oracle/keeper/update_exchange_rates.go index 369c3487b..31502eda3 100644 --- a/x/oracle/keeper/update_exchange_rates.go +++ b/x/oracle/keeper/update_exchange_rates.go @@ -12,7 +12,7 @@ import ( ) // UpdateExchangeRates updates the ExchangeRates, this is supposed to be executed on EndBlock. -func (k Keeper) UpdateExchangeRates(ctx sdk.Context) { +func (k Keeper) UpdateExchangeRates(ctx sdk.Context) types.ValidatorPerformances { k.Logger(ctx).Info("processing validator price votes") validatorPerformances := k.newValidatorPerformances(ctx) @@ -27,6 +27,8 @@ func (k Keeper) UpdateExchangeRates(ctx sdk.Context) { params, _ := k.Params.Get(ctx) k.clearVotesAndPreVotes(ctx, params.VotePeriod) k.updateWhitelist(ctx, params.Whitelist, whitelistedPairs) + k.registerAbstainsByOmission(ctx, len(params.Whitelist), validatorPerformances) + return validatorPerformances } // registerMissedVotes it parses all validators performance and increases the @@ -46,6 +48,20 @@ func (k Keeper) registerMissedVotes( } } +func (k Keeper) registerAbstainsByOmission( + ctx sdk.Context, + numMarkets int, + perfs types.ValidatorPerformances, +) { + for valAddr, perf := range perfs { + omitCount := int64(numMarkets) - (perf.WinCount + perf.AbstainCount + perf.MissCount) + if omitCount > 0 { + perf.AbstainCount += omitCount + perfs[valAddr] = perf + } + } +} + // countVotesAndUpdateExchangeRates processes the votes and updates the // ExchangeRates based on the results. func (k Keeper) countVotesAndUpdateExchangeRates( @@ -59,7 +75,7 @@ func (k Keeper) countVotesAndUpdateExchangeRates( orderedBallotsMap := omap.OrderedMap_Pair[types.ExchangeRateBallots](pairBallotsMap) for pair := range orderedBallotsMap.Range() { ballots := pairBallotsMap[pair] - exchangeRate := Tally(ballots, rewardBand, validatorPerformances) + exchangeRate, _ := Tally(ballots, rewardBand, validatorPerformances) k.SetPrice(ctx, pair, exchangeRate) diff --git a/x/oracle/keeper/update_exchange_rates_test.go b/x/oracle/keeper/update_exchange_rates_test.go index 2f169f1b4..87766b4ba 100644 --- a/x/oracle/keeper/update_exchange_rates_test.go +++ b/x/oracle/keeper/update_exchange_rates_test.go @@ -24,7 +24,7 @@ func TestOracleThreshold(t *testing.T) { exchangeRates := types.ExchangeRateTuples{ { Pair: asset.Registry.Pair(denoms.BTC, denoms.NUSD), - ExchangeRate: randomExchangeRate, + ExchangeRate: TEST_ERATE, }, } exchangeRateStr, err := exchangeRates.ToString() @@ -68,12 +68,12 @@ func TestOracleThreshold(t *testing.T) { fixture.OracleKeeper.UpdateExchangeRates(fixture.Ctx) rate, err := fixture.OracleKeeper.ExchangeRates.Get(fixture.Ctx, exchangeRates[0].Pair) require.NoError(t, err) - assert.Equal(t, randomExchangeRate, rate.ExchangeRate) + assert.Equal(t, TEST_ERATE, rate.ExchangeRate) // Case 3. // Increase voting power of absent validator, exchange rate consensus fails val, _ := fixture.StakingKeeper.GetValidator(fixture.Ctx, ValAddrs[4]) - _, _ = fixture.StakingKeeper.Delegate(fixture.Ctx.WithBlockHeight(0), Addrs[4], stakingAmt.MulRaw(8), stakingtypes.Unbonded, val, false) + _, _ = fixture.StakingKeeper.Delegate(fixture.Ctx.WithBlockHeight(0), Addrs[4], TEST_STAKING_AMT.MulRaw(8), stakingtypes.Unbonded, val, false) for i := 0; i < 4; i++ { salt := fmt.Sprintf("%d", i) @@ -104,7 +104,7 @@ func TestResetExchangeRates(t *testing.T) { fixture.OracleKeeper.Params.Set(fixture.Ctx, params) // Post a price at block 1 - fixture.OracleKeeper.SetPrice(fixture.Ctx.WithBlockHeight(1), pair, randomExchangeRate) + fixture.OracleKeeper.SetPrice(fixture.Ctx.WithBlockHeight(1), pair, TEST_ERATE) // reset exchange rates at block 2 // Price should still be there because not expired yet @@ -121,7 +121,7 @@ func TestResetExchangeRates(t *testing.T) { // Post a price at block 69 // reset exchange rates at block 79 // Price should not be there anymore because expired - fixture.OracleKeeper.SetPrice(fixture.Ctx.WithBlockHeight(69), pair, randomExchangeRate) + fixture.OracleKeeper.SetPrice(fixture.Ctx.WithBlockHeight(69), pair, TEST_ERATE) fixture.OracleKeeper.resetExchangeRates(fixture.Ctx.WithBlockHeight(79), emptyBallot) _, err = fixture.OracleKeeper.ExchangeRates.Get(fixture.Ctx, pair) @@ -153,7 +153,7 @@ func TestOracleTally(t *testing.T) { require.NoError(t, err1) require.NoError(t, err2) - power := stakingAmt.QuoRaw(int64(6)).Int64() + power := TEST_STAKING_AMT.QuoRaw(int64(6)).Int64() if decExchangeRate.IsZero() { power = int64(0) } @@ -207,11 +207,12 @@ func TestOracleTally(t *testing.T) { expectedValidatorClaimMap[key] = claim } - tallyMedian := Tally( + tallyMedian, perfs := Tally( ballot, fixture.OracleKeeper.RewardBand(fixture.Ctx), validatorClaimMap) assert.Equal(t, expectedValidatorClaimMap, validatorClaimMap) assert.Equal(t, tallyMedian.MulInt64(100).TruncateInt(), weightedMedian.MulInt64(100).TruncateInt()) + assert.NotEqualValues(t, 0, perfs.GetTotalRewardWeight(), perfs.String()) } func TestOracleRewardBand(t *testing.T) { @@ -228,26 +229,26 @@ func TestOracleRewardBand(t *testing.T) { } fixture.OracleKeeper.WhitelistedPairs.Insert(fixture.Ctx, asset.Registry.Pair(denoms.NIBI, denoms.NUSD)) - rewardSpread := randomExchangeRate.Mul(fixture.OracleKeeper.RewardBand(fixture.Ctx).QuoInt64(2)) + rewardSpread := TEST_ERATE.Mul(fixture.OracleKeeper.RewardBand(fixture.Ctx).QuoInt64(2)) // Account 1, nibi:nusd MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate.Sub(rewardSpread)}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE.Sub(rewardSpread)}, }, 0) // Account 2, nibi:nusd MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}, }, 1) // Account 3, nibi:nusd MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}, }, 2) // Account 4, nibi:nusd MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate.Add(rewardSpread)}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE.Add(rewardSpread)}, }, 3) fixture.OracleKeeper.UpdateExchangeRates(fixture.Ctx) @@ -260,22 +261,22 @@ func TestOracleRewardBand(t *testing.T) { // Account 1 will miss the vote due to raward band condition // Account 1, nibi:nusd MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate.Sub(rewardSpread.Add(sdk.OneDec()))}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE.Sub(rewardSpread.Add(sdk.OneDec()))}, }, 0) // Account 2, nibi:nusd MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}, }, 1) // Account 3, nibi:nusd MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}, }, 2) // Account 4, nibi:nusd MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate.Add(rewardSpread)}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE.Add(rewardSpread)}, }, 3) fixture.OracleKeeper.UpdateExchangeRates(fixture.Ctx) @@ -403,62 +404,81 @@ func TestWhitelistedPairs(t *testing.T) { params, err := fixture.OracleKeeper.Params.Get(fixture.Ctx) require.NoError(t, err) + t.Log("whitelist ONLY nibi:nusd") for _, p := range fixture.OracleKeeper.WhitelistedPairs.Iterate(fixture.Ctx, collections.Range[asset.Pair]{}).Keys() { fixture.OracleKeeper.WhitelistedPairs.Delete(fixture.Ctx, p) } fixture.OracleKeeper.WhitelistedPairs.Insert(fixture.Ctx, asset.Registry.Pair(denoms.NIBI, denoms.NUSD)) - // nibi:nusd - MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 0) - MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 1) - MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 2) - MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 3) + t.Log("vote and prevote from all vals on nibi:nusd") + priceVoteFromVal := func(valIdx int, block int64) { + MakeAggregatePrevoteAndVote(t, fixture, msgServer, block, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}}, valIdx) + } + block := int64(0) + priceVoteFromVal(0, block) + priceVoteFromVal(1, block) + priceVoteFromVal(2, block) + priceVoteFromVal(3, block) - // add btc:nusd for next vote period + t.Log("whitelist btc:nusd for next vote period") params.Whitelist = []asset.Pair{asset.Registry.Pair(denoms.NIBI, denoms.NUSD), asset.Registry.Pair(denoms.BTC, denoms.NUSD)} fixture.OracleKeeper.Params.Set(fixture.Ctx, params) fixture.OracleKeeper.UpdateExchangeRates(fixture.Ctx) - // no missing current + t.Log("assert: no miss counts for all vals") assert.Equal(t, uint64(0), fixture.OracleKeeper.MissCounters.GetOr(fixture.Ctx, ValAddrs[0], 0)) assert.Equal(t, uint64(0), fixture.OracleKeeper.MissCounters.GetOr(fixture.Ctx, ValAddrs[1], 0)) assert.Equal(t, uint64(0), fixture.OracleKeeper.MissCounters.GetOr(fixture.Ctx, ValAddrs[2], 0)) assert.Equal(t, uint64(0), fixture.OracleKeeper.MissCounters.GetOr(fixture.Ctx, ValAddrs[3], 0)) - // whitelisted pairs are {nibi:nusd, btc:nusd} - assert.Equal(t, []asset.Pair{asset.Registry.Pair(denoms.BTC, denoms.NUSD), asset.Registry.Pair(denoms.NIBI, denoms.NUSD)}, fixture.OracleKeeper.GetWhitelistedPairs(fixture.Ctx)) + t.Log("whitelisted pairs are {nibi:nusd, btc:nusd}") + assert.Equal(t, + []asset.Pair{ + asset.Registry.Pair(denoms.BTC, denoms.NUSD), + asset.Registry.Pair(denoms.NIBI, denoms.NUSD)}, + fixture.OracleKeeper.GetWhitelistedPairs(fixture.Ctx)) - // nibi:nusd, missing btc:nusd - MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 0) - MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 1) - MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 2) - MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 3) + t.Log("vote from vals 0-3 on nibi:nusd (but not btc:nusd)") + priceVoteFromVal(0, block) + priceVoteFromVal(1, block) + priceVoteFromVal(2, block) + priceVoteFromVal(3, block) - // delete btc:nusd for next vote period + t.Log("delete btc:nusd for next vote period") params.Whitelist = []asset.Pair{asset.Registry.Pair(denoms.NIBI, denoms.NUSD)} fixture.OracleKeeper.Params.Set(fixture.Ctx, params) - fixture.OracleKeeper.UpdateExchangeRates(fixture.Ctx) - - assert.Equal(t, uint64(1), fixture.OracleKeeper.MissCounters.GetOr(fixture.Ctx, ValAddrs[0], 0)) - assert.Equal(t, uint64(1), fixture.OracleKeeper.MissCounters.GetOr(fixture.Ctx, ValAddrs[1], 0)) - assert.Equal(t, uint64(1), fixture.OracleKeeper.MissCounters.GetOr(fixture.Ctx, ValAddrs[2], 0)) - assert.Equal(t, uint64(1), fixture.OracleKeeper.MissCounters.GetOr(fixture.Ctx, ValAddrs[3], 0)) - - // btc:nusd must be deleted - assert.Equal(t, []asset.Pair{asset.Registry.Pair(denoms.NIBI, denoms.NUSD)}, fixture.OracleKeeper.GetWhitelistedPairs(fixture.Ctx)) - require.False(t, fixture.OracleKeeper.WhitelistedPairs.Has(fixture.Ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD))) - - // nibi:nusd, no missing - MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 0) - MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 1) - MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 2) - MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 3) - - fixture.OracleKeeper.UpdateExchangeRates(fixture.Ctx) - - // validators keep miss counters from last vote period - assert.Equal(t, uint64(1), fixture.OracleKeeper.MissCounters.GetOr(fixture.Ctx, ValAddrs[0], 0)) - assert.Equal(t, uint64(1), fixture.OracleKeeper.MissCounters.GetOr(fixture.Ctx, ValAddrs[1], 0)) - assert.Equal(t, uint64(1), fixture.OracleKeeper.MissCounters.GetOr(fixture.Ctx, ValAddrs[2], 0)) - assert.Equal(t, uint64(1), fixture.OracleKeeper.MissCounters.GetOr(fixture.Ctx, ValAddrs[2], 0)) + perfs := fixture.OracleKeeper.UpdateExchangeRates(fixture.Ctx) + + t.Log("validators 0-3 all voted -> expect win") + for valIdx := 0; valIdx < 4; valIdx++ { + perf := perfs[ValAddrs[valIdx].String()] + assert.EqualValues(t, 1, perf.WinCount) + assert.EqualValues(t, 0, perf.AbstainCount) + assert.EqualValues(t, 0, perf.MissCount) + } + t.Log("validators 4 didn't vote -> expect abstain") + perf := perfs[ValAddrs[4].String()] + assert.EqualValues(t, 0, perf.WinCount) + assert.EqualValues(t, 1, perf.AbstainCount) + assert.EqualValues(t, 0, perf.MissCount) + + t.Log("btc:nusd must be deleted") + assert.Equal(t, []asset.Pair{asset.Registry.Pair(denoms.NIBI, denoms.NUSD)}, + fixture.OracleKeeper.GetWhitelistedPairs(fixture.Ctx)) + require.False(t, fixture.OracleKeeper.WhitelistedPairs.Has( + fixture.Ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD))) + + t.Log("vote from vals 0-2 on nibi:nusd (same vote period)") + priceVoteFromVal(0, block) + priceVoteFromVal(1, block) + priceVoteFromVal(2, block) + perfs = fixture.OracleKeeper.UpdateExchangeRates(fixture.Ctx) + + t.Log("Although validators 0-2 voted, it's for the same period -> expect abstains for everyone") + for valIdx := 0; valIdx < 5; valIdx++ { + perf := perfs[ValAddrs[valIdx].String()] + assert.EqualValues(t, 0, perf.WinCount) + assert.EqualValues(t, 1, perf.AbstainCount) + assert.EqualValues(t, 0, perf.MissCount) + } } diff --git a/x/oracle/types/ballot.go b/x/oracle/types/ballot.go index 3851db302..9fa5098b7 100644 --- a/x/oracle/types/ballot.go +++ b/x/oracle/types/ballot.go @@ -1,6 +1,7 @@ package types import ( + "encoding/json" "fmt" "math" "sort" @@ -207,3 +208,13 @@ func (vp ValidatorPerformances) GetTotalRewardWeight() int64 { return totalRewardWeight } + +func (vp ValidatorPerformances) String() string { + jsonBz, _ := json.Marshal(vp) + return string(jsonBz) +} + +func (vp ValidatorPerformance) String() string { + jsonBz, _ := json.Marshal(vp) + return string(jsonBz) +} From b9862f45c8779d0a92a34cb54c0206f12445d119 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Tue, 5 Sep 2023 10:53:06 -0500 Subject: [PATCH 09/23] checkout x/perp from master --- x/perp/v2/keeper/admin_test.go | 28 +- x/perp/v2/keeper/amm_test.go | 316 ++--- x/perp/v2/keeper/calc_test.go | 84 +- x/perp/v2/keeper/clearing_house_test.go | 1548 +++++++++++------------ x/perp/v2/keeper/grpc_query_test.go | 302 +++-- x/perp/v2/keeper/hooks_test.go | 142 +-- x/perp/v2/keeper/liquidate_test.go | 320 +++-- x/perp/v2/keeper/margin_test.go | 198 +-- x/perp/v2/keeper/msg_server_test.go | 210 +-- x/perp/v2/keeper/twap_test.go | 90 +- x/perp/v2/keeper/withdraw_test.go | 92 +- 11 files changed, 1650 insertions(+), 1680 deletions(-) diff --git a/x/perp/v2/keeper/admin_test.go b/x/perp/v2/keeper/admin_test.go index 017e53981..b372edb12 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" - tutilaction "github.com/NibiruChain/nibiru/x/common/testutil/action" - tutilmock "github.com/NibiruChain/nibiru/x/common/testutil/mock" - perpaction "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" - perpassert "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" + . "github.com/NibiruChain/nibiru/x/common/testutil/action" + "github.com/NibiruChain/nibiru/x/common/testutil/mock" + . "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" + . "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" "github.com/NibiruChain/nibiru/x/perp/v2/keeper" "github.com/NibiruChain/nibiru/app" @@ -91,28 +91,28 @@ func TestAdmin_WithdrawFromInsuranceFund(t *testing.T) { func TestEnableMarket(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) - tests := tutilaction.TestCases{ - tutilaction.TC("true -> false"). + tests := TestCases{ + TC("true -> false"). Given( - perpaction.CreateCustomMarket(pair), - perpassert.MarketShouldBeEqual(pair, perpassert.Market_EnableShouldBeEqualTo(true)), + CreateCustomMarket(pair), + MarketShouldBeEqual(pair, Market_EnableShouldBeEqualTo(true)), ). When( - perpaction.SetMarketEnabled(pair, false), - perpassert.MarketShouldBeEqual(pair, perpassert.Market_EnableShouldBeEqualTo(false)), - perpaction.SetMarketEnabled(pair, true), + SetMarketEnabled(pair, false), + MarketShouldBeEqual(pair, Market_EnableShouldBeEqualTo(false)), + SetMarketEnabled(pair, true), ). Then( - perpassert.MarketShouldBeEqual(pair, perpassert.Market_EnableShouldBeEqualTo(true)), + MarketShouldBeEqual(pair, Market_EnableShouldBeEqualTo(true)), ), } - tutilaction.NewTestSuite(t).WithTestCases(tests...).Run() + NewTestSuite(t).WithTestCases(tests...).Run() } func TestCreateMarketFail(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) - amm := *tutilmock.TestAMMDefault() + amm := *mock.TestAMMDefault() app, ctx := testapp.NewNibiruTestAppAndContext() // Error because of invalid market diff --git a/x/perp/v2/keeper/amm_test.go b/x/perp/v2/keeper/amm_test.go index 2252a387c..3c31475bf 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" - 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/action" + . "github.com/NibiruChain/nibiru/x/common/testutil/assertion" "github.com/NibiruChain/nibiru/x/common/testutil/testapp" - 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/integration/action" + . "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 := tutilaction.TestCases{ - tutilaction.TC("same price multiplier"). + tests := TestCases{ + TC("same price multiplier"). Given( - 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)))), + 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)))), ). When( - perpaction.EditPriceMultiplier(pair, sdk.OneDec()), + EditPriceMultiplier(pair, sdk.OneDec()), ). Then( - 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()), + 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()), ), ), - tutilaction.TC("net bias zero"). + TC("net bias zero"). Given( - 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)))), + 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)))), ). When( - perpaction.EditPriceMultiplier(pair, sdk.NewDec(10)), + EditPriceMultiplier(pair, sdk.NewDec(10)), ). Then( - 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)), + 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)), ), ), - tutilaction.TC("long bias, increase price multiplier"). + TC("long bias, increase price multiplier"). Given( - 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)))), + 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)))), ). When( - perpaction.EditPriceMultiplier(pair, sdk.NewDec(10)), + EditPriceMultiplier(pair, sdk.NewDec(10)), ). Then( - 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)), + 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)), ), ), - tutilaction.TC("long bias, decrease price multiplier"). + TC("long bias, decrease price multiplier"). Given( - 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)))), + 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)))), ). When( - perpaction.EditPriceMultiplier(pair, sdk.MustNewDecFromStr("0.25")), + EditPriceMultiplier(pair, sdk.MustNewDecFromStr("0.25")), ). Then( - 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")), + 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")), ), ), - tutilaction.TC("short bias, increase price multiplier"). + TC("short bias, increase price multiplier"). Given( - 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)))), + 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)))), ). When( - perpaction.EditPriceMultiplier(pair, sdk.NewDec(10)), + EditPriceMultiplier(pair, sdk.NewDec(10)), ). Then( - 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)), + 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)), ), ), - tutilaction.TC("short bias, decrease price multiplier"). + TC("short bias, decrease price multiplier"). Given( - 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)))), + 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)))), ). When( - perpaction.EditPriceMultiplier(pair, sdk.MustNewDecFromStr("0.25")), + EditPriceMultiplier(pair, sdk.MustNewDecFromStr("0.25")), ). Then( - 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")), + 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")), ), ), } - tutilaction.NewTestSuite(t).WithTestCases(tests...).Run() + 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 := tutilaction.TestCases{ - tutilaction.TC("same swap invariant"). + tests := TestCases{ + TC("same swap invariant"). Given( - perpaction.CreateCustomMarket(pair, - perpaction.WithTotalLong(sdk.NewDec(1000)), - perpaction.WithTotalShort(sdk.NewDec(500)), - perpaction.WithSqrtDepth(sdk.NewDec(1e6)), + CreateCustomMarket(pair, + WithTotalLong(sdk.NewDec(1000)), + WithTotalShort(sdk.NewDec(500)), + 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)))), + FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - perpaction.EditSwapInvariant(pair, sdk.NewDec(1e12)), + EditSwapInvariant(pair, sdk.NewDec(1e12)), ). Then( - 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()), + 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()), ), ), - tutilaction.TC("net bias zero"). + TC("net bias zero"). Given( - perpaction.CreateCustomMarket(pair, - perpaction.WithTotalLong(sdk.NewDec(1000)), - perpaction.WithTotalShort(sdk.NewDec(1000)), - perpaction.WithSqrtDepth(sdk.NewDec(1e6)), + CreateCustomMarket(pair, + WithTotalLong(sdk.NewDec(1000)), + WithTotalShort(sdk.NewDec(1000)), + 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)))), + FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - perpaction.EditSwapInvariant(pair, sdk.NewDec(1e18)), + EditSwapInvariant(pair, sdk.NewDec(1e18)), ). Then( - 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()), + 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()), ), ), - tutilaction.TC("long bias, increase swap invariant"). + TC("long bias, increase swap invariant"). Given( - 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)))), + 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)))), ). When( - perpaction.EditSwapInvariant(pair, sdk.NewDec(1e14)), + EditSwapInvariant(pair, sdk.NewDec(1e14)), ). Then( - 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()), + 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()), ), ), - tutilaction.TC("long bias, decrease swap invariant"). + TC("long bias, decrease swap invariant"). Given( - 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)))), + 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)))), ). When( - perpaction.EditSwapInvariant(pair, sdk.NewDec(1e6)), + EditSwapInvariant(pair, sdk.NewDec(1e6)), ). Then( - 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()), + 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()), ), ), - tutilaction.TC("short bias, increase swap invariant"). + TC("short bias, increase swap invariant"). Given( - 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)))), + 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)))), ). When( - perpaction.EditSwapInvariant(pair, sdk.NewDec(1e14)), + EditSwapInvariant(pair, sdk.NewDec(1e14)), ). Then( - 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()), + 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()), ), ), - tutilaction.TC("short bias, decrease swap invariant"). + TC("short bias, decrease swap invariant"). Given( - 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)))), + 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)))), ). When( - perpaction.EditSwapInvariant(pair, sdk.NewDec(1e6)), + EditSwapInvariant(pair, sdk.NewDec(1e6)), ). Then( - 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()), + 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()), ), ), } - tutilaction.NewTestSuite(t).WithTestCases(tests...).Run() + NewTestSuite(t).WithTestCases(tests...).Run() } diff --git a/x/perp/v2/keeper/calc_test.go b/x/perp/v2/keeper/calc_test.go index dee020800..dade53d60 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" - tutilaction "github.com/NibiruChain/nibiru/x/common/testutil/action" - perpaction "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" - perpassert "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" + . "github.com/NibiruChain/nibiru/x/common/testutil/action" + . "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" + . "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" ) func TestPositionNotionalSpot(t *testing.T) { @@ -68,74 +68,74 @@ func TestPositionNotionalTWAP(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.USDC) startTime := time.Now() - tc := tutilaction.TestCases{ - tutilaction.TC("long position"). + tc := TestCases{ + TC("long position"). Given( - tutilaction.SetBlockTime(startTime), - tutilaction.SetBlockNumber(1), - perpaction.CreateCustomMarket(pair), - perpaction.InsertPosition(perpaction.WithSize(sdk.NewDec(10)), perpaction.WithTrader(alice)), - perpaction.InsertReserveSnapshot(pair, startTime, perpaction.WithPriceMultiplier(sdk.NewDec(9))), - perpaction.InsertReserveSnapshot(pair, startTime.Add(10*time.Second), perpaction.WithPriceMultiplier(sdk.MustNewDecFromStr("8.5"))), - perpaction.InsertReserveSnapshot(pair, startTime.Add(20*time.Second), perpaction.WithPriceMultiplier(sdk.MustNewDecFromStr("9.5"))), + SetBlockTime(startTime), + SetBlockNumber(1), + CreateCustomMarket(pair), + InsertPosition(WithSize(sdk.NewDec(10)), WithTrader(alice)), + InsertReserveSnapshot(pair, startTime, WithPriceMultiplier(sdk.NewDec(9))), + InsertReserveSnapshot(pair, startTime.Add(10*time.Second), WithPriceMultiplier(sdk.MustNewDecFromStr("8.5"))), + InsertReserveSnapshot(pair, startTime.Add(20*time.Second), WithPriceMultiplier(sdk.MustNewDecFromStr("9.5"))), ). When( - tutilaction.MoveToNextBlockWithDuration(30 * time.Second), + MoveToNextBlockWithDuration(30 * time.Second), ). Then( - perpassert.PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.MustNewDecFromStr("89.999999999100000000")), + PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.MustNewDecFromStr("89.999999999100000000")), ), - tutilaction.TC("short position"). + TC("short position"). Given( - tutilaction.SetBlockTime(startTime), - tutilaction.SetBlockNumber(1), - perpaction.CreateCustomMarket(pair), - perpaction.InsertPosition(perpaction.WithSize(sdk.NewDec(-10)), perpaction.WithTrader(alice)), - perpaction.InsertReserveSnapshot(pair, startTime, perpaction.WithPriceMultiplier(sdk.NewDec(9))), - perpaction.InsertReserveSnapshot(pair, startTime.Add(10*time.Second), perpaction.WithPriceMultiplier(sdk.MustNewDecFromStr("8.5"))), - perpaction.InsertReserveSnapshot(pair, startTime.Add(20*time.Second), perpaction.WithPriceMultiplier(sdk.MustNewDecFromStr("9.5"))), + SetBlockTime(startTime), + SetBlockNumber(1), + CreateCustomMarket(pair), + InsertPosition(WithSize(sdk.NewDec(-10)), WithTrader(alice)), + InsertReserveSnapshot(pair, startTime, WithPriceMultiplier(sdk.NewDec(9))), + InsertReserveSnapshot(pair, startTime.Add(10*time.Second), WithPriceMultiplier(sdk.MustNewDecFromStr("8.5"))), + InsertReserveSnapshot(pair, startTime.Add(20*time.Second), WithPriceMultiplier(sdk.MustNewDecFromStr("9.5"))), ). When( - tutilaction.MoveToNextBlockWithDuration(30 * time.Second), + MoveToNextBlockWithDuration(30 * time.Second), ). Then( - perpassert.PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.MustNewDecFromStr("90.000000000900000000")), + PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.MustNewDecFromStr("90.000000000900000000")), ), - tutilaction.TC("zero position"). + TC("zero position"). Given( - tutilaction.SetBlockTime(startTime), - tutilaction.SetBlockNumber(1), - perpaction.CreateCustomMarket(pair), - perpaction.InsertPosition(perpaction.WithSize(sdk.ZeroDec()), perpaction.WithTrader(alice)), - perpaction.InsertReserveSnapshot(pair, startTime, perpaction.WithPriceMultiplier(sdk.NewDec(9))), - perpaction.InsertReserveSnapshot(pair, startTime.Add(10*time.Second), perpaction.WithPriceMultiplier(sdk.MustNewDecFromStr("8.5"))), - perpaction.InsertReserveSnapshot(pair, startTime.Add(20*time.Second), perpaction.WithPriceMultiplier(sdk.MustNewDecFromStr("9.5"))), + SetBlockTime(startTime), + SetBlockNumber(1), + CreateCustomMarket(pair), + InsertPosition(WithSize(sdk.ZeroDec()), WithTrader(alice)), + InsertReserveSnapshot(pair, startTime, WithPriceMultiplier(sdk.NewDec(9))), + InsertReserveSnapshot(pair, startTime.Add(10*time.Second), WithPriceMultiplier(sdk.MustNewDecFromStr("8.5"))), + InsertReserveSnapshot(pair, startTime.Add(20*time.Second), WithPriceMultiplier(sdk.MustNewDecFromStr("9.5"))), ). When( - tutilaction.MoveToNextBlockWithDuration(30 * time.Second), + MoveToNextBlockWithDuration(30 * time.Second), ). Then( - perpassert.PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.ZeroDec()), + PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.ZeroDec()), ), - tutilaction.TC("single snapshot"). + TC("single snapshot"). Given( - tutilaction.SetBlockTime(startTime), - tutilaction.SetBlockNumber(1), - perpaction.CreateCustomMarket(pair), + SetBlockTime(startTime), + SetBlockNumber(1), + CreateCustomMarket(pair), ). When( - perpaction.InsertPosition(perpaction.WithSize(sdk.NewDec(100)), perpaction.WithTrader(alice)), - perpaction.InsertReserveSnapshot(pair, startTime, perpaction.WithPriceMultiplier(sdk.NewDec(9))), + InsertPosition(WithSize(sdk.NewDec(100)), WithTrader(alice)), + InsertReserveSnapshot(pair, startTime, WithPriceMultiplier(sdk.NewDec(9))), ). Then( - perpassert.PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.MustNewDecFromStr("899.999999910000000009")), + PositionNotionalTWAPShouldBeEqualTo(pair, alice, 30*time.Second, sdk.MustNewDecFromStr("899.999999910000000009")), ), } - tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() + NewTestSuite(t).WithTestCases(tc...).Run() } func TestUnrealizedPnl(t *testing.T) { diff --git a/x/perp/v2/keeper/clearing_house_test.go b/x/perp/v2/keeper/clearing_house_test.go index ecc370adf..f7a33c531 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" - 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/action" + . "github.com/NibiruChain/nibiru/x/common/testutil/assertion" "github.com/NibiruChain/nibiru/x/common/testutil/mock" "github.com/NibiruChain/nibiru/x/common/testutil/testapp" - 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/integration/action" + . "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 := tutilaction.TestCases{ - tutilaction.TC("open big short position and then close after reducing swap invariant"). + tc := TestCases{ + TC("open big short position and then close after reducing swap invariant"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pairBtcNusd, - perpaction.WithPricePeg(sdk.OneDec()), - perpaction.WithSqrtDepth(sdk.NewDec(100_000)), + WithPricePeg(sdk.OneDec()), + WithSqrtDepth(sdk.NewDec(100_000)), ), - tutilaction.SetBlockNumber(1), - tutilaction.SetBlockTime(startBlockTime), + SetBlockNumber(1), + SetBlockTime(startBlockTime), - 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)))), + 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)))), - 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()), + 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.EditSwapInvariant(pairBtcNusd, sdk.OneDec()), + EditSwapInvariant(pairBtcNusd, sdk.OneDec()), ). When( - perpaction.PartialCloseFails(alice, pairBtcNusd, sdk.NewDec(5_000), types.ErrBaseReserveAtZero), + PartialCloseFails(alice, pairBtcNusd, sdk.NewDec(5_000), types.ErrBaseReserveAtZero), ). Then( - perpaction.ClosePosition(bob, pairBtcNusd), - perpaction.PartialClose(alice, pairBtcNusd, sdk.NewDec(5_000)), + ClosePosition(bob, pairBtcNusd), + PartialClose(alice, pairBtcNusd, sdk.NewDec(5_000)), ), - tutilaction.TC("new long position"). + TC("new long position"). Given( - perpaction.CreateCustomMarket(pairBtcNusd), - tutilaction.SetBlockTime(startBlockTime), - tutilaction.SetBlockNumber(1), - tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1020)))), + CreateCustomMarket(pairBtcNusd), + SetBlockTime(startBlockTime), + SetBlockNumber(1), + FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1020)))), ). When( - perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec(), - perpaction.MarketOrderResp_PositionShouldBeEqual( + MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec(), + MarketOrderResp_PositionShouldBeEqual( types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -76,18 +76,18 @@ func TestMarketOrder(t *testing.T) { LastUpdatedBlockNumber: 1, LatestCumulativePremiumFraction: sdk.ZeroDec(), }), - 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)), + 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)), ), ). Then( - perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pairBtcNusd, 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(), })), - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -120,18 +120,18 @@ func TestMarketOrder(t *testing.T) { }), ), - tutilaction.TC("existing long position, go more long"). + TC("existing long position, go more long"). Given( - 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()), + 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()), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec(), - perpaction.MarketOrderResp_PositionShouldBeEqual( + MoveToNextBlock(), + MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec(), + MarketOrderResp_PositionShouldBeEqual( types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -141,18 +141,18 @@ func TestMarketOrder(t *testing.T) { LastUpdatedBlockNumber: 2, LatestCumulativePremiumFraction: sdk.ZeroDec(), }), - 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)), + 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)), ), ). Then( - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -176,32 +176,32 @@ func TestMarketOrder(t *testing.T) { }), ), - tutilaction.TC("existing long position, go more long but there's bad debt"). + TC("existing long position, go more long but there's bad debt"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pairBtcNusd, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.89")), - perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + WithPricePeg(sdk.MustNewDecFromStr("0.89")), + WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - 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)), + 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)), ), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), + MoveToNextBlock(), + MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), types.ErrMarginRatioTooLow, ), ). Then( - perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pairBtcNusd, Position_PositionShouldBeEqualTo(types.Position{ TraderAddress: alice.String(), Pair: pairBtcNusd, Size_: sdk.NewDec(10_000), @@ -212,32 +212,32 @@ func TestMarketOrder(t *testing.T) { })), ), - tutilaction.TC("existing long position, close a bit but there's bad debt"). + TC("existing long position, close a bit but there's bad debt"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pairBtcNusd, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.89")), - perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + WithPricePeg(sdk.MustNewDecFromStr("0.89")), + WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - 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)), + 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)), ), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), + MoveToNextBlock(), + MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), types.ErrMarginRatioTooLow, ), ). Then( - perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pairBtcNusd, Position_PositionShouldBeEqualTo(types.Position{ TraderAddress: alice.String(), Pair: pairBtcNusd, Size_: sdk.NewDec(10_000), @@ -248,39 +248,39 @@ func TestMarketOrder(t *testing.T) { })), ), - tutilaction.TC("open big long position and then close after reducing swap invariant"). + TC("open big long position and then close after reducing swap invariant"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pairBtcNusd, - perpaction.WithPricePeg(sdk.OneDec()), - perpaction.WithSqrtDepth(sdk.NewDec(100_000)), + WithPricePeg(sdk.OneDec()), + WithSqrtDepth(sdk.NewDec(100_000)), ), - 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()), + 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()), ). When( - perpaction.ClosePosition(alice, pairBtcNusd), + ClosePosition(alice, pairBtcNusd), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd), ), - tutilaction.TC("existing long position, decrease a bit"). + TC("existing long position, decrease a bit"). Given( - 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()), + 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()), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(500), sdk.NewDec(10), sdk.ZeroDec(), - perpaction.MarketOrderResp_PositionShouldBeEqual( + MoveToNextBlock(), + MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(500), sdk.NewDec(10), sdk.ZeroDec(), + MarketOrderResp_PositionShouldBeEqual( types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -291,18 +291,18 @@ func TestMarketOrder(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), }, ), - 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)), + 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)), ), ). Then( - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -326,32 +326,32 @@ func TestMarketOrder(t *testing.T) { }), ), - tutilaction.TC("existing long position, decrease a bit but there's bad debt"). + TC("existing long position, decrease a bit but there's bad debt"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pairBtcNusd, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.89")), - perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + WithPricePeg(sdk.MustNewDecFromStr("0.89")), + WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - 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)), + 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)), ), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), + MoveToNextBlock(), + MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), types.ErrMarginRatioTooLow, ), ). Then( - perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pairBtcNusd, Position_PositionShouldBeEqualTo(types.Position{ TraderAddress: alice.String(), Pair: pairBtcNusd, Size_: sdk.NewDec(10_000), @@ -362,18 +362,18 @@ func TestMarketOrder(t *testing.T) { })), ), - tutilaction.TC("existing long position, decrease a lot"). + TC("existing long position, decrease a lot"). Given( - 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()), + 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()), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), - perpaction.MarketOrderResp_PositionShouldBeEqual( + MoveToNextBlock(), + MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), + MarketOrderResp_PositionShouldBeEqual( types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -384,18 +384,18 @@ func TestMarketOrder(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), }, ), - 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)), + 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)), ), ). Then( - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -419,28 +419,28 @@ func TestMarketOrder(t *testing.T) { }), ), - tutilaction.TC("existing long position, decrease a lot but there's bad debt"). + TC("existing long position, decrease a lot but there's bad debt"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pairBtcNusd, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.89")), - perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + WithPricePeg(sdk.MustNewDecFromStr("0.89")), + WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - 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)), + 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)), ), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), - perpaction.MarketOrderResp_PositionShouldBeEqual( + MoveToNextBlock(), + MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), + MarketOrderResp_PositionShouldBeEqual( types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -451,18 +451,18 @@ func TestMarketOrder(t *testing.T) { LatestCumulativePremiumFraction: sdk.MustNewDecFromStr("0.0002"), }, ), - 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()), + 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()), ), ). Then( - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -485,16 +485,16 @@ func TestMarketOrder(t *testing.T) { }), ), - tutilaction.TC("new short position"). + TC("new short position"). Given( - perpaction.CreateCustomMarket(pairBtcNusd), - tutilaction.SetBlockTime(startBlockTime), - tutilaction.SetBlockNumber(1), - tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1020)))), + CreateCustomMarket(pairBtcNusd), + SetBlockTime(startBlockTime), + SetBlockNumber(1), + FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1020)))), ). When( - perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec(), - perpaction.MarketOrderResp_PositionShouldBeEqual( + MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec(), + MarketOrderResp_PositionShouldBeEqual( types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -504,19 +504,19 @@ func TestMarketOrder(t *testing.T) { LastUpdatedBlockNumber: 1, LatestCumulativePremiumFraction: sdk.ZeroDec(), }), - 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)), + 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)), ), ). Then( - perpassert.PositionShouldBeEqual(alice, pairBtcNusd, - perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pairBtcNusd, + 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(), }), ), - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -550,18 +550,18 @@ func TestMarketOrder(t *testing.T) { }), ), - tutilaction.TC("existing short position, go more short"). + TC("existing short position, go more short"). Given( - 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()), + 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()), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec(), - perpaction.MarketOrderResp_PositionShouldBeEqual( + MoveToNextBlock(), + MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(1000), sdk.NewDec(10), sdk.ZeroDec(), + MarketOrderResp_PositionShouldBeEqual( types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -572,18 +572,18 @@ func TestMarketOrder(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), }, ), - 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)), + 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)), ), ). Then( - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -607,31 +607,31 @@ func TestMarketOrder(t *testing.T) { }), ), - tutilaction.TC("existing short position, go more short but there's bad debt"). + TC("existing short position, go more short but there's bad debt"). Given( - perpaction.CreateCustomMarket(pairBtcNusd, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("1.11")), - perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + CreateCustomMarket(pairBtcNusd, + WithPricePeg(sdk.MustNewDecFromStr("1.11")), + WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - 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)), + 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)), ), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), + MoveToNextBlock(), + MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), types.ErrMarginRatioTooLow, ), ). Then( - perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pairBtcNusd, Position_PositionShouldBeEqualTo(types.Position{ TraderAddress: alice.String(), Pair: pairBtcNusd, Size_: sdk.NewDec(-10_000), @@ -642,18 +642,18 @@ func TestMarketOrder(t *testing.T) { })), ), - tutilaction.TC("existing short position, decrease a bit"). + TC("existing short position, decrease a bit"). Given( - 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()), + 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()), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(500), sdk.NewDec(10), sdk.ZeroDec(), - perpaction.MarketOrderResp_PositionShouldBeEqual( + MoveToNextBlock(), + MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(500), sdk.NewDec(10), sdk.ZeroDec(), + MarketOrderResp_PositionShouldBeEqual( types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -664,18 +664,18 @@ func TestMarketOrder(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), }, ), - 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)), + 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)), ), ). Then( - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -699,31 +699,31 @@ func TestMarketOrder(t *testing.T) { }), ), - tutilaction.TC("existing short position, decrease a bit but there's bad debt"). + TC("existing short position, decrease a bit but there's bad debt"). Given( - perpaction.CreateCustomMarket(pairBtcNusd, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("1.11")), - perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + CreateCustomMarket(pairBtcNusd, + WithPricePeg(sdk.MustNewDecFromStr("1.11")), + WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - 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)), + 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)), ), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), + MoveToNextBlock(), + MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec(), types.ErrMarginRatioTooLow, ), ). Then( - perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pairBtcNusd, Position_PositionShouldBeEqualTo(types.Position{ TraderAddress: alice.String(), Pair: pairBtcNusd, Size_: sdk.NewDec(-10_000), @@ -734,18 +734,18 @@ func TestMarketOrder(t *testing.T) { })), ), - tutilaction.TC("existing short position, decrease a lot"). + TC("existing short position, decrease a lot"). Given( - 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()), + 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()), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), - perpaction.MarketOrderResp_PositionShouldBeEqual( + MoveToNextBlock(), + MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), + MarketOrderResp_PositionShouldBeEqual( types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -756,18 +756,18 @@ func TestMarketOrder(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), }, ), - 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)), + 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)), ), ). Then( - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -791,27 +791,27 @@ func TestMarketOrder(t *testing.T) { }), ), - tutilaction.TC("existing short position, decrease a lot but there's bad debt"). + TC("existing short position, decrease a lot but there's bad debt"). Given( - perpaction.CreateCustomMarket(pairBtcNusd, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("1.11")), - perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + CreateCustomMarket(pairBtcNusd, + WithPricePeg(sdk.MustNewDecFromStr("1.11")), + WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - 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)), + 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)), ), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), - perpaction.MarketOrderResp_PositionShouldBeEqual( + MoveToNextBlock(), + MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(3000), sdk.NewDec(10), sdk.ZeroDec(), + MarketOrderResp_PositionShouldBeEqual( types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -822,18 +822,18 @@ func TestMarketOrder(t *testing.T) { LatestCumulativePremiumFraction: sdk.MustNewDecFromStr("0.0002"), }, ), - 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()), + 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()), ), ). Then( - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -857,208 +857,208 @@ func TestMarketOrder(t *testing.T) { }), ), - tutilaction.TC("user has insufficient funds"). + TC("user has insufficient funds"). Given( - perpaction.CreateCustomMarket(pairBtcNusd), - tutilaction.SetBlockTime(startBlockTime), - tutilaction.SetBlockNumber(1), - tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(99)))), + CreateCustomMarket(pairBtcNusd), + SetBlockTime(startBlockTime), + SetBlockNumber(1), + FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(99)))), ). When( - perpaction.MarketOrderFails( + MarketOrderFails( alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(100), sdk.OneDec(), sdk.ZeroDec(), sdkerrors.ErrInsufficientFunds), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd), ), - tutilaction.TC("new long position, can close position after market is not enabled"). + TC("new long position, can close position after market is not enabled"). Given( - 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), + 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), ). When( - perpaction.ClosePosition(alice, pairBtcNusd), + ClosePosition(alice, pairBtcNusd), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd), ), - tutilaction.TC("new long position, can not open new position after market is not enabled"). + TC("new long position, can not open new position after market is not enabled"). Given( - 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), + CreateCustomMarket(pairBtcNusd), + SetBlockTime(startBlockTime), + SetBlockNumber(1), + FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(47_714_285_715)))), + SetMarketEnabled(pairBtcNusd, false), ). When( - perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(47_619_047_619), sdk.OneDec(), sdk.ZeroDec(), + MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(47_619_047_619), sdk.OneDec(), sdk.ZeroDec(), types.ErrMarketNotEnabled), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd), ), - tutilaction.TC("existing long position, can not open new one but can close"). + TC("existing long position, can not open new one but can close"). Given( - 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), + 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), ). When( - perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(47_619_047_619), sdk.OneDec(), sdk.ZeroDec(), + MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(47_619_047_619), sdk.OneDec(), sdk.ZeroDec(), types.ErrMarketNotEnabled), - perpaction.ClosePosition(alice, pairBtcNusd), + ClosePosition(alice, pairBtcNusd), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd), ), - tutilaction.TC("market doesn't exist"). + TC("market doesn't exist"). Given( - tutilaction.SetBlockTime(startBlockTime), - tutilaction.SetBlockNumber(1), - tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(47_714_285_715)))), + SetBlockTime(startBlockTime), + SetBlockNumber(1), + FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(47_714_285_715)))), ). When( - perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(47_619_047_619), sdk.OneDec(), sdk.ZeroDec(), + MarketOrderFails(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(47_619_047_619), sdk.OneDec(), sdk.ZeroDec(), types.ErrPairNotFound), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd), ), - tutilaction.TC("zero quote asset amount"). + TC("zero quote asset amount"). Given( - tutilaction.SetBlockTime(startBlockTime), - tutilaction.SetBlockNumber(1), - perpaction.CreateCustomMarket(pairBtcNusd), - tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(47_714_285_715)))), + SetBlockTime(startBlockTime), + SetBlockNumber(1), + CreateCustomMarket(pairBtcNusd), + FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(47_714_285_715)))), ). When( - perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.ZeroInt(), sdk.OneDec(), sdk.ZeroDec(), + MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.ZeroInt(), sdk.OneDec(), sdk.ZeroDec(), types.ErrInputQuoteAmtNegative), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd), ), - tutilaction.TC("zero leverage"). + TC("zero leverage"). Given( - tutilaction.SetBlockTime(startBlockTime), - tutilaction.SetBlockNumber(1), - perpaction.CreateCustomMarket(pairBtcNusd), - tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + SetBlockTime(startBlockTime), + SetBlockNumber(1), + CreateCustomMarket(pairBtcNusd), + FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.ZeroDec(), sdk.ZeroDec(), + MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.ZeroDec(), sdk.ZeroDec(), types.ErrUserLeverageNegative), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd), ), - tutilaction.TC("user leverage greater than market max leverage"). + TC("user leverage greater than market max leverage"). Given( - tutilaction.SetBlockTime(startBlockTime), - tutilaction.SetBlockNumber(1), - perpaction.CreateCustomMarket(pairBtcNusd), - tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), + SetBlockTime(startBlockTime), + SetBlockNumber(1), + CreateCustomMarket(pairBtcNusd), + FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - perpaction.MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.NewDec(11), sdk.ZeroDec(), + MarketOrderFails(alice, pairBtcNusd, types.Direction_LONG, sdk.OneInt(), sdk.NewDec(11), sdk.ZeroDec(), types.ErrLeverageIsTooHigh), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd), ), - tutilaction.TC("position should not exist after opening a closing manually"). + TC("position should not exist after opening a closing manually"). Given( - 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)))), + 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)))), ). When( - 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()), + 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()), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd), ), - tutilaction.TC("position should not exist after opening a closing manually - reverse with leverage"). + TC("position should not exist after opening a closing manually - reverse with leverage"). Given( - 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)))), + SetBlockTime(startBlockTime), + SetBlockNumber(1), + CreateCustomMarket(pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("25001.0112"))), + FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - 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()), + 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()), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd), ), - tutilaction.TC("position should not exist after opening a closing manually - open with leverage"). + TC("position should not exist after opening a closing manually - open with leverage"). Given( - 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)))), + SetBlockTime(startBlockTime), + SetBlockNumber(1), + CreateCustomMarket(pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("25001.0112"))), + FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - 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()), + 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()), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd), ), - tutilaction.TC("position should not exist after opening a closing manually - reverse with leverage"). + TC("position should not exist after opening a closing manually - reverse with leverage"). Given( - 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)))), + SetBlockTime(startBlockTime), + SetBlockNumber(1), + CreateCustomMarket(pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("25001.0112"))), + FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - 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()), + 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()), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd), ), - tutilaction.TC("position should not exist after opening a closing manually - reverse with leverage - more steps"). + TC("position should not exist after opening a closing manually - reverse with leverage - more steps"). Given( - 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)))), + SetBlockTime(startBlockTime), + SetBlockNumber(1), + CreateCustomMarket(pairBtcNusd, WithPricePeg(sdk.MustNewDecFromStr("25000"))), + FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1e6)))), ). When( - 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()), + 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()), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd), ), } - tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() + 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 := tutilaction.TestCases{ - tutilaction.TC("partial close long position with positive PnL"). + tc := TestCases{ + TC("partial close long position with positive PnL"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pairBtcNusd, - perpaction.WithPricePeg(sdk.NewDec(2)), - perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + WithPricePeg(sdk.NewDec(2)), + WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - 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)), + 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)), ), ). When( - perpaction.PartialClose(alice, pairBtcNusd, sdk.NewDec(2_500)), + PartialClose(alice, pairBtcNusd, sdk.NewDec(2_500)), ). Then( - perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pairBtcNusd, 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"), })), - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -1254,29 +1254,29 @@ func TestPartialClose(t *testing.T) { }), ), - tutilaction.TC("partial close long position with negative PnL"). + TC("partial close long position with negative PnL"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pairBtcNusd, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.95")), - perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + WithPricePeg(sdk.MustNewDecFromStr("0.95")), + WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - 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)), + 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)), ), ). When( - perpaction.PartialClose(alice, pairBtcNusd, sdk.NewDec(2_500)), + PartialClose(alice, pairBtcNusd, sdk.NewDec(2_500)), ). Then( - perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pairBtcNusd, 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"), })), - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -1308,29 +1308,29 @@ func TestPartialClose(t *testing.T) { }), ), - tutilaction.TC("partial close long position without bad debt but below maintenance margin ratio"). + TC("partial close long position without bad debt but below maintenance margin ratio"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pairBtcNusd, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.94")), - perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + WithPricePeg(sdk.MustNewDecFromStr("0.94")), + WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - 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)), + 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)), ), ). When( - perpaction.PartialClose(alice, pairBtcNusd, sdk.NewDec(2_500)), + PartialClose(alice, pairBtcNusd, sdk.NewDec(2_500)), ). Then( - perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pairBtcNusd, 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"), })), - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + 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"), })), - tutilaction.TC("partial close long position with bad debt"). + TC("partial close long position with bad debt"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pairBtcNusd, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.59")), - perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + WithPricePeg(sdk.MustNewDecFromStr("0.59")), + WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - 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)), + 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)), ), ). When( - perpaction.PartialClose(alice, pairBtcNusd, sdk.NewDec(2_500)), + PartialClose(alice, pairBtcNusd, sdk.NewDec(2_500)), ). Then( - perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pairBtcNusd, 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"), })), - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + 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"), })), - tutilaction.TC("partial close short position with positive PnL"). + TC("partial close short position with positive PnL"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pairBtcNusd, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.10")), - perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + WithPricePeg(sdk.MustNewDecFromStr("0.10")), + WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - 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)), + 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)), ), ). When( - perpaction.PartialClose(alice, pairBtcNusd, sdk.NewDec(7_500)), + PartialClose(alice, pairBtcNusd, sdk.NewDec(7_500)), ). Then( - perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pairBtcNusd, 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"), })), - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -1469,29 +1469,29 @@ func TestPartialClose(t *testing.T) { }), ), - tutilaction.TC("partial close short position with negative PnL"). + TC("partial close short position with negative PnL"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pairBtcNusd, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("1.05")), - perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + WithPricePeg(sdk.MustNewDecFromStr("1.05")), + WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - 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)), + 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)), ), ). When( - perpaction.PartialClose(alice, pairBtcNusd, sdk.NewDec(7_500)), + PartialClose(alice, pairBtcNusd, sdk.NewDec(7_500)), ). Then( - perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pairBtcNusd, 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"), })), - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -1523,29 +1523,29 @@ func TestPartialClose(t *testing.T) { }), ), - tutilaction.TC("partial close short position with no bad debt but below maintenance margin ratio"). + TC("partial close short position with no bad debt but below maintenance margin ratio"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pairBtcNusd, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("1.09")), - perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + WithPricePeg(sdk.MustNewDecFromStr("1.09")), + WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - 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)), + 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)), ), ). When( - perpaction.PartialClose(alice, pairBtcNusd, sdk.NewDec(7_500)), + PartialClose(alice, pairBtcNusd, sdk.NewDec(7_500)), ). Then( - perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pairBtcNusd, 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"), })), - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -1577,30 +1577,30 @@ func TestPartialClose(t *testing.T) { }), ), - tutilaction.TC("partial close short position with bad debt"). + TC("partial close short position with bad debt"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pairBtcNusd, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("1.14")), - perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + WithPricePeg(sdk.MustNewDecFromStr("1.14")), + WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - 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)), + 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)), ), ). When( - perpaction.PartialClose(alice, pairBtcNusd, sdk.NewDec(7_500)), + PartialClose(alice, pairBtcNusd, sdk.NewDec(7_500)), ). Then( - perpassert.PositionShouldBeEqual(alice, pairBtcNusd, perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pairBtcNusd, 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"), })), - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + 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"), }), ), - tutilaction.TC("test partial closes fail"). + TC("test partial closes fail"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pairBtcNusd, - perpaction.WithPricePeg(sdk.OneDec()), - perpaction.WithSqrtDepth(sdk.NewDec(10_000)), + WithPricePeg(sdk.OneDec()), + WithSqrtDepth(sdk.NewDec(10_000)), ), - tutilaction.SetBlockNumber(1), - tutilaction.SetBlockTime(startBlockTime), + SetBlockNumber(1), + SetBlockTime(startBlockTime), - tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200)))), + FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200)))), - 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()), + PartialCloseFails(alice, pairBtcNusd, sdk.NewDec(5_000), collections.ErrNotFound), + MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(9_000), sdk.OneDec(), sdk.ZeroDec()), ). When( - perpaction.PartialCloseFails(alice, asset.MustNewPair("luna:usdt"), sdk.NewDec(5_000), types.ErrPairNotFound), + PartialCloseFails(alice, asset.MustNewPair("luna:usdt"), sdk.NewDec(5_000), types.ErrPairNotFound), ), } - tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() + 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 := tutilaction.TestCases{ - tutilaction.TC("close long position with positive PnL"). + tc := TestCases{ + TC("close long position with positive PnL"). Given( - perpaction.CreateCustomMarket(pairBtcNusd, - perpaction.WithPricePeg(sdk.NewDec(2)), - perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + CreateCustomMarket(pairBtcNusd, + WithPricePeg(sdk.NewDec(2)), + WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - 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)), + 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)), ), ). When( - perpaction.ClosePosition(alice, pairBtcNusd), + ClosePosition(alice, pairBtcNusd), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + PositionShouldNotExist(alice, pairBtcNusd), + PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -1706,30 +1706,30 @@ func TestClosePosition(t *testing.T) { }), ), - tutilaction.TC("close long position with negative PnL"). + TC("close long position with negative PnL"). Given( - perpaction.CreateCustomMarket(pairBtcNusd, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.99")), - perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + CreateCustomMarket(pairBtcNusd, + WithPricePeg(sdk.MustNewDecFromStr("0.99")), + WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - 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)), + 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)), ), ). When( - perpaction.ClosePosition(alice, pairBtcNusd), + ClosePosition(alice, pairBtcNusd), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + PositionShouldNotExist(alice, pairBtcNusd), + PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -1752,32 +1752,32 @@ func TestClosePosition(t *testing.T) { }), ), - tutilaction.TC("close long position with bad debt"). + TC("close long position with bad debt"). Given( - perpaction.CreateCustomMarket(pairBtcNusd, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.89")), - perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + CreateCustomMarket(pairBtcNusd, + WithPricePeg(sdk.MustNewDecFromStr("0.89")), + WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - 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)), + 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.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))), + 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))), ). When( - tutilaction.MoveToNextBlock(), - perpaction.ClosePosition(alice, pairBtcNusd), + MoveToNextBlock(), + ClosePosition(alice, pairBtcNusd), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + PositionShouldNotExist(alice, pairBtcNusd), + 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"), }), - tutilassert.ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1102)), // 1000 + 102 from perp ef - tutilassert.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(9)), + ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1102)), // 1000 + 102 from perp ef + ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(9)), ), - tutilaction.TC("close short position with positive PnL"). + TC("close short position with positive PnL"). Given( - perpaction.CreateCustomMarket(pairBtcNusd, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.10")), - perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + CreateCustomMarket(pairBtcNusd, + WithPricePeg(sdk.MustNewDecFromStr("0.10")), + WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - 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)), + 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)), ), ). When( - perpaction.ClosePosition(alice, pairBtcNusd), + ClosePosition(alice, pairBtcNusd), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + PositionShouldNotExist(alice, pairBtcNusd), + PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -1848,30 +1848,30 @@ func TestClosePosition(t *testing.T) { }), ), - tutilaction.TC("close short position with negative PnL"). + TC("close short position with negative PnL"). Given( - perpaction.CreateCustomMarket(pairBtcNusd, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("1.01")), - perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + CreateCustomMarket(pairBtcNusd, + WithPricePeg(sdk.MustNewDecFromStr("1.01")), + WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - 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)), + 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)), ), ). When( - perpaction.ClosePosition(alice, pairBtcNusd), + ClosePosition(alice, pairBtcNusd), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + PositionShouldNotExist(alice, pairBtcNusd), + PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, TraderAddress: alice.String(), @@ -1894,32 +1894,32 @@ func TestClosePosition(t *testing.T) { }), ), - tutilaction.TC("close short position with bad debt"). + TC("close short position with bad debt"). Given( - perpaction.CreateCustomMarket(pairBtcNusd, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("1.11")), - perpaction.WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), + CreateCustomMarket(pairBtcNusd, + WithPricePeg(sdk.MustNewDecFromStr("1.11")), + WithLatestMarketCPF(sdk.MustNewDecFromStr("0.0002")), ), - 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)), + 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.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))), + 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))), ). When( - tutilaction.MoveToNextBlock(), - perpaction.ClosePosition(alice, pairBtcNusd), + MoveToNextBlock(), + ClosePosition(alice, pairBtcNusd), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + PositionShouldNotExist(alice, pairBtcNusd), + 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"), }), - tutilassert.ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1098)), // 1000 + 98 from perp ef - tutilassert.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(11)), + ModuleBalanceEqual(types.VaultModuleAccount, denoms.NUSD, sdk.NewInt(1098)), // 1000 + 98 from perp ef + ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(11)), ), } - tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() + 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 := tutilaction.TestCases{ - tutilaction.TC("only long position - no change to swap invariant"). + tc := TestCases{ + TC("only long position - no change to swap invariant"). Given( - 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)))), + 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)))), ). When( - perpaction.MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), - perpaction.ClosePosition(alice, pairBtcNusd), + MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), + ClosePosition(alice, pairBtcNusd), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd), ), - tutilaction.TC("only short position - no change to swap invariant"). + TC("only short position - no change to swap invariant"). Given( - 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)))), + 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)))), ). When( - 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), + MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.NewDec(10_000_000_000_000)), + ClosePosition(alice, pairBtcNusd), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd), ), - tutilaction.TC("only long position - increasing k"). + TC("only long position - increasing k"). Given( - 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)))), + 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)))), ). When( - 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( + MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), + EditSwapInvariant(pairBtcNusd, startingSwapInvariant.MulInt64(100)), + AMMShouldBeEqual( pairBtcNusd, - perpassert.AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999999999999.999999000000000000"))), - perpaction.ClosePosition(alice, pairBtcNusd), + AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999999999999.999999000000000000"))), + ClosePosition(alice, pairBtcNusd), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), - perpassert.ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), + PositionShouldNotExist(alice, pairBtcNusd), + ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), ), - tutilaction.TC("only short position - increasing k"). + TC("only short position - increasing k"). Given( - 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)))), + 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)))), ). When( - 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( + MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), + EditSwapInvariant(pairBtcNusd, startingSwapInvariant.MulInt64(100)), + AMMShouldBeEqual( pairBtcNusd, - perpassert.AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999999999999.999999000000000000"))), - perpaction.ClosePosition(alice, pairBtcNusd), + AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999999999999.999999000000000000"))), + ClosePosition(alice, pairBtcNusd), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), - perpassert.ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.OneInt()))), + PositionShouldNotExist(alice, pairBtcNusd), + ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.OneInt()))), ), - tutilaction.TC("only long position - decreasing k"). + TC("only long position - decreasing k"). Given( - 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)))), + 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)))), ). When( - 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( + 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( pairBtcNusd, - perpassert.AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999873578.871987715651277660"))), - perpaction.ClosePosition(alice, pairBtcNusd), + AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999873578.871987715651277660"))), + ClosePosition(alice, pairBtcNusd), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), - perpassert.ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), + PositionShouldNotExist(alice, pairBtcNusd), + ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), ), - tutilaction.TC("only short position - decreasing k"). + TC("only short position - decreasing k"). Given( - 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)))), + 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)))), ). When( - 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( + 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( pairBtcNusd, - perpassert.AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999873578.871987801032774485"))), - perpaction.ClosePosition(alice, pairBtcNusd), + AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999873578.871987801032774485"))), + ClosePosition(alice, pairBtcNusd), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), - perpassert.ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), + PositionShouldNotExist(alice, pairBtcNusd), + ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), ), - tutilaction.TC("long and short position - increasing k"). + TC("long and short position - increasing k"). Given( - 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)))), + 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)))), ). When( - 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)), + 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.EditSwapInvariant(pairBtcNusd, startingSwapInvariant.MulInt64(100)), - perpassert.AMMShouldBeEqual( + EditSwapInvariant(pairBtcNusd, startingSwapInvariant.MulInt64(100)), + AMMShouldBeEqual( pairBtcNusd, - perpassert.AMM_BiasShouldBeEqual(sdk.ZeroDec()), - perpassert.AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("100000000000000000000000000.000000000000000000"))), + AMM_BiasShouldBeEqual(sdk.ZeroDec()), + AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("100000000000000000000000000.000000000000000000"))), - 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 + 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 - perpaction.ClosePosition(alice, pairBtcNusd), - perpaction.ClosePosition(bob, pairBtcNusd), + ClosePosition(alice, pairBtcNusd), + ClosePosition(bob, pairBtcNusd), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), - perpassert.PositionShouldNotExist(bob, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(bob, pairBtcNusd), - perpassert.ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), - perpassert.ModuleBalanceShouldBeEqualTo(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(39_782_394)))), + ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), + ModuleBalanceShouldBeEqualTo(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(39_782_394)))), ), - tutilaction.TC("long and short position - reducing k"). + TC("long and short position - reducing k"). Given( - 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)))), + 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)))), ). When( - 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)), + 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.EditSwapInvariant(pairBtcNusd, startingSwapInvariant.Mul(sdk.MustNewDecFromStr("0.1"))), - perpassert.AMMShouldBeEqual( + EditSwapInvariant(pairBtcNusd, startingSwapInvariant.Mul(sdk.MustNewDecFromStr("0.1"))), + AMMShouldBeEqual( pairBtcNusd, - perpassert.AMM_BiasShouldBeEqual(sdk.ZeroDec()), - perpassert.AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999873578.871987712489000000"))), + AMM_BiasShouldBeEqual(sdk.ZeroDec()), + AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999873578.871987712489000000"))), - 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 + 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 - perpaction.ClosePosition(alice, pairBtcNusd), - perpaction.ClosePosition(bob, pairBtcNusd), + ClosePosition(alice, pairBtcNusd), + ClosePosition(bob, pairBtcNusd), ). Then( - perpassert.PositionShouldNotExist(alice, pairBtcNusd), - perpassert.PositionShouldNotExist(bob, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(bob, pairBtcNusd), - perpassert.ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), - perpassert.ModuleBalanceShouldBeEqualTo(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(39_200_810)))), + ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), + ModuleBalanceShouldBeEqualTo(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(39_200_810)))), ), } - tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() + 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 86d9f894d..d01280814 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" - tutilaction "github.com/NibiruChain/nibiru/x/common/testutil/action" - perpaction "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" + . "github.com/NibiruChain/nibiru/x/common/testutil/action" + . "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 := tutilaction.TestCases{ - tutilaction.TC("positive PnL"). + tc := TestCases{ + TC("positive PnL"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pair, - perpaction.WithPricePeg(sdk.NewDec(2)), + WithPricePeg(sdk.NewDec(2)), ), - perpaction.CreateCustomMarket( + CreateCustomMarket( pair2, - perpaction.WithPricePeg(sdk.NewDec(3)), + WithPricePeg(sdk.NewDec(3)), ), ). When( - perpaction.InsertPosition( - perpaction.WithPair(pair), - perpaction.WithTrader(alice), - perpaction.WithMargin(sdk.OneDec()), - perpaction.WithSize(sdk.NewDec(10)), - perpaction.WithOpenNotional(sdk.NewDec(10)), + InsertPosition( + WithPair(pair), + 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)), + InsertPosition( + WithPair(pair2), + WithTrader(alice), + WithMargin(sdk.OneDec()), + WithSize(sdk.NewDec(10)), + WithOpenNotional(sdk.NewDec(10)), ), ). Then( - perpaction.QueryPositions(alice, - []perpaction.QueryPositionChecker{ - perpaction.QueryPosition_PositionEquals(types.Position{ + QueryPositions(alice, + []QueryPositionChecker{ + 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, }), - perpaction.QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("19.9999999998")), - perpaction.QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("9.9999999998")), - perpaction.QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.5499999999955")), + QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("19.9999999998")), + QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("9.9999999998")), + QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.5499999999955")), }, - []perpaction.QueryPositionChecker{ - perpaction.QueryPosition_PositionEquals(types.Position{ + []QueryPositionChecker{ + 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, }), - perpaction.QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("29.9999999997")), - perpaction.QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("19.9999999997")), - perpaction.QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.699999999997")), + QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("29.9999999997")), + QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("19.9999999997")), + QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.699999999997")), }, ), ), - tutilaction.TC("negative PnL, positive margin ratio"). + TC("negative PnL, positive margin ratio"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pair, - perpaction.WithPricePeg(sdk.OneDec()), + WithPricePeg(sdk.OneDec()), ), - perpaction.CreateCustomMarket( + CreateCustomMarket( pair2, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.95")), + WithPricePeg(sdk.MustNewDecFromStr("0.95")), ), ). When( - perpaction.InsertPosition( - perpaction.WithPair(pair), - perpaction.WithTrader(alice), - perpaction.WithMargin(sdk.OneDec()), - perpaction.WithSize(sdk.NewDec(10)), - perpaction.WithOpenNotional(sdk.NewDec(10)), + InsertPosition( + WithPair(pair), + 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)), + InsertPosition( + WithPair(pair2), + WithTrader(alice), + WithMargin(sdk.OneDec()), + WithSize(sdk.NewDec(10)), + WithOpenNotional(sdk.NewDec(10)), ), ). Then( - perpaction.QueryPositions(alice, - []perpaction.QueryPositionChecker{ - perpaction.QueryPosition_PositionEquals(types.Position{ + QueryPositions(alice, + []QueryPositionChecker{ + 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, }), - perpaction.QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("9.9999999999")), - perpaction.QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-0.0000000001")), - perpaction.QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.099999999991")), + QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("9.9999999999")), + QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-0.0000000001")), + QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.099999999991")), }, - []perpaction.QueryPositionChecker{ - perpaction.QueryPosition_PositionEquals(types.Position{ + []QueryPositionChecker{ + 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, }), - perpaction.QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("9.499999999905")), - perpaction.QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-0.500000000095")), - perpaction.QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.052631578937894737")), + QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("9.499999999905")), + QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-0.500000000095")), + QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.052631578937894737")), }, ), ), - tutilaction.TC("negative PnL, negative margin ratio"). + TC("negative PnL, negative margin ratio"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pair, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.5")), + WithPricePeg(sdk.MustNewDecFromStr("0.5")), ), - perpaction.CreateCustomMarket( + CreateCustomMarket( pair2, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.9")), + WithPricePeg(sdk.MustNewDecFromStr("0.9")), ), ). When( - perpaction.InsertPosition( - perpaction.WithPair(pair), - perpaction.WithTrader(alice), - perpaction.WithMargin(sdk.OneDec()), - perpaction.WithSize(sdk.NewDec(10)), - perpaction.WithOpenNotional(sdk.NewDec(10)), + InsertPosition( + WithPair(pair), + 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)), + InsertPosition( + WithPair(pair2), + WithTrader(alice), + WithMargin(sdk.OneDec()), + WithSize(sdk.NewDec(10)), + WithOpenNotional(sdk.NewDec(10)), ), ). Then( - perpaction.QueryPositions(alice, - []perpaction.QueryPositionChecker{ - perpaction.QueryPosition_PositionEquals(types.Position{ + QueryPositions(alice, + []QueryPositionChecker{ + 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, }), - perpaction.QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("4.99999999995")), - perpaction.QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-5.00000000005")), - perpaction.QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("-0.800000000018")), + QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("4.99999999995")), + QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-5.00000000005")), + QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("-0.800000000018")), }, - []perpaction.QueryPositionChecker{ - perpaction.QueryPosition_PositionEquals(types.Position{ + []QueryPositionChecker{ + 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, }), - perpaction.QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("8.99999999991")), - perpaction.QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-1.00000000009")), - perpaction.QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("-0.00000000001")), + QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("8.99999999991")), + QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-1.00000000009")), + QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("-0.00000000001")), }, ), ), } - tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() + NewTestSuite(t).WithTestCases(tc...).Run() } func TestQueryPosition(t *testing.T) { alice := testutil.AccAddress() pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) - tc := tutilaction.TestCases{ - tutilaction.TC("positive PnL"). + tc := TestCases{ + TC("positive PnL"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pair, - perpaction.WithPricePeg(sdk.NewDec(2)), + WithPricePeg(sdk.NewDec(2)), ), ). When( - perpaction.InsertPosition( - perpaction.WithPair(pair), - perpaction.WithTrader(alice), - perpaction.WithMargin(sdk.OneDec()), - perpaction.WithSize(sdk.NewDec(10)), - perpaction.WithOpenNotional(sdk.NewDec(10)), + InsertPosition( + WithPair(pair), + WithTrader(alice), + WithMargin(sdk.OneDec()), + WithSize(sdk.NewDec(10)), + WithOpenNotional(sdk.NewDec(10)), ), ). Then( - perpaction.QueryPosition(pair, alice, - perpaction.QueryPosition_PositionEquals(types.Position{ + QueryPosition(pair, alice, + 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, }), - perpaction.QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("19.9999999998")), - perpaction.QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("9.9999999998")), - perpaction.QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.5499999999955")), + QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("19.9999999998")), + QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("9.9999999998")), + QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.5499999999955")), ), ), - tutilaction.TC("negative PnL, positive margin ratio"). + TC("negative PnL, positive margin ratio"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pair, - perpaction.WithPricePeg(sdk.OneDec()), + WithPricePeg(sdk.OneDec()), ), ). When( - perpaction.InsertPosition( - perpaction.WithPair(pair), - perpaction.WithTrader(alice), - perpaction.WithMargin(sdk.OneDec()), - perpaction.WithSize(sdk.NewDec(10)), - perpaction.WithOpenNotional(sdk.NewDec(10)), + InsertPosition( + WithPair(pair), + WithTrader(alice), + WithMargin(sdk.OneDec()), + WithSize(sdk.NewDec(10)), + WithOpenNotional(sdk.NewDec(10)), ), ). Then( - perpaction.QueryPosition(pair, alice, - perpaction.QueryPosition_PositionEquals(types.Position{ + QueryPosition(pair, alice, + 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, }), - perpaction.QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("9.9999999999")), - perpaction.QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-0.0000000001")), - perpaction.QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.099999999991")), + QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("9.9999999999")), + QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-0.0000000001")), + QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("0.099999999991")), ), ), - tutilaction.TC("negative PnL, negative margin ratio"). + TC("negative PnL, negative margin ratio"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pair, - perpaction.WithPricePeg(sdk.MustNewDecFromStr("0.5")), + WithPricePeg(sdk.MustNewDecFromStr("0.5")), ), ). When( - perpaction.InsertPosition( - perpaction.WithPair(pair), - perpaction.WithTrader(alice), - perpaction.WithMargin(sdk.OneDec()), - perpaction.WithSize(sdk.NewDec(10)), - perpaction.WithOpenNotional(sdk.NewDec(10)), + InsertPosition( + WithPair(pair), + WithTrader(alice), + WithMargin(sdk.OneDec()), + WithSize(sdk.NewDec(10)), + WithOpenNotional(sdk.NewDec(10)), ), ). Then( - perpaction.QueryPosition(pair, alice, - perpaction.QueryPosition_PositionEquals(types.Position{ + QueryPosition(pair, alice, + QueryPosition_PositionEquals(types.Position{ Pair: pair, TraderAddress: alice.String(), Size_: sdk.NewDec(10), @@ -303,55 +303,53 @@ func TestQueryPosition(t *testing.T) { LatestCumulativePremiumFraction: sdk.ZeroDec(), LastUpdatedBlockNumber: 0, }), - perpaction.QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("4.99999999995")), - perpaction.QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-5.00000000005")), - perpaction.QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("-0.800000000018")), + QueryPosition_PositionNotionalEquals(sdk.MustNewDecFromStr("4.99999999995")), + QueryPosition_UnrealizedPnlEquals(sdk.MustNewDecFromStr("-5.00000000005")), + QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("-0.800000000018")), ), ), - tutilaction.TC("non existent position"). + TC("non existent position"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pair, - perpaction.WithPricePeg(sdk.NewDec(2)), + WithPricePeg(sdk.NewDec(2)), ), ). When(). Then( - perpaction.QueryPositionNotFound(pair, alice), + QueryPositionNotFound(pair, alice), ), } - tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() + NewTestSuite(t).WithTestCases(tc...).Run() } func TestQueryMarkets(t *testing.T) { alice := testutil.AccAddress() pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) - tc := tutilaction.TestCases{ - tutilaction.TC("positive PnL"). + tc := TestCases{ + TC("positive PnL"). Given( - perpaction.CreateCustomMarket( + CreateCustomMarket( pair, - perpaction.WithPricePeg(sdk.NewDec(2)), - ), - tutilaction.FundModule( - "perp_ef", sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10))), + WithPricePeg(sdk.NewDec(2)), ), + FundModule("perp_ef", sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10)))), ). When( - perpaction.InsertPosition( - perpaction.WithPair(pair), - perpaction.WithTrader(alice), - perpaction.WithMargin(sdk.OneDec()), - perpaction.WithSize(sdk.NewDec(10)), - perpaction.WithOpenNotional(sdk.NewDec(10)), + InsertPosition( + WithPair(pair), + WithTrader(alice), + WithMargin(sdk.OneDec()), + WithSize(sdk.NewDec(10)), + WithOpenNotional(sdk.NewDec(10)), ), ). Then( - perpaction.QueryMarkets(perpaction.QueryMarkets_MarketsShouldContain(*types.DefaultMarket(pair))), - perpaction.QueryModuleAccounts(perpaction.QueryModuleAccounts_ModulesBalanceShouldBe( + QueryMarkets(QueryMarkets_MarketsShouldContain(*types.DefaultMarket(pair))), + QueryModuleAccounts(QueryModuleAccounts_ModulesBalanceShouldBe( map[string]sdk.Coins{ "perp_ef": sdk.NewCoins( sdk.NewCoin(denoms.BTC, sdk.ZeroInt()), @@ -362,7 +360,7 @@ func TestQueryMarkets(t *testing.T) { ), } - tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() + 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 53a554281..fd4555cd0 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" - tutilaction "github.com/NibiruChain/nibiru/x/common/testutil/action" - epochsaction "github.com/NibiruChain/nibiru/x/epochs/integration/action" + . "github.com/NibiruChain/nibiru/x/common/testutil/action" + . "github.com/NibiruChain/nibiru/x/epochs/integration/action" epochtypes "github.com/NibiruChain/nibiru/x/epochs/types" - 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" + . "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" ) func TestAfterEpochEnd(t *testing.T) { pairBtcUsdc := asset.Registry.Pair(denoms.BTC, denoms.USDC) startTime := time.Now() - tc := tutilaction.TestCases{ - tutilaction.TC("index > mark"). + tc := TestCases{ + TC("index > mark"). Given( - perpaction.CreateCustomMarket(pairBtcUsdc), - tutilaction.SetBlockTime(startTime), - oracleaction.InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.MustNewDecFromStr("5.8")), - epochsaction.StartEpoch(epochtypes.ThirtyMinuteEpochID), + CreateCustomMarket(pairBtcUsdc), + SetBlockTime(startTime), + InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.MustNewDecFromStr("5.8")), + StartEpoch(epochtypes.ThirtyMinuteEpochID), ). When( - tutilaction.MoveToNextBlockWithDuration(30 * time.Minute), + MoveToNextBlockWithDuration(30 * time.Minute), ). Then( - perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_LatestCPFShouldBeEqualTo(sdk.MustNewDecFromStr("-0.099999999999999999"))), + MarketShouldBeEqual(pairBtcUsdc, Market_LatestCPFShouldBeEqualTo(sdk.MustNewDecFromStr("-0.099999999999999999"))), ), - tutilaction.TC("index < mark"). + TC("index < mark"). Given( - perpaction.CreateCustomMarket(pairBtcUsdc), - tutilaction.SetBlockTime(startTime), - oracleaction.InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.MustNewDecFromStr("0.52")), - epochsaction.StartEpoch(epochtypes.ThirtyMinuteEpochID), + CreateCustomMarket(pairBtcUsdc), + SetBlockTime(startTime), + InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.MustNewDecFromStr("0.52")), + StartEpoch(epochtypes.ThirtyMinuteEpochID), ). When( - tutilaction.MoveToNextBlockWithDuration(30 * time.Minute), + MoveToNextBlockWithDuration(30 * time.Minute), ). Then( - perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_LatestCPFShouldBeEqualTo(sdk.MustNewDecFromStr("0.01"))), + MarketShouldBeEqual(pairBtcUsdc, Market_LatestCPFShouldBeEqualTo(sdk.MustNewDecFromStr("0.01"))), ), - tutilaction.TC("index > mark - max funding rate"). + TC("index > mark - max funding rate"). Given( - 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), + CreateCustomMarket(pairBtcUsdc, WithMaxFundingRate(sdk.MustNewDecFromStr("0.001"))), + SetBlockTime(startTime), + InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.MustNewDecFromStr("5.8")), + StartEpoch(epochtypes.ThirtyMinuteEpochID), ). When( - tutilaction.MoveToNextBlockWithDuration(30 * time.Minute), + MoveToNextBlockWithDuration(30 * time.Minute), ). Then( - perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_LatestCPFShouldBeEqualTo(sdk.MustNewDecFromStr("-0.000120833333333333"))), + MarketShouldBeEqual(pairBtcUsdc, Market_LatestCPFShouldBeEqualTo(sdk.MustNewDecFromStr("-0.000120833333333333"))), ), - tutilaction.TC("index < mark - max funding rate"). + TC("index < mark - max funding rate"). Given( - 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), + CreateCustomMarket(pairBtcUsdc, WithMaxFundingRate(sdk.MustNewDecFromStr("0.001"))), + SetBlockTime(startTime), + InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.MustNewDecFromStr("0.52")), + StartEpoch(epochtypes.ThirtyMinuteEpochID), ). When( - tutilaction.MoveToNextBlockWithDuration(30 * time.Minute), + MoveToNextBlockWithDuration(30 * time.Minute), ). Then( - perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_LatestCPFShouldBeEqualTo(sdk.MustNewDecFromStr("0.000010833333333333"))), + MarketShouldBeEqual(pairBtcUsdc, Market_LatestCPFShouldBeEqualTo(sdk.MustNewDecFromStr("0.000010833333333333"))), ), - tutilaction.TC("index == mark"). + TC("index == mark"). Given( - perpaction.CreateCustomMarket(pairBtcUsdc), - tutilaction.SetBlockTime(startTime), - oracleaction.InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.OneDec()), - epochsaction.StartEpoch(epochtypes.ThirtyMinuteEpochID), + CreateCustomMarket(pairBtcUsdc), + SetBlockTime(startTime), + InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.OneDec()), + StartEpoch(epochtypes.ThirtyMinuteEpochID), ). When( - tutilaction.MoveToNextBlockWithDuration(30 * time.Minute), + MoveToNextBlockWithDuration(30 * time.Minute), ). Then( - perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_LatestCPFShouldBeEqualTo(sdk.ZeroDec())), + MarketShouldBeEqual(pairBtcUsdc, Market_LatestCPFShouldBeEqualTo(sdk.ZeroDec())), ), - tutilaction.TC("missing twap"). + TC("missing twap"). Given( - perpaction.CreateCustomMarket(pairBtcUsdc), - tutilaction.SetBlockTime(startTime), - epochsaction.StartEpoch(epochtypes.ThirtyMinuteEpochID), + CreateCustomMarket(pairBtcUsdc), + SetBlockTime(startTime), + StartEpoch(epochtypes.ThirtyMinuteEpochID), ). When( - tutilaction.MoveToNextBlockWithDuration(30 * time.Minute), + MoveToNextBlockWithDuration(30 * time.Minute), ). Then( - perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_LatestCPFShouldBeEqualTo(sdk.ZeroDec())), + MarketShouldBeEqual(pairBtcUsdc, Market_LatestCPFShouldBeEqualTo(sdk.ZeroDec())), ), - tutilaction.TC("0 price mark"). + TC("0 price mark"). Given( - perpaction.CreateCustomMarket(pairBtcUsdc), - tutilaction.SetBlockTime(startTime), - epochsaction.StartEpoch(epochtypes.ThirtyMinuteEpochID), - oracleaction.InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.ZeroDec()), + CreateCustomMarket(pairBtcUsdc), + SetBlockTime(startTime), + StartEpoch(epochtypes.ThirtyMinuteEpochID), + InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.ZeroDec()), ). When( - tutilaction.MoveToNextBlockWithDuration(30 * time.Minute), + MoveToNextBlockWithDuration(30 * time.Minute), ). Then( - perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_LatestCPFShouldBeEqualTo(sdk.ZeroDec())), + MarketShouldBeEqual(pairBtcUsdc, Market_LatestCPFShouldBeEqualTo(sdk.ZeroDec())), ), - tutilaction.TC("market not enabled"). + TC("market not enabled"). Given( - 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)), + CreateCustomMarket(pairBtcUsdc), + SetMarketEnabled(pairBtcUsdc, false), + SetBlockTime(startTime), + StartEpoch(epochtypes.ThirtyMinuteEpochID), + InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.NewDec(2)), ). When( - tutilaction.MoveToNextBlockWithDuration(30 * time.Minute), + MoveToNextBlockWithDuration(30 * time.Minute), ). Then( - perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_LatestCPFShouldBeEqualTo(sdk.ZeroDec())), + MarketShouldBeEqual(pairBtcUsdc, Market_LatestCPFShouldBeEqualTo(sdk.ZeroDec())), ), - tutilaction.TC("not correct epoch id"). + TC("not correct epoch id"). Given( - 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)), + CreateCustomMarket(pairBtcUsdc), + SetMarketEnabled(pairBtcUsdc, false), + SetBlockTime(startTime), + StartEpoch(epochtypes.FifteenMinuteEpochID), + InsertOraclePriceSnapshot(pairBtcUsdc, startTime.Add(15*time.Minute), sdk.NewDec(2)), ). When( - tutilaction.MoveToNextBlockWithDuration(30 * time.Minute), + MoveToNextBlockWithDuration(30 * time.Minute), ). Then( - perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_LatestCPFShouldBeEqualTo(sdk.ZeroDec())), + MarketShouldBeEqual(pairBtcUsdc, Market_LatestCPFShouldBeEqualTo(sdk.ZeroDec())), ), } - tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() + NewTestSuite(t).WithTestCases(tc...).Run() } diff --git a/x/perp/v2/keeper/liquidate_test.go b/x/perp/v2/keeper/liquidate_test.go index dde25da7c..d0a408d9f 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" - 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/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" "github.com/NibiruChain/nibiru/x/perp/v2/keeper" types "github.com/NibiruChain/nibiru/x/perp/v2/types" @@ -30,34 +30,27 @@ func TestMultiLiquidate(t *testing.T) { liquidator := testutil.AccAddress() startTime := time.Now() - tc := tutilaction.TestCases{ - tutilaction.TC("partial liquidation"). + tc := TestCases{ + TC("partial liquidation"). Given( - 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)), - ), + 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))), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MultiLiquidate(liquidator, false, - perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + MoveToNextBlock(), + MultiLiquidate(liquidator, false, + PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, ), ). Then( - 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( + 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( types.Position{ Pair: pairBtcUsdc, TraderAddress: alice.String(), @@ -71,178 +64,157 @@ func TestMultiLiquidate(t *testing.T) { ), ), - tutilaction.TC("full liquidation"). + TC("full liquidation"). Given( - 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)), - ), + 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))), ). When( - tutilaction.MoveToNextBlock(), - - perpaction.MultiLiquidate(liquidator, false, - perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, - perpaction.PairTraderTuple{Pair: pairAtomUsdc, Trader: alice, Successful: false}, + MoveToNextBlock(), + MultiLiquidate(liquidator, false, + PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + PairTraderTuple{Pair: pairAtomUsdc, Trader: alice, Successful: false}, ), ). Then( - 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), + 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), ), - tutilaction.TC("full liquidation"). + TC("full liquidation"). Given( - 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)), - ), + 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))), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MultiLiquidate(liquidator, false, - perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + MoveToNextBlock(), + MultiLiquidate(liquidator, false, + PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, ), ). Then( - 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), + 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), ), - tutilaction.TC("one fail liquidation - one correct"). + TC("one fail liquidation - one correct"). Given( - 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)), - ), + 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))), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MultiLiquidate(liquidator, false, - perpaction.PairTraderTuple{Pair: pairBtcUsdc, Successful: false}, - perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + MoveToNextBlock(), + MultiLiquidate(liquidator, false, + PairTraderTuple{Pair: pairBtcUsdc, Successful: false}, + PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, ), ). Then( - 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), + 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), ), - tutilaction.TC("one fail liquidation because market does not exists- one correct"). + TC("one fail liquidation because market does not exists- one correct"). Given( - 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)), - ), + 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))), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MultiLiquidate(liquidator, false, - perpaction.PairTraderTuple{Pair: asset.MustNewPair("luna:usdt"), Successful: false}, - perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + MoveToNextBlock(), + MultiLiquidate(liquidator, false, + PairTraderTuple{Pair: asset.MustNewPair("luna:usdt"), Successful: false}, + PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, ), ). Then( - 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), + 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), ), - tutilaction.TC("realizes bad debt"). + TC("realizes bad debt"). Given( - 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)), - ), + 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))), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MultiLiquidate(liquidator, false, - perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + MoveToNextBlock(), + MultiLiquidate(liquidator, false, + PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, ), ). Then( - 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), + 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), ), - tutilaction.TC("uses prepaid bad debt"). + TC("uses prepaid bad debt"). Given( - 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))), + 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))), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MultiLiquidate(liquidator, false, - perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + MoveToNextBlock(), + MultiLiquidate(liquidator, false, + PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, ), ). Then( - 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()), - ), + 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())), ), - tutilaction.TC("healthy position"). + TC("healthy position"). Given( - 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))), + 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))), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MultiLiquidate(liquidator, true, - perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: false}, + MoveToNextBlock(), + MultiLiquidate(liquidator, true, + PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: false}, ), ). Then( - 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( + 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( types.Position{ Pair: pairBtcUsdc, TraderAddress: alice.String(), @@ -254,7 +226,7 @@ func TestMultiLiquidate(t *testing.T) { }, ), ), - perpassertion.ContainsLiquidateEvent(&types.LiquidationFailedEvent{ + ContainsLiquidateEvent(&types.LiquidationFailedEvent{ Pair: pairBtcUsdc, Trader: alice.String(), Liquidator: liquidator.String(), @@ -262,34 +234,34 @@ func TestMultiLiquidate(t *testing.T) { }), ), - tutilaction.TC("mixed bag"). + TC("mixed bag"). Given( - 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))), + 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))), ). When( - 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 + 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 ), ). Then( - 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( + 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( types.Position{ Pair: pairBtcUsdc, TraderAddress: alice.String(), @@ -301,9 +273,9 @@ func TestMultiLiquidate(t *testing.T) { }, ), ), - perpassertion.PositionShouldNotExist(alice, pairEthUsdc), - perpassertion.PositionShouldBeEqual(alice, pairAtomUsdc, - perpassertion.Position_PositionShouldBeEqualTo( + PositionShouldNotExist(alice, pairEthUsdc), + PositionShouldBeEqual(alice, pairAtomUsdc, + Position_PositionShouldBeEqualTo( types.Position{ Pair: pairAtomUsdc, TraderAddress: alice.String(), @@ -316,19 +288,19 @@ func TestMultiLiquidate(t *testing.T) { ), ), - perpassertion.ContainsLiquidateEvent(&types.LiquidationFailedEvent{ + ContainsLiquidateEvent(&types.LiquidationFailedEvent{ Pair: pairAtomUsdc, Trader: alice.String(), Liquidator: liquidator.String(), Reason: types.LiquidationFailedEvent_POSITION_HEALTHY, }), - perpassertion.ContainsLiquidateEvent(&types.LiquidationFailedEvent{ + ContainsLiquidateEvent(&types.LiquidationFailedEvent{ Pair: pairSolUsdc, Trader: alice.String(), Liquidator: liquidator.String(), Reason: types.LiquidationFailedEvent_NONEXISTENT_PAIR, }), - perpassertion.ContainsLiquidateEvent(&types.LiquidationFailedEvent{ + ContainsLiquidateEvent(&types.LiquidationFailedEvent{ Pair: pairBtcUsdc, Trader: bob.String(), Liquidator: liquidator.String(), @@ -337,7 +309,7 @@ func TestMultiLiquidate(t *testing.T) { ), } - tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() + 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 7c2714412..b996a02d9 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" - 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/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" 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 := tutilaction.TestCases{ - tutilaction.TC("existing long position, add margin"). + tc := TestCases{ + TC("existing long position, add margin"). Given( - 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()), + 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()), ). When( - tutilaction.MoveToNextBlock(), - perpaction.AddMargin(alice, pairBtcUsdc, sdk.NewInt(1000)), + MoveToNextBlock(), + AddMargin(alice, pairBtcUsdc, sdk.NewInt(1000)), ). Then( - perpassert.PositionShouldBeEqual(alice, pairBtcUsdc, perpassert.Position_PositionShouldBeEqualTo( + PositionShouldBeEqual(alice, pairBtcUsdc, Position_PositionShouldBeEqualTo( types.Position{ Pair: pairBtcUsdc, TraderAddress: alice.String(), @@ -50,7 +50,7 @@ func TestAddMargin(t *testing.T) { LastUpdatedBlockNumber: 2, }, )), - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + 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"), }), - 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)), + BalanceEqual(alice, denoms.USDC, sdk.ZeroInt()), + ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(10)), + ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.NewInt(10)), ), - tutilaction.TC("existing short position, add margin"). + TC("existing short position, add margin"). Given( - 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()), + 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()), ). When( - tutilaction.MoveToNextBlock(), - perpaction.AddMargin(alice, pairBtcUsdc, sdk.NewInt(1000)), + MoveToNextBlock(), + AddMargin(alice, pairBtcUsdc, sdk.NewInt(1000)), ). Then( - perpassert.PositionShouldBeEqual(alice, pairBtcUsdc, perpassert.Position_PositionShouldBeEqualTo( + PositionShouldBeEqual(alice, pairBtcUsdc, Position_PositionShouldBeEqualTo( types.Position{ Pair: pairBtcUsdc, TraderAddress: alice.String(), @@ -100,7 +100,7 @@ func TestAddMargin(t *testing.T) { LastUpdatedBlockNumber: 2, }, )), - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + 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"), }), - 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)), + BalanceEqual(alice, denoms.USDC, sdk.ZeroInt()), + ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(10)), + ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.NewInt(10)), ), - tutilaction.TC("Testing fails"). + TC("Testing fails"). Given( - perpaction.CreateCustomMarket(pairBtcUsdc), - perpaction.CreateCustomMarket(pairEthUsdc), + CreateCustomMarket(pairBtcUsdc), + CreateCustomMarket(pairEthUsdc), - 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()), + 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()), ). When( - 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), + 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), - 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), + 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), ), } - tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() + 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 := tutilaction.TestCases{ - tutilaction.TC("existing long position, remove margin"). + tc := TestCases{ + TC("existing long position, remove margin"). Given( - 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()), + 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()), ). When( - tutilaction.MoveToNextBlock(), - perpaction.RemoveMargin(alice, pairBtcUsdc, sdk.NewInt(500)), + MoveToNextBlock(), + RemoveMargin(alice, pairBtcUsdc, sdk.NewInt(500)), ). Then( - perpassert.PositionShouldBeEqual(alice, pairBtcUsdc, perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pairBtcUsdc, 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, })), - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + 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"), }), - tutilassert.BalanceEqual(alice, denoms.USDC, sdk.NewInt(500)), - tutilassert.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.OneInt()), - tutilassert.ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.OneInt()), + BalanceEqual(alice, denoms.USDC, sdk.NewInt(500)), + ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.OneInt()), + ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.OneInt()), ), - tutilaction.TC("existing long position, remove almost all margin fails"). + TC("existing long position, remove almost all margin fails"). Given( - 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(), + 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(), ). When( - perpaction.RemoveMarginFail(alice, pairBtcUsdc, sdk.NewInt(999), types.ErrMarginRatioTooLow), + RemoveMarginFail(alice, pairBtcUsdc, sdk.NewInt(999), types.ErrMarginRatioTooLow), ). Then( - perpassert.PositionShouldBeEqual(alice, pairBtcUsdc, perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pairBtcUsdc, 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, })), - tutilassert.BalanceEqual(alice, denoms.USDC, sdk.ZeroInt()), - tutilassert.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.OneInt()), - tutilassert.ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.OneInt()), + BalanceEqual(alice, denoms.USDC, sdk.ZeroInt()), + ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.OneInt()), + ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.OneInt()), ), - tutilaction.TC("existing short position, remove margin"). + TC("existing short position, remove margin"). Given( - 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()), + 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()), ). When( - tutilaction.MoveToNextBlock(), - perpaction.RemoveMargin(alice, pairBtcUsdc, sdk.NewInt(500)), + MoveToNextBlock(), + RemoveMargin(alice, pairBtcUsdc, sdk.NewInt(500)), ). Then( - perpassert.PositionShouldBeEqual(alice, pairBtcUsdc, perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pairBtcUsdc, 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, })), - perpassert.PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ + 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"), }), - tutilassert.BalanceEqual(alice, denoms.USDC, sdk.NewInt(500)), - tutilassert.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.OneInt()), - tutilassert.ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.OneInt()), + BalanceEqual(alice, denoms.USDC, sdk.NewInt(500)), + ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.OneInt()), + ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.OneInt()), ), - tutilaction.TC("existing short position, remove almost all margin fails"). + TC("existing short position, remove almost all margin fails"). Given( - 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(), + 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(), ). When( - perpaction.RemoveMarginFail(alice, pairBtcUsdc, sdk.NewInt(999), types.ErrMarginRatioTooLow), + RemoveMarginFail(alice, pairBtcUsdc, sdk.NewInt(999), types.ErrMarginRatioTooLow), ). Then( - perpassert.PositionShouldBeEqual(alice, pairBtcUsdc, perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pairBtcUsdc, 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, })), - tutilassert.BalanceEqual(alice, denoms.USDC, sdk.ZeroInt()), - tutilassert.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.OneInt()), - tutilassert.ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.OneInt()), + BalanceEqual(alice, denoms.USDC, sdk.ZeroInt()), + ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.OneInt()), + ModuleBalanceEqual(types.FeePoolModuleAccount, denoms.USDC, sdk.OneInt()), ), } - tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() + 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 cd9ec1ccf..4ec0e9bec 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" - 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/action" + . "github.com/NibiruChain/nibiru/x/common/testutil/assertion" "github.com/NibiruChain/nibiru/x/common/testutil/testapp" - 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/integration/action" + . "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 := tutilaction.TestCases{ - tutilaction.TC("open long position"). + tests := TestCases{ + TC("open long position"). Given( - perpaction.CreateCustomMarket(pair), - tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), + CreateCustomMarket(pair), + FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), ). When( - perpaction.MsgServerMarketOrder(alice, pair, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroInt()), + MsgServerMarketOrder(alice, pair, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroInt()), ). Then( - perpassert.PositionShouldBeEqual(alice, pair, - perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pair, + 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, }), ), - tutilassert.BalanceEqual(alice, denoms.NUSD, sdk.NewInt(99)), + BalanceEqual(alice, denoms.NUSD, sdk.NewInt(99)), ), - tutilaction.TC("open short position"). + TC("open short position"). Given( - perpaction.CreateCustomMarket(pair), - tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), + CreateCustomMarket(pair), + FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), ). When( - perpaction.MsgServerMarketOrder(alice, pair, types.Direction_SHORT, sdk.OneInt(), sdk.OneDec(), sdk.ZeroInt()), + MsgServerMarketOrder(alice, pair, types.Direction_SHORT, sdk.OneInt(), sdk.OneDec(), sdk.ZeroInt()), ). Then( - perpassert.PositionShouldBeEqual(alice, pair, - perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pair, + 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, }), ), - tutilassert.BalanceEqual(alice, denoms.NUSD, sdk.NewInt(99)), + BalanceEqual(alice, denoms.NUSD, sdk.NewInt(99)), ), } - tutilaction.NewTestSuite(t).WithTestCases(tests...).Run() + NewTestSuite(t).WithTestCases(tests...).Run() } func TestMsgServerClosePosition(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) alice := testutil.AccAddress() - tests := tutilaction.TestCases{ - tutilaction.TC("close long position"). + tests := TestCases{ + TC("close long position"). Given( - 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(), + CreateCustomMarket(pair), + FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), + MarketOrder(alice, pair, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec()), + MoveToNextBlock(), ). When( - perpaction.MsgServerClosePosition(alice, pair), + MsgServerClosePosition(alice, pair), ). Then( - perpassert.PositionShouldNotExist(alice, pair), - tutilassert.BalanceEqual(alice, denoms.NUSD, sdk.NewInt(100)), + PositionShouldNotExist(alice, pair), + BalanceEqual(alice, denoms.NUSD, sdk.NewInt(100)), ), - tutilaction.TC("close short position"). + TC("close short position"). Given( - 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(), + CreateCustomMarket(pair), + FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), + MarketOrder(alice, pair, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec()), + MoveToNextBlock(), ). When( - perpaction.MsgServerClosePosition(alice, pair), + MsgServerClosePosition(alice, pair), ). Then( - perpassert.PositionShouldNotExist(alice, pair), - tutilassert.BalanceEqual(alice, denoms.NUSD, sdk.NewInt(100)), + PositionShouldNotExist(alice, pair), + BalanceEqual(alice, denoms.NUSD, sdk.NewInt(100)), ), } - tutilaction.NewTestSuite(t).WithTestCases(tests...).Run() + NewTestSuite(t).WithTestCases(tests...).Run() } func TestMsgServerAddMargin(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) alice := testutil.AccAddress() - tests := tutilaction.TestCases{ - tutilaction.TC("add margin"). + tests := TestCases{ + TC("add margin"). Given( - 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(), + CreateCustomMarket(pair), + FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), + MarketOrder(alice, pair, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec()), + MoveToNextBlock(), ). When( - perpaction.MsgServerAddMargin(alice, pair, sdk.OneInt()), + MsgServerAddMargin(alice, pair, sdk.OneInt()), ). Then( - perpassert.PositionShouldBeEqual(alice, pair, - perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pair, + 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, }), ), - tutilassert.BalanceEqual(alice, denoms.NUSD, sdk.NewInt(98)), + BalanceEqual(alice, denoms.NUSD, sdk.NewInt(98)), ), - tutilaction.TC("partial close"). + TC("partial close"). Given( - 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(), + CreateCustomMarket(pair), + FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), + MarketOrder(alice, pair, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec()), + MoveToNextBlock(), ). When( - perpaction.MsgServerAddMargin(alice, pair, sdk.OneInt()), + MsgServerAddMargin(alice, pair, sdk.OneInt()), ). Then( - perpassert.PositionShouldBeEqual(alice, pair, - perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pair, + 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, }), ), - tutilassert.BalanceEqual(alice, denoms.NUSD, sdk.NewInt(98)), + BalanceEqual(alice, denoms.NUSD, sdk.NewInt(98)), ), } - tutilaction.NewTestSuite(t).WithTestCases(tests...).Run() + NewTestSuite(t).WithTestCases(tests...).Run() } func TestMsgServerRemoveMargin(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) alice := testutil.AccAddress() - tests := tutilaction.TestCases{ - tutilaction.TC("add margin"). + tests := TestCases{ + TC("add margin"). Given( - 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(), + 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(), ). When( - perpaction.MsgServerRemoveMargin(alice, pair, sdk.OneInt()), + MsgServerRemoveMargin(alice, pair, sdk.OneInt()), ). Then( - perpassert.PositionShouldBeEqual(alice, pair, - perpassert.Position_PositionShouldBeEqualTo(types.Position{ + PositionShouldBeEqual(alice, pair, + 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, }), ), - tutilassert.BalanceEqual(alice, denoms.NUSD, sdk.NewInt(99)), + BalanceEqual(alice, denoms.NUSD, sdk.NewInt(99)), ), } - tutilaction.NewTestSuite(t).WithTestCases(tests...).Run() + NewTestSuite(t).WithTestCases(tests...).Run() } func TestMsgServerDonateToPerpEf(t *testing.T) { alice := testutil.AccAddress() - tests := tutilaction.TestCases{ - tutilaction.TC("success"). + tests := TestCases{ + TC("success"). Given( - tutilaction.FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), + FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(denoms.NUSD, 100))), ). When( - perpaction.MsgServerDonateToPerpEf(alice, sdk.NewInt(50)), + MsgServerDonateToPerpEf(alice, sdk.NewInt(50)), ). Then( - tutilassert.BalanceEqual(alice, denoms.NUSD, sdk.NewInt(50)), - tutilassert.ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(50)), + BalanceEqual(alice, denoms.NUSD, sdk.NewInt(50)), + ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.NUSD, sdk.NewInt(50)), ), } - tutilaction.NewTestSuite(t).WithTestCases(tests...).Run() + 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 := tutilaction.TestCases{ - tutilaction.TC("partial liquidation"). + tests := TestCases{ + TC("partial liquidation"). Given( - 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))), + 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))), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MsgServerMultiLiquidate(liquidator, false, - perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + MoveToNextBlock(), + MsgServerMultiLiquidate(liquidator, false, + PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, ), ). Then( - 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( + 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( types.Position{ Pair: pairBtcUsdc, TraderAddress: alice.String(), @@ -265,29 +265,29 @@ func TestMsgServerMultiLiquidate(t *testing.T) { ), ), - tutilaction.TC("full liquidation"). + TC("full liquidation"). Given( - 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))), + 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))), ). When( - tutilaction.MoveToNextBlock(), - perpaction.MsgServerMultiLiquidate(liquidator, false, - perpaction.PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, + MoveToNextBlock(), + MsgServerMultiLiquidate(liquidator, false, + PairTraderTuple{Pair: pairBtcUsdc, Trader: alice, Successful: true}, ), ). Then( - 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), + 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), ), } - tutilaction.NewTestSuite(t).WithTestCases(tests...).Run() + 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 cf5d94575..608af5580 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" - tutilaction "github.com/NibiruChain/nibiru/x/common/testutil/action" + . "github.com/NibiruChain/nibiru/x/common/testutil/action" "github.com/NibiruChain/nibiru/x/common/testutil/mock" "github.com/NibiruChain/nibiru/x/common/testutil/testapp" - 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/integration/action" + . "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 := tutilaction.TestCases{ - tutilaction.TC("spot twap"). + tc := TestCases{ + TC("spot twap"). Given( - 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))), + 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))), ). When( - tutilaction.MoveToNextBlockWithDuration(30 * time.Second), + MoveToNextBlockWithDuration(30 * time.Second), ). Then( - perpassert.TwapShouldBe(pairBtcUsdc, types.TwapCalcOption_SPOT, types.Direction_DIRECTION_UNSPECIFIED, sdk.ZeroDec(), 30*time.Second, sdk.NewDec(10)), + TwapShouldBe(pairBtcUsdc, types.TwapCalcOption_SPOT, types.Direction_DIRECTION_UNSPECIFIED, sdk.ZeroDec(), 30*time.Second, sdk.NewDec(10)), ), - tutilaction.TC("base asset twap, long"). + TC("base asset twap, long"). Given( - 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))), + 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))), ). When( - tutilaction.MoveToNextBlockWithDuration(30 * time.Second), + MoveToNextBlockWithDuration(30 * time.Second), ). Then( - perpassert.TwapShouldBe(pairBtcUsdc, types.TwapCalcOption_BASE_ASSET_SWAP, types.Direction_LONG, sdk.NewDec(5), 30*time.Second, sdk.MustNewDecFromStr("50.000000000250000000")), + TwapShouldBe(pairBtcUsdc, types.TwapCalcOption_BASE_ASSET_SWAP, types.Direction_LONG, sdk.NewDec(5), 30*time.Second, sdk.MustNewDecFromStr("50.000000000250000000")), ), - tutilaction.TC("base asset twap, short"). + TC("base asset twap, short"). Given( - 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))), + 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))), ). When( - tutilaction.MoveToNextBlockWithDuration(30 * time.Second), + MoveToNextBlockWithDuration(30 * time.Second), ). Then( - perpassert.TwapShouldBe(pairBtcUsdc, types.TwapCalcOption_BASE_ASSET_SWAP, types.Direction_SHORT, sdk.NewDec(5), 30*time.Second, sdk.MustNewDecFromStr("49.999999999750000000")), + TwapShouldBe(pairBtcUsdc, types.TwapCalcOption_BASE_ASSET_SWAP, types.Direction_SHORT, sdk.NewDec(5), 30*time.Second, sdk.MustNewDecFromStr("49.999999999750000000")), ), - tutilaction.TC("quote asset twap, long"). + TC("quote asset twap, long"). Given( - 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))), + 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))), ). When( - tutilaction.MoveToNextBlockWithDuration(30 * time.Second), + MoveToNextBlockWithDuration(30 * time.Second), ). Then( - perpassert.TwapShouldBe(pairBtcUsdc, types.TwapCalcOption_QUOTE_ASSET_SWAP, types.Direction_LONG, sdk.NewDec(5), 30*time.Second, sdk.MustNewDecFromStr("0.503367003366748282")), + TwapShouldBe(pairBtcUsdc, types.TwapCalcOption_QUOTE_ASSET_SWAP, types.Direction_LONG, sdk.NewDec(5), 30*time.Second, sdk.MustNewDecFromStr("0.503367003366748282")), ), - tutilaction.TC("quote asset twap, short"). + TC("quote asset twap, short"). Given( - 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))), + 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))), ). When( - tutilaction.MoveToNextBlockWithDuration(30 * time.Second), + MoveToNextBlockWithDuration(30 * time.Second), ). Then( - perpassert.TwapShouldBe(pairBtcUsdc, types.TwapCalcOption_QUOTE_ASSET_SWAP, types.Direction_SHORT, sdk.NewDec(5), 30*time.Second, sdk.MustNewDecFromStr("0.503367003367258451")), + TwapShouldBe(pairBtcUsdc, types.TwapCalcOption_QUOTE_ASSET_SWAP, types.Direction_SHORT, sdk.NewDec(5), 30*time.Second, sdk.MustNewDecFromStr("0.503367003367258451")), ), } - tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() + 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 bc17860b2..5f0d60a7f 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" - 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/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" "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 := tutilaction.TestCases{ - tutilaction.TC("successful withdraw, no bad debt"). + tc := TestCases{ + TC("successful withdraw, no bad debt"). Given( - tutilaction.SetBlockNumber(1), - tutilaction.SetBlockTime(startBlockTime), - perpaction.CreateCustomMarket(pairBtcUsdc), - tutilaction.FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1000)))), + SetBlockNumber(1), + SetBlockTime(startBlockTime), + CreateCustomMarket(pairBtcUsdc), + FundModule(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1000)))), ). When( - perpaction.WithdrawFromVault(pairBtcUsdc, alice, sdk.NewInt(1000)), + WithdrawFromVault(pairBtcUsdc, alice, sdk.NewInt(1000)), ). Then( - tutilassert.BalanceEqual(alice, denoms.USDC, sdk.NewInt(1000)), - tutilassert.ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.ZeroInt()), - perpassert.MarketShouldBeEqual(pairBtcUsdc, perpassert.Market_PrepaidBadDebtShouldBeEqualTo(sdk.ZeroInt())), + BalanceEqual(alice, denoms.USDC, sdk.NewInt(1000)), + ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.ZeroInt()), + MarketShouldBeEqual(pairBtcUsdc, Market_PrepaidBadDebtShouldBeEqualTo(sdk.ZeroInt())), ), - tutilaction.TC("successful withdraw, some bad debt"). + TC("successful withdraw, some bad debt"). Given( - 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)))), + 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)))), ). When( - perpaction.WithdrawFromVault(pairBtcUsdc, alice, sdk.NewInt(1000)), + WithdrawFromVault(pairBtcUsdc, alice, sdk.NewInt(1000)), ). Then( - 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))), + 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))), ), - tutilaction.TC("successful withdraw, all bad debt"). + TC("successful withdraw, all bad debt"). Given( - tutilaction.SetBlockNumber(1), - tutilaction.SetBlockTime(startBlockTime), - perpaction.CreateCustomMarket(pairBtcUsdc), - tutilaction.FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1000)))), + SetBlockNumber(1), + SetBlockTime(startBlockTime), + CreateCustomMarket(pairBtcUsdc), + FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1000)))), ). When( - perpaction.WithdrawFromVault(pairBtcUsdc, alice, sdk.NewInt(1000)), + WithdrawFromVault(pairBtcUsdc, alice, sdk.NewInt(1000)), ). Then( - 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))), + 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))), ), - tutilaction.TC("successful withdraw, existing bad debt"). + TC("successful withdraw, existing bad debt"). Given( - 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)))), + SetBlockNumber(1), + SetBlockTime(startBlockTime), + CreateCustomMarket(pairBtcUsdc, WithPrepaidBadDebt(sdk.NewInt(1000))), + FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.USDC, sdk.NewInt(1000)))), ). When( - perpaction.WithdrawFromVault(pairBtcUsdc, alice, sdk.NewInt(1000)), + WithdrawFromVault(pairBtcUsdc, alice, sdk.NewInt(1000)), ). Then( - 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))), + 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))), ), } - tutilaction.NewTestSuite(t).WithTestCases(tc...).Run() + NewTestSuite(t).WithTestCases(tc...).Run() } From d4e9bb80c430fa44046fd4445b50dcb0e2eee8fb Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Tue, 5 Sep 2023 10:54:19 -0500 Subject: [PATCH 10/23] change log --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 22b971361..9850a951d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,7 +59,6 @@ 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 @@ -79,6 +78,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * [#1459](https://github.com/NibiruChain/nibiru/pull/1459) - fix(spot): wire `x/spot` msgService into app router * [#1467](https://github.com/NibiruChain/nibiru/pull/1467) - fix(oracle): make `calcTwap` safer * [#1464](https://github.com/NibiruChain/nibiru/pull/1464) - fix(gov): wire legacy proposal handlers +* [#1565](https://github.com/NibiruChain/nibiru/pull/1565) - fix(oracle)!: Count vote omission as abstain for less slashing + more stability ### State Machine Breaking @@ -640,4 +640,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Testing * [#695](https://github.com/NibiruChain/nibiru/pull/695) Add `OpenPosition` integration tests. -* [#692](https://github.com/NibiruChain/nibiru/pull/692) Add test coverage for Perp MsgServer methods. \ No newline at end of file +* [#692](https://github.com/NibiruChain/nibiru/pull/692) Add test coverage for Perp MsgServer methods. From 70001e90c971d733693dba14b54cb825760514ef Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Tue, 5 Sep 2023 18:22:10 -0500 Subject: [PATCH 11/23] test(oracle/types): more unit tests --- x/oracle/types/ballot.go | 4 ++-- x/oracle/types/ballot_test.go | 28 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/x/oracle/types/ballot.go b/x/oracle/types/ballot.go index 9fa5098b7..ea93a0a6c 100644 --- a/x/oracle/types/ballot.go +++ b/x/oracle/types/ballot.go @@ -210,11 +210,11 @@ func (vp ValidatorPerformances) GetTotalRewardWeight() int64 { } func (vp ValidatorPerformances) String() string { - jsonBz, _ := json.Marshal(vp) + jsonBz, _ := json.MarshalIndent(vp, "", " ") return string(jsonBz) } func (vp ValidatorPerformance) String() string { - jsonBz, _ := json.Marshal(vp) + jsonBz, _ := json.MarshalIndent(vp, "", " ") return string(jsonBz) } diff --git a/x/oracle/types/ballot_test.go b/x/oracle/types/ballot_test.go index df05fd677..4a79bd898 100644 --- a/x/oracle/types/ballot_test.go +++ b/x/oracle/types/ballot_test.go @@ -20,7 +20,7 @@ import ( "github.com/NibiruChain/nibiru/x/oracle/types" ) -func TestToMap(t *testing.T) { +func TestExchangeRateBallots_ToMap(t *testing.T) { tests := struct { votes []types.ExchangeRateBallot isValid []bool @@ -59,6 +59,9 @@ func TestToMap(t *testing.T) { require.False(t, ok) } } + require.NotPanics(t, func() { + types.ExchangeRateBallots(tests.votes).NumValidVoters() + }) } func TestToCrossRate(t *testing.T) { @@ -334,3 +337,26 @@ func TestNewClaim(t *testing.T) { ValAddress: addr, }, claim) } + +func TestValidatorPerformances(t *testing.T) { + power := int64(42) + valNames := []string{"val0", "val1", "val2", "val3"} + perfList := []types.ValidatorPerformance{ + types.NewValidatorPerformance(power, sdk.ValAddress([]byte(valNames[0]))), + types.NewValidatorPerformance(power, sdk.ValAddress([]byte(valNames[1]))), + types.NewValidatorPerformance(power, sdk.ValAddress([]byte(valNames[2]))), + types.NewValidatorPerformance(power, sdk.ValAddress([]byte(valNames[3]))), + } + perfs := make(types.ValidatorPerformances) + for idx, perf := range perfList { + perfs[valNames[idx]] = perf + } + + require.NotPanics(t, func() { + out := perfs.String() + require.NotEmpty(t, out) + + out = perfs[valNames[0]].String() + require.NotEmpty(t, out) + }) +} From a0cf6567649d8a37064f040b9e1f4fbffc1f465b Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Wed, 6 Sep 2023 13:16:41 -0500 Subject: [PATCH 12/23] refactor: use types. instead of New for names args --- error.txt | 39 ++++++++++++++++++++++++++++++++++ x/oracle/keeper/ballot_test.go | 14 ++++++------ 2 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 error.txt diff --git a/error.txt b/error.txt new file mode 100644 index 000000000..b3c99cab1 --- /dev/null +++ b/error.txt @@ -0,0 +1,39 @@ +CGO_ENABLED=1 +CGO_CFLAGS="-I/home/realu/warpath/NibiruChain/nibiru/temp/include" +CGO_LDFLAGS="-L/home/realu/warpath/NibiruChain/nibiru/temp/lib -lrocksdb +-lstdc++ -lm -ldl -static -lwasmvm_muslc" go build -mod=readonly -tags "netgo +osusergo rocksdb grocksdb_no_link static_wasm muslc ledger" -ldflags '-X +github.com/cosmos/cosmos-sdk/version.Name=nibiru -X +github.com/cosmos/cosmos-sdk/version.AppName=nibid -X +github.com/cosmos/cosmos-sdk/version.Version=v0.22.0-rc.1 -X +github.com/cosmos/cosmos-sdk/version.Commit=f741d7f13a56a049d0010aa938841e3f7e56f042 +-X +"github.com/cosmos/cosmos-sdk/version.BuildTags=netgo,osusergo,rocksdb,grocksdb_no_link,static_wasm,muslc,ledger" +-X github.com/tendermint/tendermint/version.TMCoreSemVer=v0.37.2 -X +github.com/cosmos/cosmos-sdk/types.DBBackend=rocksdb -linkmode=external -w -s' +-o /home/realu/warpath/NibiruChain/nibiru/build/ ./... +# github.com/NibiruChain/nibiru/cmd/nibid + +/usr/bin/ld: +/home/realu/warpath/NibiruChain/nibiru/temp/lib/librocksdb.a(env_posix.o): in +function `rocksdb::(anonymous +namespace)::PosixEnv::LoadLibrary(std::__cxx11::basic_string, std::allocator > const&, +std::__cxx11::basic_string, std::allocator > +const&, std::shared_ptr*)': +/root/rocksdb/env/env_posix.cc:251: warning: Using 'dlopen' in statically +linked applications requires at runtime the shared libraries from the glibc +version used for linking +/usr/bin/ld: +/home/realu/warpath/NibiruChain/nibiru/temp/lib/libwasmvm_muslc.a(std-df986adbef8b2eb0.std.3c2b20ec-cgu.0.rcgu.o): +in function `std::sys::unix::os::home_dir::fallback': +/rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/sys/unix/os.rs:626: +warning: Using 'getpwuid_r' in statically linked applications requires at +runtime the shared libraries from the glibc version used for linking +/usr/bin/ld: +/home/realu/warpath/NibiruChain/nibiru/temp/lib/libwasmvm_muslc.a(std-df986adbef8b2eb0.std.3c2b20ec-cgu.0.rcgu.o): +in function `>::try_from::{{closure}}': +/rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/sys_common/net.rs:206: +warning: Using 'getaddrinfo' in statically linked applications requires at +runtime the shared libraries from the glibc version used for linking diff --git a/x/oracle/keeper/ballot_test.go b/x/oracle/keeper/ballot_test.go index 92261d9b2..24031da28 100644 --- a/x/oracle/keeper/ballot_test.go +++ b/x/oracle/keeper/ballot_test.go @@ -37,15 +37,17 @@ func TestGroupBallotsByPair(t *testing.T) { require.NoError(t, err) staking.EndBlocker(fixture.Ctx, &fixture.StakingKeeper) + pairBtc := asset.Registry.Pair(denoms.BTC, denoms.NUSD) + pairEth := asset.Registry.Pair(denoms.ETH, denoms.NUSD) btcBallots := types.ExchangeRateBallots{ - types.NewExchangeRateBallot(sdk.NewDec(17), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[0], power), - types.NewExchangeRateBallot(sdk.NewDec(10), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[1], power), - types.NewExchangeRateBallot(sdk.NewDec(6), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[2], power), + {Pair: pairBtc, ExchangeRate: sdk.NewDec(17), Voter: ValAddrs[0], Power: power}, + {Pair: pairBtc, ExchangeRate: sdk.NewDec(10), Voter: ValAddrs[1], Power: power}, + {Pair: pairBtc, ExchangeRate: sdk.NewDec(6), Voter: ValAddrs[2], Power: power}, } ethBallots := types.ExchangeRateBallots{ - types.NewExchangeRateBallot(sdk.NewDec(1000), asset.Registry.Pair(denoms.ETH, denoms.NUSD), ValAddrs[0], power), - types.NewExchangeRateBallot(sdk.NewDec(1300), asset.Registry.Pair(denoms.ETH, denoms.NUSD), ValAddrs[1], power), - types.NewExchangeRateBallot(sdk.NewDec(2000), asset.Registry.Pair(denoms.ETH, denoms.NUSD), ValAddrs[2], power), + {Pair: pairEth, ExchangeRate: sdk.NewDec(1_000), Voter: ValAddrs[0], Power: power}, + {Pair: pairEth, ExchangeRate: sdk.NewDec(1_300), Voter: ValAddrs[1], Power: power}, + {Pair: pairEth, ExchangeRate: sdk.NewDec(2_000), Voter: ValAddrs[2], Power: power}, } for i, ballot := range btcBallots { From 0a9be17c37abfdb06fc56afca6f42e4c058377d4 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Wed, 6 Sep 2023 13:53:27 -0500 Subject: [PATCH 13/23] rm error.txt --- error.txt | 39 --------------------------------------- 1 file changed, 39 deletions(-) delete mode 100644 error.txt diff --git a/error.txt b/error.txt deleted file mode 100644 index b3c99cab1..000000000 --- a/error.txt +++ /dev/null @@ -1,39 +0,0 @@ -CGO_ENABLED=1 -CGO_CFLAGS="-I/home/realu/warpath/NibiruChain/nibiru/temp/include" -CGO_LDFLAGS="-L/home/realu/warpath/NibiruChain/nibiru/temp/lib -lrocksdb --lstdc++ -lm -ldl -static -lwasmvm_muslc" go build -mod=readonly -tags "netgo -osusergo rocksdb grocksdb_no_link static_wasm muslc ledger" -ldflags '-X -github.com/cosmos/cosmos-sdk/version.Name=nibiru -X -github.com/cosmos/cosmos-sdk/version.AppName=nibid -X -github.com/cosmos/cosmos-sdk/version.Version=v0.22.0-rc.1 -X -github.com/cosmos/cosmos-sdk/version.Commit=f741d7f13a56a049d0010aa938841e3f7e56f042 --X -"github.com/cosmos/cosmos-sdk/version.BuildTags=netgo,osusergo,rocksdb,grocksdb_no_link,static_wasm,muslc,ledger" --X github.com/tendermint/tendermint/version.TMCoreSemVer=v0.37.2 -X -github.com/cosmos/cosmos-sdk/types.DBBackend=rocksdb -linkmode=external -w -s' --o /home/realu/warpath/NibiruChain/nibiru/build/ ./... -# github.com/NibiruChain/nibiru/cmd/nibid - -/usr/bin/ld: -/home/realu/warpath/NibiruChain/nibiru/temp/lib/librocksdb.a(env_posix.o): in -function `rocksdb::(anonymous -namespace)::PosixEnv::LoadLibrary(std::__cxx11::basic_string, std::allocator > const&, -std::__cxx11::basic_string, std::allocator > -const&, std::shared_ptr*)': -/root/rocksdb/env/env_posix.cc:251: warning: Using 'dlopen' in statically -linked applications requires at runtime the shared libraries from the glibc -version used for linking -/usr/bin/ld: -/home/realu/warpath/NibiruChain/nibiru/temp/lib/libwasmvm_muslc.a(std-df986adbef8b2eb0.std.3c2b20ec-cgu.0.rcgu.o): -in function `std::sys::unix::os::home_dir::fallback': -/rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/sys/unix/os.rs:626: -warning: Using 'getpwuid_r' in statically linked applications requires at -runtime the shared libraries from the glibc version used for linking -/usr/bin/ld: -/home/realu/warpath/NibiruChain/nibiru/temp/lib/libwasmvm_muslc.a(std-df986adbef8b2eb0.std.3c2b20ec-cgu.0.rcgu.o): -in function `>::try_from::{{closure}}': -/rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/sys_common/net.rs:206: -warning: Using 'getaddrinfo' in statically linked applications requires at -runtime the shared libraries from the glibc version used for linking From fad86ca39065df3b9616ddcb4bbb6caa2bc25ced Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Wed, 6 Sep 2023 17:41:16 -0500 Subject: [PATCH 14/23] del zombie code --- x/oracle/keeper/slash_test.go | 27 +-------------------------- x/oracle/types/ballot_test.go | 2 +- 2 files changed, 2 insertions(+), 27 deletions(-) diff --git a/x/oracle/keeper/slash_test.go b/x/oracle/keeper/slash_test.go index 7c4d6c127..fb55687c1 100644 --- a/x/oracle/keeper/slash_test.go +++ b/x/oracle/keeper/slash_test.go @@ -169,7 +169,6 @@ func TestWhitelistSlashing(t *testing.T) { input, msgServer := Setup(t) votePeriodsPerWindow := sdk.NewDec(int64(input.OracleKeeper.SlashWindow(input.Ctx))).QuoInt64(int64(input.OracleKeeper.VotePeriod(input.Ctx))).TruncateInt64() - // slashFraction := input.OracleKeeper.SlashFraction(input.Ctx) minValidPerWindow := input.OracleKeeper.MinValidPerWindow(input.Ctx) pair := asset.Registry.Pair(denoms.NIBI, denoms.NUSD) @@ -182,28 +181,18 @@ func TestWhitelistSlashing(t *testing.T) { perfs := input.OracleKeeper.UpdateExchangeRates(input.Ctx) require.EqualValues(t, 0, perfs.GetTotalRewardWeight()) - block := input.Ctx.BlockHeight() + 50 - priceVoteFromVal(0, block, TEST_ERATE) - priceVoteFromVal(1, block, TEST_ERATE) - input.Ctx = input.Ctx.WithBlockHeight(block + 100) - allowedMissPct := sdk.OneDec().Sub(minValidPerWindow) allowedMissPeriods := allowedMissPct.MulInt64(votePeriodsPerWindow). TruncateInt64() - - // TODO - t.Logf("For %v blocks, valoper0 does not vote, while 1 and 2 do.", allowedMissPeriods) for idxMissPeriod := uint64(0); idxMissPeriod < uint64(allowedMissPeriods); idxMissPeriod++ { block := input.Ctx.BlockHeight() + 1 input.Ctx = input.Ctx.WithBlockHeight(block) - valIdx := 0 + valIdx := 0 // Valoper doesn't vote (abstain) priceVoteFromVal(valIdx+1, block, TEST_ERATE) priceVoteFromVal(valIdx+2, block, TEST_ERATE) - // Valoper 0 votes outside band (miss) - perfs := input.OracleKeeper.UpdateExchangeRates(input.Ctx) missCount := input.OracleKeeper.MissCounters.GetOr(input.Ctx, ValAddrs[0], 0) require.EqualValues(t, 0, missCount, perfs.String()) @@ -212,20 +201,6 @@ func TestWhitelistSlashing(t *testing.T) { t.Log("valoper0 should not be slashed") validator := input.StakingKeeper.Validator(input.Ctx, ValAddrs[0]) require.Equal(t, TEST_STAKING_AMT, validator.GetBondedTokens()) - - // // one more miss vote will inccur Account 1 slashing - // - // // Account 2, govstable - // MakeAggregatePrevoteAndVote(t, input, msgServer, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 1) - // // Account 3, govstable - // MakeAggregatePrevoteAndVote(t, input, msgServer, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 2) - // - // input.Ctx = input.Ctx.WithBlockHeight(votePeriodsPerWindow - 1) - // input.OracleKeeper.UpdateExchangeRates(input.Ctx) - // input.OracleKeeper.SlashAndResetMissCounters(input.Ctx) - // // input.OracleKeeper.UpdateExchangeRates(input.Ctx) - // validator = input.StakingKeeper.Validator(input.Ctx, ValAddrs[0]) - // require.Equal(t, sdk.OneDec().Sub(slashFraction).MulInt(stakingAmt).TruncateInt(), validator.GetBondedTokens()) } func TestNotPassedBallotSlashing(t *testing.T) { diff --git a/x/oracle/types/ballot_test.go b/x/oracle/types/ballot_test.go index 4a79bd898..af2b1f017 100644 --- a/x/oracle/types/ballot_test.go +++ b/x/oracle/types/ballot_test.go @@ -20,7 +20,7 @@ import ( "github.com/NibiruChain/nibiru/x/oracle/types" ) -func TestExchangeRateBallots_ToMap(t *testing.T) { +func TestExchangeRateBallotsToMap(t *testing.T) { tests := struct { votes []types.ExchangeRateBallot isValid []bool From f8bba05eac4550cd634187ddb2365a209d99ee8d Mon Sep 17 00:00:00 2001 From: Kevin Yang <5478483+k-yang@users.noreply.github.com> Date: Wed, 27 Sep 2023 17:12:21 -0400 Subject: [PATCH 15/23] variable renaming --- x/oracle/keeper/msg_server_test.go | 2 +- x/oracle/keeper/reward_test.go | 2 +- x/oracle/keeper/slash_test.go | 46 +++++++++---------- x/oracle/keeper/test_utils.go | 14 +++--- x/oracle/keeper/update_exchange_rates_test.go | 35 +++++++------- 5 files changed, 50 insertions(+), 49 deletions(-) diff --git a/x/oracle/keeper/msg_server_test.go b/x/oracle/keeper/msg_server_test.go index f76d1deb3..d8f392439 100644 --- a/x/oracle/keeper/msg_server_test.go +++ b/x/oracle/keeper/msg_server_test.go @@ -17,7 +17,7 @@ func TestFeederDelegation(t *testing.T) { exchangeRates := types.ExchangeRateTuples{ { Pair: asset.Registry.Pair(denoms.BTC, denoms.NUSD), - ExchangeRate: TEST_ERATE, + ExchangeRate: testExchangeRate, }, } diff --git a/x/oracle/keeper/reward_test.go b/x/oracle/keeper/reward_test.go index 19e0bc85b..8b75c3e2a 100644 --- a/x/oracle/keeper/reward_test.go +++ b/x/oracle/keeper/reward_test.go @@ -40,7 +40,7 @@ func TestKeeperRewardsDistributionMultiVotePeriods(t *testing.T) { MakeAggregatePrevoteAndVote(t, fixture, msgServer, fixture.Ctx.BlockHeight(), types.ExchangeRateTuples{ { Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), - ExchangeRate: TEST_ERATE, + ExchangeRate: testExchangeRate, }, }, valIndex) } diff --git a/x/oracle/keeper/slash_test.go b/x/oracle/keeper/slash_test.go index fb55687c1..954d27a63 100644 --- a/x/oracle/keeper/slash_test.go +++ b/x/oracle/keeper/slash_test.go @@ -105,22 +105,22 @@ func TestInvalidVotesSlashing(t *testing.T) { // Account 1, govstable MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate}, }, 0) // Account 2, govstable, miss vote MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE.Add(sdk.NewDec(100000000000000))}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate.Add(sdk.NewDec(100000000000000))}, }, 1) // Account 3, govstable MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate}, }, 2) // Account 4, govstable MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate}, }, 3) input.OracleKeeper.UpdateExchangeRates(input.Ctx) @@ -131,27 +131,27 @@ func TestInvalidVotesSlashing(t *testing.T) { } validator := input.StakingKeeper.Validator(input.Ctx, ValAddrs[1]) - require.Equal(t, TEST_STAKING_AMT, validator.GetBondedTokens()) + require.Equal(t, testStakingAmt, validator.GetBondedTokens()) // one more miss vote will inccur ValAddrs[1] slashing // Account 1, govstable MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate}, }, 0) // Account 2, govstable, miss vote MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE.Add(sdk.NewDec(100000000000000))}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate.Add(sdk.NewDec(100000000000000))}, }, 1) // Account 3, govstable MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate}, }, 2) // Account 4, govstable MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate}, }, 3) input.Ctx = input.Ctx.WithBlockHeight(votePeriodsPerWindow - 1) @@ -160,7 +160,7 @@ func TestInvalidVotesSlashing(t *testing.T) { // input.OracleKeeper.UpdateExchangeRates(input.Ctx) validator = input.StakingKeeper.Validator(input.Ctx, ValAddrs[1]) - require.Equal(t, sdk.OneDec().Sub(slashFraction).MulInt(TEST_STAKING_AMT).TruncateInt(), validator.GetBondedTokens()) + require.Equal(t, sdk.OneDec().Sub(slashFraction).MulInt(testStakingAmt).TruncateInt(), validator.GetBondedTokens()) } // TestWhitelistSlashing: Creates a scenario where one valoper (valIdx 0) does @@ -168,8 +168,8 @@ func TestInvalidVotesSlashing(t *testing.T) { func TestWhitelistSlashing(t *testing.T) { input, msgServer := Setup(t) - votePeriodsPerWindow := sdk.NewDec(int64(input.OracleKeeper.SlashWindow(input.Ctx))).QuoInt64(int64(input.OracleKeeper.VotePeriod(input.Ctx))).TruncateInt64() - minValidPerWindow := input.OracleKeeper.MinValidPerWindow(input.Ctx) + votePeriodsPerSlashWindow := sdk.NewDec(int64(input.OracleKeeper.SlashWindow(input.Ctx))).QuoInt64(int64(input.OracleKeeper.VotePeriod(input.Ctx))).TruncateInt64() + minValidVotePeriodsPerWindow := input.OracleKeeper.MinValidPerWindow(input.Ctx) pair := asset.Registry.Pair(denoms.NIBI, denoms.NUSD) priceVoteFromVal := func(valIdx int, block int64, erate sdk.Dec) { @@ -181,17 +181,17 @@ func TestWhitelistSlashing(t *testing.T) { perfs := input.OracleKeeper.UpdateExchangeRates(input.Ctx) require.EqualValues(t, 0, perfs.GetTotalRewardWeight()) - allowedMissPct := sdk.OneDec().Sub(minValidPerWindow) - allowedMissPeriods := allowedMissPct.MulInt64(votePeriodsPerWindow). + allowedMissPct := sdk.OneDec().Sub(minValidVotePeriodsPerWindow) + allowedMissVotePeriods := allowedMissPct.MulInt64(votePeriodsPerSlashWindow). TruncateInt64() - t.Logf("For %v blocks, valoper0 does not vote, while 1 and 2 do.", allowedMissPeriods) - for idxMissPeriod := uint64(0); idxMissPeriod < uint64(allowedMissPeriods); idxMissPeriod++ { + t.Logf("For %v blocks, valoper0 does not vote, while 1 and 2 do.", allowedMissVotePeriods) + for idxMissPeriod := uint64(0); idxMissPeriod < uint64(allowedMissVotePeriods); idxMissPeriod++ { block := input.Ctx.BlockHeight() + 1 input.Ctx = input.Ctx.WithBlockHeight(block) valIdx := 0 // Valoper doesn't vote (abstain) - priceVoteFromVal(valIdx+1, block, TEST_ERATE) - priceVoteFromVal(valIdx+2, block, TEST_ERATE) + priceVoteFromVal(valIdx+1, block, testExchangeRate) + priceVoteFromVal(valIdx+2, block, testExchangeRate) perfs := input.OracleKeeper.UpdateExchangeRates(input.Ctx) missCount := input.OracleKeeper.MissCounters.GetOr(input.Ctx, ValAddrs[0], 0) @@ -200,7 +200,7 @@ func TestWhitelistSlashing(t *testing.T) { t.Log("valoper0 should not be slashed") validator := input.StakingKeeper.Validator(input.Ctx, ValAddrs[0]) - require.Equal(t, TEST_STAKING_AMT, validator.GetBondedTokens()) + require.Equal(t, testStakingAmt, validator.GetBondedTokens()) } func TestNotPassedBallotSlashing(t *testing.T) { @@ -219,7 +219,7 @@ func TestNotPassedBallotSlashing(t *testing.T) { input.Ctx = input.Ctx.WithBlockHeight(input.Ctx.BlockHeight() + 1) // Account 1, govstable - MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}}, 0) + MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate}}, 0) input.OracleKeeper.UpdateExchangeRates(input.Ctx) input.OracleKeeper.SlashAndResetMissCounters(input.Ctx) @@ -249,13 +249,13 @@ func TestAbstainSlashing(t *testing.T) { input.Ctx = input.Ctx.WithBlockHeight(input.Ctx.BlockHeight() + 1) // Account 1, govstable - MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}}, 0) + MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate}}, 0) // Account 2, govstable, abstain vote MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: sdk.ZeroDec()}}, 1) // Account 3, govstable - MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}}, 2) + MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate}}, 2) input.OracleKeeper.UpdateExchangeRates(input.Ctx) input.OracleKeeper.SlashAndResetMissCounters(input.Ctx) @@ -264,5 +264,5 @@ func TestAbstainSlashing(t *testing.T) { } validator := input.StakingKeeper.Validator(input.Ctx, ValAddrs[1]) - require.Equal(t, TEST_STAKING_AMT, validator.GetBondedTokens()) + require.Equal(t, testStakingAmt, validator.GetBondedTokens()) } diff --git a/x/oracle/keeper/test_utils.go b/x/oracle/keeper/test_utils.go index 7e95f40a2..5cef19d0e 100644 --- a/x/oracle/keeper/test_utils.go +++ b/x/oracle/keeper/test_utils.go @@ -285,8 +285,8 @@ func AllocateRewards(t *testing.T, input TestFixture, rewards sdk.Coins, votePer } var ( - TEST_STAKING_AMT = sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction) - TEST_ERATE = sdk.NewDec(1700) + testStakingAmt = sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction) + testExchangeRate = sdk.NewDec(1700) ) func Setup(t *testing.T) (TestFixture, types.MsgServer) { @@ -304,15 +304,15 @@ func Setup(t *testing.T) (TestFixture, types.MsgServer) { sh := stakingkeeper.NewMsgServerImpl(&fixture.StakingKeeper) // Validator created - _, err := sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[0], ValPubKeys[0], TEST_STAKING_AMT)) + _, err := sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[0], ValPubKeys[0], testStakingAmt)) require.NoError(t, err) - _, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[1], ValPubKeys[1], TEST_STAKING_AMT)) + _, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[1], ValPubKeys[1], testStakingAmt)) require.NoError(t, err) - _, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[2], ValPubKeys[2], TEST_STAKING_AMT)) + _, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[2], ValPubKeys[2], testStakingAmt)) require.NoError(t, err) - _, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[3], ValPubKeys[3], TEST_STAKING_AMT)) + _, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[3], ValPubKeys[3], testStakingAmt)) require.NoError(t, err) - _, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[4], ValPubKeys[4], TEST_STAKING_AMT)) + _, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[4], ValPubKeys[4], testStakingAmt)) require.NoError(t, err) staking.EndBlocker(fixture.Ctx, &fixture.StakingKeeper) diff --git a/x/oracle/keeper/update_exchange_rates_test.go b/x/oracle/keeper/update_exchange_rates_test.go index 87766b4ba..b6c937b13 100644 --- a/x/oracle/keeper/update_exchange_rates_test.go +++ b/x/oracle/keeper/update_exchange_rates_test.go @@ -24,7 +24,7 @@ func TestOracleThreshold(t *testing.T) { exchangeRates := types.ExchangeRateTuples{ { Pair: asset.Registry.Pair(denoms.BTC, denoms.NUSD), - ExchangeRate: TEST_ERATE, + ExchangeRate: testExchangeRate, }, } exchangeRateStr, err := exchangeRates.ToString() @@ -68,12 +68,12 @@ func TestOracleThreshold(t *testing.T) { fixture.OracleKeeper.UpdateExchangeRates(fixture.Ctx) rate, err := fixture.OracleKeeper.ExchangeRates.Get(fixture.Ctx, exchangeRates[0].Pair) require.NoError(t, err) - assert.Equal(t, TEST_ERATE, rate.ExchangeRate) + assert.Equal(t, testExchangeRate, rate.ExchangeRate) // Case 3. // Increase voting power of absent validator, exchange rate consensus fails val, _ := fixture.StakingKeeper.GetValidator(fixture.Ctx, ValAddrs[4]) - _, _ = fixture.StakingKeeper.Delegate(fixture.Ctx.WithBlockHeight(0), Addrs[4], TEST_STAKING_AMT.MulRaw(8), stakingtypes.Unbonded, val, false) + _, _ = fixture.StakingKeeper.Delegate(fixture.Ctx.WithBlockHeight(0), Addrs[4], testStakingAmt.MulRaw(8), stakingtypes.Unbonded, val, false) for i := 0; i < 4; i++ { salt := fmt.Sprintf("%d", i) @@ -104,7 +104,7 @@ func TestResetExchangeRates(t *testing.T) { fixture.OracleKeeper.Params.Set(fixture.Ctx, params) // Post a price at block 1 - fixture.OracleKeeper.SetPrice(fixture.Ctx.WithBlockHeight(1), pair, TEST_ERATE) + fixture.OracleKeeper.SetPrice(fixture.Ctx.WithBlockHeight(1), pair, testExchangeRate) // reset exchange rates at block 2 // Price should still be there because not expired yet @@ -121,7 +121,7 @@ func TestResetExchangeRates(t *testing.T) { // Post a price at block 69 // reset exchange rates at block 79 // Price should not be there anymore because expired - fixture.OracleKeeper.SetPrice(fixture.Ctx.WithBlockHeight(69), pair, TEST_ERATE) + fixture.OracleKeeper.SetPrice(fixture.Ctx.WithBlockHeight(69), pair, testExchangeRate) fixture.OracleKeeper.resetExchangeRates(fixture.Ctx.WithBlockHeight(79), emptyBallot) _, err = fixture.OracleKeeper.ExchangeRates.Get(fixture.Ctx, pair) @@ -153,7 +153,7 @@ func TestOracleTally(t *testing.T) { require.NoError(t, err1) require.NoError(t, err2) - power := TEST_STAKING_AMT.QuoRaw(int64(6)).Int64() + power := testStakingAmt.QuoRaw(int64(6)).Int64() if decExchangeRate.IsZero() { power = int64(0) } @@ -229,26 +229,26 @@ func TestOracleRewardBand(t *testing.T) { } fixture.OracleKeeper.WhitelistedPairs.Insert(fixture.Ctx, asset.Registry.Pair(denoms.NIBI, denoms.NUSD)) - rewardSpread := TEST_ERATE.Mul(fixture.OracleKeeper.RewardBand(fixture.Ctx).QuoInt64(2)) + rewardSpread := testExchangeRate.Mul(fixture.OracleKeeper.RewardBand(fixture.Ctx).QuoInt64(2)) // Account 1, nibi:nusd MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE.Sub(rewardSpread)}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate.Sub(rewardSpread)}, }, 0) // Account 2, nibi:nusd MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate}, }, 1) // Account 3, nibi:nusd MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate}, }, 2) // Account 4, nibi:nusd MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE.Add(rewardSpread)}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate.Add(rewardSpread)}, }, 3) fixture.OracleKeeper.UpdateExchangeRates(fixture.Ctx) @@ -261,22 +261,22 @@ func TestOracleRewardBand(t *testing.T) { // Account 1 will miss the vote due to raward band condition // Account 1, nibi:nusd MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE.Sub(rewardSpread.Add(sdk.OneDec()))}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate.Sub(rewardSpread.Add(sdk.OneDec()))}, }, 0) // Account 2, nibi:nusd MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate}, }, 1) // Account 3, nibi:nusd MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate}, }, 2) // Account 4, nibi:nusd MakeAggregatePrevoteAndVote(t, fixture, msgServer, 0, types.ExchangeRateTuples{ - {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE.Add(rewardSpread)}, + {Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate.Add(rewardSpread)}, }, 3) fixture.OracleKeeper.UpdateExchangeRates(fixture.Ctx) @@ -412,7 +412,7 @@ func TestWhitelistedPairs(t *testing.T) { t.Log("vote and prevote from all vals on nibi:nusd") priceVoteFromVal := func(valIdx int, block int64) { - MakeAggregatePrevoteAndVote(t, fixture, msgServer, block, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}}, valIdx) + MakeAggregatePrevoteAndVote(t, fixture, msgServer, block, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate}}, valIdx) } block := int64(0) priceVoteFromVal(0, block) @@ -435,7 +435,8 @@ func TestWhitelistedPairs(t *testing.T) { assert.Equal(t, []asset.Pair{ asset.Registry.Pair(denoms.BTC, denoms.NUSD), - asset.Registry.Pair(denoms.NIBI, denoms.NUSD)}, + asset.Registry.Pair(denoms.NIBI, denoms.NUSD), + }, fixture.OracleKeeper.GetWhitelistedPairs(fixture.Ctx)) t.Log("vote from vals 0-3 on nibi:nusd (but not btc:nusd)") From 1ee6408342ad59dd6f8dc7e0098d0dda33872a03 Mon Sep 17 00:00:00 2001 From: Kevin Yang <5478483+k-yang@users.noreply.github.com> Date: Tue, 10 Oct 2023 15:08:09 -0500 Subject: [PATCH 16/23] rename exchange rate ballot to exchange rate vote --- x/oracle/keeper/ballot.go | 14 +++--- x/oracle/keeper/ballot_test.go | 48 +++++++++---------- x/oracle/keeper/update_exchange_rates.go | 8 ++-- x/oracle/keeper/update_exchange_rates_test.go | 6 +-- x/oracle/types/ballot.go | 32 ++++++------- x/oracle/types/ballot_test.go | 22 ++++----- 6 files changed, 65 insertions(+), 65 deletions(-) diff --git a/x/oracle/keeper/ballot.go b/x/oracle/keeper/ballot.go index 1e05387fd..a94ec5ece 100644 --- a/x/oracle/keeper/ballot.go +++ b/x/oracle/keeper/ballot.go @@ -23,8 +23,8 @@ import ( func (k Keeper) groupBallotsByPair( ctx sdk.Context, valPerfMap types.ValidatorPerformances, -) (pairBallotsMap map[asset.Pair]types.ExchangeRateBallots) { - pairBallotsMap = map[asset.Pair]types.ExchangeRateBallots{} +) (pairBallotsMap map[asset.Pair]types.ExchangeRateVotes) { + pairBallotsMap = map[asset.Pair]types.ExchangeRateVotes{} for _, value := range k.Votes.Iterate(ctx, collections.Range[sdk.ValAddress]{}).KeyValues() { voterAddr, aggregateVote := value.Key, value.Value @@ -77,7 +77,7 @@ func (k Keeper) clearVotesAndPreVotes(ctx sdk.Context, votePeriod uint64) { // isPassingVoteThreshold ballot is passing the threshold amount of voting power func isPassingVoteThreshold( - ballots types.ExchangeRateBallots, thresholdVotingPower sdkmath.Int, minVoters uint64, + ballots types.ExchangeRateVotes, thresholdVotingPower sdkmath.Int, minVoters uint64, ) bool { ballotPower := sdk.NewInt(ballots.Power()) if ballotPower.IsZero() { @@ -104,8 +104,8 @@ func isPassingVoteThreshold( // anymore. func (k Keeper) removeInvalidBallots( ctx sdk.Context, - pairBallotsMap map[asset.Pair]types.ExchangeRateBallots, -) (map[asset.Pair]types.ExchangeRateBallots, set.Set[asset.Pair]) { + pairBallotsMap map[asset.Pair]types.ExchangeRateVotes, +) (map[asset.Pair]types.ExchangeRateVotes, set.Set[asset.Pair]) { whitelistedPairs := set.New(k.GetWhitelistedPairs(ctx)...) totalBondedPower := sdk.TokensToConsensusPower( @@ -115,7 +115,7 @@ func (k Keeper) removeInvalidBallots( minVoters := k.MinVoters(ctx) // Iterate through sorted keys for deterministic ordering. - orderedBallotsMap := omap.OrderedMap_Pair[types.ExchangeRateBallots](pairBallotsMap) + orderedBallotsMap := omap.OrderedMap_Pair[types.ExchangeRateVotes](pairBallotsMap) for pair := range orderedBallotsMap.Range() { ballots := pairBallotsMap[pair] // If pair is not whitelisted, or the ballot for it has failed, then skip @@ -144,7 +144,7 @@ func (k Keeper) removeInvalidBallots( // ALERT: This function mutates validatorPerformances slice based on the votes // made by the validators. func Tally( - ballots types.ExchangeRateBallots, + ballots types.ExchangeRateVotes, rewardBand sdk.Dec, validatorPerformances types.ValidatorPerformances, ) (sdk.Dec, types.ValidatorPerformances) { diff --git a/x/oracle/keeper/ballot_test.go b/x/oracle/keeper/ballot_test.go index a609e5458..4f4d23b93 100644 --- a/x/oracle/keeper/ballot_test.go +++ b/x/oracle/keeper/ballot_test.go @@ -39,12 +39,12 @@ func TestGroupBallotsByPair(t *testing.T) { pairBtc := asset.Registry.Pair(denoms.BTC, denoms.NUSD) pairEth := asset.Registry.Pair(denoms.ETH, denoms.NUSD) - btcBallots := types.ExchangeRateBallots{ + btcBallots := types.ExchangeRateVotes{ {Pair: pairBtc, ExchangeRate: sdk.NewDec(17), Voter: ValAddrs[0], Power: power}, {Pair: pairBtc, ExchangeRate: sdk.NewDec(10), Voter: ValAddrs[1], Power: power}, {Pair: pairBtc, ExchangeRate: sdk.NewDec(6), Voter: ValAddrs[2], Power: power}, } - ethBallots := types.ExchangeRateBallots{ + ethBallots := types.ExchangeRateVotes{ {Pair: pairEth, ExchangeRate: sdk.NewDec(1_000), Voter: ValAddrs[0], Power: power}, {Pair: pairEth, ExchangeRate: sdk.NewDec(1_300), Voter: ValAddrs[1], Power: power}, {Pair: pairEth, ExchangeRate: sdk.NewDec(2_000), Voter: ValAddrs[2], Power: power}, @@ -109,12 +109,12 @@ func TestClearBallots(t *testing.T) { require.NoError(t, err) staking.EndBlocker(fixture.Ctx, &fixture.StakingKeeper) - btcBallot := types.ExchangeRateBallots{ + btcBallot := types.ExchangeRateVotes{ types.NewExchangeRateBallot(sdk.NewDec(17), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[0], power), types.NewExchangeRateBallot(sdk.NewDec(10), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[1], power), types.NewExchangeRateBallot(sdk.NewDec(6), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[2], power), } - ethBallot := types.ExchangeRateBallots{ + ethBallot := types.ExchangeRateVotes{ types.NewExchangeRateBallot(sdk.NewDec(1000), asset.Registry.Pair(denoms.ETH, denoms.NUSD), ValAddrs[0], power), types.NewExchangeRateBallot(sdk.NewDec(1300), asset.Registry.Pair(denoms.ETH, denoms.NUSD), ValAddrs[1], power), types.NewExchangeRateBallot(sdk.NewDec(2000), asset.Registry.Pair(denoms.ETH, denoms.NUSD), ValAddrs[2], power), @@ -169,8 +169,8 @@ func TestFuzzTally(t *testing.T) { (*e)[validator] = types.NewValidatorPerformance(power, addr) } }, - func(e *types.ExchangeRateBallots, c fuzz.Continue) { - ballot := types.ExchangeRateBallots{} + func(e *types.ExchangeRateVotes, c fuzz.Continue) { + ballot := types.ExchangeRateVotes{} for addr, power := range validators { addr, _ := sdk.ValAddressFromBech32(addr) @@ -190,7 +190,7 @@ func TestFuzzTally(t *testing.T) { claimMap := types.ValidatorPerformances{} f.Fuzz(&claimMap) - ballot := types.ExchangeRateBallots{} + ballot := types.ExchangeRateVotes{} f.Fuzz(&ballot) var rewardBand sdk.Dec @@ -201,7 +201,7 @@ func TestFuzzTally(t *testing.T) { }) } -type VoteMap = map[asset.Pair]types.ExchangeRateBallots +type VoteMap = map[asset.Pair]types.ExchangeRateVotes func TestRemoveInvalidBallots(t *testing.T) { testCases := []struct { @@ -211,35 +211,35 @@ func TestRemoveInvalidBallots(t *testing.T) { { name: "empty key, empty ballot", voteMap: VoteMap{ - "": types.ExchangeRateBallots{}, + "": types.ExchangeRateVotes{}, }, }, { name: "nonempty key, empty ballot", voteMap: VoteMap{ - "xxx": types.ExchangeRateBallots{}, + "xxx": types.ExchangeRateVotes{}, }, }, { name: "nonempty keys, empty ballot", voteMap: VoteMap{ - "xxx": types.ExchangeRateBallots{}, - "abc123": types.ExchangeRateBallots{}, + "xxx": types.ExchangeRateVotes{}, + "abc123": types.ExchangeRateVotes{}, }, }, { name: "mixed empty keys, empty ballot", voteMap: VoteMap{ - "xxx": types.ExchangeRateBallots{}, - "": types.ExchangeRateBallots{}, - "abc123": types.ExchangeRateBallots{}, - "0x": types.ExchangeRateBallots{}, + "xxx": types.ExchangeRateVotes{}, + "": types.ExchangeRateVotes{}, + "abc123": types.ExchangeRateVotes{}, + "0x": types.ExchangeRateVotes{}, }, }, { name: "empty key, nonempty ballot, not whitelisted", voteMap: VoteMap{ - "": types.ExchangeRateBallots{ + "": types.ExchangeRateVotes{ {Pair: "", ExchangeRate: sdk.ZeroDec(), Voter: sdk.ValAddress{}, Power: 0}, }, }, @@ -247,13 +247,13 @@ func TestRemoveInvalidBallots(t *testing.T) { { name: "nonempty key, nonempty ballot, whitelisted", voteMap: VoteMap{ - "x": types.ExchangeRateBallots{ + "x": types.ExchangeRateVotes{ {Pair: "x", ExchangeRate: sdk.Dec{}, Voter: sdk.ValAddress{123}, Power: 5}, }, - asset.Registry.Pair(denoms.BTC, denoms.NUSD): types.ExchangeRateBallots{ + asset.Registry.Pair(denoms.BTC, denoms.NUSD): types.ExchangeRateVotes{ {Pair: asset.Registry.Pair(denoms.BTC, denoms.NUSD), ExchangeRate: sdk.Dec{}, Voter: sdk.ValAddress{123}, Power: 5}, }, - asset.Registry.Pair(denoms.ETH, denoms.NUSD): types.ExchangeRateBallots{ + asset.Registry.Pair(denoms.ETH, denoms.NUSD): types.ExchangeRateVotes{ {Pair: asset.Registry.Pair(denoms.BTC, denoms.NUSD), ExchangeRate: sdk.Dec{}, Voter: sdk.ValAddress{123}, Power: 5}, }, }, @@ -301,12 +301,12 @@ func TestFuzzPickReferencePair(t *testing.T) { (*e)[sdk.ValAddress(secp256k1.GenPrivKey().PubKey().Address()).String()] = int64(c.Intn(100) + 1) } }, - func(e *map[asset.Pair]types.ExchangeRateBallots, c fuzz.Continue) { + func(e *map[asset.Pair]types.ExchangeRateVotes, c fuzz.Continue) { validators := map[string]int64{} c.Fuzz(&validators) for _, pair := range pairs { - ballots := types.ExchangeRateBallots{} + ballots := types.ExchangeRateVotes{} for addr, power := range validators { addr, _ := sdk.ValAddressFromBech32(addr) @@ -338,7 +338,7 @@ func TestFuzzPickReferencePair(t *testing.T) { } // test OracleKeeper.RemoveInvalidBallots - voteMap := map[asset.Pair]types.ExchangeRateBallots{} + voteMap := map[asset.Pair]types.ExchangeRateVotes{} f.Fuzz(&voteMap) assert.NotPanics(t, func() { @@ -347,7 +347,7 @@ func TestFuzzPickReferencePair(t *testing.T) { } func TestZeroBallotPower(t *testing.T) { - btcBallots := types.ExchangeRateBallots{ + btcBallots := types.ExchangeRateVotes{ types.NewExchangeRateBallot(sdk.NewDec(17), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[0], 0), types.NewExchangeRateBallot(sdk.NewDec(10), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[1], 0), types.NewExchangeRateBallot(sdk.NewDec(6), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[2], 0), diff --git a/x/oracle/keeper/update_exchange_rates.go b/x/oracle/keeper/update_exchange_rates.go index 31502eda3..0c907e52f 100644 --- a/x/oracle/keeper/update_exchange_rates.go +++ b/x/oracle/keeper/update_exchange_rates.go @@ -66,13 +66,13 @@ func (k Keeper) registerAbstainsByOmission( // ExchangeRates based on the results. func (k Keeper) countVotesAndUpdateExchangeRates( ctx sdk.Context, - pairBallotsMap map[asset.Pair]types.ExchangeRateBallots, + pairBallotsMap map[asset.Pair]types.ExchangeRateVotes, validatorPerformances types.ValidatorPerformances, ) { rewardBand := k.RewardBand(ctx) // Iterate through sorted keys for deterministic ordering. - orderedBallotsMap := omap.OrderedMap_Pair[types.ExchangeRateBallots](pairBallotsMap) + orderedBallotsMap := omap.OrderedMap_Pair[types.ExchangeRateVotes](pairBallotsMap) for pair := range orderedBallotsMap.Range() { ballots := pairBallotsMap[pair] exchangeRate, _ := Tally(ballots, rewardBand, validatorPerformances) @@ -92,7 +92,7 @@ func (k Keeper) countVotesAndUpdateExchangeRates( func (k Keeper) getPairBallotsMapAndWhitelistedPairs( ctx sdk.Context, validatorPerformances types.ValidatorPerformances, -) (pairBallotsMap map[asset.Pair]types.ExchangeRateBallots, whitelistedPairsMap set.Set[asset.Pair]) { +) (pairBallotsMap map[asset.Pair]types.ExchangeRateVotes, whitelistedPairsMap set.Set[asset.Pair]) { pairBallotsMap = k.groupBallotsByPair(ctx, validatorPerformances) return k.removeInvalidBallots(ctx, pairBallotsMap) @@ -100,7 +100,7 @@ func (k Keeper) getPairBallotsMapAndWhitelistedPairs( // resetExchangeRates removes all exchange rates from the state // We remove the price for pair with expired prices or valid ballots -func (k Keeper) resetExchangeRates(ctx sdk.Context, pairBallotsMap map[asset.Pair]types.ExchangeRateBallots) { +func (k Keeper) resetExchangeRates(ctx sdk.Context, pairBallotsMap map[asset.Pair]types.ExchangeRateVotes) { params, _ := k.Params.Get(ctx) expirationBlocks := params.ExpirationBlocks diff --git a/x/oracle/keeper/update_exchange_rates_test.go b/x/oracle/keeper/update_exchange_rates_test.go index b6c937b13..4ab00c15d 100644 --- a/x/oracle/keeper/update_exchange_rates_test.go +++ b/x/oracle/keeper/update_exchange_rates_test.go @@ -95,8 +95,8 @@ func TestResetExchangeRates(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) fixture, _ := Setup(t) - emptyBallot := map[asset.Pair]types.ExchangeRateBallots{} - validBallot := map[asset.Pair]types.ExchangeRateBallots{pair: {}} + emptyBallot := map[asset.Pair]types.ExchangeRateVotes{} + validBallot := map[asset.Pair]types.ExchangeRateVotes{pair: {}} // Set expiration blocks to 10 params, _ := fixture.OracleKeeper.Params.Get(fixture.Ctx) @@ -131,7 +131,7 @@ func TestResetExchangeRates(t *testing.T) { func TestOracleTally(t *testing.T) { fixture, _ := Setup(t) - ballot := types.ExchangeRateBallots{} + ballot := types.ExchangeRateVotes{} rates, valAddrs, stakingKeeper := types.GenerateRandomTestCase() fixture.OracleKeeper.StakingKeeper = stakingKeeper h := NewMsgServerImpl(fixture.OracleKeeper) diff --git a/x/oracle/types/ballot.go b/x/oracle/types/ballot.go index ea93a0a6c..b1ecce8e4 100644 --- a/x/oracle/types/ballot.go +++ b/x/oracle/types/ballot.go @@ -15,8 +15,8 @@ import ( // NOTE: we don't need to implement proto interface on this file // these are not used in store or rpc response -// ExchangeRateBallot is a convenience wrapper to reduce redundant lookup cost -type ExchangeRateBallot struct { +// ExchangeRateVote is a convenience wrapper to reduce redundant lookup cost +type ExchangeRateVote struct { Pair asset.Pair ExchangeRate sdk.Dec // aka price Voter sdk.ValAddress @@ -24,8 +24,8 @@ type ExchangeRateBallot struct { } // NewExchangeRateBallot returns a new ExchangeRateBallot instance -func NewExchangeRateBallot(rate sdk.Dec, pair asset.Pair, voter sdk.ValAddress, power int64) ExchangeRateBallot { - return ExchangeRateBallot{ +func NewExchangeRateBallot(rate sdk.Dec, pair asset.Pair, voter sdk.ValAddress, power int64) ExchangeRateVote { + return ExchangeRateVote{ ExchangeRate: rate, Pair: pair, Voter: voter, @@ -33,11 +33,11 @@ func NewExchangeRateBallot(rate sdk.Dec, pair asset.Pair, voter sdk.ValAddress, } } -// ExchangeRateBallots is a convenience wrapper around a ExchangeRateVote slice -type ExchangeRateBallots []ExchangeRateBallot +// ExchangeRateVotes is a convenience wrapper around a ExchangeRateVote slice +type ExchangeRateVotes []ExchangeRateVote // ToMap return organized exchange rate map by validator -func (pb ExchangeRateBallots) ToMap() map[string]sdk.Dec { +func (pb ExchangeRateVotes) ToMap() map[string]sdk.Dec { validatorExchangeRateMap := make(map[string]sdk.Dec) for _, vote := range pb { if vote.ExchangeRate.IsPositive() { @@ -49,7 +49,7 @@ func (pb ExchangeRateBallots) ToMap() map[string]sdk.Dec { } // ToCrossRate return cross_rate(base/exchange_rate) ballot -func (pb ExchangeRateBallots) ToCrossRate(bases map[string]sdk.Dec) (cb ExchangeRateBallots) { +func (pb ExchangeRateVotes) ToCrossRate(bases map[string]sdk.Dec) (cb ExchangeRateVotes) { for i := range pb { vote := pb[i] @@ -68,7 +68,7 @@ func (pb ExchangeRateBallots) ToCrossRate(bases map[string]sdk.Dec) (cb Exchange } // NumValidVoters returns the number of voters who actually voted (i.e. did not abstain from voting for a pair). -func (b ExchangeRateBallots) NumValidVoters() uint64 { +func (b ExchangeRateVotes) NumValidVoters() uint64 { count := 0 for _, ballot := range b { if ballot.ExchangeRate.IsPositive() { @@ -79,7 +79,7 @@ func (b ExchangeRateBallots) NumValidVoters() uint64 { } // Power returns the total amount of voting power in the ballot -func (b ExchangeRateBallots) Power() int64 { +func (b ExchangeRateVotes) Power() int64 { totalPower := int64(0) for _, vote := range b { totalPower += vote.Power @@ -90,7 +90,7 @@ func (b ExchangeRateBallots) Power() int64 { // WeightedMedian returns the median weighted by the power of the ExchangeRateVote. // CONTRACT: ballot must be sorted -func (pb ExchangeRateBallots) WeightedMedian() sdk.Dec { +func (pb ExchangeRateVotes) WeightedMedian() sdk.Dec { totalPower := pb.Power() if pb.Len() > 0 { pivot := int64(0) @@ -107,7 +107,7 @@ func (pb ExchangeRateBallots) WeightedMedian() sdk.Dec { } // WeightedMedianWithAssertion returns the median weighted by the power of the ExchangeRateVote. -func (pb ExchangeRateBallots) WeightedMedianWithAssertion() sdk.Dec { +func (pb ExchangeRateVotes) WeightedMedianWithAssertion() sdk.Dec { sort.Sort(pb) totalPower := pb.Power() if pb.Len() > 0 { @@ -125,7 +125,7 @@ func (pb ExchangeRateBallots) WeightedMedianWithAssertion() sdk.Dec { } // StandardDeviation returns the standard deviation by the power of the ExchangeRateVote. -func (pb ExchangeRateBallots) StandardDeviation(median sdk.Dec) (standardDeviation sdk.Dec) { +func (pb ExchangeRateVotes) StandardDeviation(median sdk.Dec) (standardDeviation sdk.Dec) { if len(pb) == 0 { return sdk.ZeroDec() } @@ -157,18 +157,18 @@ func (pb ExchangeRateBallots) StandardDeviation(median sdk.Dec) (standardDeviati } // Len implements sort.Interface -func (pb ExchangeRateBallots) Len() int { +func (pb ExchangeRateVotes) Len() int { return len(pb) } // Less reports whether the element with // index i should sort before the element with index j. -func (pb ExchangeRateBallots) Less(i, j int) bool { +func (pb ExchangeRateVotes) Less(i, j int) bool { return pb[i].ExchangeRate.LT(pb[j].ExchangeRate) } // Swap implements sort.Interface. -func (pb ExchangeRateBallots) Swap(i, j int) { +func (pb ExchangeRateVotes) Swap(i, j int) { pb[i], pb[j] = pb[j], pb[i] } diff --git a/x/oracle/types/ballot_test.go b/x/oracle/types/ballot_test.go index af2b1f017..3d739aaed 100644 --- a/x/oracle/types/ballot_test.go +++ b/x/oracle/types/ballot_test.go @@ -22,10 +22,10 @@ import ( func TestExchangeRateBallotsToMap(t *testing.T) { tests := struct { - votes []types.ExchangeRateBallot + votes []types.ExchangeRateVote isValid []bool }{ - []types.ExchangeRateBallot{ + []types.ExchangeRateVote{ { Voter: sdk.ValAddress(secp256k1.GenPrivKey().PubKey().Address()), Pair: asset.Registry.Pair(denoms.BTC, denoms.NUSD), @@ -48,7 +48,7 @@ func TestExchangeRateBallotsToMap(t *testing.T) { []bool{true, false, true}, } - pb := types.ExchangeRateBallots(tests.votes) + pb := types.ExchangeRateVotes(tests.votes) mapData := pb.ToMap() for i, vote := range tests.votes { exchangeRate, ok := mapData[string(vote.Voter)] @@ -60,7 +60,7 @@ func TestExchangeRateBallotsToMap(t *testing.T) { } } require.NotPanics(t, func() { - types.ExchangeRateBallots(tests.votes).NumValidVoters() + types.ExchangeRateVotes(tests.votes).NumValidVoters() }) } @@ -87,9 +87,9 @@ func TestToCrossRate(t *testing.T) { }, } - pbBase := types.ExchangeRateBallots{} - pbQuote := types.ExchangeRateBallots{} - cb := types.ExchangeRateBallots{} + pbBase := types.ExchangeRateVotes{} + pbQuote := types.ExchangeRateVotes{} + cb := types.ExchangeRateVotes{} for _, data := range data { valAddr := sdk.ValAddress(secp256k1.GenPrivKey().PubKey().Address()) if !data.base.IsZero() { @@ -126,7 +126,7 @@ func TestSqrt(t *testing.T) { func TestPBPower(t *testing.T) { ctx := sdk.NewContext(nil, tmproto.Header{}, false, nil) _, valAccAddrs, sk := types.GenerateRandomTestCase() - pb := types.ExchangeRateBallots{} + pb := types.ExchangeRateVotes{} ballotPower := int64(0) for i := 0; i < len(sk.Validators()); i++ { @@ -206,7 +206,7 @@ func TestPBWeightedMedian(t *testing.T) { } for _, tc := range tests { - pb := types.ExchangeRateBallots{} + pb := types.ExchangeRateVotes{} for i, input := range tc.inputs { valAddr := sdk.ValAddress(secp256k1.GenPrivKey().PubKey().Address()) @@ -276,7 +276,7 @@ func TestPBStandardDeviation(t *testing.T) { base := math.Pow10(types.OracleDecPrecision) for _, tc := range tests { - pb := types.ExchangeRateBallots{} + pb := types.ExchangeRateVotes{} for i, input := range tc.inputs { valAddr := sdk.ValAddress(secp256k1.GenPrivKey().PubKey().Address()) @@ -304,7 +304,7 @@ func TestPBStandardDeviationOverflow(t *testing.T) { exchangeRate, err := sdk.NewDecFromStr("100000000000000000000000000000000000000000000000000000000.0") require.NoError(t, err) - pb := types.ExchangeRateBallots{types.NewExchangeRateBallot( + pb := types.ExchangeRateVotes{types.NewExchangeRateBallot( sdk.ZeroDec(), asset.Registry.Pair(denoms.ETH, denoms.NUSD), valAddr, From bff235a16cddd26366949727dacc904a01e71619 Mon Sep 17 00:00:00 2001 From: Kevin Yang <5478483+k-yang@users.noreply.github.com> Date: Tue, 10 Oct 2023 15:27:51 -0500 Subject: [PATCH 17/23] rename ballot to vote --- x/oracle/keeper/ballot.go | 87 ++++++++++--------- x/oracle/keeper/ballot_test.go | 28 +++--- x/oracle/keeper/reward.go | 4 +- x/oracle/keeper/update_exchange_rates.go | 36 ++++---- x/oracle/keeper/update_exchange_rates_test.go | 2 +- x/oracle/types/ballot.go | 24 ++--- x/oracle/types/ballot_test.go | 22 ++--- 7 files changed, 104 insertions(+), 99 deletions(-) diff --git a/x/oracle/keeper/ballot.go b/x/oracle/keeper/ballot.go index a94ec5ece..dfdeb428b 100644 --- a/x/oracle/keeper/ballot.go +++ b/x/oracle/keeper/ballot.go @@ -13,24 +13,24 @@ import ( "github.com/NibiruChain/nibiru/x/oracle/types" ) -// groupBallotsByPair takes a collection of votes and organizes them by their +// groupVotesByPair takes a collection of votes and groups them by their // associated asset pair. This method only considers votes from active validators // and disregards votes from validators that are not in the provided validator set. // // Note that any abstain votes (votes with a non-positive exchange rate) are // assigned zero vote power. This function then returns a map where each -// asset pair is associated with its collection of ExchangeRateBallots. -func (k Keeper) groupBallotsByPair( +// asset pair is associated with its collection of ExchangeRateVotes. +func (k Keeper) groupVotesByPair( ctx sdk.Context, - valPerfMap types.ValidatorPerformances, -) (pairBallotsMap map[asset.Pair]types.ExchangeRateVotes) { - pairBallotsMap = map[asset.Pair]types.ExchangeRateVotes{} + validatorPerformances types.ValidatorPerformances, +) (pairVotes map[asset.Pair]types.ExchangeRateVotes) { + pairVotes = map[asset.Pair]types.ExchangeRateVotes{} for _, value := range k.Votes.Iterate(ctx, collections.Range[sdk.ValAddress]{}).KeyValues() { voterAddr, aggregateVote := value.Key, value.Value // organize ballot only for the active validators - if validatorPerformance, exists := valPerfMap[aggregateVote.Voter]; exists { + if validatorPerformance, exists := validatorPerformances[aggregateVote.Voter]; exists { for _, exchangeRateTuple := range aggregateVote.ExchangeRateTuples { power := validatorPerformance.Power if !exchangeRateTuple.ExchangeRate.IsPositive() { @@ -38,8 +38,9 @@ func (k Keeper) groupBallotsByPair( power = 0 } - pairBallotsMap[exchangeRateTuple.Pair] = append(pairBallotsMap[exchangeRateTuple.Pair], - types.NewExchangeRateBallot( + pairVotes[exchangeRateTuple.Pair] = append( + pairVotes[exchangeRateTuple.Pair], + types.NewExchangeRateVote( exchangeRateTuple.ExchangeRate, exchangeRateTuple.Pair, voterAddr, @@ -77,64 +78,64 @@ func (k Keeper) clearVotesAndPreVotes(ctx sdk.Context, votePeriod uint64) { // isPassingVoteThreshold ballot is passing the threshold amount of voting power func isPassingVoteThreshold( - ballots types.ExchangeRateVotes, thresholdVotingPower sdkmath.Int, minVoters uint64, + votes types.ExchangeRateVotes, thresholdVotingPower sdkmath.Int, minVoters uint64, ) bool { - ballotPower := sdk.NewInt(ballots.Power()) - if ballotPower.IsZero() { + totalPower := sdk.NewInt(votes.Power()) + if totalPower.IsZero() { return false } - if ballotPower.LT(thresholdVotingPower) { + if totalPower.LT(thresholdVotingPower) { return false } - if ballots.NumValidVoters() < minVoters { + if votes.NumValidVoters() < minVoters { return false } return true } -// removeInvalidBallots removes the ballots which have not reached the vote +// removeInvalidVotes removes the ballots which have not reached the vote // threshold or which are not part of the whitelisted pairs anymore: example // when params change during a vote period but some votes were already made. // -// ALERT: This function mutates pairBallotMap slice, it removes the ballot for +// ALERT: This function mutates the pairVotes map, it removes the votes for // the pair which is not passing the threshold or which is not whitelisted // anymore. -func (k Keeper) removeInvalidBallots( +func (k Keeper) removeInvalidVotes( ctx sdk.Context, - pairBallotsMap map[asset.Pair]types.ExchangeRateVotes, + pairVotes map[asset.Pair]types.ExchangeRateVotes, ) (map[asset.Pair]types.ExchangeRateVotes, set.Set[asset.Pair]) { whitelistedPairs := set.New(k.GetWhitelistedPairs(ctx)...) totalBondedPower := sdk.TokensToConsensusPower( k.StakingKeeper.TotalBondedTokens(ctx), k.StakingKeeper.PowerReduction(ctx), ) - thresholdVotingPower := k.VoteThreshold(ctx).MulInt64(totalBondedPower).RoundInt() - minVoters := k.MinVoters(ctx) // Iterate through sorted keys for deterministic ordering. - orderedBallotsMap := omap.OrderedMap_Pair[types.ExchangeRateVotes](pairBallotsMap) + orderedBallotsMap := omap.OrderedMap_Pair[types.ExchangeRateVotes](pairVotes) for pair := range orderedBallotsMap.Range() { - ballots := pairBallotsMap[pair] // If pair is not whitelisted, or the ballot for it has failed, then skip // and remove it from pairBallotsMap for iteration efficiency - if _, exists := whitelistedPairs[pair]; !exists { - delete(pairBallotsMap, pair) - continue + if !whitelistedPairs.Has(pair) { + delete(pairVotes, pair) } // If the ballot is not passed, remove it from the whitelistedPairs set // to prevent slashing validators who did valid vote. - if !isPassingVoteThreshold(ballots, thresholdVotingPower, minVoters) { + if !isPassingVoteThreshold( + pairVotes[pair], + k.VoteThreshold(ctx).MulInt64(totalBondedPower).RoundInt(), + k.MinVoters(ctx), + ) { delete(whitelistedPairs, pair) - delete(pairBallotsMap, pair) + delete(pairVotes, pair) continue } } - return pairBallotsMap, whitelistedPairs + return pairVotes, whitelistedPairs } // Tally calculates the median and returns it. Sets the set of voters to be @@ -144,36 +145,40 @@ func (k Keeper) removeInvalidBallots( // ALERT: This function mutates validatorPerformances slice based on the votes // made by the validators. func Tally( - ballots types.ExchangeRateVotes, + votes types.ExchangeRateVotes, rewardBand sdk.Dec, validatorPerformances types.ValidatorPerformances, ) (sdk.Dec, types.ValidatorPerformances) { - weightedMedian := ballots.WeightedMedianWithAssertion() - standardDeviation := ballots.StandardDeviation(weightedMedian) + weightedMedian := votes.WeightedMedianWithAssertion() + standardDeviation := votes.StandardDeviation(weightedMedian) rewardSpread := weightedMedian.Mul(rewardBand.QuoInt64(2)) if standardDeviation.GT(rewardSpread) { rewardSpread = standardDeviation } - for _, ballot := range ballots { + for _, v := range votes { // Filter ballot winners & abstain voters - voteInsideSpread := ballot.ExchangeRate.GTE(weightedMedian.Sub(rewardSpread)) && - ballot.ExchangeRate.LTE(weightedMedian.Add(rewardSpread)) - isAbstainVote := !ballot.ExchangeRate.IsPositive() - isMiss := !(voteInsideSpread || isAbstainVote) - voterAddr := ballot.Voter.String() - validatorPerformance := validatorPerformances[voterAddr] + isInsideSpread := v.ExchangeRate.GTE(weightedMedian.Sub(rewardSpread)) && + v.ExchangeRate.LTE(weightedMedian.Add(rewardSpread)) + + isAbstainVote := !v.ExchangeRate.IsPositive() // strictly less than zero, don't want to include zero + isMiss := !isInsideSpread && !isAbstainVote + + validatorPerformance := validatorPerformances[v.Voter.String()] + switch { - case voteInsideSpread: - validatorPerformance.RewardWeight += ballot.Power + case isInsideSpread: + validatorPerformance.RewardWeight += v.Power validatorPerformance.WinCount++ case isMiss: validatorPerformance.MissCount++ case isAbstainVote: validatorPerformance.AbstainCount++ } - validatorPerformances[voterAddr] = validatorPerformance + + validatorPerformances[v.Voter.String()] = validatorPerformance } + return weightedMedian, validatorPerformances } diff --git a/x/oracle/keeper/ballot_test.go b/x/oracle/keeper/ballot_test.go index 4f4d23b93..8b4091170 100644 --- a/x/oracle/keeper/ballot_test.go +++ b/x/oracle/keeper/ballot_test.go @@ -65,7 +65,7 @@ func TestGroupBallotsByPair(t *testing.T) { } // organize votes by pair - ballotMap := fixture.OracleKeeper.groupBallotsByPair(fixture.Ctx, types.ValidatorPerformances{ + ballotMap := fixture.OracleKeeper.groupVotesByPair(fixture.Ctx, types.ValidatorPerformances{ ValAddrs[0].String(): { Power: power, WinCount: 0, @@ -110,14 +110,14 @@ func TestClearBallots(t *testing.T) { staking.EndBlocker(fixture.Ctx, &fixture.StakingKeeper) btcBallot := types.ExchangeRateVotes{ - types.NewExchangeRateBallot(sdk.NewDec(17), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[0], power), - types.NewExchangeRateBallot(sdk.NewDec(10), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[1], power), - types.NewExchangeRateBallot(sdk.NewDec(6), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[2], power), + types.NewExchangeRateVote(sdk.NewDec(17), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[0], power), + types.NewExchangeRateVote(sdk.NewDec(10), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[1], power), + types.NewExchangeRateVote(sdk.NewDec(6), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[2], power), } ethBallot := types.ExchangeRateVotes{ - types.NewExchangeRateBallot(sdk.NewDec(1000), asset.Registry.Pair(denoms.ETH, denoms.NUSD), ValAddrs[0], power), - types.NewExchangeRateBallot(sdk.NewDec(1300), asset.Registry.Pair(denoms.ETH, denoms.NUSD), ValAddrs[1], power), - types.NewExchangeRateBallot(sdk.NewDec(2000), asset.Registry.Pair(denoms.ETH, denoms.NUSD), ValAddrs[2], power), + types.NewExchangeRateVote(sdk.NewDec(1000), asset.Registry.Pair(denoms.ETH, denoms.NUSD), ValAddrs[0], power), + types.NewExchangeRateVote(sdk.NewDec(1300), asset.Registry.Pair(denoms.ETH, denoms.NUSD), ValAddrs[1], power), + types.NewExchangeRateVote(sdk.NewDec(2000), asset.Registry.Pair(denoms.ETH, denoms.NUSD), ValAddrs[2], power), } for i := range btcBallot { @@ -177,7 +177,7 @@ func TestFuzzTally(t *testing.T) { var rate sdk.Dec c.Fuzz(&rate) - ballot = append(ballot, types.NewExchangeRateBallot(rate, asset.NewPair(c.RandString(), c.RandString()), addr, power)) + ballot = append(ballot, types.NewExchangeRateVote(rate, asset.NewPair(c.RandString(), c.RandString()), addr, power)) } *e = ballot @@ -265,7 +265,7 @@ func TestRemoveInvalidBallots(t *testing.T) { t.Run(tc.name, func(t *testing.T) { fixture, _ := Setup(t) assert.NotPanics(t, func() { - fixture.OracleKeeper.removeInvalidBallots(fixture.Ctx, tc.voteMap) + fixture.OracleKeeper.removeInvalidVotes(fixture.Ctx, tc.voteMap) }, "voteMap: %v", tc.voteMap) }) } @@ -314,7 +314,7 @@ func TestFuzzPickReferencePair(t *testing.T) { var rate sdk.Dec c.Fuzz(&rate) - ballots = append(ballots, types.NewExchangeRateBallot(rate, pair, addr, power)) + ballots = append(ballots, types.NewExchangeRateVote(rate, pair, addr, power)) } (*e)[pair] = ballots @@ -342,15 +342,15 @@ func TestFuzzPickReferencePair(t *testing.T) { f.Fuzz(&voteMap) assert.NotPanics(t, func() { - input.OracleKeeper.removeInvalidBallots(input.Ctx, voteMap) + input.OracleKeeper.removeInvalidVotes(input.Ctx, voteMap) }, "voteMap: %v", voteMap) } func TestZeroBallotPower(t *testing.T) { btcBallots := types.ExchangeRateVotes{ - types.NewExchangeRateBallot(sdk.NewDec(17), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[0], 0), - types.NewExchangeRateBallot(sdk.NewDec(10), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[1], 0), - types.NewExchangeRateBallot(sdk.NewDec(6), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[2], 0), + types.NewExchangeRateVote(sdk.NewDec(17), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[0], 0), + types.NewExchangeRateVote(sdk.NewDec(10), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[1], 0), + types.NewExchangeRateVote(sdk.NewDec(6), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[2], 0), } assert.False(t, isPassingVoteThreshold(btcBallots, sdk.ZeroInt(), 0)) diff --git a/x/oracle/keeper/reward.go b/x/oracle/keeper/reward.go index 89a9f65dd..d5a14d11d 100644 --- a/x/oracle/keeper/reward.go +++ b/x/oracle/keeper/reward.go @@ -25,9 +25,9 @@ func (k Keeper) AllocateRewards(ctx sdk.Context, funderModule string, totalCoins return k.bankKeeper.SendCoinsFromModuleToModule(ctx, funderModule, types.ModuleName, totalCoins) } -// rewardBallotWinners gives out a portion of spread fees collected in the +// rewardWinners gives out a portion of spread fees collected in the // oracle reward pool to the oracle voters that voted faithfully. -func (k Keeper) rewardBallotWinners( +func (k Keeper) rewardWinners( ctx sdk.Context, validatorPerformances types.ValidatorPerformances, ) { diff --git a/x/oracle/keeper/update_exchange_rates.go b/x/oracle/keeper/update_exchange_rates.go index 0c907e52f..57b1b49c0 100644 --- a/x/oracle/keeper/update_exchange_rates.go +++ b/x/oracle/keeper/update_exchange_rates.go @@ -16,13 +16,13 @@ func (k Keeper) UpdateExchangeRates(ctx sdk.Context) types.ValidatorPerformances k.Logger(ctx).Info("processing validator price votes") validatorPerformances := k.newValidatorPerformances(ctx) - pairBallotsMap, whitelistedPairs := k.getPairBallotsMapAndWhitelistedPairs(ctx, validatorPerformances) + pairVotes, whitelistedPairs := k.getPairVotesAndWhitelistedPairs(ctx, validatorPerformances) - k.resetExchangeRates(ctx, pairBallotsMap) - k.countVotesAndUpdateExchangeRates(ctx, pairBallotsMap, validatorPerformances) + k.resetExchangeRates(ctx, pairVotes) + k.countVotesAndUpdateExchangeRates(ctx, pairVotes, validatorPerformances) k.registerMissedVotes(ctx, whitelistedPairs, validatorPerformances) - k.rewardBallotWinners(ctx, validatorPerformances) + k.rewardWinners(ctx, validatorPerformances) params, _ := k.Params.Get(ctx) k.clearVotesAndPreVotes(ctx, params.VotePeriod) @@ -66,16 +66,16 @@ func (k Keeper) registerAbstainsByOmission( // ExchangeRates based on the results. func (k Keeper) countVotesAndUpdateExchangeRates( ctx sdk.Context, - pairBallotsMap map[asset.Pair]types.ExchangeRateVotes, + pairVotes map[asset.Pair]types.ExchangeRateVotes, validatorPerformances types.ValidatorPerformances, ) { rewardBand := k.RewardBand(ctx) // Iterate through sorted keys for deterministic ordering. - orderedBallotsMap := omap.OrderedMap_Pair[types.ExchangeRateVotes](pairBallotsMap) - for pair := range orderedBallotsMap.Range() { - ballots := pairBallotsMap[pair] - exchangeRate, _ := Tally(ballots, rewardBand, validatorPerformances) + orderedPairVotes := omap.OrderedMap_Pair[types.ExchangeRateVotes](pairVotes) + for pair := range orderedPairVotes.Range() { + votes := pairVotes[pair] + exchangeRate, _ := Tally(votes, rewardBand, validatorPerformances) k.SetPrice(ctx, pair, exchangeRate) @@ -87,29 +87,29 @@ func (k Keeper) countVotesAndUpdateExchangeRates( } } -// getPairBallotsMapAndWhitelistedPairs returns a map of pairs and ballots excluding invalid Ballots -// and a map with all whitelisted pairs. -func (k Keeper) getPairBallotsMapAndWhitelistedPairs( +// getPairVotesAndWhitelistedPairs returns a map of pairs and votes excluding abstained votes +// and a set of all whitelisted pairs. +func (k Keeper) getPairVotesAndWhitelistedPairs( ctx sdk.Context, validatorPerformances types.ValidatorPerformances, -) (pairBallotsMap map[asset.Pair]types.ExchangeRateVotes, whitelistedPairsMap set.Set[asset.Pair]) { - pairBallotsMap = k.groupBallotsByPair(ctx, validatorPerformances) +) (pairVotes map[asset.Pair]types.ExchangeRateVotes, whitelistedPairs set.Set[asset.Pair]) { + pairVotes = k.groupVotesByPair(ctx, validatorPerformances) - return k.removeInvalidBallots(ctx, pairBallotsMap) + return k.removeInvalidVotes(ctx, pairVotes) } // resetExchangeRates removes all exchange rates from the state // We remove the price for pair with expired prices or valid ballots -func (k Keeper) resetExchangeRates(ctx sdk.Context, pairBallotsMap map[asset.Pair]types.ExchangeRateVotes) { +func (k Keeper) resetExchangeRates(ctx sdk.Context, pairVotes map[asset.Pair]types.ExchangeRateVotes) { params, _ := k.Params.Get(ctx) expirationBlocks := params.ExpirationBlocks for _, key := range k.ExchangeRates.Iterate(ctx, collections.Range[asset.Pair]{}).Keys() { - _, validBallot := pairBallotsMap[key] + _, isValid := pairVotes[key] exchangeRate, _ := k.ExchangeRates.Get(ctx, key) isExpired := exchangeRate.CreatedBlock+expirationBlocks <= uint64(ctx.BlockHeight()) - if validBallot || isExpired { + if isValid || isExpired { err := k.ExchangeRates.Delete(ctx, key) if err != nil { k.Logger(ctx).Error("failed to delete exchange rate", "pair", key.String(), "error", err) diff --git a/x/oracle/keeper/update_exchange_rates_test.go b/x/oracle/keeper/update_exchange_rates_test.go index 4ab00c15d..b77b348f9 100644 --- a/x/oracle/keeper/update_exchange_rates_test.go +++ b/x/oracle/keeper/update_exchange_rates_test.go @@ -158,7 +158,7 @@ func TestOracleTally(t *testing.T) { power = int64(0) } - vote := types.NewExchangeRateBallot( + vote := types.NewExchangeRateVote( decExchangeRate, asset.Registry.Pair(denoms.BTC, denoms.NUSD), valAddrs[i], power) ballot = append(ballot, vote) diff --git a/x/oracle/types/ballot.go b/x/oracle/types/ballot.go index b1ecce8e4..94e3fd1d8 100644 --- a/x/oracle/types/ballot.go +++ b/x/oracle/types/ballot.go @@ -23,8 +23,8 @@ type ExchangeRateVote struct { Power int64 // how much tendermint consensus power this vote should have } -// NewExchangeRateBallot returns a new ExchangeRateBallot instance -func NewExchangeRateBallot(rate sdk.Dec, pair asset.Pair, voter sdk.ValAddress, power int64) ExchangeRateVote { +// NewExchangeRateVote returns a new ExchangeRateVote instance +func NewExchangeRateVote(rate sdk.Dec, pair asset.Pair, voter sdk.ValAddress, power int64) ExchangeRateVote { return ExchangeRateVote{ ExchangeRate: rate, Pair: pair, @@ -68,10 +68,10 @@ func (pb ExchangeRateVotes) ToCrossRate(bases map[string]sdk.Dec) (cb ExchangeRa } // NumValidVoters returns the number of voters who actually voted (i.e. did not abstain from voting for a pair). -func (b ExchangeRateVotes) NumValidVoters() uint64 { +func (v ExchangeRateVotes) NumValidVoters() uint64 { count := 0 - for _, ballot := range b { - if ballot.ExchangeRate.IsPositive() { + for _, vote := range v { + if vote.ExchangeRate.IsPositive() { count++ } } @@ -79,9 +79,9 @@ func (b ExchangeRateVotes) NumValidVoters() uint64 { } // Power returns the total amount of voting power in the ballot -func (b ExchangeRateVotes) Power() int64 { +func (v ExchangeRateVotes) Power() int64 { totalPower := int64(0) - for _, vote := range b { + for _, vote := range v { totalPower += vote.Power } @@ -89,12 +89,12 @@ func (b ExchangeRateVotes) Power() int64 { } // WeightedMedian returns the median weighted by the power of the ExchangeRateVote. -// CONTRACT: ballot must be sorted -func (pb ExchangeRateVotes) WeightedMedian() sdk.Dec { - totalPower := pb.Power() - if pb.Len() > 0 { +// CONTRACT: votes must be sorted +func (votes ExchangeRateVotes) WeightedMedian() sdk.Dec { + totalPower := votes.Power() + if votes.Len() > 0 { pivot := int64(0) - for _, v := range pb { + for _, v := range votes { votePower := v.Power pivot += votePower diff --git a/x/oracle/types/ballot_test.go b/x/oracle/types/ballot_test.go index 3d739aaed..1498b6057 100644 --- a/x/oracle/types/ballot_test.go +++ b/x/oracle/types/ballot_test.go @@ -20,7 +20,7 @@ import ( "github.com/NibiruChain/nibiru/x/oracle/types" ) -func TestExchangeRateBallotsToMap(t *testing.T) { +func TestExchangeRateVotesToMap(t *testing.T) { tests := struct { votes []types.ExchangeRateVote isValid []bool @@ -93,15 +93,15 @@ func TestToCrossRate(t *testing.T) { for _, data := range data { valAddr := sdk.ValAddress(secp256k1.GenPrivKey().PubKey().Address()) if !data.base.IsZero() { - pbBase = append(pbBase, types.NewExchangeRateBallot(data.base, asset.Registry.Pair(denoms.BTC, denoms.NUSD), valAddr, 100)) + pbBase = append(pbBase, types.NewExchangeRateVote(data.base, asset.Registry.Pair(denoms.BTC, denoms.NUSD), valAddr, 100)) } - pbQuote = append(pbQuote, types.NewExchangeRateBallot(data.quote, asset.Registry.Pair(denoms.BTC, denoms.NUSD), valAddr, 100)) + pbQuote = append(pbQuote, types.NewExchangeRateVote(data.quote, asset.Registry.Pair(denoms.BTC, denoms.NUSD), valAddr, 100)) if !data.base.IsZero() && !data.quote.IsZero() { - cb = append(cb, types.NewExchangeRateBallot(data.base.Quo(data.quote), asset.Registry.Pair(denoms.BTC, denoms.NUSD), valAddr, 100)) + cb = append(cb, types.NewExchangeRateVote(data.base.Quo(data.quote), asset.Registry.Pair(denoms.BTC, denoms.NUSD), valAddr, 100)) } else { - cb = append(cb, types.NewExchangeRateBallot(sdk.ZeroDec(), asset.Registry.Pair(denoms.BTC, denoms.NUSD), valAddr, 0)) + cb = append(cb, types.NewExchangeRateVote(sdk.ZeroDec(), asset.Registry.Pair(denoms.BTC, denoms.NUSD), valAddr, 0)) } } @@ -131,7 +131,7 @@ func TestPBPower(t *testing.T) { for i := 0; i < len(sk.Validators()); i++ { power := sk.Validator(ctx, valAccAddrs[i]).GetConsensusPower(sdk.DefaultPowerReduction) - vote := types.NewExchangeRateBallot( + vote := types.NewExchangeRateVote( sdk.ZeroDec(), asset.Registry.Pair(denoms.ETH, denoms.NUSD), valAccAddrs[i], @@ -150,7 +150,7 @@ func TestPBPower(t *testing.T) { // Mix in a fake validator, the total power should not have changed. pubKey := secp256k1.GenPrivKey().PubKey() faceValAddr := sdk.ValAddress(pubKey.Address()) - fakeVote := types.NewExchangeRateBallot( + fakeVote := types.NewExchangeRateVote( sdk.OneDec(), asset.Registry.Pair(denoms.ETH, denoms.NUSD), faceValAddr, @@ -215,7 +215,7 @@ func TestPBWeightedMedian(t *testing.T) { power = 0 } - vote := types.NewExchangeRateBallot( + vote := types.NewExchangeRateVote( sdk.NewDec(int64(input)), asset.Registry.Pair(denoms.ETH, denoms.NUSD), valAddr, @@ -285,7 +285,7 @@ func TestPBStandardDeviation(t *testing.T) { power = 0 } - vote := types.NewExchangeRateBallot( + vote := types.NewExchangeRateVote( sdk.NewDecWithPrec(int64(input*base), int64(types.OracleDecPrecision)), asset.Registry.Pair(denoms.ETH, denoms.NUSD), valAddr, @@ -304,12 +304,12 @@ func TestPBStandardDeviationOverflow(t *testing.T) { exchangeRate, err := sdk.NewDecFromStr("100000000000000000000000000000000000000000000000000000000.0") require.NoError(t, err) - pb := types.ExchangeRateVotes{types.NewExchangeRateBallot( + pb := types.ExchangeRateVotes{types.NewExchangeRateVote( sdk.ZeroDec(), asset.Registry.Pair(denoms.ETH, denoms.NUSD), valAddr, 2, - ), types.NewExchangeRateBallot( + ), types.NewExchangeRateVote( exchangeRate, asset.Registry.Pair(denoms.ETH, denoms.NUSD), valAddr, From 968958d95011ef2920b6bc68b0c5837c0c514b57 Mon Sep 17 00:00:00 2001 From: Kevin Yang <5478483+k-yang@users.noreply.github.com> Date: Tue, 10 Oct 2023 16:01:33 -0500 Subject: [PATCH 18/23] rename variables --- x/oracle/keeper/ballot.go | 11 ++--- x/oracle/keeper/ballot_test.go | 16 +++--- x/oracle/keeper/reward.go | 2 +- x/oracle/keeper/slash_test.go | 2 +- x/oracle/keeper/update_exchange_rates.go | 49 +++++++++---------- x/oracle/keeper/update_exchange_rates_test.go | 2 +- x/oracle/types/ballot.go | 4 +- 7 files changed, 40 insertions(+), 46 deletions(-) diff --git a/x/oracle/keeper/ballot.go b/x/oracle/keeper/ballot.go index dfdeb428b..6e8232878 100644 --- a/x/oracle/keeper/ballot.go +++ b/x/oracle/keeper/ballot.go @@ -54,8 +54,8 @@ func (k Keeper) groupVotesByPair( return } -// clearVotesAndPreVotes clears all tallied prevotes and votes from the store -func (k Keeper) clearVotesAndPreVotes(ctx sdk.Context, votePeriod uint64) { +// clearVotesAndPrevotes clears all tallied prevotes and votes from the store +func (k Keeper) clearVotesAndPrevotes(ctx sdk.Context, votePeriod uint64) { // Clear all aggregate prevotes for _, prevote := range k.Prevotes.Iterate(ctx, collections.Range[sdk.ValAddress]{}).KeyValues() { valAddr, aggregatePrevote := prevote.Key, prevote.Value @@ -106,9 +106,8 @@ func isPassingVoteThreshold( func (k Keeper) removeInvalidVotes( ctx sdk.Context, pairVotes map[asset.Pair]types.ExchangeRateVotes, -) (map[asset.Pair]types.ExchangeRateVotes, set.Set[asset.Pair]) { - whitelistedPairs := set.New(k.GetWhitelistedPairs(ctx)...) - + whitelistedPairs set.Set[asset.Pair], +) { totalBondedPower := sdk.TokensToConsensusPower( k.StakingKeeper.TotalBondedTokens(ctx), k.StakingKeeper.PowerReduction(ctx), ) @@ -134,8 +133,6 @@ func (k Keeper) removeInvalidVotes( continue } } - - return pairVotes, whitelistedPairs } // Tally calculates the median and returns it. Sets the set of voters to be diff --git a/x/oracle/keeper/ballot_test.go b/x/oracle/keeper/ballot_test.go index 8b4091170..b92030805 100644 --- a/x/oracle/keeper/ballot_test.go +++ b/x/oracle/keeper/ballot_test.go @@ -134,7 +134,7 @@ func TestClearBallots(t *testing.T) { }, ValAddrs[i])) } - fixture.OracleKeeper.clearVotesAndPreVotes(fixture.Ctx, 10) + fixture.OracleKeeper.clearVotesAndPrevotes(fixture.Ctx, 10) prevoteCounter := len(fixture.OracleKeeper.Prevotes.Iterate(fixture.Ctx, collections.Range[sdk.ValAddress]{}).Keys()) voteCounter := len(fixture.OracleKeeper.Votes.Iterate(fixture.Ctx, collections.Range[sdk.ValAddress]{}).Keys()) @@ -143,7 +143,7 @@ func TestClearBallots(t *testing.T) { require.Equal(t, voteCounter, 0) // vote period starts at b=10, clear the votes at b=0 and below. - fixture.OracleKeeper.clearVotesAndPreVotes(fixture.Ctx.WithBlockHeight(fixture.Ctx.BlockHeight()+10), 10) + fixture.OracleKeeper.clearVotesAndPrevotes(fixture.Ctx.WithBlockHeight(fixture.Ctx.BlockHeight()+10), 10) prevoteCounter = len(fixture.OracleKeeper.Prevotes.Iterate(fixture.Ctx, collections.Range[sdk.ValAddress]{}).Keys()) require.Equal(t, prevoteCounter, 0) } @@ -265,7 +265,10 @@ func TestRemoveInvalidBallots(t *testing.T) { t.Run(tc.name, func(t *testing.T) { fixture, _ := Setup(t) assert.NotPanics(t, func() { - fixture.OracleKeeper.removeInvalidVotes(fixture.Ctx, tc.voteMap) + fixture.OracleKeeper.removeInvalidVotes(fixture.Ctx, tc.voteMap, set.New[asset.Pair]( + asset.NewPair(denoms.BTC, denoms.NUSD), + asset.NewPair(denoms.ETH, denoms.NUSD), + )) }, "voteMap: %v", tc.voteMap) }) } @@ -330,11 +333,10 @@ func TestFuzzPickReferencePair(t *testing.T) { // test OracleKeeper.Pairs.Insert voteTargets := set.Set[asset.Pair]{} f.Fuzz(&voteTargets) - whitelistedPairs := make(set.Set[string]) + whitelistedPairs := make(set.Set[asset.Pair]) for key := range voteTargets { - input.OracleKeeper.WhitelistedPairs.Insert(input.Ctx, key) - whitelistedPairs.Add(key.String()) + whitelistedPairs.Add(key) } // test OracleKeeper.RemoveInvalidBallots @@ -342,7 +344,7 @@ func TestFuzzPickReferencePair(t *testing.T) { f.Fuzz(&voteMap) assert.NotPanics(t, func() { - input.OracleKeeper.removeInvalidVotes(input.Ctx, voteMap) + input.OracleKeeper.removeInvalidVotes(input.Ctx, voteMap, whitelistedPairs) }, "voteMap: %v", voteMap) } diff --git a/x/oracle/keeper/reward.go b/x/oracle/keeper/reward.go index d5a14d11d..75c1fe751 100644 --- a/x/oracle/keeper/reward.go +++ b/x/oracle/keeper/reward.go @@ -31,7 +31,7 @@ func (k Keeper) rewardWinners( ctx sdk.Context, validatorPerformances types.ValidatorPerformances, ) { - totalRewardWeight := validatorPerformances.GetTotalRewardWeight() + totalRewardWeight := validatorPerformances.TotalRewardWeight() if totalRewardWeight == 0 { return } diff --git a/x/oracle/keeper/slash_test.go b/x/oracle/keeper/slash_test.go index 954d27a63..586dceb51 100644 --- a/x/oracle/keeper/slash_test.go +++ b/x/oracle/keeper/slash_test.go @@ -179,7 +179,7 @@ func TestWhitelistSlashing(t *testing.T) { } input.OracleKeeper.WhitelistedPairs.Insert(input.Ctx, pair) perfs := input.OracleKeeper.UpdateExchangeRates(input.Ctx) - require.EqualValues(t, 0, perfs.GetTotalRewardWeight()) + require.EqualValues(t, 0, perfs.TotalRewardWeight()) allowedMissPct := sdk.OneDec().Sub(minValidVotePeriodsPerWindow) allowedMissVotePeriods := allowedMissPct.MulInt64(votePeriodsPerSlashWindow). diff --git a/x/oracle/keeper/update_exchange_rates.go b/x/oracle/keeper/update_exchange_rates.go index 57b1b49c0..b58450a3c 100644 --- a/x/oracle/keeper/update_exchange_rates.go +++ b/x/oracle/keeper/update_exchange_rates.go @@ -14,18 +14,19 @@ import ( // UpdateExchangeRates updates the ExchangeRates, this is supposed to be executed on EndBlock. func (k Keeper) UpdateExchangeRates(ctx sdk.Context) types.ValidatorPerformances { k.Logger(ctx).Info("processing validator price votes") - validatorPerformances := k.newValidatorPerformances(ctx) - pairVotes, whitelistedPairs := k.getPairVotesAndWhitelistedPairs(ctx, validatorPerformances) + whitelistedPairs := set.New[asset.Pair](k.GetWhitelistedPairs(ctx)...) + + pairVotes := k.getPairVotesAndWhitelistedPairs(ctx, validatorPerformances, whitelistedPairs) k.resetExchangeRates(ctx, pairVotes) - k.countVotesAndUpdateExchangeRates(ctx, pairVotes, validatorPerformances) + k.tallyVotesAndUpdatePrices(ctx, pairVotes, validatorPerformances) k.registerMissedVotes(ctx, whitelistedPairs, validatorPerformances) k.rewardWinners(ctx, validatorPerformances) params, _ := k.Params.Get(ctx) - k.clearVotesAndPreVotes(ctx, params.VotePeriod) + k.clearVotesAndPrevotes(ctx, params.VotePeriod) k.updateWhitelist(ctx, params.Whitelist, whitelistedPairs) k.registerAbstainsByOmission(ctx, len(params.Whitelist), validatorPerformances) return validatorPerformances @@ -42,7 +43,9 @@ func (k Keeper) registerMissedVotes( if int(validatorPerformance.MissCount) > 0 { k.MissCounters.Insert( ctx, validatorPerformance.ValAddress, - k.MissCounters.GetOr(ctx, validatorPerformance.ValAddress, 0)+1) + k.MissCounters.GetOr(ctx, validatorPerformance.ValAddress, 0)+uint64(validatorPerformance.MissCount), + ) + k.Logger(ctx).Info("vote miss", "validator", validatorPerformance.ValAddress.String()) } } @@ -50,40 +53,29 @@ func (k Keeper) registerMissedVotes( func (k Keeper) registerAbstainsByOmission( ctx sdk.Context, - numMarkets int, - perfs types.ValidatorPerformances, + numPairs int, + validatorPerformances types.ValidatorPerformances, ) { - for valAddr, perf := range perfs { - omitCount := int64(numMarkets) - (perf.WinCount + perf.AbstainCount + perf.MissCount) + for valAddr, performance := range validatorPerformances { + omitCount := int64(numPairs) - (performance.WinCount + performance.AbstainCount + performance.MissCount) if omitCount > 0 { - perf.AbstainCount += omitCount - perfs[valAddr] = perf + performance.AbstainCount += omitCount + validatorPerformances[valAddr] = performance } } } -// countVotesAndUpdateExchangeRates processes the votes and updates the -// ExchangeRates based on the results. -func (k Keeper) countVotesAndUpdateExchangeRates( +// tallyVotesAndUpdatePrices processes the votes and updates the ExchangeRates based on the results. +func (k Keeper) tallyVotesAndUpdatePrices( ctx sdk.Context, pairVotes map[asset.Pair]types.ExchangeRateVotes, validatorPerformances types.ValidatorPerformances, ) { - rewardBand := k.RewardBand(ctx) - // Iterate through sorted keys for deterministic ordering. orderedPairVotes := omap.OrderedMap_Pair[types.ExchangeRateVotes](pairVotes) for pair := range orderedPairVotes.Range() { - votes := pairVotes[pair] - exchangeRate, _ := Tally(votes, rewardBand, validatorPerformances) - + exchangeRate, _ := Tally(pairVotes[pair], k.RewardBand(ctx), validatorPerformances) k.SetPrice(ctx, pair, exchangeRate) - - _ = ctx.EventManager().EmitTypedEvent(&types.EventPriceUpdate{ - Pair: pair.String(), - Price: exchangeRate, - TimestampMs: ctx.BlockTime().UnixMilli(), - }) } } @@ -92,10 +84,13 @@ func (k Keeper) countVotesAndUpdateExchangeRates( func (k Keeper) getPairVotesAndWhitelistedPairs( ctx sdk.Context, validatorPerformances types.ValidatorPerformances, -) (pairVotes map[asset.Pair]types.ExchangeRateVotes, whitelistedPairs set.Set[asset.Pair]) { + whitelistedPairs set.Set[asset.Pair], +) (pairVotes map[asset.Pair]types.ExchangeRateVotes) { pairVotes = k.groupVotesByPair(ctx, validatorPerformances) - return k.removeInvalidVotes(ctx, pairVotes) + k.removeInvalidVotes(ctx, pairVotes, whitelistedPairs) + + return pairVotes } // resetExchangeRates removes all exchange rates from the state diff --git a/x/oracle/keeper/update_exchange_rates_test.go b/x/oracle/keeper/update_exchange_rates_test.go index b77b348f9..66914d221 100644 --- a/x/oracle/keeper/update_exchange_rates_test.go +++ b/x/oracle/keeper/update_exchange_rates_test.go @@ -212,7 +212,7 @@ func TestOracleTally(t *testing.T) { assert.Equal(t, expectedValidatorClaimMap, validatorClaimMap) assert.Equal(t, tallyMedian.MulInt64(100).TruncateInt(), weightedMedian.MulInt64(100).TruncateInt()) - assert.NotEqualValues(t, 0, perfs.GetTotalRewardWeight(), perfs.String()) + assert.NotEqualValues(t, 0, perfs.TotalRewardWeight(), perfs.String()) } func TestOracleRewardBand(t *testing.T) { diff --git a/x/oracle/types/ballot.go b/x/oracle/types/ballot.go index 94e3fd1d8..c68246d70 100644 --- a/x/oracle/types/ballot.go +++ b/x/oracle/types/ballot.go @@ -199,8 +199,8 @@ func NewValidatorPerformance(power int64, recipient sdk.ValAddress) ValidatorPer type ValidatorPerformances map[string]ValidatorPerformance -// GetTotalRewardWeight returns the sum of the reward weight of all the validators included in the map -func (vp ValidatorPerformances) GetTotalRewardWeight() int64 { +// TotalRewardWeight returns the sum of the reward weight of all the validators included in the map +func (vp ValidatorPerformances) TotalRewardWeight() int64 { totalRewardWeight := int64(0) for _, validator := range vp { totalRewardWeight += validator.RewardWeight From d1467ff52aff084f6b59a4a5dc025e09218ce65d Mon Sep 17 00:00:00 2001 From: Kevin Yang <5478483+k-yang@users.noreply.github.com> Date: Tue, 10 Oct 2023 16:13:19 -0500 Subject: [PATCH 19/23] rename variables --- x/oracle/keeper/ballot.go | 44 ++++++++++--------- x/oracle/keeper/update_exchange_rates.go | 15 +++---- x/oracle/keeper/update_exchange_rates_test.go | 12 ++--- 3 files changed, 36 insertions(+), 35 deletions(-) diff --git a/x/oracle/keeper/ballot.go b/x/oracle/keeper/ballot.go index 6e8232878..674134f78 100644 --- a/x/oracle/keeper/ballot.go +++ b/x/oracle/keeper/ballot.go @@ -29,25 +29,28 @@ func (k Keeper) groupVotesByPair( for _, value := range k.Votes.Iterate(ctx, collections.Range[sdk.ValAddress]{}).KeyValues() { voterAddr, aggregateVote := value.Key, value.Value - // organize ballot only for the active validators - if validatorPerformance, exists := validatorPerformances[aggregateVote.Voter]; exists { - for _, exchangeRateTuple := range aggregateVote.ExchangeRateTuples { - power := validatorPerformance.Power - if !exchangeRateTuple.ExchangeRate.IsPositive() { - // Make the power of abstain vote zero - power = 0 - } - - pairVotes[exchangeRateTuple.Pair] = append( - pairVotes[exchangeRateTuple.Pair], - types.NewExchangeRateVote( - exchangeRateTuple.ExchangeRate, - exchangeRateTuple.Pair, - voterAddr, - power, - ), - ) + // skip votes from inactive validators + validatorPerformance, exists := validatorPerformances[aggregateVote.Voter] + if !exists { + continue + } + + for _, tuple := range aggregateVote.ExchangeRateTuples { + power := validatorPerformance.Power + if !tuple.ExchangeRate.IsPositive() { + // Make the power of abstain vote zero + power = 0 } + + pairVotes[tuple.Pair] = append( + pairVotes[tuple.Pair], + types.NewExchangeRateVote( + tuple.ExchangeRate, + tuple.Pair, + voterAddr, + power, + ), + ) } } @@ -145,7 +148,7 @@ func Tally( votes types.ExchangeRateVotes, rewardBand sdk.Dec, validatorPerformances types.ValidatorPerformances, -) (sdk.Dec, types.ValidatorPerformances) { +) sdk.Dec { weightedMedian := votes.WeightedMedianWithAssertion() standardDeviation := votes.StandardDeviation(weightedMedian) rewardSpread := weightedMedian.Mul(rewardBand.QuoInt64(2)) @@ -158,7 +161,6 @@ func Tally( // Filter ballot winners & abstain voters isInsideSpread := v.ExchangeRate.GTE(weightedMedian.Sub(rewardSpread)) && v.ExchangeRate.LTE(weightedMedian.Add(rewardSpread)) - isAbstainVote := !v.ExchangeRate.IsPositive() // strictly less than zero, don't want to include zero isMiss := !isInsideSpread && !isAbstainVote @@ -177,5 +179,5 @@ func Tally( validatorPerformances[v.Voter.String()] = validatorPerformance } - return weightedMedian, validatorPerformances + return weightedMedian } diff --git a/x/oracle/keeper/update_exchange_rates.go b/x/oracle/keeper/update_exchange_rates.go index b58450a3c..e967a7e14 100644 --- a/x/oracle/keeper/update_exchange_rates.go +++ b/x/oracle/keeper/update_exchange_rates.go @@ -17,7 +17,7 @@ func (k Keeper) UpdateExchangeRates(ctx sdk.Context) types.ValidatorPerformances validatorPerformances := k.newValidatorPerformances(ctx) whitelistedPairs := set.New[asset.Pair](k.GetWhitelistedPairs(ctx)...) - pairVotes := k.getPairVotesAndWhitelistedPairs(ctx, validatorPerformances, whitelistedPairs) + pairVotes := k.getPairVotes(ctx, validatorPerformances, whitelistedPairs) k.resetExchangeRates(ctx, pairVotes) k.tallyVotesAndUpdatePrices(ctx, pairVotes, validatorPerformances) @@ -71,17 +71,17 @@ func (k Keeper) tallyVotesAndUpdatePrices( pairVotes map[asset.Pair]types.ExchangeRateVotes, validatorPerformances types.ValidatorPerformances, ) { + rewardBand := k.RewardBand(ctx) // Iterate through sorted keys for deterministic ordering. orderedPairVotes := omap.OrderedMap_Pair[types.ExchangeRateVotes](pairVotes) for pair := range orderedPairVotes.Range() { - exchangeRate, _ := Tally(pairVotes[pair], k.RewardBand(ctx), validatorPerformances) + exchangeRate := Tally(pairVotes[pair], rewardBand, validatorPerformances) k.SetPrice(ctx, pair, exchangeRate) } } -// getPairVotesAndWhitelistedPairs returns a map of pairs and votes excluding abstained votes -// and a set of all whitelisted pairs. -func (k Keeper) getPairVotesAndWhitelistedPairs( +// getPairVotes returns a map of pairs and votes excluding abstained votes and ballots that don't meet the threshold criteria +func (k Keeper) getPairVotes( ctx sdk.Context, validatorPerformances types.ValidatorPerformances, whitelistedPairs set.Set[asset.Pair], @@ -97,12 +97,11 @@ func (k Keeper) getPairVotesAndWhitelistedPairs( // We remove the price for pair with expired prices or valid ballots func (k Keeper) resetExchangeRates(ctx sdk.Context, pairVotes map[asset.Pair]types.ExchangeRateVotes) { params, _ := k.Params.Get(ctx) - expirationBlocks := params.ExpirationBlocks for _, key := range k.ExchangeRates.Iterate(ctx, collections.Range[asset.Pair]{}).Keys() { _, isValid := pairVotes[key] - exchangeRate, _ := k.ExchangeRates.Get(ctx, key) - isExpired := exchangeRate.CreatedBlock+expirationBlocks <= uint64(ctx.BlockHeight()) + previousExchangeRate, _ := k.ExchangeRates.Get(ctx, key) + isExpired := previousExchangeRate.CreatedBlock+params.ExpirationBlocks <= uint64(ctx.BlockHeight()) if isValid || isExpired { err := k.ExchangeRates.Delete(ctx, key) diff --git a/x/oracle/keeper/update_exchange_rates_test.go b/x/oracle/keeper/update_exchange_rates_test.go index 66914d221..58ae565b3 100644 --- a/x/oracle/keeper/update_exchange_rates_test.go +++ b/x/oracle/keeper/update_exchange_rates_test.go @@ -168,9 +168,9 @@ func TestOracleTally(t *testing.T) { } } - validatorClaimMap := make(types.ValidatorPerformances) + validatorPerformances := make(types.ValidatorPerformances) for _, valAddr := range valAddrs { - validatorClaimMap[valAddr.String()] = types.NewValidatorPerformance( + validatorPerformances[valAddr.String()] = types.NewValidatorPerformance( stakingKeeper.Validator(fixture.Ctx, valAddr).GetConsensusPower(sdk.DefaultPowerReduction), valAddr, ) @@ -207,12 +207,12 @@ func TestOracleTally(t *testing.T) { expectedValidatorClaimMap[key] = claim } - tallyMedian, perfs := Tally( - ballot, fixture.OracleKeeper.RewardBand(fixture.Ctx), validatorClaimMap) + tallyMedian := Tally( + ballot, fixture.OracleKeeper.RewardBand(fixture.Ctx), validatorPerformances) - assert.Equal(t, expectedValidatorClaimMap, validatorClaimMap) + assert.Equal(t, expectedValidatorClaimMap, validatorPerformances) assert.Equal(t, tallyMedian.MulInt64(100).TruncateInt(), weightedMedian.MulInt64(100).TruncateInt()) - assert.NotEqualValues(t, 0, perfs.TotalRewardWeight(), perfs.String()) + assert.NotEqualValues(t, 0, validatorPerformances.TotalRewardWeight(), validatorPerformances.String()) } func TestOracleRewardBand(t *testing.T) { From 32c34cbb65b13cfa27285ad69e7040f473d5254e Mon Sep 17 00:00:00 2001 From: Kevin Yang <5478483+k-yang@users.noreply.github.com> Date: Tue, 10 Oct 2023 16:22:38 -0500 Subject: [PATCH 20/23] rename variables --- x/oracle/keeper/slash_test.go | 12 ++++++------ x/oracle/keeper/update_exchange_rates.go | 19 ++++++++++--------- x/oracle/keeper/update_exchange_rates_test.go | 8 ++++---- x/oracle/keeper/whitelist.go | 4 ++-- x/oracle/keeper/whitelist_test.go | 6 +++--- 5 files changed, 25 insertions(+), 24 deletions(-) diff --git a/x/oracle/keeper/slash_test.go b/x/oracle/keeper/slash_test.go index 586dceb51..6a99b8e89 100644 --- a/x/oracle/keeper/slash_test.go +++ b/x/oracle/keeper/slash_test.go @@ -231,12 +231,12 @@ func TestNotPassedBallotSlashing(t *testing.T) { func TestAbstainSlashing(t *testing.T) { input, h := Setup(t) + + // reset whitelisted pairs params, err := input.OracleKeeper.Params.Get(input.Ctx) require.NoError(t, err) params.Whitelist = []asset.Pair{asset.Registry.Pair(denoms.NIBI, denoms.NUSD)} input.OracleKeeper.Params.Set(input.Ctx, params) - - // clear tobin tax to reset vote targets for _, p := range input.OracleKeeper.WhitelistedPairs.Iterate(input.Ctx, collections.Range[asset.Pair]{}).Keys() { input.OracleKeeper.WhitelistedPairs.Delete(input.Ctx, p) } @@ -248,13 +248,13 @@ func TestAbstainSlashing(t *testing.T) { for i := uint64(0); i <= uint64(sdk.OneDec().Sub(minValidPerWindow).MulInt64(votePeriodsPerWindow).TruncateInt64()); i++ { input.Ctx = input.Ctx.WithBlockHeight(input.Ctx.BlockHeight() + 1) - // Account 1, govstable + // Account 1, NIBI/NUSD MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate}}, 0) - // Account 2, govstable, abstain vote - MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: sdk.ZeroDec()}}, 1) + // Account 2, NIBI/NUSD, abstain vote + MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: sdk.OneDec().Neg()}}, 1) - // Account 3, govstable + // Account 3, NIBI/NUSD MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: testExchangeRate}}, 2) input.OracleKeeper.UpdateExchangeRates(input.Ctx) diff --git a/x/oracle/keeper/update_exchange_rates.go b/x/oracle/keeper/update_exchange_rates.go index e967a7e14..0369ee15b 100644 --- a/x/oracle/keeper/update_exchange_rates.go +++ b/x/oracle/keeper/update_exchange_rates.go @@ -19,22 +19,23 @@ func (k Keeper) UpdateExchangeRates(ctx sdk.Context) types.ValidatorPerformances pairVotes := k.getPairVotes(ctx, validatorPerformances, whitelistedPairs) - k.resetExchangeRates(ctx, pairVotes) + k.clearExchangeRates(ctx, pairVotes) k.tallyVotesAndUpdatePrices(ctx, pairVotes, validatorPerformances) - k.registerMissedVotes(ctx, whitelistedPairs, validatorPerformances) + k.incrementMissCounters(ctx, whitelistedPairs, validatorPerformances) + k.incrementAbstainsByOmission(ctx, len(whitelistedPairs), validatorPerformances) + k.rewardWinners(ctx, validatorPerformances) params, _ := k.Params.Get(ctx) k.clearVotesAndPrevotes(ctx, params.VotePeriod) - k.updateWhitelist(ctx, params.Whitelist, whitelistedPairs) - k.registerAbstainsByOmission(ctx, len(params.Whitelist), validatorPerformances) + k.refreshWhitelist(ctx, params.Whitelist, whitelistedPairs) return validatorPerformances } -// registerMissedVotes it parses all validators performance and increases the +// incrementMissCounters it parses all validators performance and increases the // missed vote of those that did not vote. -func (k Keeper) registerMissedVotes( +func (k Keeper) incrementMissCounters( ctx sdk.Context, whitelistedPairs set.Set[asset.Pair], validatorPerformances types.ValidatorPerformances, @@ -51,7 +52,7 @@ func (k Keeper) registerMissedVotes( } } -func (k Keeper) registerAbstainsByOmission( +func (k Keeper) incrementAbstainsByOmission( ctx sdk.Context, numPairs int, validatorPerformances types.ValidatorPerformances, @@ -93,9 +94,9 @@ func (k Keeper) getPairVotes( return pairVotes } -// resetExchangeRates removes all exchange rates from the state +// clearExchangeRates removes all exchange rates from the state // We remove the price for pair with expired prices or valid ballots -func (k Keeper) resetExchangeRates(ctx sdk.Context, pairVotes map[asset.Pair]types.ExchangeRateVotes) { +func (k Keeper) clearExchangeRates(ctx sdk.Context, pairVotes map[asset.Pair]types.ExchangeRateVotes) { params, _ := k.Params.Get(ctx) for _, key := range k.ExchangeRates.Iterate(ctx, collections.Range[asset.Pair]{}).Keys() { diff --git a/x/oracle/keeper/update_exchange_rates_test.go b/x/oracle/keeper/update_exchange_rates_test.go index 58ae565b3..f8b1b1f33 100644 --- a/x/oracle/keeper/update_exchange_rates_test.go +++ b/x/oracle/keeper/update_exchange_rates_test.go @@ -91,7 +91,7 @@ func TestOracleThreshold(t *testing.T) { assert.Error(t, err) } -func TestResetExchangeRates(t *testing.T) { +func TestClearExchangeRates(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) fixture, _ := Setup(t) @@ -108,13 +108,13 @@ func TestResetExchangeRates(t *testing.T) { // reset exchange rates at block 2 // Price should still be there because not expired yet - fixture.OracleKeeper.resetExchangeRates(fixture.Ctx.WithBlockHeight(2), emptyBallot) + fixture.OracleKeeper.clearExchangeRates(fixture.Ctx.WithBlockHeight(2), emptyBallot) _, err := fixture.OracleKeeper.ExchangeRates.Get(fixture.Ctx, pair) assert.NoError(t, err) // reset exchange rates at block 3 but pair is in ballot // Price should be removed there because there was a valid ballot - fixture.OracleKeeper.resetExchangeRates(fixture.Ctx.WithBlockHeight(3), validBallot) + fixture.OracleKeeper.clearExchangeRates(fixture.Ctx.WithBlockHeight(3), validBallot) _, err = fixture.OracleKeeper.ExchangeRates.Get(fixture.Ctx, pair) assert.Error(t, err) @@ -122,7 +122,7 @@ func TestResetExchangeRates(t *testing.T) { // reset exchange rates at block 79 // Price should not be there anymore because expired fixture.OracleKeeper.SetPrice(fixture.Ctx.WithBlockHeight(69), pair, testExchangeRate) - fixture.OracleKeeper.resetExchangeRates(fixture.Ctx.WithBlockHeight(79), emptyBallot) + fixture.OracleKeeper.clearExchangeRates(fixture.Ctx.WithBlockHeight(79), emptyBallot) _, err = fixture.OracleKeeper.ExchangeRates.Get(fixture.Ctx, pair) assert.Error(t, err) diff --git a/x/oracle/keeper/whitelist.go b/x/oracle/keeper/whitelist.go index 636506701..f1662c19e 100644 --- a/x/oracle/keeper/whitelist.go +++ b/x/oracle/keeper/whitelist.go @@ -19,9 +19,9 @@ func (k Keeper) GetWhitelistedPairs(ctx sdk.Context) []asset.Pair { return k.WhitelistedPairs.Iterate(ctx, collections.Range[asset.Pair]{}).Keys() } -// updateWhitelist updates the whitelist by detecting possible changes between +// refreshWhitelist updates the whitelist by detecting possible changes between // the current vote targets and the current updated whitelist. -func (k Keeper) updateWhitelist(ctx sdk.Context, nextWhitelist []asset.Pair, currentWhitelist set.Set[asset.Pair]) { +func (k Keeper) refreshWhitelist(ctx sdk.Context, nextWhitelist []asset.Pair, currentWhitelist set.Set[asset.Pair]) { updateRequired := false if len(currentWhitelist) != len(nextWhitelist) { diff --git a/x/oracle/keeper/whitelist_test.go b/x/oracle/keeper/whitelist_test.go index 95dbed286..6061d73e3 100644 --- a/x/oracle/keeper/whitelist_test.go +++ b/x/oracle/keeper/whitelist_test.go @@ -106,7 +106,7 @@ func TestUpdateWhitelist(t *testing.T) { sort.Slice(whitelistSlice, func(i, j int) bool { return whitelistSlice[i].String() < whitelistSlice[j].String() }) - fixture.OracleKeeper.updateWhitelist(fixture.Ctx, whitelistSlice, currentWhitelist) + fixture.OracleKeeper.refreshWhitelist(fixture.Ctx, whitelistSlice, currentWhitelist) assert.Equal(t, whitelistSlice, fixture.OracleKeeper.GetWhitelistedPairs(fixture.Ctx)) // len update (fast path) @@ -115,7 +115,7 @@ func TestUpdateWhitelist(t *testing.T) { sort.Slice(whitelistSlice, func(i, j int) bool { return whitelistSlice[i].String() < whitelistSlice[j].String() }) - fixture.OracleKeeper.updateWhitelist(fixture.Ctx, whitelistSlice, currentWhitelist) + fixture.OracleKeeper.refreshWhitelist(fixture.Ctx, whitelistSlice, currentWhitelist) assert.Equal(t, whitelistSlice, fixture.OracleKeeper.GetWhitelistedPairs(fixture.Ctx)) // diff update (slow path) @@ -124,6 +124,6 @@ func TestUpdateWhitelist(t *testing.T) { sort.Slice(whitelistSlice, func(i, j int) bool { return whitelistSlice[i].String() < whitelistSlice[j].String() }) - fixture.OracleKeeper.updateWhitelist(fixture.Ctx, whitelistSlice, currentWhitelist) + fixture.OracleKeeper.refreshWhitelist(fixture.Ctx, whitelistSlice, currentWhitelist) assert.Equal(t, whitelistSlice, fixture.OracleKeeper.GetWhitelistedPairs(fixture.Ctx)) } From 51b18cc3b3d519967f8e61aff314c5445ebf4913 Mon Sep 17 00:00:00 2001 From: Kevin Yang <5478483+k-yang@users.noreply.github.com> Date: Tue, 10 Oct 2023 16:28:58 -0500 Subject: [PATCH 21/23] fix TestWhitelistedPairs --- x/oracle/keeper/update_exchange_rates_test.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/x/oracle/keeper/update_exchange_rates_test.go b/x/oracle/keeper/update_exchange_rates_test.go index f8b1b1f33..82621feff 100644 --- a/x/oracle/keeper/update_exchange_rates_test.go +++ b/x/oracle/keeper/update_exchange_rates_test.go @@ -91,7 +91,7 @@ func TestOracleThreshold(t *testing.T) { assert.Error(t, err) } -func TestClearExchangeRates(t *testing.T) { +func TestResetExchangeRates(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) fixture, _ := Setup(t) @@ -454,13 +454,13 @@ func TestWhitelistedPairs(t *testing.T) { for valIdx := 0; valIdx < 4; valIdx++ { perf := perfs[ValAddrs[valIdx].String()] assert.EqualValues(t, 1, perf.WinCount) - assert.EqualValues(t, 0, perf.AbstainCount) + assert.EqualValues(t, 1, perf.AbstainCount) assert.EqualValues(t, 0, perf.MissCount) } t.Log("validators 4 didn't vote -> expect abstain") perf := perfs[ValAddrs[4].String()] assert.EqualValues(t, 0, perf.WinCount) - assert.EqualValues(t, 1, perf.AbstainCount) + assert.EqualValues(t, 2, perf.AbstainCount) assert.EqualValues(t, 0, perf.MissCount) t.Log("btc:nusd must be deleted") @@ -469,17 +469,22 @@ func TestWhitelistedPairs(t *testing.T) { require.False(t, fixture.OracleKeeper.WhitelistedPairs.Has( fixture.Ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD))) - t.Log("vote from vals 0-2 on nibi:nusd (same vote period)") + t.Log("vote from vals 0-3 on nibi:nusd") priceVoteFromVal(0, block) priceVoteFromVal(1, block) priceVoteFromVal(2, block) + priceVoteFromVal(3, block) perfs = fixture.OracleKeeper.UpdateExchangeRates(fixture.Ctx) t.Log("Although validators 0-2 voted, it's for the same period -> expect abstains for everyone") - for valIdx := 0; valIdx < 5; valIdx++ { + for valIdx := 0; valIdx < 4; valIdx++ { perf := perfs[ValAddrs[valIdx].String()] - assert.EqualValues(t, 0, perf.WinCount) - assert.EqualValues(t, 1, perf.AbstainCount) + assert.EqualValues(t, 1, perf.WinCount) + assert.EqualValues(t, 0, perf.AbstainCount) assert.EqualValues(t, 0, perf.MissCount) } + perf = perfs[ValAddrs[4].String()] + assert.EqualValues(t, 0, perf.WinCount) + assert.EqualValues(t, 1, perf.AbstainCount) + assert.EqualValues(t, 0, perf.MissCount) } From 1b48b9010a3f05d210836baa4a1b33248614c884 Mon Sep 17 00:00:00 2001 From: Kevin Yang <5478483+k-yang@users.noreply.github.com> Date: Tue, 10 Oct 2023 16:32:52 -0500 Subject: [PATCH 22/23] rename variables --- x/oracle/keeper/update_exchange_rates_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/x/oracle/keeper/update_exchange_rates_test.go b/x/oracle/keeper/update_exchange_rates_test.go index 82621feff..c279f61ac 100644 --- a/x/oracle/keeper/update_exchange_rates_test.go +++ b/x/oracle/keeper/update_exchange_rates_test.go @@ -184,9 +184,9 @@ func TestOracleTally(t *testing.T) { maxSpread = standardDeviation } - expectedValidatorClaimMap := make(types.ValidatorPerformances) + expectedValidatorPerformances := make(types.ValidatorPerformances) for _, valAddr := range valAddrs { - expectedValidatorClaimMap[valAddr.String()] = types.NewValidatorPerformance( + expectedValidatorPerformances[valAddr.String()] = types.NewValidatorPerformance( stakingKeeper.Validator(fixture.Ctx, valAddr).GetConsensusPower(sdk.DefaultPowerReduction), valAddr, ) @@ -194,23 +194,23 @@ func TestOracleTally(t *testing.T) { for _, vote := range ballot { key := vote.Voter.String() - claim := expectedValidatorClaimMap[key] + validatorPerformance := expectedValidatorPerformances[key] if vote.ExchangeRate.GTE(weightedMedian.Sub(maxSpread)) && vote.ExchangeRate.LTE(weightedMedian.Add(maxSpread)) { - claim.RewardWeight += vote.Power - claim.WinCount++ + validatorPerformance.RewardWeight += vote.Power + validatorPerformance.WinCount++ } else if !vote.ExchangeRate.IsPositive() { - claim.AbstainCount++ + validatorPerformance.AbstainCount++ } else { - claim.MissCount++ + validatorPerformance.MissCount++ } - expectedValidatorClaimMap[key] = claim + expectedValidatorPerformances[key] = validatorPerformance } tallyMedian := Tally( ballot, fixture.OracleKeeper.RewardBand(fixture.Ctx), validatorPerformances) - assert.Equal(t, expectedValidatorClaimMap, validatorPerformances) + assert.Equal(t, expectedValidatorPerformances, validatorPerformances) assert.Equal(t, tallyMedian.MulInt64(100).TruncateInt(), weightedMedian.MulInt64(100).TruncateInt()) assert.NotEqualValues(t, 0, validatorPerformances.TotalRewardWeight(), validatorPerformances.String()) } From af92024a4c2ca141e363544b1fd8265889066195 Mon Sep 17 00:00:00 2001 From: Kevin Yang <5478483+k-yang@users.noreply.github.com> Date: Tue, 10 Oct 2023 16:37:57 -0500 Subject: [PATCH 23/23] rename ballots to votes --- x/oracle/keeper/ballot.go | 14 ++-- x/oracle/keeper/ballot_test.go | 74 +++++++++---------- x/oracle/keeper/params.go | 6 +- x/oracle/keeper/update_exchange_rates.go | 4 +- x/oracle/keeper/update_exchange_rates_test.go | 28 +++---- x/oracle/types/ballot.go | 4 +- x/oracle/types/ballot_test.go | 12 +-- x/oracle/types/test_utils.go | 2 +- 8 files changed, 72 insertions(+), 72 deletions(-) diff --git a/x/oracle/keeper/ballot.go b/x/oracle/keeper/ballot.go index 674134f78..7af68d545 100644 --- a/x/oracle/keeper/ballot.go +++ b/x/oracle/keeper/ballot.go @@ -79,7 +79,7 @@ func (k Keeper) clearVotesAndPrevotes(ctx sdk.Context, votePeriod uint64) { } } -// isPassingVoteThreshold ballot is passing the threshold amount of voting power +// isPassingVoteThreshold votes is passing the threshold amount of voting power func isPassingVoteThreshold( votes types.ExchangeRateVotes, thresholdVotingPower sdkmath.Int, minVoters uint64, ) bool { @@ -99,7 +99,7 @@ func isPassingVoteThreshold( return true } -// removeInvalidVotes removes the ballots which have not reached the vote +// removeInvalidVotes removes the votes which have not reached the vote // threshold or which are not part of the whitelisted pairs anymore: example // when params change during a vote period but some votes were already made. // @@ -116,15 +116,15 @@ func (k Keeper) removeInvalidVotes( ) // Iterate through sorted keys for deterministic ordering. - orderedBallotsMap := omap.OrderedMap_Pair[types.ExchangeRateVotes](pairVotes) - for pair := range orderedBallotsMap.Range() { - // If pair is not whitelisted, or the ballot for it has failed, then skip + orderedPairVotes := omap.OrderedMap_Pair[types.ExchangeRateVotes](pairVotes) + for pair := range orderedPairVotes.Range() { + // If pair is not whitelisted, or the votes for it has failed, then skip // and remove it from pairBallotsMap for iteration efficiency if !whitelistedPairs.Has(pair) { delete(pairVotes, pair) } - // If the ballot is not passed, remove it from the whitelistedPairs set + // If the votes is not passed, remove it from the whitelistedPairs set // to prevent slashing validators who did valid vote. if !isPassingVoteThreshold( pairVotes[pair], @@ -158,7 +158,7 @@ func Tally( } for _, v := range votes { - // Filter ballot winners & abstain voters + // Filter votes winners & abstain voters isInsideSpread := v.ExchangeRate.GTE(weightedMedian.Sub(rewardSpread)) && v.ExchangeRate.LTE(weightedMedian.Add(rewardSpread)) isAbstainVote := !v.ExchangeRate.IsPositive() // strictly less than zero, don't want to include zero diff --git a/x/oracle/keeper/ballot_test.go b/x/oracle/keeper/ballot_test.go index b92030805..b66e78033 100644 --- a/x/oracle/keeper/ballot_test.go +++ b/x/oracle/keeper/ballot_test.go @@ -21,7 +21,7 @@ import ( "github.com/NibiruChain/nibiru/x/oracle/types" ) -func TestGroupBallotsByPair(t *testing.T) { +func TestGroupVotesByPair(t *testing.T) { fixture := CreateTestFixture(t) power := int64(100) @@ -39,25 +39,25 @@ func TestGroupBallotsByPair(t *testing.T) { pairBtc := asset.Registry.Pair(denoms.BTC, denoms.NUSD) pairEth := asset.Registry.Pair(denoms.ETH, denoms.NUSD) - btcBallots := types.ExchangeRateVotes{ + btcVotes := types.ExchangeRateVotes{ {Pair: pairBtc, ExchangeRate: sdk.NewDec(17), Voter: ValAddrs[0], Power: power}, {Pair: pairBtc, ExchangeRate: sdk.NewDec(10), Voter: ValAddrs[1], Power: power}, {Pair: pairBtc, ExchangeRate: sdk.NewDec(6), Voter: ValAddrs[2], Power: power}, } - ethBallots := types.ExchangeRateVotes{ + ethVotes := types.ExchangeRateVotes{ {Pair: pairEth, ExchangeRate: sdk.NewDec(1_000), Voter: ValAddrs[0], Power: power}, {Pair: pairEth, ExchangeRate: sdk.NewDec(1_300), Voter: ValAddrs[1], Power: power}, {Pair: pairEth, ExchangeRate: sdk.NewDec(2_000), Voter: ValAddrs[2], Power: power}, } - for i, ballot := range btcBallots { + for i, v := range btcVotes { fixture.OracleKeeper.Votes.Insert( fixture.Ctx, ValAddrs[i], types.NewAggregateExchangeRateVote( types.ExchangeRateTuples{ - {Pair: ballot.Pair, ExchangeRate: ballot.ExchangeRate}, - {Pair: ethBallots[i].Pair, ExchangeRate: ethBallots[i].ExchangeRate}, + {Pair: v.Pair, ExchangeRate: v.ExchangeRate}, + {Pair: ethVotes[i].Pair, ExchangeRate: ethVotes[i].ExchangeRate}, }, ValAddrs[i], ), @@ -65,7 +65,7 @@ func TestGroupBallotsByPair(t *testing.T) { } // organize votes by pair - ballotMap := fixture.OracleKeeper.groupVotesByPair(fixture.Ctx, types.ValidatorPerformances{ + pairVotes := fixture.OracleKeeper.groupVotesByPair(fixture.Ctx, types.ValidatorPerformances{ ValAddrs[0].String(): { Power: power, WinCount: 0, @@ -83,17 +83,17 @@ func TestGroupBallotsByPair(t *testing.T) { }, }) - // sort each ballot for comparison - sort.Sort(btcBallots) - sort.Sort(ethBallots) - sort.Sort(ballotMap[asset.Registry.Pair(denoms.BTC, denoms.NUSD)]) - sort.Sort(ballotMap[asset.Registry.Pair(denoms.ETH, denoms.NUSD)]) + // sort each votes for comparison + sort.Sort(btcVotes) + sort.Sort(ethVotes) + sort.Sort(pairVotes[asset.Registry.Pair(denoms.BTC, denoms.NUSD)]) + sort.Sort(pairVotes[asset.Registry.Pair(denoms.ETH, denoms.NUSD)]) - require.Equal(t, btcBallots, ballotMap[asset.Registry.Pair(denoms.BTC, denoms.NUSD)]) - require.Equal(t, ethBallots, ballotMap[asset.Registry.Pair(denoms.ETH, denoms.NUSD)]) + require.Equal(t, btcVotes, pairVotes[asset.Registry.Pair(denoms.BTC, denoms.NUSD)]) + require.Equal(t, ethVotes, pairVotes[asset.Registry.Pair(denoms.ETH, denoms.NUSD)]) } -func TestClearBallots(t *testing.T) { +func TestClearVotesAndPrevotes(t *testing.T) { fixture := CreateTestFixture(t) power := int64(100) @@ -109,18 +109,18 @@ func TestClearBallots(t *testing.T) { require.NoError(t, err) staking.EndBlocker(fixture.Ctx, &fixture.StakingKeeper) - btcBallot := types.ExchangeRateVotes{ + btcVotes := types.ExchangeRateVotes{ types.NewExchangeRateVote(sdk.NewDec(17), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[0], power), types.NewExchangeRateVote(sdk.NewDec(10), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[1], power), types.NewExchangeRateVote(sdk.NewDec(6), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[2], power), } - ethBallot := types.ExchangeRateVotes{ + ethVotes := types.ExchangeRateVotes{ types.NewExchangeRateVote(sdk.NewDec(1000), asset.Registry.Pair(denoms.ETH, denoms.NUSD), ValAddrs[0], power), types.NewExchangeRateVote(sdk.NewDec(1300), asset.Registry.Pair(denoms.ETH, denoms.NUSD), ValAddrs[1], power), types.NewExchangeRateVote(sdk.NewDec(2000), asset.Registry.Pair(denoms.ETH, denoms.NUSD), ValAddrs[2], power), } - for i := range btcBallot { + for i := range btcVotes { fixture.OracleKeeper.Prevotes.Insert(fixture.Ctx, ValAddrs[i], types.AggregateExchangeRatePrevote{ Hash: "", Voter: ValAddrs[i].String(), @@ -129,8 +129,8 @@ func TestClearBallots(t *testing.T) { fixture.OracleKeeper.Votes.Insert(fixture.Ctx, ValAddrs[i], types.NewAggregateExchangeRateVote(types.ExchangeRateTuples{ - {Pair: btcBallot[i].Pair, ExchangeRate: btcBallot[i].ExchangeRate}, - {Pair: ethBallot[i].Pair, ExchangeRate: ethBallot[i].ExchangeRate}, + {Pair: btcVotes[i].Pair, ExchangeRate: btcVotes[i].ExchangeRate}, + {Pair: ethVotes[i].Pair, ExchangeRate: ethVotes[i].ExchangeRate}, }, ValAddrs[i])) } @@ -170,17 +170,17 @@ func TestFuzzTally(t *testing.T) { } }, func(e *types.ExchangeRateVotes, c fuzz.Continue) { - ballot := types.ExchangeRateVotes{} + votes := types.ExchangeRateVotes{} for addr, power := range validators { addr, _ := sdk.ValAddressFromBech32(addr) var rate sdk.Dec c.Fuzz(&rate) - ballot = append(ballot, types.NewExchangeRateVote(rate, asset.NewPair(c.RandString(), c.RandString()), addr, power)) + votes = append(votes, types.NewExchangeRateVote(rate, asset.NewPair(c.RandString(), c.RandString()), addr, power)) } - *e = ballot + *e = votes }, ) @@ -190,14 +190,14 @@ func TestFuzzTally(t *testing.T) { claimMap := types.ValidatorPerformances{} f.Fuzz(&claimMap) - ballot := types.ExchangeRateVotes{} - f.Fuzz(&ballot) + votes := types.ExchangeRateVotes{} + f.Fuzz(&votes) var rewardBand sdk.Dec f.Fuzz(&rewardBand) require.NotPanics(t, func() { - Tally(ballot, rewardBand, claimMap) + Tally(votes, rewardBand, claimMap) }) } @@ -209,26 +209,26 @@ func TestRemoveInvalidBallots(t *testing.T) { voteMap VoteMap }{ { - name: "empty key, empty ballot", + name: "empty key, empty votes", voteMap: VoteMap{ "": types.ExchangeRateVotes{}, }, }, { - name: "nonempty key, empty ballot", + name: "nonempty key, empty votes", voteMap: VoteMap{ "xxx": types.ExchangeRateVotes{}, }, }, { - name: "nonempty keys, empty ballot", + name: "nonempty keys, empty votes", voteMap: VoteMap{ "xxx": types.ExchangeRateVotes{}, "abc123": types.ExchangeRateVotes{}, }, }, { - name: "mixed empty keys, empty ballot", + name: "mixed empty keys, empty votes", voteMap: VoteMap{ "xxx": types.ExchangeRateVotes{}, "": types.ExchangeRateVotes{}, @@ -237,7 +237,7 @@ func TestRemoveInvalidBallots(t *testing.T) { }, }, { - name: "empty key, nonempty ballot, not whitelisted", + name: "empty key, nonempty votes, not whitelisted", voteMap: VoteMap{ "": types.ExchangeRateVotes{ {Pair: "", ExchangeRate: sdk.ZeroDec(), Voter: sdk.ValAddress{}, Power: 0}, @@ -245,7 +245,7 @@ func TestRemoveInvalidBallots(t *testing.T) { }, }, { - name: "nonempty key, nonempty ballot, whitelisted", + name: "nonempty key, nonempty votes, whitelisted", voteMap: VoteMap{ "x": types.ExchangeRateVotes{ {Pair: "x", ExchangeRate: sdk.Dec{}, Voter: sdk.ValAddress{123}, Power: 5}, @@ -309,7 +309,7 @@ func TestFuzzPickReferencePair(t *testing.T) { c.Fuzz(&validators) for _, pair := range pairs { - ballots := types.ExchangeRateVotes{} + votes := types.ExchangeRateVotes{} for addr, power := range validators { addr, _ := sdk.ValAddressFromBech32(addr) @@ -317,10 +317,10 @@ func TestFuzzPickReferencePair(t *testing.T) { var rate sdk.Dec c.Fuzz(&rate) - ballots = append(ballots, types.NewExchangeRateVote(rate, pair, addr, power)) + votes = append(votes, types.NewExchangeRateVote(rate, pair, addr, power)) } - (*e)[pair] = ballots + (*e)[pair] = votes } }, ) @@ -349,11 +349,11 @@ func TestFuzzPickReferencePair(t *testing.T) { } func TestZeroBallotPower(t *testing.T) { - btcBallots := types.ExchangeRateVotes{ + btcVotess := types.ExchangeRateVotes{ types.NewExchangeRateVote(sdk.NewDec(17), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[0], 0), types.NewExchangeRateVote(sdk.NewDec(10), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[1], 0), types.NewExchangeRateVote(sdk.NewDec(6), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[2], 0), } - assert.False(t, isPassingVoteThreshold(btcBallots, sdk.ZeroInt(), 0)) + assert.False(t, isPassingVoteThreshold(btcVotess, sdk.ZeroInt(), 0)) } diff --git a/x/oracle/keeper/params.go b/x/oracle/keeper/params.go index 3724d46bc..f45f7b3fb 100644 --- a/x/oracle/keeper/params.go +++ b/x/oracle/keeper/params.go @@ -18,20 +18,20 @@ func (k Keeper) VotePeriod(ctx sdk.Context) (res uint64) { return params.VotePeriod } -// VoteThreshold returns the minimum percentage of votes that must be received for a ballot to pass. +// VoteThreshold returns the minimum percentage of votes that must be received for a votes to pass. func (k Keeper) VoteThreshold(ctx sdk.Context) (res sdk.Dec) { params, _ := k.Params.Get(ctx) return params.VoteThreshold } -// MinVoters returns the minimum percentage of votes that must be received for a ballot to pass. +// MinVoters returns the minimum percentage of votes that must be received for a votes to pass. func (k Keeper) MinVoters(ctx sdk.Context) (res uint64) { params, _ := k.Params.Get(ctx) return params.MinVoters } // RewardBand returns a maxium divergence that a price vote can have from the -// weighted median in the ballot. If a vote lies within the valid range +// weighted median in the votes. If a vote lies within the valid range // defined by: // // μ := weightedMedian, diff --git a/x/oracle/keeper/update_exchange_rates.go b/x/oracle/keeper/update_exchange_rates.go index 0369ee15b..99a1251b1 100644 --- a/x/oracle/keeper/update_exchange_rates.go +++ b/x/oracle/keeper/update_exchange_rates.go @@ -81,7 +81,7 @@ func (k Keeper) tallyVotesAndUpdatePrices( } } -// getPairVotes returns a map of pairs and votes excluding abstained votes and ballots that don't meet the threshold criteria +// getPairVotes returns a map of pairs and votes excluding abstained votes and votes that don't meet the threshold criteria func (k Keeper) getPairVotes( ctx sdk.Context, validatorPerformances types.ValidatorPerformances, @@ -95,7 +95,7 @@ func (k Keeper) getPairVotes( } // clearExchangeRates removes all exchange rates from the state -// We remove the price for pair with expired prices or valid ballots +// We remove the price for pair with expired prices or valid votes func (k Keeper) clearExchangeRates(ctx sdk.Context, pairVotes map[asset.Pair]types.ExchangeRateVotes) { params, _ := k.Params.Get(ctx) diff --git a/x/oracle/keeper/update_exchange_rates_test.go b/x/oracle/keeper/update_exchange_rates_test.go index c279f61ac..074d68d7c 100644 --- a/x/oracle/keeper/update_exchange_rates_test.go +++ b/x/oracle/keeper/update_exchange_rates_test.go @@ -95,8 +95,8 @@ func TestResetExchangeRates(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) fixture, _ := Setup(t) - emptyBallot := map[asset.Pair]types.ExchangeRateVotes{} - validBallot := map[asset.Pair]types.ExchangeRateVotes{pair: {}} + emptyVotes := map[asset.Pair]types.ExchangeRateVotes{} + validVotes := map[asset.Pair]types.ExchangeRateVotes{pair: {}} // Set expiration blocks to 10 params, _ := fixture.OracleKeeper.Params.Get(fixture.Ctx) @@ -108,13 +108,13 @@ func TestResetExchangeRates(t *testing.T) { // reset exchange rates at block 2 // Price should still be there because not expired yet - fixture.OracleKeeper.clearExchangeRates(fixture.Ctx.WithBlockHeight(2), emptyBallot) + fixture.OracleKeeper.clearExchangeRates(fixture.Ctx.WithBlockHeight(2), emptyVotes) _, err := fixture.OracleKeeper.ExchangeRates.Get(fixture.Ctx, pair) assert.NoError(t, err) - // reset exchange rates at block 3 but pair is in ballot - // Price should be removed there because there was a valid ballot - fixture.OracleKeeper.clearExchangeRates(fixture.Ctx.WithBlockHeight(3), validBallot) + // reset exchange rates at block 3 but pair is in votes + // Price should be removed there because there was a valid votes + fixture.OracleKeeper.clearExchangeRates(fixture.Ctx.WithBlockHeight(3), validVotes) _, err = fixture.OracleKeeper.ExchangeRates.Get(fixture.Ctx, pair) assert.Error(t, err) @@ -122,7 +122,7 @@ func TestResetExchangeRates(t *testing.T) { // reset exchange rates at block 79 // Price should not be there anymore because expired fixture.OracleKeeper.SetPrice(fixture.Ctx.WithBlockHeight(69), pair, testExchangeRate) - fixture.OracleKeeper.clearExchangeRates(fixture.Ctx.WithBlockHeight(79), emptyBallot) + fixture.OracleKeeper.clearExchangeRates(fixture.Ctx.WithBlockHeight(79), emptyVotes) _, err = fixture.OracleKeeper.ExchangeRates.Get(fixture.Ctx, pair) assert.Error(t, err) @@ -131,7 +131,7 @@ func TestResetExchangeRates(t *testing.T) { func TestOracleTally(t *testing.T) { fixture, _ := Setup(t) - ballot := types.ExchangeRateVotes{} + votes := types.ExchangeRateVotes{} rates, valAddrs, stakingKeeper := types.GenerateRandomTestCase() fixture.OracleKeeper.StakingKeeper = stakingKeeper h := NewMsgServerImpl(fixture.OracleKeeper) @@ -160,7 +160,7 @@ func TestOracleTally(t *testing.T) { vote := types.NewExchangeRateVote( decExchangeRate, asset.Registry.Pair(denoms.BTC, denoms.NUSD), valAddrs[i], power) - ballot = append(ballot, vote) + votes = append(votes, vote) // change power of every three validator if i%3 == 0 { @@ -175,9 +175,9 @@ func TestOracleTally(t *testing.T) { valAddr, ) } - sort.Sort(ballot) - weightedMedian := ballot.WeightedMedianWithAssertion() - standardDeviation := ballot.StandardDeviation(weightedMedian) + sort.Sort(votes) + weightedMedian := votes.WeightedMedianWithAssertion() + standardDeviation := votes.StandardDeviation(weightedMedian) maxSpread := weightedMedian.Mul(fixture.OracleKeeper.RewardBand(fixture.Ctx).QuoInt64(2)) if standardDeviation.GT(maxSpread) { @@ -192,7 +192,7 @@ func TestOracleTally(t *testing.T) { ) } - for _, vote := range ballot { + for _, vote := range votes { key := vote.Voter.String() validatorPerformance := expectedValidatorPerformances[key] if vote.ExchangeRate.GTE(weightedMedian.Sub(maxSpread)) && @@ -208,7 +208,7 @@ func TestOracleTally(t *testing.T) { } tallyMedian := Tally( - ballot, fixture.OracleKeeper.RewardBand(fixture.Ctx), validatorPerformances) + votes, fixture.OracleKeeper.RewardBand(fixture.Ctx), validatorPerformances) assert.Equal(t, expectedValidatorPerformances, validatorPerformances) 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 c68246d70..e0784ef21 100644 --- a/x/oracle/types/ballot.go +++ b/x/oracle/types/ballot.go @@ -48,7 +48,7 @@ func (pb ExchangeRateVotes) ToMap() map[string]sdk.Dec { return validatorExchangeRateMap } -// ToCrossRate return cross_rate(base/exchange_rate) ballot +// ToCrossRate return cross_rate(base/exchange_rate) votes func (pb ExchangeRateVotes) ToCrossRate(bases map[string]sdk.Dec) (cb ExchangeRateVotes) { for i := range pb { vote := pb[i] @@ -78,7 +78,7 @@ func (v ExchangeRateVotes) NumValidVoters() uint64 { return uint64(count) } -// Power returns the total amount of voting power in the ballot +// Power returns the total amount of voting power in the votes func (v ExchangeRateVotes) Power() int64 { totalPower := int64(0) for _, vote := range v { diff --git a/x/oracle/types/ballot_test.go b/x/oracle/types/ballot_test.go index 1498b6057..58de574bc 100644 --- a/x/oracle/types/ballot_test.go +++ b/x/oracle/types/ballot_test.go @@ -105,8 +105,8 @@ func TestToCrossRate(t *testing.T) { } } - baseMapBallot := pbBase.ToMap() - require.Equal(t, cb, pbQuote.ToCrossRate(baseMapBallot)) + basePairPrices := pbBase.ToMap() + require.Equal(t, cb, pbQuote.ToCrossRate(basePairPrices)) sort.Sort(cb) } @@ -127,7 +127,7 @@ func TestPBPower(t *testing.T) { ctx := sdk.NewContext(nil, tmproto.Header{}, false, nil) _, valAccAddrs, sk := types.GenerateRandomTestCase() pb := types.ExchangeRateVotes{} - ballotPower := int64(0) + totalPower := int64(0) for i := 0; i < len(sk.Validators()); i++ { power := sk.Validator(ctx, valAccAddrs[i]).GetConsensusPower(sdk.DefaultPowerReduction) @@ -142,10 +142,10 @@ func TestPBPower(t *testing.T) { require.NotEqual(t, int64(0), vote.Power) - ballotPower += vote.Power + totalPower += vote.Power } - require.Equal(t, ballotPower, pb.Power()) + require.Equal(t, totalPower, pb.Power()) // Mix in a fake validator, the total power should not have changed. pubKey := secp256k1.GenPrivKey().PubKey() @@ -158,7 +158,7 @@ func TestPBPower(t *testing.T) { ) pb = append(pb, fakeVote) - require.Equal(t, ballotPower, pb.Power()) + require.Equal(t, totalPower, pb.Power()) } func TestPBWeightedMedian(t *testing.T) { diff --git a/x/oracle/types/test_utils.go b/x/oracle/types/test_utils.go index 7832bfcb7..67b51d075 100644 --- a/x/oracle/types/test_utils.go +++ b/x/oracle/types/test_utils.go @@ -48,7 +48,7 @@ func GenerateRandomTestCase() (rates []float64, valAddrs []sdk.ValAddress, staki var _ StakingKeeper = DummyStakingKeeper{} -// DummyStakingKeeper dummy staking keeper to test ballot +// DummyStakingKeeper dummy staking keeper to test votes type DummyStakingKeeper struct { validators []MockValidator }