Skip to content

Commit

Permalink
Use direct path in testpoolswap when available (#1115)
Browse files Browse the repository at this point in the history
* Use direct path in testpoolswap when available
* Add 'composite' path option in testpoolswap

Co-authored-by: Prasanna Loganathar <[email protected]>
  • Loading branch information
Jouzo and prasannavl authored Mar 9, 2022
1 parent 50b2884 commit 3c2ec74
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/masternodes/rpc_poolpair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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."
},
{
Expand Down Expand Up @@ -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."});

Expand Down Expand Up @@ -1087,7 +1090,7 @@ UniValue testpoolswap(const JSONRPCRequest& request) {
auto compositeSwap = CPoolSwap(poolSwapMsg, targetHeight);

std::vector<DCT_ID> poolIds;
if (path == "auto") {
if (path == "auto" || path == "composite") {
poolIds = compositeSwap.CalculateSwaps(mnview_dummy, true);
} else {
path = "custom";
Expand Down
44 changes: 44 additions & 0 deletions test/functional/feature_poolswap_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 3c2ec74

Please sign in to comment.