diff --git a/aclmapping/utils/resource_type.go b/aclmapping/utils/resource_type.go index e7170ff1aa..a99eac462d 100644 --- a/aclmapping/utils/resource_type.go +++ b/aclmapping/utils/resource_type.go @@ -37,12 +37,14 @@ var StoreKeyToResourceTypePrefixMap = aclsdktypes.StoreKeyToResourceTypePrefixMa aclsdktypes.ResourceType_KV_DEX_PAIR_PREFIX: aclsdktypes.EmptyPrefix, aclsdktypes.ResourceType_KV_DEX_TWAP: dextypes.KeyPrefix(dextypes.TwapKey), aclsdktypes.ResourceType_KV_DEX_PRICE: dextypes.KeyPrefix(dextypes.PriceKey), + aclsdktypes.ResourceType_KV_DEX_SETTLEMENT_ENTRY: dextypes.KeyPrefix(dextypes.SettlementEntryKey), aclsdktypes.ResourceType_KV_DEX_REGISTERED_PAIR: dextypes.KeyPrefix(dextypes.RegisteredPairKey), aclsdktypes.ResourceType_KV_DEX_ORDER: dextypes.KeyPrefix(dextypes.OrderKey), aclsdktypes.ResourceType_KV_DEX_CANCEL: dextypes.KeyPrefix(dextypes.CancelKey), aclsdktypes.ResourceType_KV_DEX_ACCOUNT_ACTIVE_ORDERS: dextypes.KeyPrefix(dextypes.AccountActiveOrdersKey), aclsdktypes.ResourceType_KV_DEX_ASSET_LIST: dextypes.KeyPrefix(dextypes.AssetListKey), aclsdktypes.ResourceType_KV_DEX_NEXT_ORDER_ID: dextypes.KeyPrefix(dextypes.NextOrderIDKey), + aclsdktypes.ResourceType_KV_DEX_NEXT_SETTLEMENT_ID: dextypes.KeyPrefix(dextypes.NextSettlementIDKey), aclsdktypes.ResourceType_KV_DEX_MATCH_RESULT: dextypes.KeyPrefix(dextypes.MatchResultKey), aclsdktypes.ResourceType_KV_DEX_CONTRACT: dextypes.KeyPrefix(dexkeeper.ContractPrefixKey), aclsdktypes.ResourceType_KV_DEX_ORDER_BOOK: dextypes.KeyPrefix(dextypes.NextOrderIDKey), diff --git a/cmd/seid/cmd/iavl_parser.go b/cmd/seid/cmd/iavl_parser.go index ab46a1ba7b..fcb54a182f 100644 --- a/cmd/seid/cmd/iavl_parser.go +++ b/cmd/seid/cmd/iavl_parser.go @@ -110,11 +110,13 @@ func MatchAndExtractDexAddressPrefixKeys(key []byte) (bool, []string, []byte, er dextypes.ShortBookKey, dextypes.PriceKey, dextypes.TwapKey, + dextypes.SettlementEntryKey, dextypes.RegisteredPairKey, dextypes.OrderKey, dextypes.CancelKey, dextypes.AccountActiveOrdersKey, dextypes.NextOrderIDKey, + dextypes.NextSettlementIDKey, dextypes.MatchResultKey, dextypes.MemOrderKey, dextypes.MemCancelKey, diff --git a/x/dex/cache/cache.go b/x/dex/cache/cache.go index 531473a459..2edf41a03d 100644 --- a/x/dex/cache/cache.go +++ b/x/dex/cache/cache.go @@ -54,27 +54,25 @@ func (s *MemState) GetAllBlockOrders(ctx sdk.Context, contractAddr types.Contrac return } -func (s *MemState) GetBlockOrders(ctx sdk.Context, contractAddr types.ContractAddress, pair types.PairString) *BlockOrders { +func (s *MemState) GetBlockOrders(ctx sdk.Context, contractAddr types.ContractAddress, pair types.Pair) *BlockOrders { s.SynchronizeAccess(ctx, contractAddr) - pairType := types.GetPair(pair) return NewOrders( prefix.NewStore( ctx.KVStore(s.storeKey), types.MemOrderPrefixForPair( - string(contractAddr), pairType.PriceDenom, pairType.AssetDenom, + string(contractAddr), pair.PriceDenom, pair.AssetDenom, ), ), ) } -func (s *MemState) GetBlockCancels(ctx sdk.Context, contractAddr types.ContractAddress, pair types.PairString) *BlockCancellations { +func (s *MemState) GetBlockCancels(ctx sdk.Context, contractAddr types.ContractAddress, pair types.Pair) *BlockCancellations { s.SynchronizeAccess(ctx, contractAddr) - pairType := types.GetPair(pair) return NewCancels( prefix.NewStore( ctx.KVStore(s.storeKey), types.MemCancelPrefixForPair( - string(contractAddr), pairType.PriceDenom, pairType.AssetDenom, + string(contractAddr), pair.PriceDenom, pair.AssetDenom, ), ), ) @@ -125,17 +123,14 @@ func (s *MemState) Clear(ctx sdk.Context) { s.contractsToProcess = &newContractToDependencies } -func (s *MemState) ClearCancellationForPair(ctx sdk.Context, contractAddr types.ContractAddress, pair types.PairString) { +func (s *MemState) ClearCancellationForPair(ctx sdk.Context, contractAddr types.ContractAddress, pair types.Pair) { s.SynchronizeAccess(ctx, contractAddr) DeepDelete(ctx.KVStore(s.storeKey), types.KeyPrefix(types.MemCancelKey), func(v []byte) bool { var c types.Cancellation if err := c.Unmarshal(v); err != nil { panic(err) } - return c.ContractAddr == string(contractAddr) && types.GetPairString(&types.Pair{ - AssetDenom: c.AssetDenom, - PriceDenom: c.PriceDenom, - }) == pair + return c.ContractAddr == string(contractAddr) && c.PriceDenom == pair.PriceDenom && c.AssetDenom == pair.AssetDenom }) } diff --git a/x/dex/cache/cache_test.go b/x/dex/cache/cache_test.go index d975c68e5d..c6603e7914 100644 --- a/x/dex/cache/cache_test.go +++ b/x/dex/cache/cache_test.go @@ -14,48 +14,47 @@ import ( const ( TEST_CONTRACT = "test" - TEST_PAIR = "pair" ) func TestDeepCopy(t *testing.T) { keeper, ctx := keepertest.DexKeeper(t) stateOne := dex.NewMemState(keeper.GetMemStoreKey()) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 1, Account: "test", ContractAddr: TEST_CONTRACT, }) stateTwo := stateOne.DeepCopy() cachedCtx, _ := store.GetCachedContext(ctx) - stateTwo.GetBlockOrders(cachedCtx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateTwo.GetBlockOrders(cachedCtx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 2, Account: "test", ContractAddr: TEST_CONTRACT, }) // old state must not be changed - require.Equal(t, 1, len(stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Get())) + require.Equal(t, 1, len(stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Get())) // new state must be changed - require.Equal(t, 2, len(stateTwo.GetBlockOrders(cachedCtx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Get())) + require.Equal(t, 2, len(stateTwo.GetBlockOrders(cachedCtx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Get())) } func TestDeepFilterAccounts(t *testing.T) { keeper, ctx := keepertest.DexKeeper(t) stateOne := dex.NewMemState(keeper.GetMemStoreKey()) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 1, Account: "test", ContractAddr: TEST_CONTRACT, }) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 2, Account: "test2", ContractAddr: TEST_CONTRACT, }) - stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{ + stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{ Id: 1, Creator: "test", }) - stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{ + stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{ Id: 2, Creator: "test2", }) @@ -68,14 +67,14 @@ func TestDeepFilterAccounts(t *testing.T) { stateOne.DeepFilterAccount(ctx, "test") require.Equal(t, 1, len(stateOne.GetAllBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT)))) - require.Equal(t, 1, len(stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Get())) + require.Equal(t, 1, len(stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Get())) require.Equal(t, 1, len(stateOne.GetDepositInfo(ctx, types.ContractAddress(TEST_CONTRACT)).Get())) } func TestDeepDelete(t *testing.T) { keeper, ctx := keepertest.DexKeeper(t) stateOne := dex.NewMemState(keeper.GetMemStoreKey()) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 1, Account: "test", ContractAddr: TEST_CONTRACT, @@ -86,47 +85,47 @@ func TestDeepDelete(t *testing.T) { func TestClear(t *testing.T) { keeper, ctx := keepertest.DexKeeper(t) stateOne := dex.NewMemState(keeper.GetMemStoreKey()) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 1, Account: "test", ContractAddr: TEST_CONTRACT, }) - stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{ + stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{ Id: 2, ContractAddr: TEST_CONTRACT, }) stateOne.Clear(ctx) - require.Equal(t, 0, len(stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Get())) - require.Equal(t, 0, len(stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Get())) + require.Equal(t, 0, len(stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Get())) + require.Equal(t, 0, len(stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Get())) } func TestClearCancellationForPair(t *testing.T) { keeper, ctx := keepertest.DexKeeper(t) stateOne := dex.NewMemState(keeper.GetMemStoreKey()) - stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{ + stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{ Id: 1, ContractAddr: TEST_CONTRACT, PriceDenom: "USDC", AssetDenom: "ATOM", }) - stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{ + stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{ Id: 2, ContractAddr: TEST_CONTRACT, PriceDenom: "USDC", AssetDenom: "ATOM", }) - stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{ + stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{ Id: 3, ContractAddr: TEST_CONTRACT, PriceDenom: "USDC", AssetDenom: "SEI", }) - stateOne.ClearCancellationForPair(ctx, TEST_CONTRACT, types.GetPairString(&types.Pair{ + stateOne.ClearCancellationForPair(ctx, TEST_CONTRACT, types.Pair{ PriceDenom: "USDC", AssetDenom: "ATOM", - })) - require.Equal(t, 1, len(stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Get())) - require.Equal(t, uint64(3), stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Get()[0].Id) + }) + require.Equal(t, 1, len(stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Get())) + require.Equal(t, uint64(3), stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Get()[0].Id) } func TestSynchronization(t *testing.T) { diff --git a/x/dex/cache/cancel_test.go b/x/dex/cache/cancel_test.go index f6073d0e4a..8f9d51d80f 100644 --- a/x/dex/cache/cancel_test.go +++ b/x/dex/cache/cancel_test.go @@ -12,12 +12,12 @@ import ( func TestCancelGetIdsToCancel(t *testing.T) { keeper, ctx := keepertest.DexKeeper(t) stateOne := dex.NewMemState(keeper.GetMemStoreKey()) - stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{ + stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{ Id: 1, Creator: "abc", ContractAddr: TEST_CONTRACT, }) - ids := stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).GetIdsToCancel() + ids := stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).GetIdsToCancel() require.Equal(t, 1, len(ids)) require.Equal(t, uint64(1), ids[0]) } @@ -25,50 +25,50 @@ func TestCancelGetIdsToCancel(t *testing.T) { func TestCancelGetCancels(t *testing.T) { keeper, ctx := keepertest.DexKeeper(t) stateOne := dex.NewMemState(keeper.GetMemStoreKey()) - stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{ + stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{ Id: 1, Creator: "abc", ContractAddr: TEST_CONTRACT, }) - stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{ + stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{ Id: 2, Creator: "def", ContractAddr: TEST_CONTRACT, }) - stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{ + stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{ Id: 3, Creator: "efg", ContractAddr: TEST_CONTRACT, }) - stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{ + stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{ Id: 4, Creator: "efg", ContractAddr: TEST_CONTRACT, }) - cancels := stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Get() + cancels := stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Get() require.Equal(t, 4, len(cancels)) - require.True(t, stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Has(&types.Cancellation{ + require.True(t, stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Has(&types.Cancellation{ Id: 1, Creator: "abc", ContractAddr: TEST_CONTRACT, })) - require.True(t, stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Has(&types.Cancellation{ + require.True(t, stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Has(&types.Cancellation{ Id: 2, Creator: "def", ContractAddr: TEST_CONTRACT, })) - require.True(t, stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Has(&types.Cancellation{ + require.True(t, stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Has(&types.Cancellation{ Id: 3, Creator: "efg", ContractAddr: TEST_CONTRACT, })) - require.True(t, stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Has(&types.Cancellation{ + require.True(t, stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Has(&types.Cancellation{ Id: 4, Creator: "efg", ContractAddr: TEST_CONTRACT, })) - require.False(t, stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Has(&types.Cancellation{ + require.False(t, stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Has(&types.Cancellation{ Id: 5, Creator: "efg", ContractAddr: TEST_CONTRACT, diff --git a/x/dex/cache/order_test.go b/x/dex/cache/order_test.go index 50b317f5a9..b5ab037156 100644 --- a/x/dex/cache/order_test.go +++ b/x/dex/cache/order_test.go @@ -13,7 +13,7 @@ import ( func TestMarkFailedToPlace(t *testing.T) { keeper, ctx := keepertest.DexKeeper(t) stateOne := dex.NewMemState(keeper.GetMemStoreKey()) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 1, Account: "test", ContractAddr: TEST_CONTRACT, @@ -22,17 +22,17 @@ func TestMarkFailedToPlace(t *testing.T) { ID: 1, Reason: "some reason", } - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).MarkFailedToPlace([]types.UnsuccessfulOrder{unsuccessfulOrder}) + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).MarkFailedToPlace([]types.UnsuccessfulOrder{unsuccessfulOrder}) require.Equal(t, types.OrderStatus_FAILED_TO_PLACE, - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Get()[0].Status) + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Get()[0].Status) require.Equal(t, "some reason", - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Get()[0].StatusDescription) + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Get()[0].StatusDescription) } func TestGetByID(t *testing.T) { keeper, ctx := keepertest.DexKeeper(t) stateOne := dex.NewMemState(keeper.GetMemStoreKey()) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 1, Account: "test1", ContractAddr: TEST_CONTRACT, @@ -40,7 +40,7 @@ func TestGetByID(t *testing.T) { OrderType: types.OrderType_LIMIT, Price: sdk.MustNewDecFromStr("150"), }) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 2, Account: "test2", ContractAddr: TEST_CONTRACT, @@ -50,9 +50,9 @@ func TestGetByID(t *testing.T) { }) order1 := stateOne.GetBlockOrders( - ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).GetByID(uint64(1)) + ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).GetByID(uint64(1)) order2 := stateOne.GetBlockOrders( - ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).GetByID(uint64(2)) + ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).GetByID(uint64(2)) require.Equal(t, uint64(1), order1.Id) require.Equal(t, uint64(2), order2.Id) require.Equal(t, "test1", order1.Account) @@ -70,7 +70,7 @@ func TestGetByID(t *testing.T) { func TestGetSortedMarketOrders(t *testing.T) { keeper, ctx := keepertest.DexKeeper(t) stateOne := dex.NewMemState(keeper.GetMemStoreKey()) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 1, Account: "test", ContractAddr: TEST_CONTRACT, @@ -78,7 +78,7 @@ func TestGetSortedMarketOrders(t *testing.T) { OrderType: types.OrderType_MARKET, Price: sdk.MustNewDecFromStr("150"), }) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 2, Account: "test", ContractAddr: TEST_CONTRACT, @@ -86,7 +86,7 @@ func TestGetSortedMarketOrders(t *testing.T) { OrderType: types.OrderType_MARKET, Price: sdk.MustNewDecFromStr("100"), }) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 3, Account: "test", ContractAddr: TEST_CONTRACT, @@ -94,7 +94,7 @@ func TestGetSortedMarketOrders(t *testing.T) { OrderType: types.OrderType_MARKET, Price: sdk.MustNewDecFromStr("0"), }) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 4, Account: "test", ContractAddr: TEST_CONTRACT, @@ -102,7 +102,7 @@ func TestGetSortedMarketOrders(t *testing.T) { OrderType: types.OrderType_MARKET, Price: sdk.MustNewDecFromStr("100"), }) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 5, Account: "test", ContractAddr: TEST_CONTRACT, @@ -110,7 +110,7 @@ func TestGetSortedMarketOrders(t *testing.T) { OrderType: types.OrderType_MARKET, Price: sdk.MustNewDecFromStr("80"), }) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 6, Account: "test", ContractAddr: TEST_CONTRACT, @@ -118,7 +118,7 @@ func TestGetSortedMarketOrders(t *testing.T) { OrderType: types.OrderType_MARKET, Price: sdk.MustNewDecFromStr("0"), }) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 7, Account: "test", ContractAddr: TEST_CONTRACT, @@ -126,7 +126,7 @@ func TestGetSortedMarketOrders(t *testing.T) { OrderType: types.OrderType_LIMIT, Price: sdk.MustNewDecFromStr("100"), }) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 8, Account: "test", ContractAddr: TEST_CONTRACT, @@ -134,7 +134,7 @@ func TestGetSortedMarketOrders(t *testing.T) { OrderType: types.OrderType_LIMIT, Price: sdk.MustNewDecFromStr("100"), }) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 9, Account: "test", ContractAddr: TEST_CONTRACT, @@ -142,7 +142,7 @@ func TestGetSortedMarketOrders(t *testing.T) { OrderType: types.OrderType_FOKMARKET, Price: sdk.MustNewDecFromStr("100"), }) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 10, Account: "test", ContractAddr: TEST_CONTRACT, @@ -150,7 +150,7 @@ func TestGetSortedMarketOrders(t *testing.T) { OrderType: types.OrderType_FOKMARKET, Price: sdk.MustNewDecFromStr("0"), }) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 11, Account: "test", ContractAddr: TEST_CONTRACT, @@ -158,7 +158,7 @@ func TestGetSortedMarketOrders(t *testing.T) { OrderType: types.OrderType_FOKMARKET, Price: sdk.MustNewDecFromStr("150"), }) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 12, Account: "test", ContractAddr: TEST_CONTRACT, @@ -166,7 +166,7 @@ func TestGetSortedMarketOrders(t *testing.T) { OrderType: types.OrderType_FOKMARKET, Price: sdk.MustNewDecFromStr("0"), }) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 13, Account: "test", ContractAddr: TEST_CONTRACT, @@ -174,7 +174,7 @@ func TestGetSortedMarketOrders(t *testing.T) { OrderType: types.OrderType_FOKMARKET, Price: sdk.MustNewDecFromStr("150"), }) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 14, Account: "test", ContractAddr: TEST_CONTRACT, @@ -182,7 +182,7 @@ func TestGetSortedMarketOrders(t *testing.T) { OrderType: types.OrderType_FOKMARKET, Price: sdk.MustNewDecFromStr("100"), }) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 15, Account: "test", ContractAddr: TEST_CONTRACT, @@ -190,7 +190,7 @@ func TestGetSortedMarketOrders(t *testing.T) { OrderType: types.OrderType_FOKMARKETBYVALUE, Price: sdk.MustNewDecFromStr("100"), }) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 16, Account: "test", ContractAddr: TEST_CONTRACT, @@ -198,7 +198,7 @@ func TestGetSortedMarketOrders(t *testing.T) { OrderType: types.OrderType_FOKMARKETBYVALUE, Price: sdk.MustNewDecFromStr("0"), }) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 17, Account: "test", ContractAddr: TEST_CONTRACT, @@ -206,7 +206,7 @@ func TestGetSortedMarketOrders(t *testing.T) { OrderType: types.OrderType_FOKMARKETBYVALUE, Price: sdk.MustNewDecFromStr("150"), }) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 18, Account: "test", ContractAddr: TEST_CONTRACT, @@ -214,7 +214,7 @@ func TestGetSortedMarketOrders(t *testing.T) { OrderType: types.OrderType_FOKMARKETBYVALUE, Price: sdk.MustNewDecFromStr("0"), }) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 19, Account: "test", ContractAddr: TEST_CONTRACT, @@ -222,7 +222,7 @@ func TestGetSortedMarketOrders(t *testing.T) { OrderType: types.OrderType_FOKMARKETBYVALUE, Price: sdk.MustNewDecFromStr("150"), }) - stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{ + stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{ Id: 20, Account: "test", ContractAddr: TEST_CONTRACT, @@ -232,7 +232,7 @@ func TestGetSortedMarketOrders(t *testing.T) { }) marketBuys := stateOne.GetBlockOrders( - ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).GetSortedMarketOrders(types.PositionDirection_LONG) + ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).GetSortedMarketOrders(types.PositionDirection_LONG) require.Equal(t, uint64(16), marketBuys[0].Id) require.Equal(t, uint64(10), marketBuys[1].Id) require.Equal(t, uint64(3), marketBuys[2].Id) @@ -244,7 +244,7 @@ func TestGetSortedMarketOrders(t *testing.T) { require.Equal(t, uint64(15), marketBuys[8].Id) marketSells := stateOne.GetBlockOrders( - ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).GetSortedMarketOrders(types.PositionDirection_SHORT) + ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).GetSortedMarketOrders(types.PositionDirection_SHORT) require.Equal(t, uint64(18), marketSells[0].Id) require.Equal(t, uint64(12), marketSells[1].Id) require.Equal(t, uint64(6), marketSells[2].Id) diff --git a/x/dex/contract/execution.go b/x/dex/contract/execution.go index 4715ce914b..db31f2f086 100644 --- a/x/dex/contract/execution.go +++ b/x/dex/contract/execution.go @@ -51,17 +51,16 @@ func ExecutePair( orderbook *types.OrderBook, ) []*types.SettlementEntry { typedContractAddr := types.ContractAddress(contractAddr) - typedPairStr := types.GetPairString(&pair) // First cancel orders cancelForPair(ctx, dexkeeper, typedContractAddr, pair) // Add all limit orders to the orderbook - orders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, typedContractAddr, typedPairStr) + orders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, typedContractAddr, pair) limitBuys := orders.GetLimitOrders(types.PositionDirection_LONG) limitSells := orders.GetLimitOrders(types.PositionDirection_SHORT) exchange.AddOutstandingLimitOrdersToOrderbook(ctx, dexkeeper, limitBuys, limitSells) // Fill market orders - marketOrderOutcome := matchMarketOrderForPair(ctx, typedContractAddr, typedPairStr, orderbook) + marketOrderOutcome := matchMarketOrderForPair(ctx, typedContractAddr, pair, orderbook) // Fill limit orders limitOrderOutcome := exchange.MatchLimitOrders(ctx, orderbook) totalOutcome := marketOrderOutcome.Merge(&limitOrderOutcome) @@ -77,17 +76,17 @@ func cancelForPair( contractAddress types.ContractAddress, pair types.Pair, ) { - cancels := dexutils.GetMemState(ctx.Context()).GetBlockCancels(ctx, contractAddress, types.GetPairString(&pair)) + cancels := dexutils.GetMemState(ctx.Context()).GetBlockCancels(ctx, contractAddress, pair) exchange.CancelOrders(ctx, keeper, contractAddress, pair, cancels.Get()) } func matchMarketOrderForPair( ctx sdk.Context, typedContractAddr types.ContractAddress, - typedPairStr types.PairString, + pair types.Pair, orderbook *types.OrderBook, ) exchange.ExecutionOutcome { - orders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, typedContractAddr, typedPairStr) + orders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, typedContractAddr, pair) marketBuys := orders.GetSortedMarketOrders(types.PositionDirection_LONG) marketSells := orders.GetSortedMarketOrders(types.PositionDirection_SHORT) marketBuyOutcome := exchange.MatchMarketOrders( @@ -110,10 +109,10 @@ func matchMarketOrderForPair( func GetMatchResults( ctx sdk.Context, typedContractAddr types.ContractAddress, - typedPairStr types.PairString, + pair types.Pair, ) ([]*types.Order, []*types.Cancellation) { - orderResults := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, typedContractAddr, typedPairStr).Get() - cancelResults := dexutils.GetMemState(ctx.Context()).GetBlockCancels(ctx, typedContractAddr, typedPairStr).Get() + orderResults := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, typedContractAddr, pair).Get() + cancelResults := dexutils.GetMemState(ctx.Context()).GetBlockCancels(ctx, typedContractAddr, pair).Get() return orderResults, cancelResults } @@ -148,15 +147,15 @@ func ExecutePairsInParallel(ctx sdk.Context, contractAddr string, dexkeeper *kee pairStr := types.GetPairString(&pairCopy) orderbook, found := orderBooks.Load(pairStr) if !found { - panic(fmt.Sprintf("Orderbook not found for %s", pairStr)) + panic(fmt.Sprintf("Orderbook not found for %s", pairCopy.String())) } pairSettlements := ExecutePair(pairCtx, contractAddr, pair, dexkeeper, orderbook) orderIDToSettledQuantities := GetOrderIDToSettledQuantities(pairSettlements) - PrepareCancelUnfulfilledMarketOrders(pairCtx, typedContractAddr, pairStr, orderIDToSettledQuantities) + PrepareCancelUnfulfilledMarketOrders(pairCtx, typedContractAddr, pairCopy, orderIDToSettledQuantities) mu.Lock() defer mu.Unlock() - orders, cancels := GetMatchResults(ctx, typedContractAddr, types.GetPairString(&pairCopy)) + orders, cancels := GetMatchResults(ctx, typedContractAddr, pairCopy) orderResults = append(orderResults, orders...) cancelResults = append(cancelResults, cancels...) settlements = append(settlements, pairSettlements...) diff --git a/x/dex/contract/execution_test.go b/x/dex/contract/execution_test.go index 887a153fb5..e83aa372ab 100644 --- a/x/dex/contract/execution_test.go +++ b/x/dex/contract/execution_test.go @@ -114,7 +114,7 @@ func TestExecutePair(t *testing.T) { require.Equal(t, len(settlements), 0) // add Market orders to the orderbook - dexutil.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.GetPairString(&pair)).Add( + dexutil.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), pair).Add( &types.Order{ Id: 1, Account: TEST_ACCOUNT, @@ -128,7 +128,7 @@ func TestExecutePair(t *testing.T) { Data: "{\"position_effect\":\"Open\",\"leverage\":\"1\"}", }, ) - dexutil.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.GetPairString(&pair)).Add( + dexutil.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), pair).Add( &types.Order{ Id: 2, Account: TEST_ACCOUNT, @@ -142,7 +142,7 @@ func TestExecutePair(t *testing.T) { Data: "{\"position_effect\":\"Open\",\"leverage\":\"1\"}", }, ) - dexutil.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.GetPairString(&pair)).Add( + dexutil.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), pair).Add( &types.Order{ Id: 3, Account: TEST_ACCOUNT, @@ -173,7 +173,7 @@ func TestExecutePair(t *testing.T) { matches, cancels := contract.GetMatchResults( ctx, TEST_CONTRACT, - types.GetPairString(&pair), + pair, ) require.Equal(t, 3, len(matches)) require.Equal(t, 0, len(cancels)) @@ -268,7 +268,7 @@ func TestExecutePairInParallel(t *testing.T) { require.Equal(t, len(settlements), 0) // add Market orders to the orderbook - dexutil.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.GetPairString(&pair)).Add( + dexutil.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), pair).Add( &types.Order{ Id: 1, Account: TEST_ACCOUNT, @@ -282,7 +282,7 @@ func TestExecutePairInParallel(t *testing.T) { Data: "{\"position_effect\":\"Open\",\"leverage\":\"1\"}", }, ) - dexutil.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.GetPairString(&pair)).Add( + dexutil.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), pair).Add( &types.Order{ Id: 2, Account: TEST_ACCOUNT, @@ -296,7 +296,7 @@ func TestExecutePairInParallel(t *testing.T) { Data: "{\"position_effect\":\"Open\",\"leverage\":\"1\"}", }, ) - dexutil.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.GetPairString(&pair)).Add( + dexutil.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), pair).Add( &types.Order{ Id: 3, Account: TEST_ACCOUNT, @@ -310,7 +310,7 @@ func TestExecutePairInParallel(t *testing.T) { Data: "{\"position_effect\":\"Open\",\"leverage\":\"1\"}", }, ) - dexutil.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.GetPairString(&pair)).Add( + dexutil.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), pair).Add( &types.Order{ Id: 11, Account: TEST_ACCOUNT, diff --git a/x/dex/contract/market_order.go b/x/dex/contract/market_order.go index f3b1b8cec4..3b3c9e4474 100644 --- a/x/dex/contract/market_order.go +++ b/x/dex/contract/market_order.go @@ -15,12 +15,12 @@ import ( func PrepareCancelUnfulfilledMarketOrders( ctx sdk.Context, typedContractAddr types.ContractAddress, - typedPairStr types.PairString, + pair types.Pair, orderIDToSettledQuantities map[uint64]sdk.Dec, ) { - dexutils.GetMemState(ctx.Context()).ClearCancellationForPair(ctx, typedContractAddr, typedPairStr) - for _, marketOrderID := range getUnfulfilledPlacedMarketOrderIds(ctx, typedContractAddr, typedPairStr, orderIDToSettledQuantities) { - dexutils.GetMemState(ctx.Context()).GetBlockCancels(ctx, typedContractAddr, typedPairStr).Add(&types.Cancellation{ + dexutils.GetMemState(ctx.Context()).ClearCancellationForPair(ctx, typedContractAddr, pair) + for _, marketOrderID := range getUnfulfilledPlacedMarketOrderIds(ctx, typedContractAddr, pair, orderIDToSettledQuantities) { + dexutils.GetMemState(ctx.Context()).GetBlockCancels(ctx, typedContractAddr, pair).Add(&types.Cancellation{ Id: marketOrderID, Initiator: types.CancellationInitiator_USER, }) @@ -30,11 +30,11 @@ func PrepareCancelUnfulfilledMarketOrders( func getUnfulfilledPlacedMarketOrderIds( ctx sdk.Context, typedContractAddr types.ContractAddress, - typedPairStr types.PairString, + pair types.Pair, orderIDToSettledQuantities map[uint64]sdk.Dec, ) []uint64 { res := []uint64{} - for _, order := range dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, typedContractAddr, typedPairStr).Get() { + for _, order := range dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, typedContractAddr, pair).Get() { if order.Status == types.OrderStatus_FAILED_TO_PLACE { continue } diff --git a/x/dex/exchange/market_order_fuzz_test.go b/x/dex/exchange/market_order_fuzz_test.go index 989f77202b..2cc64f4466 100644 --- a/x/dex/exchange/market_order_fuzz_test.go +++ b/x/dex/exchange/market_order_fuzz_test.go @@ -36,7 +36,7 @@ func fuzzTargetMatchMarketOrders( ) { dexkeeper, TestFuzzMarketCtx := keepertest.DexKeeper(t) TestFuzzMarketCtx = TestFuzzMarketCtx.WithBlockHeight(1).WithBlockTime(time.Now()) - blockOrders := dexutils.GetMemState(TestFuzzMarketCtx.Context()).GetBlockOrders(TestFuzzMarketCtx, "testAccount", "USDC|ATOM") + blockOrders := dexutils.GetMemState(TestFuzzMarketCtx.Context()).GetBlockOrders(TestFuzzMarketCtx, "testAccount", types.Pair{PriceDenom: "USDC", AssetDenom: "ATOM"}) entries := fuzzing.GetOrderBookEntries(!takerLong, keepertest.TestPriceDenom, keepertest.TestAssetDenom, entryWeights, accountIndices, allocationWeights) for _, entry := range entries { if takerLong { diff --git a/x/dex/exchange/market_order_test.go b/x/dex/exchange/market_order_test.go index e48f8013e3..86339654b8 100644 --- a/x/dex/exchange/market_order_test.go +++ b/x/dex/exchange/market_order_test.go @@ -52,7 +52,7 @@ func TestMatchFoKMarketOrderFromShortBookNotEnoughLiquidity(t *testing.T) { }, }, } - blockOrders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, "testAccount", "USDC|ATOM") + blockOrders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, "testAccount", types.Pair{PriceDenom: "USDC", AssetDenom: "ATOM"}) blockOrders.Add(&types.Order{ Id: 1, Account: "abc", @@ -126,7 +126,7 @@ func TestMatchFoKMarketOrderFromShortBookHappyPath(t *testing.T) { } orderbook := keeperutil.PopulateOrderbook(ctx, dexkeeper, types.ContractAddress("test"), types.Pair{PriceDenom: "USDC", AssetDenom: "ATOM"}) entries := orderbook.Shorts - blockOrders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, "testAccount", "USDC|ATOM") + blockOrders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, "testAccount", types.Pair{PriceDenom: "USDC", AssetDenom: "ATOM"}) blockOrders.Add(&types.Order{ Id: 1, Account: "abc", @@ -228,7 +228,7 @@ func TestMatchByValueFOKMarketOrderFromShortBookNotEnoughLiquidity(t *testing.T) }, }, } - blockOrders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, "testAccount", "USDC|ATOM") + blockOrders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, "testAccount", types.Pair{PriceDenom: "USDC", AssetDenom: "ATOM"}) blockOrders.Add(longOrders[0]) for _, e := range shortBook { dexkeeper.SetShortOrderBookEntry(ctx, "test", e) @@ -300,7 +300,7 @@ func TestMatchByValueFOKMarketOrderFromShortBookHappyPath(t *testing.T) { }, }, } - blockOrders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, "testAccount", "USDC|ATOM") + blockOrders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, "testAccount", types.Pair{PriceDenom: "USDC", AssetDenom: "ATOM"}) blockOrders.Add(longOrders[0]) for _, e := range shortBook { dexkeeper.SetShortOrderBookEntry(ctx, "test", e) @@ -380,7 +380,7 @@ func TestMatchSingleMarketOrderFromShortBook(t *testing.T) { }, }, } - blockOrders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, "testAccount", "USDC|ATOM") + blockOrders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, "testAccount", types.Pair{PriceDenom: "USDC", AssetDenom: "ATOM"}) blockOrders.Add(longOrders[0]) for _, e := range shortBook { dexkeeper.SetShortOrderBookEntry(ctx, "test", e) @@ -460,7 +460,7 @@ func TestMatchSingleMarketOrderFromLongBook(t *testing.T) { }, }, } - blockOrders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, "testAccount", "USDC|ATOM") + blockOrders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, "testAccount", types.Pair{PriceDenom: "USDC", AssetDenom: "ATOM"}) blockOrders.Add(shortOrders[0]) for _, e := range longBook { dexkeeper.SetLongOrderBookEntry(ctx, "test", e) @@ -558,7 +558,7 @@ func TestMatchSingleMarketOrderFromMultipleShortBook(t *testing.T) { }, }, } - blockOrders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, "testAccount", "USDC|ATOM") + blockOrders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, "testAccount", types.Pair{PriceDenom: "USDC", AssetDenom: "ATOM"}) blockOrders.Add(longOrders[0]) for _, e := range shortBook { dexkeeper.SetShortOrderBookEntry(ctx, "test", e) @@ -698,7 +698,7 @@ func TestMatchSingleMarketOrderFromMultipleLongBook(t *testing.T) { }, }, } - blockOrders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, "testAccount", "USDC|ATOM") + blockOrders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, "testAccount", types.Pair{PriceDenom: "USDC", AssetDenom: "ATOM"}) blockOrders.Add(shortOrders[0]) for _, e := range longBook { dexkeeper.SetLongOrderBookEntry(ctx, "test", e) @@ -860,7 +860,7 @@ func TestMatchMultipleMarketOrderFromMultipleShortBook(t *testing.T) { }, }, } - blockOrders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, "testAccount", "USDC|ATOM") + blockOrders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, "testAccount", types.Pair{PriceDenom: "USDC", AssetDenom: "ATOM"}) blockOrders.Add(longOrders[0]) blockOrders.Add(longOrders[1]) blockOrders.Add(longOrders[2]) @@ -1051,7 +1051,7 @@ func TestMatchMultipleMarketOrderFromMultipleLongBook(t *testing.T) { }, }, } - blockOrders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, "testAccount", "USDC|ATOM") + blockOrders := dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, "testAccount", types.Pair{PriceDenom: "USDC", AssetDenom: "ATOM"}) blockOrders.Add(shortOrders[0]) blockOrders.Add(shortOrders[1]) blockOrders.Add(shortOrders[2]) diff --git a/x/dex/keeper/abci/end_block_cancel_orders.go b/x/dex/keeper/abci/end_block_cancel_orders.go index b72081743d..8c471124cd 100644 --- a/x/dex/keeper/abci/end_block_cancel_orders.go +++ b/x/dex/keeper/abci/end_block_cancel_orders.go @@ -33,8 +33,7 @@ func (w KeeperWrapper) HandleEBCancelOrders(ctx context.Context, sdkCtx sdk.Cont func (w KeeperWrapper) getCancelSudoMsg(sdkCtx sdk.Context, typedContractAddr types.ContractAddress, registeredPairs []types.Pair) types.SudoOrderCancellationMsg { idsToCancel := []uint64{} for _, pair := range registeredPairs { - typedPairStr := types.GetPairString(&pair) //nolint:gosec // THIS MAY BE CAUSE FOR CONCERN AND WE MIGHT WANT TO REFACTOR. - for _, cancel := range dexutils.GetMemState(sdkCtx.Context()).GetBlockCancels(sdkCtx, typedContractAddr, typedPairStr).Get() { + for _, cancel := range dexutils.GetMemState(sdkCtx.Context()).GetBlockCancels(sdkCtx, typedContractAddr, pair).Get() { idsToCancel = append(idsToCancel, cancel.Id) } } diff --git a/x/dex/keeper/abci/end_block_place_orders.go b/x/dex/keeper/abci/end_block_place_orders.go index 22a5af680d..8ab95c6e3e 100644 --- a/x/dex/keeper/abci/end_block_place_orders.go +++ b/x/dex/keeper/abci/end_block_place_orders.go @@ -49,9 +49,8 @@ func (w KeeperWrapper) HandleEBPlaceOrders(ctx context.Context, sdkCtx sdk.Conte } for _, pair := range registeredPairs { - typedPairStr := types.GetPairString(&pair) //nolint:gosec // USING THE POINTER HERE COULD BE BAD, LET'S CHECK IT. for _, response := range responses { - dexutils.GetMemState(sdkCtx.Context()).GetBlockOrders(sdkCtx, typedContractAddr, typedPairStr).MarkFailedToPlace(response.UnsuccessfulOrders) + dexutils.GetMemState(sdkCtx.Context()).GetBlockOrders(sdkCtx, typedContractAddr, pair).MarkFailedToPlace(response.UnsuccessfulOrders) } } return nil @@ -61,8 +60,7 @@ func (w KeeperWrapper) GetPlaceSudoMsg(ctx sdk.Context, typedContractAddr types. msgs := []types.SudoOrderPlacementMsg{} contractOrderPlacements := []types.Order{} for _, pair := range registeredPairs { - typedPairStr := types.GetPairString(&pair) //nolint:gosec // USING THE POINTER HERE COULD BE BAD, LET'S CHECK IT. - for _, order := range dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, typedContractAddr, typedPairStr).Get() { + for _, order := range dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, typedContractAddr, pair).Get() { contractOrderPlacements = append(contractOrderPlacements, *order) if len(contractOrderPlacements) == MaxOrdersPerSudoCall { msgs = append(msgs, types.SudoOrderPlacementMsg{ diff --git a/x/dex/keeper/abci/end_block_place_orders_test.go b/x/dex/keeper/abci/end_block_place_orders_test.go index cbb18d6f09..556c3119cf 100644 --- a/x/dex/keeper/abci/end_block_place_orders_test.go +++ b/x/dex/keeper/abci/end_block_place_orders_test.go @@ -19,7 +19,7 @@ import ( func TestGetPlaceSudoMsg(t *testing.T) { pair := types.Pair{PriceDenom: keepertest.TestPriceDenom, AssetDenom: keepertest.TestAssetDenom} keeper, ctx := keepertest.DexKeeper(t) - dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, keepertest.TestContract, types.GetPairString(&pair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, keepertest.TestContract, pair).Add( &types.Order{ Id: 1, Price: sdk.OneDec(), diff --git a/x/dex/keeper/msgserver/msg_server_cancel_orders.go b/x/dex/keeper/msgserver/msg_server_cancel_orders.go index 817d8aca37..0381e2eaef 100644 --- a/x/dex/keeper/msgserver/msg_server_cancel_orders.go +++ b/x/dex/keeper/msgserver/msg_server_cancel_orders.go @@ -34,8 +34,7 @@ func (k msgServer) CancelOrders(goCtx context.Context, msg *types.MsgCancelOrder return nil, errors.New("cannot cancel orders created by others") } pair := types.Pair{PriceDenom: cancellation.PriceDenom, AssetDenom: cancellation.AssetDenom} - pairStr := types.GetPairString(&pair) - pairBlockCancellations := utils.GetMemState(ctx.Context()).GetBlockCancels(ctx, types.ContractAddress(msg.GetContractAddr()), pairStr) + pairBlockCancellations := utils.GetMemState(ctx.Context()).GetBlockCancels(ctx, types.ContractAddress(msg.GetContractAddr()), pair) if !pairBlockCancellations.Has(cancellation) { // only cancel if it's not cancelled in a previous tx in the same block cancel := types.Cancellation{ diff --git a/x/dex/keeper/msgserver/msg_server_cancel_orders_test.go b/x/dex/keeper/msgserver/msg_server_cancel_orders_test.go index ad09ad02a8..1735fcfffa 100644 --- a/x/dex/keeper/msgserver/msg_server_cancel_orders_test.go +++ b/x/dex/keeper/msgserver/msg_server_cancel_orders_test.go @@ -52,7 +52,7 @@ func TestCancelOrder(t *testing.T) { server := msgserver.NewMsgServerImpl(*keeper) _, err := server.CancelOrders(wctx, msg) - pairBlockCancellations := dexutils.GetMemState(ctx.Context()).GetBlockCancels(ctx, keepertest.TestContract, types.GetPairString(&keepertest.TestPair)) + pairBlockCancellations := dexutils.GetMemState(ctx.Context()).GetBlockCancels(ctx, keepertest.TestContract, keepertest.TestPair) require.Nil(t, err) require.Equal(t, 1, len(pairBlockCancellations.Get())) require.Equal(t, uint64(1), pairBlockCancellations.Get()[0].Id) diff --git a/x/dex/keeper/msgserver/msg_server_place_orders.go b/x/dex/keeper/msgserver/msg_server_place_orders.go index 79671b187e..32f478ec1d 100644 --- a/x/dex/keeper/msgserver/msg_server_place_orders.go +++ b/x/dex/keeper/msgserver/msg_server_place_orders.go @@ -70,11 +70,10 @@ func (k msgServer) PlaceOrders(goCtx context.Context, msg *types.MsgPlaceOrders) return nil, sdkerrors.Wrapf(sdkerrors.ErrKeyNotFound, "the pair {price:%s,asset:%s} has no quantity ticksize configured", order.PriceDenom, order.AssetDenom) } pair := types.Pair{PriceDenom: order.PriceDenom, AssetDenom: order.AssetDenom, PriceTicksize: &priceTicksize, QuantityTicksize: &quantityTicksize} - pairStr := types.GetPairString(&pair) order.Id = nextID order.Account = msg.Creator order.ContractAddr = msg.GetContractAddr() - utils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(msg.GetContractAddr()), pairStr).Add(order) + utils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(msg.GetContractAddr()), pair).Add(order) idsInResp = append(idsInResp, nextID) events = append(events, sdk.NewEvent( types.EventTypePlaceOrder, diff --git a/x/dex/keeper/msgserver/msg_server_register_pairs.go b/x/dex/keeper/msgserver/msg_server_register_pairs.go index 64117a7b88..62a5d26552 100644 --- a/x/dex/keeper/msgserver/msg_server_register_pairs.go +++ b/x/dex/keeper/msgserver/msg_server_register_pairs.go @@ -2,6 +2,7 @@ package msgserver import ( "context" + "strings" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -36,6 +37,9 @@ func (k msgServer) RegisterPairs(goCtx context.Context, msg *types.MsgRegisterPa // Loop through each batch contract pair an individual contract pair, token pair // tuple and register them individually for _, pair := range batchPair.Pairs { + if !isValidDenom(pair.PriceDenom) || !isValidDenom(pair.AssetDenom) { + return nil, sdkerrors.ErrInvalidRequest + } k.AddRegisteredPair(ctx, contractAddr, *pair) events = append(events, sdk.NewEvent( types.EventTypeRegisterPair, @@ -50,3 +54,7 @@ func (k msgServer) RegisterPairs(goCtx context.Context, msg *types.MsgRegisterPa return &types.MsgRegisterPairsResponse{}, nil } + +func isValidDenom(denom string) bool { + return denom != "" && !strings.Contains(denom, types.PairDelim) +} diff --git a/x/dex/keeper/query/grpc_query_order_simulation.go b/x/dex/keeper/query/grpc_query_order_simulation.go index 32876f6155..ab3277e3f8 100644 --- a/x/dex/keeper/query/grpc_query_order_simulation.go +++ b/x/dex/keeper/query/grpc_query_order_simulation.go @@ -52,7 +52,7 @@ func (k KeeperWrapper) getMatchedPriceQuantities(ctx sdk.Context, req *types.Que // exclude liquidity to be cancelled pair := types.Pair{PriceDenom: req.Order.PriceDenom, AssetDenom: req.Order.AssetDenom} - for _, cancel := range dexutils.GetMemState(ctx.Context()).GetBlockCancels(ctx, types.ContractAddress(req.ContractAddr), types.GetPairString(&pair)).Get() { + for _, cancel := range dexutils.GetMemState(ctx.Context()).GetBlockCancels(ctx, types.ContractAddress(req.ContractAddr), pair).Get() { var cancelledAllocation *types.Allocation var found bool if cancel.PositionDirection == types.PositionDirection_LONG { @@ -85,7 +85,7 @@ func (k KeeperWrapper) getMatchedPriceQuantities(ctx sdk.Context, req *types.Que // exclude liquidity to be taken ptr := 0 - for _, order := range dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(req.ContractAddr), types.GetPairString(&pair)).GetSortedMarketOrders(orderDirection) { + for _, order := range dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(req.ContractAddr), pair).GetSortedMarketOrders(orderDirection) { // If existing market order has price zero, it means it doesn't specify a worst price and will always have precedence over the simulated // order if !order.Price.IsZero() { diff --git a/x/dex/keeper/query/grpc_query_order_simulation_test.go b/x/dex/keeper/query/grpc_query_order_simulation_test.go index 8406ace3ec..07bfb2ccb1 100644 --- a/x/dex/keeper/query/grpc_query_order_simulation_test.go +++ b/x/dex/keeper/query/grpc_query_order_simulation_test.go @@ -76,7 +76,7 @@ func TestGetOrderSimulation(t *testing.T) { }, }, }) - dexutils.GetMemState(ctx.Context()).GetBlockCancels(ctx, types.ContractAddress(keepertest.TestContract), types.GetPairString(&keepertest.TestPair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockCancels(ctx, types.ContractAddress(keepertest.TestContract), keepertest.TestPair).Add( &types.Cancellation{Id: 1, Price: sdk.MustNewDecFromStr("9"), PositionDirection: types.PositionDirection_SHORT, PriceDenom: keepertest.TestPriceDenom, AssetDenom: keepertest.TestAssetDenom}, ) res, err = wrapper.GetOrderSimulation(wctx, &types.QueryOrderSimulationRequest{Order: &testOrder, ContractAddr: keepertest.TestContract}) @@ -84,7 +84,7 @@ func TestGetOrderSimulation(t *testing.T) { require.Equal(t, sdk.OneDec(), *res.ExecutedQuantity) // liquidity taken by earlier market orders - dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(keepertest.TestContract), types.GetPairString(&keepertest.TestPair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(keepertest.TestContract), keepertest.TestPair).Add( &types.Order{ Account: keepertest.TestAccount, PriceDenom: keepertest.TestPriceDenom, diff --git a/x/dex/module_test.go b/x/dex/module_test.go index 0f5525ba00..cea426c10e 100644 --- a/x/dex/module_test.go +++ b/x/dex/module_test.go @@ -84,7 +84,7 @@ func TestEndBlockMarketOrder(t *testing.T) { panic(err) } dexkeeper.AddRegisteredPair(ctx, contractAddr.String(), pair) - dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), types.GetPairString(&pair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), pair).Add( &types.Order{ Id: 1, Account: testAccount.String(), @@ -98,7 +98,7 @@ func TestEndBlockMarketOrder(t *testing.T) { Data: "{\"position_effect\":\"Open\",\"leverage\":\"1\"}", }, ) - dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), types.GetPairString(&pair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), pair).Add( &types.Order{ Id: 2, Account: testAccount.String(), @@ -128,7 +128,7 @@ func TestEndBlockMarketOrder(t *testing.T) { require.True(t, found) dexutils.GetMemState(ctx.Context()).Clear(ctx) - dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), types.GetPairString(&pair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), pair).Add( &types.Order{ Id: 3, Account: testAccount.String(), @@ -160,7 +160,7 @@ func TestEndBlockMarketOrder(t *testing.T) { require.Equal(t, 2, len(matchResults.Settlements)) dexutils.GetMemState(ctx.Context()).Clear(ctx) - dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), types.GetPairString(&pair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), pair).Add( &types.Order{ Id: 4, Account: testAccount.String(), @@ -217,7 +217,7 @@ func TestEndBlockLimitOrder(t *testing.T) { dexkeeper.SetContract(ctx, &types.ContractInfoV2{CodeId: 123, ContractAddr: contractAddr.String(), NeedHook: false, NeedOrderMatching: true, RentBalance: 100000000}) dexkeeper.AddRegisteredPair(ctx, contractAddr.String(), pair) - dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), types.GetPairString(&pair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), pair).Add( &types.Order{ Id: 1, Account: testAccount.String(), @@ -231,7 +231,7 @@ func TestEndBlockLimitOrder(t *testing.T) { Data: "{\"position_effect\":\"Open\",\"leverage\":\"1\"}", }, ) - dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), types.GetPairString(&pair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), pair).Add( &types.Order{ Id: 2, Account: testAccount.String(), @@ -245,7 +245,7 @@ func TestEndBlockLimitOrder(t *testing.T) { Data: "{\"position_effect\":\"Open\",\"leverage\":\"1\"}", }, ) - dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), types.GetPairString(&pair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), pair).Add( &types.Order{ Id: 3, Account: testAccount.String(), @@ -278,7 +278,7 @@ func TestEndBlockLimitOrder(t *testing.T) { require.True(t, found) dexutils.GetMemState(ctx.Context()).Clear(ctx) - dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), types.GetPairString(&pair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), pair).Add( &types.Order{ Id: 4, Account: testAccount.String(), @@ -292,7 +292,7 @@ func TestEndBlockLimitOrder(t *testing.T) { Data: "{\"position_effect\":\"Open\",\"leverage\":\"1\"}", }, ) - dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), types.GetPairString(&pair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), pair).Add( &types.Order{ Id: 5, Account: testAccount.String(), @@ -325,7 +325,7 @@ func TestEndBlockLimitOrder(t *testing.T) { require.Equal(t, 4, len(matchResults.Settlements)) dexutils.GetMemState(ctx.Context()).Clear(ctx) - dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), types.GetPairString(&pair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), pair).Add( &types.Order{ Id: 6, Account: testAccount.String(), @@ -366,7 +366,7 @@ func TestEndBlockRollback(t *testing.T) { dexkeeper.SetContract(ctx, &types.ContractInfoV2{CodeId: 123, ContractAddr: keepertest.TestContract, NeedHook: false, NeedOrderMatching: true, RentBalance: 100000000}) dexkeeper.AddRegisteredPair(ctx, keepertest.TestContract, pair) // place one order to a nonexistent contract - dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(keepertest.TestContract), types.GetPairString(&pair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(keepertest.TestContract), pair).Add( &types.Order{ Id: 1, Account: keepertest.TestAccount, @@ -402,7 +402,7 @@ func TestEndBlockPartialRollback(t *testing.T) { dexkeeper.SetContract(ctx, &types.ContractInfoV2{CodeId: 123, ContractAddr: keepertest.TestContract, NeedHook: false, NeedOrderMatching: true, RentBalance: 100000000}) dexkeeper.AddRegisteredPair(ctx, keepertest.TestContract, pair) // place one order to a nonexistent contract - dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(keepertest.TestContract), types.GetPairString(&pair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(keepertest.TestContract), pair).Add( &types.Order{ Id: 1, Account: keepertest.TestAccount, @@ -443,7 +443,7 @@ func TestEndBlockPartialRollback(t *testing.T) { dexkeeper.SetContract(ctx, &types.ContractInfoV2{CodeId: 123, ContractAddr: contractAddr.String(), NeedHook: false, NeedOrderMatching: true, RentBalance: 100000000}) dexkeeper.AddRegisteredPair(ctx, contractAddr.String(), pair) // place one order to the good contract - dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), types.GetPairString(&pair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), pair).Add( &types.Order{ Id: 2, Account: testAccount.String(), @@ -549,7 +549,7 @@ func TestEndBlockPanicHandling(t *testing.T) { } dexkeeper.SetContract(ctx, &types.ContractInfoV2{CodeId: 123, ContractAddr: contractAddr.String(), NeedHook: false, NeedOrderMatching: true, RentBalance: 100000000}) dexkeeper.AddRegisteredPair(ctx, contractAddr.String(), pair) - dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), types.GetPairString(&pair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), pair).Add( &types.Order{ Id: 1, Account: testAccount.String(), @@ -609,7 +609,7 @@ func TestEndBlockRollbackWithRentCharge(t *testing.T) { dexkeeper.SetContract(ctx, &types.ContractInfoV2{CodeId: 123, ContractAddr: contractAddr.String(), NeedHook: false, NeedOrderMatching: true, RentBalance: 1}) dexkeeper.AddRegisteredPair(ctx, contractAddr.String(), pair) // place one order to a nonexistent contract - dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), types.GetPairString(&pair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), pair).Add( &types.Order{ Id: 2, Account: testAccount.String(), @@ -737,7 +737,7 @@ func TestOrderCountUpdate(t *testing.T) { dexkeeper.SetContract(ctx, &types.ContractInfoV2{CodeId: 123, ContractAddr: contractAddr.String(), NeedHook: false, NeedOrderMatching: true, RentBalance: 100000000}) dexkeeper.AddRegisteredPair(ctx, contractAddr.String(), pair) - dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), types.GetPairString(&pair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), pair).Add( &types.Order{ Id: 1, Account: testAccount.String(), @@ -751,7 +751,7 @@ func TestOrderCountUpdate(t *testing.T) { Data: "{\"position_effect\":\"Open\",\"leverage\":\"1\"}", }, ) - dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), types.GetPairString(&pair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), pair).Add( &types.Order{ Id: 2, Account: testAccount.String(), @@ -765,7 +765,7 @@ func TestOrderCountUpdate(t *testing.T) { Data: "{\"position_effect\":\"Open\",\"leverage\":\"1\"}", }, ) - dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), types.GetPairString(&pair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), pair).Add( &types.Order{ Id: 3, Account: testAccount.String(), @@ -798,7 +798,7 @@ func TestOrderCountUpdate(t *testing.T) { require.Equal(t, uint64(1), dexkeeper.GetOrderCountState(ctx, contractAddr.String(), pair.PriceDenom, pair.AssetDenom, types.PositionDirection_SHORT, sdk.NewDec(3))) dexutils.GetMemState(ctx.Context()).Clear(ctx) - dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), types.GetPairString(&pair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, types.ContractAddress(contractAddr.String()), pair).Add( &types.Order{ Id: 4, Account: testAccount.String(), @@ -812,7 +812,7 @@ func TestOrderCountUpdate(t *testing.T) { Data: "{\"position_effect\":\"Open\",\"leverage\":\"1\"}", }, ) - dexutils.GetMemState(ctx.Context()).GetBlockCancels(ctx, types.ContractAddress(contractAddr.String()), types.GetPairString(&pair)).Add( + dexutils.GetMemState(ctx.Context()).GetBlockCancels(ctx, types.ContractAddress(contractAddr.String()), pair).Add( &types.Cancellation{ Id: 2, Creator: testAccount.String(), diff --git a/x/dex/types/alias.go b/x/dex/types/alias.go index 768b0ae645..e506036314 100644 --- a/x/dex/types/alias.go +++ b/x/dex/types/alias.go @@ -10,22 +10,14 @@ type ( PairString string ) -const Separator = "|" +const PairDelim = "|" func GetPairString(pair *Pair) PairString { return PairString( - fmt.Sprintf("%s%s%s", pair.PriceDenom, Separator, pair.AssetDenom), + fmt.Sprintf("%s%s%s", pair.PriceDenom, PairDelim, pair.AssetDenom), ) } -func GetPair(s PairString) *Pair { - parts := strings.Split(string(s), Separator) - return &Pair{ - PriceDenom: parts[0], - AssetDenom: parts[1], - } -} - func GetPriceAssetString(pairString PairString) (string, string) { output := strings.Split(string(pairString), "|") return output[0], output[1] diff --git a/x/dex/types/keys.go b/x/dex/types/keys.go index 0b4b4a7980..cc6f0c9fdd 100644 --- a/x/dex/types/keys.go +++ b/x/dex/types/keys.go @@ -163,14 +163,16 @@ const ( AccountActiveOrdersKey = "account-active-orders" CancelKey = "cancel" - TwapKey = "TWAP-" - PriceKey = "Price-" - NextOrderIDKey = "noid" - RegisteredPairKey = "rp" - AssetListKey = "AssetList-" - MatchResultKey = "MatchResult-" - LongOrderCountKey = "loc-" - ShortOrderCountKey = "soc-" + TwapKey = "TWAP-" + PriceKey = "Price-" + SettlementEntryKey = "SettlementEntry-" + NextSettlementIDKey = "NextSettlementID-" + NextOrderIDKey = "noid" + RegisteredPairKey = "rp" + AssetListKey = "AssetList-" + MatchResultKey = "MatchResult-" + LongOrderCountKey = "loc-" + ShortOrderCountKey = "soc-" MemOrderKey = "MemOrder-" MemDepositKey = "MemDeposit-"