From e9c6331b226906f97c583a5ea80b564ffcc8064e Mon Sep 17 00:00:00 2001 From: jouzo Date: Tue, 22 Feb 2022 12:42:55 +0100 Subject: [PATCH 1/2] Use direct path in testpoolswap when available --- src/masternodes/rpc_poolpair.cpp | 4 +++- test/functional/feature_poolswap_composite.py | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/masternodes/rpc_poolpair.cpp b/src/masternodes/rpc_poolpair.cpp index 13201d912f5..44d09888c33 100644 --- a/src/masternodes/rpc_poolpair.cpp +++ b/src/masternodes/rpc_poolpair.cpp @@ -1050,9 +1050,11 @@ UniValue testpoolswap(const JSONRPCRequest& request) { int targetHeight = ::ChainActive().Height() + 1; + auto poolPair = mnview_dummy.GetPoolPair(poolSwapMsg.idTokenFrom, poolSwapMsg.idTokenTo); + if (poolPair) path = "direct"; + // If no direct swap found search for composite swap if (path == "direct") { - auto poolPair = mnview_dummy.GetPoolPair(poolSwapMsg.idTokenFrom, poolSwapMsg.idTokenTo); if (!poolPair) throw JSONRPCError(RPC_INVALID_REQUEST, std::string{"Direct pool pair not found. Use 'auto' mode to use composite swap."}); diff --git a/test/functional/feature_poolswap_composite.py b/test/functional/feature_poolswap_composite.py index 47536e80758..2bc78450399 100755 --- a/test/functional/feature_poolswap_composite.py +++ b/test/functional/feature_poolswap_composite.py @@ -68,6 +68,7 @@ def run_test(self): self.setup_tokens(tokens) disconnect_nodes(self.nodes[0], 1) + symbolDFI = "DFI" symbolDOGE = "DOGE#" + self.get_id_token("DOGE") symbolTSLA = "TSLA#" + self.get_id_token("TSLA") symbolDUSD = "DUSD#" + self.get_id_token("DUSD") @@ -300,6 +301,25 @@ def run_test(self): psTestTokenId = customPathPoolSwap[1] assert_equal(psTestTokenId, idDOGE) + estimateCompositePathsResDirect = self.nodes[0].testpoolswap({ + "from": source, + "tokenFrom": symbolLTC, + "amountFrom": 1, + "to": destination, + "tokenTo": symbolDFI, + }, "direct", True) + + estimateCompositePathsResAuto = self.nodes[0].testpoolswap({ + "from": source, + "tokenFrom": symbolLTC, + "amountFrom": 1, + "to": destination, + "tokenTo": symbolDFI, + }, "auto", True) + + assert_equal(estimateCompositePathsResAuto['path'], "direct") + assert_equal(estimateCompositePathsResDirect, estimateCompositePathsResAuto) + assert_raises_rpc_error(-32600, "Custom pool path is invalid.", self.nodes[0].testpoolswap, { "from": source, From 8c38863db609d205d70efa28d9e8a596d405e75c Mon Sep 17 00:00:00 2001 From: jouzo Date: Wed, 2 Mar 2022 14:27:30 +0100 Subject: [PATCH 2/2] Add 'composite' path option in testpoolswap --- src/masternodes/rpc_poolpair.cpp | 5 ++-- test/functional/feature_poolswap_composite.py | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/masternodes/rpc_poolpair.cpp b/src/masternodes/rpc_poolpair.cpp index 44d09888c33..aae87522eb9 100644 --- a/src/masternodes/rpc_poolpair.cpp +++ b/src/masternodes/rpc_poolpair.cpp @@ -997,6 +997,7 @@ UniValue testpoolswap(const JSONRPCRequest& request) { "One of auto/direct (default = direct)\n" "auto - automatically use composite swap or direct swap as needed.\n" "direct - uses direct path only or fails.\n" + "composite - uses composite path only or fails.\n" "Note: The default will be switched to auto in the upcoming versions." }, { @@ -1051,7 +1052,7 @@ UniValue testpoolswap(const JSONRPCRequest& request) { int targetHeight = ::ChainActive().Height() + 1; auto poolPair = mnview_dummy.GetPoolPair(poolSwapMsg.idTokenFrom, poolSwapMsg.idTokenTo); - if (poolPair) path = "direct"; + if (poolPair && path == "auto") path = "direct"; // If no direct swap found search for composite swap if (path == "direct") { @@ -1089,7 +1090,7 @@ UniValue testpoolswap(const JSONRPCRequest& request) { auto compositeSwap = CPoolSwap(poolSwapMsg, targetHeight); std::vector poolIds; - if (path == "auto") { + if (path == "auto" || path == "composite") { poolIds = compositeSwap.CalculateSwaps(mnview_dummy, true); } else { path = "custom"; diff --git a/test/functional/feature_poolswap_composite.py b/test/functional/feature_poolswap_composite.py index 2bc78450399..29755ec1885 100755 --- a/test/functional/feature_poolswap_composite.py +++ b/test/functional/feature_poolswap_composite.py @@ -320,6 +320,30 @@ def run_test(self): assert_equal(estimateCompositePathsResAuto['path'], "direct") assert_equal(estimateCompositePathsResDirect, estimateCompositePathsResAuto) + estimateCompositePathsResComposite = self.nodes[0].testpoolswap({ + "from": source, + "tokenFrom": symbolLTC, + "amountFrom": 1, + "to": destination, + "tokenTo": symbolDFI, + }, "composite", True) + + assert_equal(estimateCompositePathsResComposite['path'], "composite") + + poolLTC_USDC = list(self.nodes[0].getpoolpair("LTC-USDC").keys())[0] + poolDOGE_USDC = list(self.nodes[0].getpoolpair("DOGE-USDC").keys())[0] + poolDOGE_DFI = list(self.nodes[0].getpoolpair("DOGE-DFI").keys())[0] + assert_equal(estimateCompositePathsResComposite['pools'], [poolLTC_USDC, poolDOGE_USDC, poolDOGE_DFI]) + + assert_raises_rpc_error(-32600, "Cannot find usable pool pair.", self.nodes[0].testpoolswap, + { + "from": source, + "tokenFrom": symbolDUSD, + "amountFrom": 100, + "to": destination, + "tokenTo": symbolDFI, + }, "composite") + assert_raises_rpc_error(-32600, "Custom pool path is invalid.", self.nodes[0].testpoolswap, { "from": source,