From 0028c57b816fb113d283243e7ff078ed34a36400 Mon Sep 17 00:00:00 2001 From: Mambisi Zempare Date: Sun, 26 Dec 2021 07:44:12 +0000 Subject: [PATCH 1/5] Enable composite swap for `testpoolswap` (#954) * Add support for composite swap in `testpoolswap` Co-authored-by: jouzo Co-authored-by: Prasanna Loganathar --- src/masternodes/mn_checks.cpp | 70 +++++----- src/masternodes/mn_checks.h | 2 + src/masternodes/rpc_poolpair.cpp | 124 ++++++++++++++---- src/rpc/client.cpp | 1 + test/functional/feature_poolswap.py | 18 ++- test/functional/feature_poolswap_composite.py | 32 ++++- 6 files changed, 186 insertions(+), 61 deletions(-) diff --git a/src/masternodes/mn_checks.cpp b/src/masternodes/mn_checks.cpp index cf15e51063..11f3d9fcc0 100644 --- a/src/masternodes/mn_checks.cpp +++ b/src/masternodes/mn_checks.cpp @@ -3367,6 +3367,39 @@ bool IsMempooledCustomTxCreate(const CTxMemPool & pool, const uint256 & txid) std::vector CPoolSwap::CalculateSwaps(CCustomCSView& view) { + std::vector> poolPaths = CalculatePoolPaths(view); + + // Record best pair + std::pair, CAmount> bestPair{{}, 0}; + + // Loop through all common pairs + for (const auto& path : poolPaths) { + + // Test on copy of view + CCustomCSView dummy(view); + + // Execute pool path + auto res = ExecuteSwap(dummy, path); + + // Add error for RPC user feedback + if (!res) { + const auto token = dummy.GetToken(currentID); + if (token) { + errors.emplace_back(token->symbol, res.msg); + } + } + + // Record amount if more than previous or default value + if (res && result > bestPair.second) { + bestPair = {path, result}; + } + } + + return bestPair.first; +} + +std::vector> CPoolSwap::CalculatePoolPaths(CCustomCSView& view) { + // For tokens to be traded get all pairs and pool IDs std::multimap fromPoolsID, toPoolsID; view.ForEachPoolPair([&](DCT_ID const & id, const CPoolPair& pool) { @@ -3393,8 +3426,8 @@ std::vector CPoolSwap::CalculateSwaps(CCustomCSView& view) { set_intersection(fromPoolsID.begin(), fromPoolsID.end(), toPoolsID.begin(), toPoolsID.end(), std::inserter(commonPairs, commonPairs.begin()), [](std::pair a, std::pair b) { - return a.first < b.first; - }); + return a.first < b.first; + }); // Loop through all common pairs and record direct pool to pool swaps std::vector> poolPaths; @@ -3425,7 +3458,7 @@ std::vector CPoolSwap::CalculateSwaps(CCustomCSView& view) { // If a pool pairs matches from pair and to pair add it to the pool paths if ((fromIt->first == pool.idTokenA.v && toIt->first == pool.idTokenB.v) || - (fromIt->first == pool.idTokenB.v && toIt->first == pool.idTokenA.v)) { + (fromIt->first == pool.idTokenB.v && toIt->first == pool.idTokenA.v)) { poolPaths.push_back({fromIt->second, id, toIt->second}); } } @@ -3433,33 +3466,8 @@ std::vector CPoolSwap::CalculateSwaps(CCustomCSView& view) { return true; }, {0}); - // Record best pair - std::pair, CAmount> bestPair{{}, 0}; - - // Loop through all common pairs - for (const auto& path : poolPaths) { - - // Test on copy of view - CCustomCSView dummy(view); - - // Execute pool path - auto res = ExecuteSwap(dummy, path); - - // Add error for RPC user feedback - if (!res) { - const auto token = dummy.GetToken(currentID); - if (token) { - errors.emplace_back(token->symbol, res.msg); - } - } - - // Record amount if more than previous or default value - if (res && result > bestPair.second) { - bestPair = {path, result}; - } - } - - return bestPair.first; + // return pool paths + return poolPaths; } Res CPoolSwap::ExecuteSwap(CCustomCSView& view, std::vector poolIDs) { @@ -3548,7 +3556,7 @@ Res CPoolSwap::ExecuteSwap(CCustomCSView& view, std::vector poolIDs) { } intermediateView.Flush(); - return res; + return Res::Ok(swapAmountResult.ToString()); }, static_cast(height)); if (!poolResult) { diff --git a/src/masternodes/mn_checks.h b/src/masternodes/mn_checks.h index 3f37050036..05dd19d09f 100644 --- a/src/masternodes/mn_checks.h +++ b/src/masternodes/mn_checks.h @@ -484,6 +484,8 @@ class CPoolSwap { std::vector CalculateSwaps(CCustomCSView& view); Res ExecuteSwap(CCustomCSView& view, std::vector poolIDs); + std::vector> CalculatePoolPaths(CCustomCSView& view); + }; #endif // DEFI_MASTERNODES_MN_CHECKS_H diff --git a/src/masternodes/rpc_poolpair.cpp b/src/masternodes/rpc_poolpair.cpp index 7eb0feed21..51287a57db 100644 --- a/src/masternodes/rpc_poolpair.cpp +++ b/src/masternodes/rpc_poolpair.cpp @@ -86,6 +86,18 @@ UniValue poolShareToJSON(DCT_ID const & poolId, CScript const & provider, CAmoun return ret; } + +UniValue poolPathsToJSON(std::vector> & poolPaths) { + UniValue paths(UniValue::VARR); + for (auto poolIds : poolPaths) { + UniValue pathObj(UniValue::VARR); + for (auto poolId : poolIds){ + pathObj.push_back(poolId.ToString()); + } + paths.push_back(pathObj); + } + return paths; +} void CheckAndFillPoolSwapMessage(const JSONRPCRequest& request, CPoolSwapMessage &poolSwapMsg) { std::string tokenFrom, tokenTo; UniValue metadataObj = request.params[0].get_obj(); @@ -974,6 +986,17 @@ UniValue testpoolswap(const JSONRPCRequest& request) { "Maximum acceptable price"}, }, }, + { + "path", RPCArg::Type::STR, RPCArg::Optional::OMITTED, + "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" + "Note: The default will be switched to auto in the upcoming versions." + }, + { + "verbose", RPCArg::Type::BOOL, RPCArg::Optional::OMITTED, + "Returns estimated composite path when true (default = false)" + }, }, RPCResult{ "\"amount@tokenId\" (string) The string with amount result of poolswap in format AMOUNT@TOKENID.\n" @@ -996,7 +1019,21 @@ UniValue testpoolswap(const JSONRPCRequest& request) { }, }.Check(request); - RPCTypeCheck(request.params, {UniValue::VOBJ}, true); + RPCTypeCheck(request.params, {UniValue::VOBJ, UniValue::VSTR, UniValue::VBOOL}, true); + + std::string path = "direct"; + if (request.params.size() > 1) { + path = request.params[1].get_str(); + if (!(path == "direct" || path == "auto")) + throw JSONRPCError(RPC_INVALID_REQUEST, std::string{"Invalid path"}); + } + + bool verbose = false; + if (request.params.size() > 2) { + verbose = request.params[2].get_bool(); + } + + UniValue pools{UniValue::VARR}; CPoolSwapMessage poolSwapMsg{}; CheckAndFillPoolSwapMessage(request, poolSwapMsg); @@ -1008,22 +1045,57 @@ UniValue testpoolswap(const JSONRPCRequest& request) { CCustomCSView mnview_dummy(*pcustomcsview); // create dummy cache for test state writing int targetHeight = ::ChainActive().Height() + 1; - auto poolPair = mnview_dummy.GetPoolPair(poolSwapMsg.idTokenFrom, poolSwapMsg.idTokenTo); - CPoolPair pp = poolPair->second; - res = pp.Swap({poolSwapMsg.idTokenFrom, poolSwapMsg.amountFrom}, poolSwapMsg.maxPrice, [&] (const CTokenAmount &tokenAmount) { - auto resPP = mnview_dummy.SetPoolPair(poolPair->first, targetHeight, pp); - if (!resPP) { - return resPP; - } + // 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."}); + + CPoolPair pp = poolPair->second; + res = pp.Swap({poolSwapMsg.idTokenFrom, poolSwapMsg.amountFrom}, poolSwapMsg.maxPrice, [&] (const CTokenAmount &tokenAmount) { + auto resPP = mnview_dummy.SetPoolPair(poolPair->first, targetHeight, pp); + if (!resPP) { + return resPP; + } - return Res::Ok(tokenAmount.ToString()); - }, targetHeight >= Params().GetConsensus().BayfrontGardensHeight); + return Res::Ok(tokenAmount.ToString()); + }, targetHeight >= Params().GetConsensus().BayfrontGardensHeight); - if (!res) - throw JSONRPCError(RPC_VERIFY_ERROR, res.msg); + if (!res) + throw JSONRPCError(RPC_VERIFY_ERROR, res.msg); + + pools.push_back(poolPair->first.ToString()); + } else { + auto compositeSwap = CPoolSwap(poolSwapMsg, targetHeight); + std::vector poolIds = compositeSwap.CalculateSwaps(mnview_dummy); + res = compositeSwap.ExecuteSwap(mnview_dummy, poolIds); + if (!res) { + std::string errorMsg{"Cannot find usable pool pair."}; + if (!compositeSwap.errors.empty()) { + errorMsg += " Details: ("; + for (size_t i{0}; i < compositeSwap.errors.size(); ++i) { + errorMsg += '"' + compositeSwap.errors[i].first + "\":\"" + compositeSwap.errors[i].second + '"' + (i + 1 < compositeSwap.errors.size() ? "," : ""); + } + errorMsg += ')'; + } + throw JSONRPCError(RPC_INVALID_REQUEST, errorMsg); + } + + for (const auto& id : poolIds) { + pools.push_back(id.ToString()); + } + } + } + if (verbose) { + UniValue swapObj{UniValue::VOBJ}; + swapObj.pushKV("path", path); + swapObj.pushKV("pools", pools); + swapObj.pushKV("amount", res.msg); + return swapObj; } - return UniValue(res.msg); + + return res.msg; } UniValue listpoolshares(const JSONRPCRequest& request) { @@ -1123,19 +1195,19 @@ UniValue listpoolshares(const JSONRPCRequest& request) { } static const CRPCCommand commands[] = -{ -// category name actor (function) params -// ------------- ----------------------- --------------------- ---------- - {"poolpair", "listpoolpairs", &listpoolpairs, {"pagination", "verbose"}}, - {"poolpair", "getpoolpair", &getpoolpair, {"key", "verbose" }}, - {"poolpair", "addpoolliquidity", &addpoolliquidity, {"from", "shareAddress", "inputs"}}, - {"poolpair", "removepoolliquidity", &removepoolliquidity, {"from", "amount", "inputs"}}, - {"poolpair", "createpoolpair", &createpoolpair, {"metadata", "inputs"}}, - {"poolpair", "updatepoolpair", &updatepoolpair, {"metadata", "inputs"}}, - {"poolpair", "poolswap", &poolswap, {"metadata", "inputs"}}, - {"poolpair", "compositeswap", &compositeswap, {"metadata", "inputs"}}, - {"poolpair", "listpoolshares", &listpoolshares, {"pagination", "verbose", "is_mine_only"}}, - {"poolpair", "testpoolswap", &testpoolswap, {"metadata"}}, +{ +// category name actor (function) params +// ------------- ----------------------- --------------------- ---------- + {"poolpair", "listpoolpairs", &listpoolpairs, {"pagination", "verbose"}}, + {"poolpair", "getpoolpair", &getpoolpair, {"key", "verbose" }}, + {"poolpair", "addpoolliquidity", &addpoolliquidity, {"from", "shareAddress", "inputs"}}, + {"poolpair", "removepoolliquidity", &removepoolliquidity, {"from", "amount", "inputs"}}, + {"poolpair", "createpoolpair", &createpoolpair, {"metadata", "inputs"}}, + {"poolpair", "updatepoolpair", &updatepoolpair, {"metadata", "inputs"}}, + {"poolpair", "poolswap", &poolswap, {"metadata", "inputs"}}, + {"poolpair", "compositeswap", &compositeswap, {"metadata", "inputs"}}, + {"poolpair", "listpoolshares", &listpoolshares, {"pagination", "verbose", "is_mine_only"}}, + {"poolpair", "testpoolswap", &testpoolswap, {"metadata", "path", "verbose"}}, }; void RegisterPoolpairRPCCommands(CRPCTable& tableRPC) { diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index c09da34837..1b32d7d64c 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -295,6 +295,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "compositeswap", 0, "metadata" }, { "compositeswap", 1, "inputs" }, { "testpoolswap", 0, "metadata"}, + { "testpoolswap", 2, "verbose"}, { "listpoolshares", 0, "pagination" }, { "listpoolshares", 1, "verbose" }, { "listpoolshares", 2, "is_mine_only" }, diff --git a/test/functional/feature_poolswap.py b/test/functional/feature_poolswap.py index 33827d65a6..d01d621386 100755 --- a/test/functional/feature_poolswap.py +++ b/test/functional/feature_poolswap.py @@ -183,12 +183,24 @@ def run_test(self): print("goldCheckPS:", goldCheckPS) print("testPoolSwapRes:", testPoolSwapRes) - testPoolSwapRes = str(testPoolSwapRes).split("@", 2) + testPoolSwapSplit = str(testPoolSwapRes).split("@", 2) - psTestAmount = testPoolSwapRes[0] - psTestTokenId = testPoolSwapRes[1] + psTestAmount = testPoolSwapSplit[0] + psTestTokenId = testPoolSwapSplit[1] assert_equal(psTestTokenId, idGold) + testPoolSwapVerbose = self.nodes[0].testpoolswap({ + "from": accountGN0, + "tokenFrom": symbolSILVER, + "amountFrom": 10, + "to": accountSN1, + "tokenTo": symbolGOLD, + }, "direct", True) + + assert_equal(testPoolSwapVerbose["path"], "direct") + assert_equal(testPoolSwapVerbose["pools"][0], idGS) + assert_equal(testPoolSwapVerbose["amount"], testPoolSwapRes) + self.nodes[0].poolswap({ "from": accountGN0, "tokenFrom": symbolSILVER, diff --git a/test/functional/feature_poolswap_composite.py b/test/functional/feature_poolswap_composite.py index 80655bbc2c..4a6ed6b90b 100755 --- a/test/functional/feature_poolswap_composite.py +++ b/test/functional/feature_poolswap_composite.py @@ -76,7 +76,6 @@ def run_test(self): idDOGE = list(self.nodes[0].gettoken(symbolDOGE).keys())[0] idTSLA = list(self.nodes[0].gettoken(symbolTSLA).keys())[0] idLTC = list(self.nodes[0].gettoken(symbolLTC).keys())[0] - coin = 100000000 # Creating poolpairs @@ -168,6 +167,7 @@ def run_test(self): }, collateral, []) self.nodes[0].generate(1) + self.nodes[0].compositeswap({ "from": source, "tokenFrom": symbolLTC, @@ -242,6 +242,34 @@ def run_test(self): }, collateral, []) self.nodes[0].generate(1) + estimateCompositePathsRes = self.nodes[0].testpoolswap({ + "from": source, + "tokenFrom": symbolLTC, + "amountFrom": ltc_to_doge_from, + "to": destination, + "tokenTo": symbolDOGE, + }, "auto", True) + + assert_equal(estimateCompositePathsRes['path'], 'auto') + + poolLTC_USDC = list(self.nodes[0].getpoolpair("LTC-USDC").keys())[0] + poolDOGE_USDC = list(self.nodes[0].getpoolpair("DOGE-USDC").keys())[0] + assert_equal(estimateCompositePathsRes['pools'], [poolLTC_USDC, poolDOGE_USDC]) + + testCPoolSwapRes = self.nodes[0].testpoolswap({ + "from": source, + "tokenFrom": symbolLTC, + "amountFrom": ltc_to_doge_from, + "to": destination, + "tokenTo": symbolDOGE, + }, "auto") + + testCPoolSwapRes = str(testCPoolSwapRes).split("@", 2) + + psTestAmount = testCPoolSwapRes[0] + psTestTokenId = testCPoolSwapRes[1] + assert_equal(psTestTokenId, idDOGE) + self.nodes[0].compositeswap({ "from": source, "tokenFrom": symbolLTC, @@ -261,6 +289,8 @@ def run_test(self): dest_balance = self.nodes[0].getaccount(destination, {}, True) assert_equal(dest_balance[idDOGE], doge_received * 2) assert_equal(len(dest_balance), 1) + # Check test swap correctness + assert_equal(Decimal(psTestAmount), dest_balance[idDOGE]) # Set up addresses for swapping source = self.nodes[0].getnewaddress("", "legacy") From bd788ba1e938673bb9ab20c08a522d1cb6916c43 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Mon, 27 Dec 2021 16:16:38 +0530 Subject: [PATCH 2/5] Version 2.3.2 (#1000) --- configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 36b5399d16..c8816ae27a 100644 --- a/configure.ac +++ b/configure.ac @@ -2,7 +2,7 @@ dnl require autoconf 2.60 (AS_ECHO/AS_ECHO_N) AC_PREREQ([2.60]) define(_CLIENT_VERSION_MAJOR, 2) define(_CLIENT_VERSION_MINOR, 3) -define(_CLIENT_VERSION_REVISION, 1) +define(_CLIENT_VERSION_REVISION, 2) define(_CLIENT_VERSION_BUILD, 0) define(_CLIENT_VERSION_RC, 0) define(_CLIENT_VERSION_IS_RELEASE, true) From 0035a7e2c887d5077b2f82b097895c0fa8ee0c53 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Mon, 27 Dec 2021 20:36:39 +0530 Subject: [PATCH 3/5] Fix excess logging of composite swap (#1003) --- src/masternodes/mn_checks.cpp | 2 +- src/masternodes/mn_checks.h | 2 +- src/masternodes/rpc_poolpair.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/masternodes/mn_checks.cpp b/src/masternodes/mn_checks.cpp index 11f3d9fcc0..d691f43cc2 100644 --- a/src/masternodes/mn_checks.cpp +++ b/src/masternodes/mn_checks.cpp @@ -3556,7 +3556,7 @@ Res CPoolSwap::ExecuteSwap(CCustomCSView& view, std::vector poolIDs) { } intermediateView.Flush(); - return Res::Ok(swapAmountResult.ToString()); + return res; }, static_cast(height)); if (!poolResult) { diff --git a/src/masternodes/mn_checks.h b/src/masternodes/mn_checks.h index 05dd19d09f..e2c83af087 100644 --- a/src/masternodes/mn_checks.h +++ b/src/masternodes/mn_checks.h @@ -485,7 +485,7 @@ class CPoolSwap { std::vector CalculateSwaps(CCustomCSView& view); Res ExecuteSwap(CCustomCSView& view, std::vector poolIDs); std::vector> CalculatePoolPaths(CCustomCSView& view); - + CTokenAmount GetResult() { return CTokenAmount{obj.idTokenTo, result}; }; }; #endif // DEFI_MASTERNODES_MN_CHECKS_H diff --git a/src/masternodes/rpc_poolpair.cpp b/src/masternodes/rpc_poolpair.cpp index 51287a57db..f91bb46772 100644 --- a/src/masternodes/rpc_poolpair.cpp +++ b/src/masternodes/rpc_poolpair.cpp @@ -1081,10 +1081,10 @@ UniValue testpoolswap(const JSONRPCRequest& request) { } throw JSONRPCError(RPC_INVALID_REQUEST, errorMsg); } - for (const auto& id : poolIds) { pools.push_back(id.ToString()); } + res.msg = compositeSwap.GetResult().ToString(); } } if (verbose) { From ef03ef40853b42c946f8015a11ff0e0410547b55 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Tue, 28 Dec 2021 01:54:50 +0530 Subject: [PATCH 4/5] Add support for test only composite swap without balance transfers (#1004) * Add support for test only composite swap without balance transfers --- src/masternodes/mn_checks.cpp | 28 ++++++++++++++++++++-------- src/masternodes/mn_checks.h | 2 +- src/masternodes/rpc_poolpair.cpp | 2 +- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/masternodes/mn_checks.cpp b/src/masternodes/mn_checks.cpp index d691f43cc2..e1da8542d0 100644 --- a/src/masternodes/mn_checks.cpp +++ b/src/masternodes/mn_checks.cpp @@ -3470,7 +3470,10 @@ std::vector> CPoolSwap::CalculatePoolPaths(CCustomCSView& vi return poolPaths; } -Res CPoolSwap::ExecuteSwap(CCustomCSView& view, std::vector poolIDs) { +// Note: `testOnly` doesn't update views, and as such can result in a previous price calculations +// for a pool, if used multiple times (or duplicated pool IDs) with the same view. +// testOnly is only meant for one-off tests per well defined view. +Res CPoolSwap::ExecuteSwap(CCustomCSView& view, std::vector poolIDs, bool testOnly) { CTokenAmount swapAmountResult{{},0}; Res poolResult = Res::Ok(); @@ -3496,10 +3499,12 @@ Res CPoolSwap::ExecuteSwap(CCustomCSView& view, std::vector poolIDs) { poolPrice = obj.maxPrice; } - CCustomCSView mnview(view); - mnview.CalculateOwnerRewards(obj.from, height); - mnview.CalculateOwnerRewards(obj.to, height); - mnview.Flush(); + if (!testOnly) { + CCustomCSView mnview(view); + mnview.CalculateOwnerRewards(obj.from, height); + mnview.CalculateOwnerRewards(obj.to, height); + mnview.Flush(); + } for (size_t i{0}; i < poolIDs.size(); ++i) { @@ -3532,14 +3537,21 @@ Res CPoolSwap::ExecuteSwap(CCustomCSView& view, std::vector poolIDs) { // Perform swap poolResult = pool->Swap(swapAmount, poolPrice, [&] (const CTokenAmount &tokenAmount) { + // Save swap amount for next loop + swapAmountResult = tokenAmount; + + // If we're just testing, don't do any balance transfers. + // Just go over pools and return result. The only way this can + // cause inaccurate result is if we go over the same path twice, + // which shouldn't happen in the first place. + if (testOnly) + return Res::Ok(); + auto res = view.SetPoolPair(currentID, height, *pool); if (!res) { return res; } - // Save swap amount for next loop - swapAmountResult = tokenAmount; - CCustomCSView intermediateView(view); // hide interemidiate swaps auto& subView = i == 0 ? view : intermediateView; diff --git a/src/masternodes/mn_checks.h b/src/masternodes/mn_checks.h index e2c83af087..afd7c3b2a4 100644 --- a/src/masternodes/mn_checks.h +++ b/src/masternodes/mn_checks.h @@ -483,7 +483,7 @@ class CPoolSwap { : obj(obj), height(height) {} std::vector CalculateSwaps(CCustomCSView& view); - Res ExecuteSwap(CCustomCSView& view, std::vector poolIDs); + Res ExecuteSwap(CCustomCSView& view, std::vector poolIDs, bool testOnly = false); std::vector> CalculatePoolPaths(CCustomCSView& view); CTokenAmount GetResult() { return CTokenAmount{obj.idTokenTo, result}; }; }; diff --git a/src/masternodes/rpc_poolpair.cpp b/src/masternodes/rpc_poolpair.cpp index f91bb46772..4be5b54db9 100644 --- a/src/masternodes/rpc_poolpair.cpp +++ b/src/masternodes/rpc_poolpair.cpp @@ -1069,7 +1069,7 @@ UniValue testpoolswap(const JSONRPCRequest& request) { } else { auto compositeSwap = CPoolSwap(poolSwapMsg, targetHeight); std::vector poolIds = compositeSwap.CalculateSwaps(mnview_dummy); - res = compositeSwap.ExecuteSwap(mnview_dummy, poolIds); + res = compositeSwap.ExecuteSwap(mnview_dummy, poolIds, true); if (!res) { std::string errorMsg{"Cannot find usable pool pair."}; if (!compositeSwap.errors.empty()) { From 3e56d57fdc734360501c386d6494eff26df05e89 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Tue, 28 Dec 2021 04:47:13 +0530 Subject: [PATCH 5/5] Add CalculateSwap testonly support --- src/masternodes/mn_checks.cpp | 4 ++-- src/masternodes/mn_checks.h | 2 +- src/masternodes/rpc_poolpair.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/masternodes/mn_checks.cpp b/src/masternodes/mn_checks.cpp index e1da8542d0..8ae9360749 100644 --- a/src/masternodes/mn_checks.cpp +++ b/src/masternodes/mn_checks.cpp @@ -3365,7 +3365,7 @@ bool IsMempooledCustomTxCreate(const CTxMemPool & pool, const uint256 & txid) return false; } -std::vector CPoolSwap::CalculateSwaps(CCustomCSView& view) { +std::vector CPoolSwap::CalculateSwaps(CCustomCSView& view, bool testOnly) { std::vector> poolPaths = CalculatePoolPaths(view); @@ -3379,7 +3379,7 @@ std::vector CPoolSwap::CalculateSwaps(CCustomCSView& view) { CCustomCSView dummy(view); // Execute pool path - auto res = ExecuteSwap(dummy, path); + auto res = ExecuteSwap(dummy, path, testOnly); // Add error for RPC user feedback if (!res) { diff --git a/src/masternodes/mn_checks.h b/src/masternodes/mn_checks.h index afd7c3b2a4..a69cfcc470 100644 --- a/src/masternodes/mn_checks.h +++ b/src/masternodes/mn_checks.h @@ -482,7 +482,7 @@ class CPoolSwap { CPoolSwap(const CPoolSwapMessage& obj, uint32_t height) : obj(obj), height(height) {} - std::vector CalculateSwaps(CCustomCSView& view); + std::vector CalculateSwaps(CCustomCSView& view, bool testOnly = false); Res ExecuteSwap(CCustomCSView& view, std::vector poolIDs, bool testOnly = false); std::vector> CalculatePoolPaths(CCustomCSView& view); CTokenAmount GetResult() { return CTokenAmount{obj.idTokenTo, result}; }; diff --git a/src/masternodes/rpc_poolpair.cpp b/src/masternodes/rpc_poolpair.cpp index 4be5b54db9..9d1065123a 100644 --- a/src/masternodes/rpc_poolpair.cpp +++ b/src/masternodes/rpc_poolpair.cpp @@ -1068,7 +1068,7 @@ UniValue testpoolswap(const JSONRPCRequest& request) { pools.push_back(poolPair->first.ToString()); } else { auto compositeSwap = CPoolSwap(poolSwapMsg, targetHeight); - std::vector poolIds = compositeSwap.CalculateSwaps(mnview_dummy); + std::vector poolIds = compositeSwap.CalculateSwaps(mnview_dummy, true); res = compositeSwap.ExecuteSwap(mnview_dummy, poolIds, true); if (!res) { std::string errorMsg{"Cannot find usable pool pair."};