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

Poolswap to empty to address swaps to from address making to optional #1534

Merged
merged 10 commits into from
Nov 11, 2022
6 changes: 5 additions & 1 deletion src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4436,7 +4436,11 @@ Res CPoolSwap::ExecuteSwap(CCustomCSView& view, std::vector<DCT_ID> poolIDs, boo
intermediateView.Flush();

auto& addView = lastSwap ? view : intermediateView;
res = addView.AddBalance(lastSwap ? obj.to : obj.from, swapAmountResult);
if (height >= static_cast<uint32_t>(Params().GetConsensus().GrandCentralHeight)) {
res = addView.AddBalance(lastSwap ? (obj.to.empty() ? obj.from : obj.to) : obj.from, swapAmountResult);
} else {
res = addView.AddBalance(lastSwap ? obj.to : obj.from, swapAmountResult);
}
if (!res) {
return res;
}
Expand Down
49 changes: 38 additions & 11 deletions test/functional/feature_poolswap.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def set_test_params(self):
# node0: main (Foundation)
self.setup_clean_chain = True
self.extra_args = [
['-txnotokens=0', '-amkheight=50', '-bayfrontheight=50', '-bayfrontgardensheight=0', '-dakotaheight=160', '-fortcanningheight=163', '-fortcanninghillheight=170', '-fortcanningroadheight=177', '-acindex=1', '-dexstats'],
['-txnotokens=0', '-amkheight=50', '-bayfrontheight=50', '-bayfrontgardensheight=0', '-dakotaheight=160', '-fortcanningheight=163', '-fortcanninghillheight=170', '-fortcanningroadheight=177', '-acindex=1', '-dexstats'],
['-txnotokens=0', '-amkheight=50', '-bayfrontheight=50', '-bayfrontgardensheight=0', '-dakotaheight=160', '-fortcanningheight=163', '-fortcanninghillheight=170', '-fortcanningroadheight=177', '-dexstats']]
['-txnotokens=0', '-amkheight=50', '-bayfrontheight=50', '-bayfrontgardensheight=0', '-dakotaheight=160', '-fortcanningheight=163', '-fortcanninghillheight=170', '-fortcanningroadheight=177', '-grandcentralheight=200', '-acindex=1', '-dexstats'],
['-txnotokens=0', '-amkheight=50', '-bayfrontheight=50', '-bayfrontgardensheight=0', '-dakotaheight=160', '-fortcanningheight=163', '-fortcanninghillheight=170', '-fortcanningroadheight=177', '-grandcentralheight=200', '-acindex=1', '-dexstats'],
['-txnotokens=0', '-amkheight=50', '-bayfrontheight=50', '-bayfrontgardensheight=0', '-dakotaheight=160', '-fortcanningheight=163', '-fortcanninghillheight=170', '-fortcanningroadheight=177', '-grandcentralheight=200', '-dexstats']]

def setup(self):
assert_equal(len(self.nodes[0].listtokens()), 1) # only one token == DFI
Expand Down Expand Up @@ -311,20 +311,24 @@ def test_fort_canning_max_price_one_satoshi_below(self):
assert("Price is higher than indicated" in errorString)

def setup_new_pool_BTC_LTC(self):

self.symbolBTC = "BTC"
self.symbolLTC = "LTC"

self.nodes[0].createtoken({
"symbol": "BTC",
"name": "Bitcoin",
"collateralAddress": self.accountGN0
"symbol": self.symbolBTC,
"name": self.symbolBTC,
"collateralAddress": self.accountGN0,
"isDAT" : True
})
self.nodes[0].createtoken({
"symbol": "LTC",
"name": "Litecoin",
"collateralAddress": self.accountGN0
"symbol": self.symbolLTC,
"name": self.symbolLTC,
"collateralAddress": self.accountGN0,
"isDAT" : True
})
self.nodes[0].generate(1)

self.symbolBTC = "BTC#" + self.get_id_token("BTC")
self.symbolLTC = "LTC#" + self.get_id_token("LTC")
self.idBTC = list(self.nodes[0].gettoken(self.symbolBTC).keys())[0]
self.idLTC = list(self.nodes[0].gettoken(self.symbolLTC).keys())[0]

Expand Down Expand Up @@ -528,6 +532,28 @@ def update_comission_and_fee_to_1pct_pool2(self):
assert_equal(attributes['v0/live/economy/dex/%s/fee_burn_b'%(self.idBL)], round(dexinfee, 8))
assert_equal(attributes['v0/live/economy/dex/%s/fee_burn_a'%(self.idBL)], Decimal(str(round(dexoutfee, 8))))

def test_swap_to_self(self):

# Move to fork
self.nodes[0].generate(200 - self.nodes[0].getblockcount())

# Get and fund new address
address = self.nodes[0].getnewaddress()
self.nodes[0].accounttoaccount(self.accountGN0, {address: f"1@{self.symbolLTC}"})
self.nodes[0].generate(1)

# Swap to self
self.nodes[0].poolswap({
"from": address,
"tokenFrom": self.symbolLTC,
"amountFrom": 1,
"tokenTo": self.symbolBTC
})
self.nodes[0].generate(1)

# Check that LTC has gone and only BTC is present
assert_equal(self.nodes[0].getaccount(address), [f'0.00759157@{self.symbolBTC}'])

def test_testpoolswap_errors(self):
assert_raises_rpc_error(-8, "tokenFrom is empty", self.nodes[0].testpoolswap, {
"amountFrom": 0.1, "tokenFrom": "", "tokenTo": self.symbolBTC, "from": self.accountGN0, "to": self.accountSN1, "maxPrice": 0.1})
Expand Down Expand Up @@ -564,6 +590,7 @@ def run_test(self):
self.update_comission_and_fee_to_1pct_pool1()
self.update_comission_and_fee_to_1pct_pool2()
self.test_testpoolswap_errors()
self.test_swap_to_self()
self.revert_to_initial_state()

if __name__ == '__main__':
Expand Down