Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use direct path in testpoolswap when available #1115

Merged
merged 4 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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