From 715dbe03943fbad224e1ee334df027f8917bb702 Mon Sep 17 00:00:00 2001 From: Vincent Chau <99756290+vincentwschau@users.noreply.github.com> Date: Thu, 21 Mar 2024 21:12:04 -0400 Subject: [PATCH] Address nits. --- protocol/x/clob/keeper/process_operations.go | 6 +- protocol/x/clob/types/expected_keepers.go | 2 +- ...hdrawal_and_transfers_blocked_info_test.go | 37 +++- .../keeper/negative_tnc_subaccount.go | 24 ++- .../keeper/negative_tnc_subaccount_test.go | 44 +++-- .../x/subaccounts/keeper/subaccount_test.go | 164 ++++++++++-------- 6 files changed, 169 insertions(+), 108 deletions(-) diff --git a/protocol/x/clob/keeper/process_operations.go b/protocol/x/clob/keeper/process_operations.go index 818b0412c4..a99c8b6c0e 100644 --- a/protocol/x/clob/keeper/process_operations.go +++ b/protocol/x/clob/keeper/process_operations.go @@ -694,13 +694,9 @@ func (k Keeper) PersistMatchDeleveragingToState( metrics.GetLabelForBoolValue(metrics.IsLong, position.GetIsLong()), metrics.GetLabelForBoolValue(metrics.DeliverTx, true), ) - collateralPoolAddress, err := k.subaccountsKeeper.GetCollateralPoolFromPerpetualId(ctx, perpetualId) - if err != nil { - return err - } k.subaccountsKeeper.SetNegativeTncSubaccountSeenAtBlock( ctx, - collateralPoolAddress, + perpetualId, lib.MustConvertIntegerToUint32(ctx.BlockHeight()), ) return nil diff --git a/protocol/x/clob/types/expected_keepers.go b/protocol/x/clob/types/expected_keepers.go index 662c989d8f..469a47f767 100644 --- a/protocol/x/clob/types/expected_keepers.go +++ b/protocol/x/clob/types/expected_keepers.go @@ -61,7 +61,7 @@ type SubaccountsKeeper interface { ) SetNegativeTncSubaccountSeenAtBlock( ctx sdk.Context, - collateralPoolAddr sdk.AccAddress, + perpetualId uint32, blockHeight uint32, ) TransferFeesToFeeCollectorModule(ctx sdk.Context, assetId uint32, amount *big.Int, perpetualId uint32) error diff --git a/protocol/x/subaccounts/keeper/grpc_query_withdrawal_and_transfers_blocked_info_test.go b/protocol/x/subaccounts/keeper/grpc_query_withdrawal_and_transfers_blocked_info_test.go index ef704aabd7..1f2d132e37 100644 --- a/protocol/x/subaccounts/keeper/grpc_query_withdrawal_and_transfers_blocked_info_test.go +++ b/protocol/x/subaccounts/keeper/grpc_query_withdrawal_and_transfers_blocked_info_test.go @@ -51,7 +51,11 @@ func TestQueryWithdrawalAndTransfersBlockedInfo(t *testing.T) { `Negative TNC subaccount seen in state returns withdrawals and transfers unblocked after the delay`: { setup: func(ctx sdktypes.Context, sk sakeeper.Keeper, bk btkeeper.Keeper) { - sk.SetNegativeTncSubaccountSeenAtBlock(ctx, types.ModuleAddress, 7) + sk.SetNegativeTncSubaccountSeenAtBlock( + ctx, + constants.BtcUsd_NoMarginRequirement.Params.Id, + 7, + ) }, request: &types.QueryGetWithdrawalAndTransfersBlockedInfoRequest{ @@ -66,9 +70,13 @@ func TestQueryWithdrawalAndTransfersBlockedInfo(t *testing.T) { }, }, `Negative TNC subaccount seen in state returns withdrawals and transfers unblocked - after the delay (for isolated collateral pool address)`: { + after the delay (for isolated perpetual)`: { setup: func(ctx sdktypes.Context, sk sakeeper.Keeper, bk btkeeper.Keeper) { - sk.SetNegativeTncSubaccountSeenAtBlock(ctx, constants.IsoCollateralPoolAddress, 5) + sk.SetNegativeTncSubaccountSeenAtBlock( + ctx, + constants.IsoUsd_IsolatedMarket.Params.Id, + 5, + ) }, request: &types.QueryGetWithdrawalAndTransfersBlockedInfoRequest{ @@ -120,7 +128,11 @@ func TestQueryWithdrawalAndTransfersBlockedInfo(t *testing.T) { `Negative TNC subaccount and chain outage seen in state returns withdrawals and transfers unblocked after the max block number + delay (negative TNC subaccount block greater)`: { setup: func(ctx sdktypes.Context, sk sakeeper.Keeper, bk btkeeper.Keeper) { - sk.SetNegativeTncSubaccountSeenAtBlock(ctx, types.ModuleAddress, 27) + sk.SetNegativeTncSubaccountSeenAtBlock( + ctx, + constants.BtcUsd_NoMarginRequirement.Params.Id, + 27, + ) bk.SetAllDowntimeInfo( ctx, &blocktimetypes.AllDowntimeInfo{ @@ -157,7 +169,11 @@ func TestQueryWithdrawalAndTransfersBlockedInfo(t *testing.T) { `Negative TNC subaccount and chain outage seen in state returns withdrawals and transfers unblocked after the max block number + delay (chain outage block greater)`: { setup: func(ctx sdktypes.Context, sk sakeeper.Keeper, bk btkeeper.Keeper) { - sk.SetNegativeTncSubaccountSeenAtBlock(ctx, types.ModuleAddress, 37) + sk.SetNegativeTncSubaccountSeenAtBlock( + ctx, + constants.BtcUsd_NoMarginRequirement.Params.Id, + 37, + ) bk.SetAllDowntimeInfo( ctx, &blocktimetypes.AllDowntimeInfo{ @@ -194,7 +210,11 @@ func TestQueryWithdrawalAndTransfersBlockedInfo(t *testing.T) { `Negative TNC subaccount and chain outage seen in state returns withdrawals and transfers unblocked after the max block number + delay (both blocks equal)`: { setup: func(ctx sdktypes.Context, sk sakeeper.Keeper, bk btkeeper.Keeper) { - sk.SetNegativeTncSubaccountSeenAtBlock(ctx, types.ModuleAddress, 3) + sk.SetNegativeTncSubaccountSeenAtBlock( + ctx, + constants.BtcUsd_NoMarginRequirement.Params.Id, + 3, + ) bk.SetAllDowntimeInfo( ctx, &blocktimetypes.AllDowntimeInfo{ @@ -230,7 +250,10 @@ func TestQueryWithdrawalAndTransfersBlockedInfo(t *testing.T) { }, } { t.Run(testName, func(t *testing.T) { - ctx, keeper, _, _, _, _, _, blocktimeKeeper, _ := keepertest.SubaccountsKeepers(t, true) + ctx, keeper, pricesKeeper, perpetualsKeeper, _, _, _, blocktimeKeeper, _ := keepertest.SubaccountsKeepers(t, true) + keepertest.CreateTestMarkets(t, ctx, pricesKeeper) + keepertest.CreateTestLiquidityTiers(t, ctx, perpetualsKeeper) + keepertest.CreateTestPerpetuals(t, ctx, perpetualsKeeper) tc.setup(ctx, *keeper, *blocktimeKeeper) response, err := keeper.GetWithdrawalAndTransfersBlockedInfo(ctx, tc.request) if tc.err != nil { diff --git a/protocol/x/subaccounts/keeper/negative_tnc_subaccount.go b/protocol/x/subaccounts/keeper/negative_tnc_subaccount.go index 121852cdb8..53f055c250 100644 --- a/protocol/x/subaccounts/keeper/negative_tnc_subaccount.go +++ b/protocol/x/subaccounts/keeper/negative_tnc_subaccount.go @@ -14,13 +14,13 @@ import ( // seen in state for the given collateral pool address and a boolean for whether it exists in state. func (k Keeper) GetNegativeTncSubaccountSeenAtBlock( ctx sdk.Context, - collateralPoolAddr sdk.AccAddress, + collateralPoolAddress sdk.AccAddress, ) (uint32, bool) { store := prefix.NewStore( ctx.KVStore(k.storeKey), []byte(types.NegativeTncSubaccountForCollateralPoolSeenAtBlockKeyPrefix), ) - return k.getNegativeTncSubaccountSeenAtBlock(store, collateralPoolAddr) + return k.getNegativeTncSubaccountSeenAtBlock(store, collateralPoolAddress) } // getNegativeTncSubaccountSeenAtBlock is a helper function that takes a store and returns the last @@ -28,10 +28,10 @@ func (k Keeper) GetNegativeTncSubaccountSeenAtBlock( // and a boolean for whether it exists in state. func (k Keeper) getNegativeTncSubaccountSeenAtBlock( store storetypes.KVStore, - collateralPoolAddr sdk.AccAddress, + collateralPoolAddress sdk.AccAddress, ) (uint32, bool) { b := store.Get( - collateralPoolAddr, + collateralPoolAddress, ) blockHeight := gogotypes.UInt32Value{Value: 0} exists := false @@ -48,7 +48,7 @@ func (k Keeper) getNegativeTncSubaccountSeenAtBlock( // This function will panic if the old block height is greater than the new block height. func (k Keeper) SetNegativeTncSubaccountSeenAtBlock( ctx sdk.Context, - collateralPoolAddr sdk.AccAddress, + perpetualId uint32, blockHeight uint32, ) { store := prefix.NewStore( @@ -56,8 +56,18 @@ func (k Keeper) SetNegativeTncSubaccountSeenAtBlock( []byte(types.NegativeTncSubaccountForCollateralPoolSeenAtBlockKeyPrefix), ) + collateralPoolAddress, err := k.GetCollateralPoolFromPerpetualId(ctx, perpetualId) + if err != nil { + panic( + fmt.Sprintf( + "SetNegativeTncSubaccountSeenAtBlock: non-existent perpetual id %d", + perpetualId, + ), + ) + } + // Panic if the stored block height value exists and is greater than the new block height value. - currentValue, exists := k.getNegativeTncSubaccountSeenAtBlock(store, collateralPoolAddr) + currentValue, exists := k.getNegativeTncSubaccountSeenAtBlock(store, collateralPoolAddress) if exists && blockHeight < currentValue { panic( fmt.Sprintf( @@ -70,7 +80,7 @@ func (k Keeper) SetNegativeTncSubaccountSeenAtBlock( blockHeightValue := gogotypes.UInt32Value{Value: blockHeight} store.Set( - collateralPoolAddr.Bytes(), + collateralPoolAddress.Bytes(), k.cdc.MustMarshal(&blockHeightValue), ) } diff --git a/protocol/x/subaccounts/keeper/negative_tnc_subaccount_test.go b/protocol/x/subaccounts/keeper/negative_tnc_subaccount_test.go index b303e17188..6e1494d34f 100644 --- a/protocol/x/subaccounts/keeper/negative_tnc_subaccount_test.go +++ b/protocol/x/subaccounts/keeper/negative_tnc_subaccount_test.go @@ -13,6 +13,11 @@ import ( ) func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) { + testPerpetualIds := []uint32{ + constants.IsoUsd_IsolatedMarket.Params.Id, + constants.Iso2Usd_IsolatedMarket.Params.Id, + constants.BtcUsd_NoMarginRequirement.Params.Id, + } testCollateralPoolAddresses := []sdk.AccAddress{ constants.IsoCollateralPoolAddress, constants.Iso2CollateralPoolAddress, @@ -42,8 +47,8 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) { }, "Block height can be updated": { setupTestAndPerformAssertions: func(ctx sdk.Context, k keeper.Keeper) { - for _, collateralPoolAddr := range testCollateralPoolAddresses { - k.SetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr, 1) + for i, collateralPoolAddr := range testCollateralPoolAddresses { + k.SetNegativeTncSubaccountSeenAtBlock(ctx, testPerpetualIds[i], 1) block, exists := k.GetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr) require.True(t, exists) require.Equal( @@ -62,8 +67,8 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) { }, "Block height can be updated more than once": { setupTestAndPerformAssertions: func(ctx sdk.Context, k keeper.Keeper) { - for _, collateralPoolAddr := range testCollateralPoolAddresses { - k.SetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr, 1) + for i, collateralPoolAddr := range testCollateralPoolAddresses { + k.SetNegativeTncSubaccountSeenAtBlock(ctx, testPerpetualIds[i], 1) block, exists := k.GetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr) require.True(t, exists) require.Equal( @@ -72,7 +77,7 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) { block, ) - k.SetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr, 2) + k.SetNegativeTncSubaccountSeenAtBlock(ctx, testPerpetualIds[i], 2) block, exists = k.GetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr) require.True(t, exists) require.Equal( @@ -81,7 +86,7 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) { block, ) - k.SetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr, 3) + k.SetNegativeTncSubaccountSeenAtBlock(ctx, testPerpetualIds[i], 3) block, exists = k.GetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr) require.True(t, exists) require.Equal( @@ -90,7 +95,7 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) { block, ) - k.SetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr, 10) + k.SetNegativeTncSubaccountSeenAtBlock(ctx, testPerpetualIds[i], 10) block, exists = k.GetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr) require.True(t, exists) require.Equal( @@ -111,8 +116,8 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) { }, "Block height can be updated to same block height": { setupTestAndPerformAssertions: func(ctx sdk.Context, k keeper.Keeper) { - for _, collateralPoolAddr := range testCollateralPoolAddresses { - k.SetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr, 0) + for i, collateralPoolAddr := range testCollateralPoolAddresses { + k.SetNegativeTncSubaccountSeenAtBlock(ctx, testPerpetualIds[i], 0) block, exists := k.GetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr) require.True(t, exists) require.Equal( @@ -121,7 +126,7 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) { block, ) - k.SetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr, 0) + k.SetNegativeTncSubaccountSeenAtBlock(ctx, testPerpetualIds[i], 0) block, exists = k.GetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr) require.True(t, exists) require.Equal( @@ -143,7 +148,7 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) { "Block height can be updated to different block heights for each collateral pool address": { setupTestAndPerformAssertions: func(ctx sdk.Context, k keeper.Keeper) { for i, collateralPoolAddr := range testCollateralPoolAddresses { - k.SetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr, uint32(i)) + k.SetNegativeTncSubaccountSeenAtBlock(ctx, testPerpetualIds[i], uint32(i)) block, exists := k.GetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr) require.True(t, exists) require.Equal( @@ -152,7 +157,7 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) { block, ) - k.SetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr, uint32(2*i+1)) + k.SetNegativeTncSubaccountSeenAtBlock(ctx, testPerpetualIds[i], uint32(2*i+1)) block, exists = k.GetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr) require.True(t, exists) require.Equal( @@ -176,7 +181,10 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) { for name, tc := range tests { t.Run(name, func(t *testing.T) { // Setup keeper state and test parameters. - ctx, subaccountsKeeper, _, _, _, _, _, _, _ := keepertest.SubaccountsKeepers(t, true) + ctx, subaccountsKeeper, pricesKeeper, perpetualsKeeper, _, _, _, _, _ := keepertest.SubaccountsKeepers(t, true) + keepertest.CreateTestMarkets(t, ctx, pricesKeeper) + keepertest.CreateTestLiquidityTiers(t, ctx, perpetualsKeeper) + keepertest.CreateTestPerpetuals(t, ctx, perpetualsKeeper) // Set the tracer on the multistore to verify the performed writes are correct. traceDecoder := &tracer.TraceDecoder{} @@ -195,13 +203,15 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) { func TestGetSetNegativeTncSubaccountSeenAtBlock_PanicsOnDecreasingBlock(t *testing.T) { // Setup keeper state and test parameters. - ctx, subaccountsKeeper, _, _, _, _, _, _, _ := keepertest.SubaccountsKeepers(t, true) - - subaccountsKeeper.SetNegativeTncSubaccountSeenAtBlock(ctx, types.ModuleAddress, 2) + ctx, subaccountsKeeper, pricesKeeper, perpetualsKeeper, _, _, _, _, _ := keepertest.SubaccountsKeepers(t, true) + keepertest.CreateTestMarkets(t, ctx, pricesKeeper) + keepertest.CreateTestLiquidityTiers(t, ctx, perpetualsKeeper) + keepertest.CreateTestPerpetuals(t, ctx, perpetualsKeeper) + subaccountsKeeper.SetNegativeTncSubaccountSeenAtBlock(ctx, uint32(0), 2) require.PanicsWithValue( t, "SetNegativeTncSubaccountSeenAtBlock: new block height (1) is less than the current block height (2)", - func() { subaccountsKeeper.SetNegativeTncSubaccountSeenAtBlock(ctx, types.ModuleAddress, 1) }, + func() { subaccountsKeeper.SetNegativeTncSubaccountSeenAtBlock(ctx, uint32(0), 1) }, ) } diff --git a/protocol/x/subaccounts/keeper/subaccount_test.go b/protocol/x/subaccounts/keeper/subaccount_test.go index bd82a737e1..65b32eb7c3 100644 --- a/protocol/x/subaccounts/keeper/subaccount_test.go +++ b/protocol/x/subaccounts/keeper/subaccount_test.go @@ -2411,7 +2411,7 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { // Negative TNC subaccount state currentBlock uint32 - negativeTncSubaccountSeenAtBlock map[string]uint32 + negativeTncSubaccountSeenAtBlock map[uint32]uint32 // Update type updateType types.UpdateType @@ -2420,6 +2420,9 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { expectedQuoteBalance: big.NewInt(100), expectedSuccess: true, expectedSuccessPerUpdate: []types.UpdateResult{types.Success}, + perpetuals: []perptypes.Perpetual{ + constants.BtcUsd_SmallMarginRequirement, + }, updates: []types.Update{ { AssetUpdates: testutil.CreateUsdcAssetUpdate(big.NewInt(100)), @@ -2444,8 +2447,8 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - types.ModuleAddress.String(): 100, + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.BtcUsd_NoMarginRequirement.Params.Id: 100, }, updateType: types.Deposit, @@ -2455,6 +2458,9 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { expectedQuoteBalance: big.NewInt(100), expectedSuccess: true, expectedSuccessPerUpdate: []types.UpdateResult{types.Success}, + perpetuals: []perptypes.Perpetual{ + constants.BtcUsd_SmallMarginRequirement, + }, updates: []types.Update{ { AssetUpdates: testutil.CreateUsdcAssetUpdate(big.NewInt(100)), @@ -2479,8 +2485,8 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - types.ModuleAddress.String(): 100 - + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.BtcUsd_NoMarginRequirement.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS + 1, }, @@ -2490,6 +2496,9 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { expectedQuoteBalance: big.NewInt(100), expectedSuccess: true, expectedSuccessPerUpdate: []types.UpdateResult{types.Success}, + perpetuals: []perptypes.Perpetual{ + constants.BtcUsd_SmallMarginRequirement, + }, updates: []types.Update{ { AssetUpdates: testutil.CreateUsdcAssetUpdate(big.NewInt(100)), @@ -2514,8 +2523,8 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - types.ModuleAddress.String(): 0, + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.BtcUsd_NoMarginRequirement.Params.Id: 0, }, updateType: types.Deposit, @@ -2545,8 +2554,8 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - types.ModuleAddress.String(): 100, + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.BtcUsd_NoMarginRequirement.Params.Id: 100, }, updateType: types.Withdrawal, @@ -2577,8 +2586,8 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - types.ModuleAddress.String(): 100 - + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.BtcUsd_NoMarginRequirement.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS + 1, }, @@ -2622,8 +2631,8 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - types.ModuleAddress.String(): 100 - + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.BtcUsd_NoMarginRequirement.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS, }, @@ -2666,8 +2675,8 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - types.ModuleAddress.String(): 0, + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.BtcUsd_NoMarginRequirement.Params.Id: 0, }, updateType: types.Withdrawal, @@ -2680,6 +2689,7 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { expectedSuccessPerUpdate: []types.UpdateResult{types.Success}, perpetuals: []perptypes.Perpetual{ constants.BtcUsd_SmallMarginRequirement, + constants.IsoUsd_IsolatedMarket, }, perpetualPositions: map[types.SubaccountId][]*types.PerpetualPosition{ firstSubaccountId: {&constants.PerpetualPosition_OneBTCLong}, @@ -2711,8 +2721,8 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - constants.IsoCollateralPoolAddress.String(): 100 - + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.IsoUsd_IsolatedMarket.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS + 1, }, @@ -2746,8 +2756,8 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - constants.IsoCollateralPoolAddress.String(): 100 - + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.IsoUsd_IsolatedMarket.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS + 1, }, @@ -2790,10 +2800,10 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - constants.IsoCollateralPoolAddress.String(): 100 - + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.IsoUsd_IsolatedMarket.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS + 1, - types.ModuleAddress.String(): 0, + constants.BtcUsd_NoMarginRequirement.Params.Id: 0, }, updateType: types.Withdrawal, @@ -2836,10 +2846,10 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - constants.IsoCollateralPoolAddress.String(): 100 - + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.IsoUsd_IsolatedMarket.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS + 1, - types.ModuleAddress.String(): 100 - + constants.BtcUsd_NoMarginRequirement.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS, }, @@ -2885,9 +2895,9 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - constants.IsoCollateralPoolAddress.String(): 0, - constants.Iso2CollateralPoolAddress.String(): 100 - + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.IsoUsd_IsolatedMarket.Params.Id: 0, + constants.Iso2Usd_IsolatedMarket.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS + 1, }, @@ -2934,10 +2944,10 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - constants.IsoCollateralPoolAddress.String(): 100 - + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.IsoUsd_IsolatedMarket.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS, - constants.Iso2CollateralPoolAddress.String(): 100 - + constants.Iso2Usd_IsolatedMarket.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS + 1, }, @@ -2987,8 +2997,8 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: false, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - types.ModuleAddress.String(): 100, + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.BtcUsd_NoMarginRequirement.Params.Id: 100, }, updateType: types.Match, @@ -3038,8 +3048,8 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: false, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - types.ModuleAddress.String(): 100 - + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.BtcUsd_NoMarginRequirement.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS + 1, }, @@ -3089,8 +3099,8 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: false, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - types.ModuleAddress.String(): 0, + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.BtcUsd_NoMarginRequirement.Params.Id: 0, }, updateType: types.Match, @@ -3122,8 +3132,8 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - types.ModuleAddress.String(): 100, + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.BtcUsd_NoMarginRequirement.Params.Id: 100, }, updateType: types.Match, }, @@ -3155,8 +3165,8 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - types.ModuleAddress.String(): 100 - + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.BtcUsd_NoMarginRequirement.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS + 1, }, @@ -3189,8 +3199,8 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - types.ModuleAddress.String(): 0, + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.BtcUsd_NoMarginRequirement.Params.Id: 0, }, updateType: types.Match, @@ -3202,7 +3212,9 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { types.WithdrawalsAndTransfersBlocked, types.WithdrawalsAndTransfersBlocked, }, - perpetuals: []perptypes.Perpetual{}, + perpetuals: []perptypes.Perpetual{ + constants.BtcUsd_SmallMarginRequirement, + }, perpetualPositions: map[types.SubaccountId][]*types.PerpetualPosition{}, expectedPerpetualPositions: map[types.SubaccountId][]*types.PerpetualPosition{}, expectedAssetPositions: map[types.SubaccountId][]*types.AssetPosition{}, @@ -3223,8 +3235,8 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - types.ModuleAddress.String(): 100, + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.BtcUsd_NoMarginRequirement.Params.Id: 100, }, updateType: types.Transfer, @@ -3237,7 +3249,9 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { types.WithdrawalsAndTransfersBlocked, types.WithdrawalsAndTransfersBlocked, }, - perpetuals: []perptypes.Perpetual{}, + perpetuals: []perptypes.Perpetual{ + constants.BtcUsd_SmallMarginRequirement, + }, perpetualPositions: map[types.SubaccountId][]*types.PerpetualPosition{}, expectedPerpetualPositions: map[types.SubaccountId][]*types.PerpetualPosition{}, expectedAssetPositions: map[types.SubaccountId][]*types.AssetPosition{}, @@ -3258,8 +3272,8 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - types.ModuleAddress.String(): 100 - + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.BtcUsd_NoMarginRequirement.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS + 1, }, @@ -3273,7 +3287,9 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { types.NewlyUndercollateralized, types.Success, }, - perpetuals: []perptypes.Perpetual{}, + perpetuals: []perptypes.Perpetual{ + constants.BtcUsd_SmallMarginRequirement, + }, perpetualPositions: map[types.SubaccountId][]*types.PerpetualPosition{}, expectedPerpetualPositions: map[types.SubaccountId][]*types.PerpetualPosition{}, expectedAssetPositions: map[types.SubaccountId][]*types.AssetPosition{}, @@ -3294,8 +3310,8 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - types.ModuleAddress.String(): 100 - + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.BtcUsd_NoMarginRequirement.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS, }, @@ -3308,7 +3324,9 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { types.NewlyUndercollateralized, types.Success, }, - perpetuals: []perptypes.Perpetual{}, + perpetuals: []perptypes.Perpetual{ + constants.BtcUsd_SmallMarginRequirement, + }, perpetualPositions: map[types.SubaccountId][]*types.PerpetualPosition{}, expectedPerpetualPositions: map[types.SubaccountId][]*types.PerpetualPosition{}, expectedAssetPositions: map[types.SubaccountId][]*types.AssetPosition{}, @@ -3329,8 +3347,8 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - types.ModuleAddress.String(): 0, + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.BtcUsd_NoMarginRequirement.Params.Id: 0, }, updateType: types.Transfer, @@ -3346,6 +3364,7 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { }, perpetuals: []perptypes.Perpetual{ constants.IsoUsd_IsolatedMarket, + constants.Iso2Usd_IsolatedMarket, }, perpetualPositions: map[types.SubaccountId][]*types.PerpetualPosition{ secondSubaccountId: {&constants.PerpetualPosition_OneISOLong}, @@ -3371,8 +3390,8 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - constants.Iso2CollateralPoolAddress.String(): 100 - + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.Iso2Usd_IsolatedMarket.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS + 1, }, @@ -3389,6 +3408,7 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { types.WithdrawalsAndTransfersBlocked, }, perpetuals: []perptypes.Perpetual{ + constants.BtcUsd_SmallMarginRequirement, constants.IsoUsd_IsolatedMarket, }, perpetualPositions: map[types.SubaccountId][]*types.PerpetualPosition{ @@ -3415,10 +3435,10 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - constants.IsoCollateralPoolAddress.String(): 100 - + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.IsoUsd_IsolatedMarket.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS + 1, - types.ModuleAddress.String(): 0, + constants.BtcUsd_NoMarginRequirement.Params.Id: 0, }, updateType: types.Transfer, @@ -3435,6 +3455,7 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { types.WithdrawalsAndTransfersBlocked, }, perpetuals: []perptypes.Perpetual{ + constants.BtcUsd_SmallMarginRequirement, constants.IsoUsd_IsolatedMarket, }, perpetualPositions: map[types.SubaccountId][]*types.PerpetualPosition{ @@ -3461,10 +3482,10 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - constants.IsoCollateralPoolAddress.String(): 100 - + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.IsoUsd_IsolatedMarket.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS + 1, - types.ModuleAddress.String(): 100 - + constants.BtcUsd_NoMarginRequirement.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS, }, @@ -3510,9 +3531,9 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - constants.IsoCollateralPoolAddress.String(): 0, - constants.Iso2CollateralPoolAddress.String(): 100 - + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.IsoUsd_IsolatedMarket.Params.Id: 0, + constants.Iso2Usd_IsolatedMarket.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS + 1, }, @@ -3559,10 +3580,10 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { msgSenderEnabled: true, currentBlock: 100, - negativeTncSubaccountSeenAtBlock: map[string]uint32{ - constants.IsoCollateralPoolAddress.String(): 100 - + negativeTncSubaccountSeenAtBlock: map[uint32]uint32{ + constants.IsoUsd_IsolatedMarket.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS, - constants.Iso2CollateralPoolAddress.String(): 100 - + constants.Iso2Usd_IsolatedMarket.Params.Id: 100 - types.WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS + 1, }, @@ -3646,10 +3667,11 @@ func TestUpdateSubaccounts_WithdrawalsBlocked(t *testing.T) { subaccountId := *subaccounts[0].Id // Set the negative TNC subaccount seen at block in state if it's greater than 0. - for collateralPoolAddress, negativeTncSubaccountSeenAtBlock := range tc.negativeTncSubaccountSeenAtBlock { + for perpetualId, negativeTncSubaccountSeenAtBlock := range tc.negativeTncSubaccountSeenAtBlock { if negativeTncSubaccountSeenAtBlock != 0 { keeper.SetNegativeTncSubaccountSeenAtBlock( - ctx, sdk.MustAccAddressFromBech32(collateralPoolAddress), + ctx, + perpetualId, negativeTncSubaccountSeenAtBlock, ) }