Skip to content

Commit

Permalink
Poolswap to empty to address swaps to from address making to optional (
Browse files Browse the repository at this point in the history
…#1534)

* Remove negative interest from loan amount

* Swap to from address if to address empty

* Increment DB version

* Add swap to self test

* Revert "Increment DB version"

This reverts commit 217fd0a.

* Revert "Remove negative interest from loan amount"

This reverts commit 383f143.

* Poolswap to self when to not set from GC fork

* Expand ternary conditional for readability

Co-authored-by: Prasanna Loganathar <[email protected]>
  • Loading branch information
Bushstar and prasannavl authored Nov 11, 2022
1 parent 03b6e27 commit f2883af
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
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

0 comments on commit f2883af

Please sign in to comment.