Skip to content

Commit

Permalink
Allow internal zero swaps (#1071)
Browse files Browse the repository at this point in the history
* Allow internal zero swaps

* Add pre FCH guard

* Add comment

* Adjust zero amount catching

Signed-off-by: Anthony Fieroni <[email protected]>

Co-authored-by: Anthony Fieroni <[email protected]>
  • Loading branch information
prasannavl and bvbfan authored Jan 26, 2022
1 parent 28c8cbc commit 39f6784
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3656,7 +3656,7 @@ std::vector<DCT_ID> CPoolSwap::CalculateSwaps(CCustomCSView& view, bool testOnly
std::vector<std::vector<DCT_ID>> poolPaths = CalculatePoolPaths(view);

// Record best pair
std::pair<std::vector<DCT_ID>, CAmount> bestPair{{}, 0};
std::pair<std::vector<DCT_ID>, CAmount> bestPair{{}, -1};

// Loop through all common pairs
for (const auto& path : poolPaths) {
Expand Down Expand Up @@ -3768,6 +3768,10 @@ Res CPoolSwap::ExecuteSwap(CCustomCSView& view, std::vector<DCT_ID> poolIDs, boo
poolIDs.clear();
}

if (obj.amountFrom <= 0) {
return Res::Err("Input amount should be positive");
}

// Single swap if no pool IDs provided
auto poolPrice = POOLPRICE_MAX;
boost::optional<std::pair<DCT_ID, CPoolPair> > poolPair;
Expand Down
7 changes: 5 additions & 2 deletions src/masternodes/poolpairs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,11 @@ Res CPoolPair::Swap(CTokenAmount in, CAmount dexfeeInPct, PoolPrice const & maxP
if (in.nTokenId != idTokenA && in.nTokenId != idTokenB)
return Res::Err("Error, input token ID (" + in.nTokenId.ToString() + ") doesn't match pool tokens (" + idTokenA.ToString() + "," + idTokenB.ToString() + ")");

if (in.nValue <= 0)
return Res::Err("Input amount should be positive!");
// TODO: The whole block of the fork condition can be removed safely after FCH.
if (height < Params().GetConsensus().FortCanningHillHeight) {
if (in.nValue <= 0)
return Res::Err("Input amount should be positive!");
}

if (!status)
return Res::Err("Pool trading is turned off!");
Expand Down
3 changes: 3 additions & 0 deletions src/masternodes/rpc_poolpair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,9 @@ UniValue testpoolswap(const JSONRPCRequest& request) {
if (!poolPair)
throw JSONRPCError(RPC_INVALID_REQUEST, std::string{"Direct pool pair not found. Use 'auto' mode to use composite swap."});

if (poolSwapMsg.amountFrom <= 0)
throw JSONRPCError(RPC_INVALID_REQUEST, "Input amount should be positive");

CPoolPair pp = poolPair->second;
auto dexfeeInPct = mnview_dummy.GetDexFeePct(poolPair->first, poolSwapMsg.idTokenFrom);

Expand Down

0 comments on commit 39f6784

Please sign in to comment.