From 3c2ec74656de22b201628004ad77cf19ded849ec Mon Sep 17 00:00:00 2001 From: Jouzo Date: Wed, 9 Mar 2022 10:14:43 +0100 Subject: [PATCH] Use direct path in testpoolswap when available (#1115) * Use direct path in testpoolswap when available * Add 'composite' path option in testpoolswap Co-authored-by: Prasanna Loganathar --- src/masternodes/rpc_poolpair.cpp | 7 ++- test/functional/feature_poolswap_composite.py | 44 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/masternodes/rpc_poolpair.cpp b/src/masternodes/rpc_poolpair.cpp index 13201d912f..aae87522eb 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." }, { @@ -1050,9 +1051,11 @@ UniValue testpoolswap(const JSONRPCRequest& request) { int targetHeight = ::ChainActive().Height() + 1; + auto poolPair = mnview_dummy.GetPoolPair(poolSwapMsg.idTokenFrom, poolSwapMsg.idTokenTo); + if (poolPair && path == "auto") 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."}); @@ -1087,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 47536e8075..29755ec188 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,49 @@ 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) + + 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,