Skip to content

Commit

Permalink
Fix swap when direct path disabled (#1392)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar authored Jul 19, 2022
1 parent 4a69fd5 commit afb1c12
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/masternodes/rpc_poolpair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,8 @@ UniValue compositeswap(const JSONRPCRequest& request) {
{
LOCK(cs_main);
// If no direct swap found search for composite swap
if (!pcustomcsview->GetPoolPair(poolSwapMsg.idTokenFrom, poolSwapMsg.idTokenTo)) {
auto directPool = pcustomcsview->GetPoolPair(poolSwapMsg.idTokenFrom, poolSwapMsg.idTokenTo);
if (!directPool || !directPool->second.status) {

auto compositeSwap = CPoolSwap(poolSwapMsg, targetHeight);
poolSwapMsgV2.poolIDs = compositeSwap.CalculateSwaps(*pcustomcsview);
Expand Down
66 changes: 46 additions & 20 deletions test/functional/feature_poolswap_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def run_test(self):
"commission": 0.1,
"status": True,
"ownerAddress": owner
}, [])
})
self.nodes[0].generate(1)

self.nodes[0].createpoolpair({
Expand All @@ -98,7 +98,7 @@ def run_test(self):
"commission": 0.1,
"status": True,
"ownerAddress": owner
}, [])
})
self.nodes[0].generate(1)

self.nodes[0].createpoolpair({
Expand All @@ -107,7 +107,7 @@ def run_test(self):
"commission": 0.1,
"status": True,
"ownerAddress": owner
}, [])
})
self.nodes[0].generate(1)

self.nodes[0].createpoolpair({
Expand All @@ -116,7 +116,7 @@ def run_test(self):
"commission": 0.1,
"status": True,
"ownerAddress": owner
}, [])
})
self.nodes[0].generate(1)

self.nodes[0].createpoolpair({
Expand All @@ -125,7 +125,7 @@ def run_test(self):
"commission": 0.1,
"status": True,
"ownerAddress": owner
}, [])
})
self.nodes[0].generate(1)

# Tokenise DFI
Expand All @@ -147,7 +147,7 @@ def run_test(self):
"amountFrom": ltc_to_doge_from,
"to": destination,
"tokenTo": symbolDOGE,
}, [])
})
except JSONRPCException as e:
errorString = e.error['message']
assert('"LTC-DFI":"Lack of liquidity."' in errorString)
Expand Down Expand Up @@ -176,7 +176,7 @@ def run_test(self):
"amountFrom": ltc_to_doge_from,
"to": destination,
"tokenTo": symbolDOGE,
}, [])
})
self.nodes[0].generate(1)

# Check source
Expand All @@ -202,7 +202,7 @@ def run_test(self):
"to": destination,
"tokenTo": symbolDOGE,
"maxPrice": ltc_per_doge
}, [])
})
self.nodes[0].generate(1)

# Check source
Expand All @@ -227,7 +227,7 @@ def run_test(self):
"to": destination,
"tokenTo": symbolDOGE,
"maxPrice": ltc_per_doge - Decimal('0.00000001'),
}, [])
})
except JSONRPCException as e:
errorString = e.error['message']
assert('"DOGE-DFI":"Price is higher than indicated."' in errorString)
Expand Down Expand Up @@ -360,7 +360,7 @@ def run_test(self):
"to": destination,
"tokenTo": symbolDOGE,
"maxPrice": ltc_per_doge
}, [])
})
self.nodes[0].generate(1)

# Check source
Expand Down Expand Up @@ -391,7 +391,7 @@ def run_test(self):
"amountFrom": tsla_to_ltc_from,
"to": destination,
"tokenTo": symbolLTC
}, [])
})
except JSONRPCException as e:
errorString = e.error['message']
assert('Cannot find usable pool pair.' in errorString)
Expand All @@ -403,7 +403,7 @@ def run_test(self):
"commission": 0.1,
"status": True,
"ownerAddress": owner
}, [])
})
self.nodes[0].generate(1)

# Now swap TSLA to
Expand All @@ -415,7 +415,7 @@ def run_test(self):
"amountFrom": tsla_to_ltc_from,
"to": destination,
"tokenTo": symbolLTC
}, [])
})
except JSONRPCException as e:
errorString = e.error['message']
assert('"DUSD-DFI":"Lack of liquidity."' in errorString)
Expand All @@ -435,7 +435,7 @@ def run_test(self):
"to": destination,
"tokenTo": symbolLTC,
"maxPrice": "0.15311841"
}, [])
})
except JSONRPCException as e:
errorString = e.error['message']
assert('"LTC-DFI":"Price is higher than indicated."' in errorString)
Expand All @@ -447,7 +447,7 @@ def run_test(self):
"to": destination,
"tokenTo": symbolLTC,
"maxPrice": "0.15311842"
}, [])
})
self.nodes[0].generate(1)

# Check source
Expand All @@ -467,7 +467,7 @@ def run_test(self):
"commission": 0.1,
"status": True,
"ownerAddress": owner
}, [])
})
self.nodes[0].generate(1)

# Add some liquidity
Expand All @@ -491,20 +491,20 @@ def run_test(self):
"to": destination,
"tokenTo": symbolLTC,
"maxPrice": "0.03361577"
}, [])
})
except JSONRPCException as e:
errorString = e.error['message']
assert('"LTC-DFI":"Price is higher than indicated."' in errorString)
assert('"LTC-USDC":"Price is higher than indicated."' in errorString)

self.nodes[0].compositeswap({
tx = self.nodes[0].compositeswap({
"from": source,
"tokenFrom": symbolTSLA,
"amountFrom": tsla_to_ltc_from,
"to": destination,
"tokenTo": symbolLTC,
"maxPrice": "0.03361578"
}, [])
})
self.nodes[0].generate(1)

# Check source
Expand All @@ -517,6 +517,32 @@ def run_test(self):
assert_equal(dest_balance[idLTC], Decimal('29.74793123'))
assert_equal(len(dest_balance), 1)

# Add disabled direct path
self.nodes[0].createpoolpair({
"tokenA": symbolTSLA,
"tokenB": symbolLTC,
"commission": 0.1,
"status": False,
"ownerAddress": owner
})
self.nodes[0].generate(1)

# Try swap again with disabled pool
tx = self.nodes[0].compositeswap({
"from": source,
"tokenFrom": symbolTSLA,
"amountFrom": tsla_to_ltc_from,
"to": destination,
"tokenTo": symbolLTC
})

# Check result uses composite swap
result = self.nodes[0].getcustomtx(tx)
assert_equal(result['results']['compositeDex'], 'TSLA-DUSD/DUSD-USDC/LTC-USDC')

# Wipe mempool
self.nodes[0].clearmempool()

# Fund source and move to Fort Canning Hill height
self.nodes[0].sendtoaddress(source, 0.1)
self.nodes[0].generate(200 - self.nodes[0].getblockcount())
Expand All @@ -528,7 +554,7 @@ def run_test(self):
"amountFrom": tsla_to_ltc_from,
"to": destination,
"tokenTo": 0
}, [])
})

rawtx_verbose = self.nodes[0].getrawtransaction(tx, 1)
metadata = rawtx_verbose['vout'][0]['scriptPubKey']['hex']
Expand Down

0 comments on commit afb1c12

Please sign in to comment.