diff --git a/test/functional/feature_loan_payback_dfi.py b/test/functional/feature_loan_payback_dfi.py index 07565fbf7f3..25570a24817 100755 --- a/test/functional/feature_loan_payback_dfi.py +++ b/test/functional/feature_loan_payback_dfi.py @@ -7,6 +7,7 @@ from test_framework.test_framework import DefiTestFramework from test_framework.util import assert_equal, assert_raises_rpc_error +from test_framework.authproxy import JSONRPCException import calendar import time @@ -285,6 +286,66 @@ def run_test(self): 'amounts': "10@DFI" }) + # Multiple token payback pre FCR + # Disable loan payback + self.nodes[0].setgov({"ATTRIBUTES":{'v0/token/' + iddUSD + '/payback_dfi':'false'}}) + self.nodes[0].generate(1) + vaultId6 = self.nodes[0].createvault(account0, 'LOAN150') + self.nodes[0].generate(1) + + self.nodes[0].utxostoaccount({account0: "100@DFI"}) + self.nodes[0].generate(1) + self.nodes[0].deposittovault(vaultId6, account0, "100@DFI") + self.nodes[0].generate(1) + + self.nodes[0].generate(1) + # Create and fill addres with 10DFI + 70DUSD + self.nodes[0].utxostoaccount({account0: "11@DFI"}) + self.nodes[0].generate(1) + addr_DFI_DUSD = self.nodes[0].getnewaddress("", "legacy") + toAmounts = {addr_DFI_DUSD: ["11@DFI", "71@DUSD"]} + self.nodes[0].accounttoaccount(account0, toAmounts) + self.nodes[0].generate(1) + + self.nodes[0].takeloan({ + 'vaultId': vaultId6, + 'amounts': "100@DUSD" + }) + self.nodes[0].generate(1) + + vaultBefore = self.nodes[0].getvault(vaultId6) + [balanceDFIBefore, _] = self.nodes[0].getaccount(addr_DFI_DUSD)[0].split('@') + [balanceDUSDBefore, _] = self.nodes[0].getaccount(addr_DFI_DUSD)[1].split('@') + assert_equal(balanceDUSDBefore, '71.00000000') + assert_equal(balanceDFIBefore, '11.00000000') + + try: + self.nodes[0].paybackloan({ + 'vaultId': vaultId6, + 'from': addr_DFI_DUSD, + 'amounts': ["70@DUSD", "10@DFI"] + }) + except JSONRPCException as e: + errorString = e.error['message'] + assert("Payback of loan via DFI token is not currently active" in errorString) + + self.nodes[0].setgov({"ATTRIBUTES":{'v0/token/' + iddUSD + '/payback_dfi':'true'}}) + self.nodes[0].generate(1) + + self.nodes[0].paybackloan({ + 'vaultId': vaultId6, + 'from': addr_DFI_DUSD, + 'amounts': ["70@DUSD", "10@DFI"] + }) + self.nodes[0].generate(1) + + vaultAfter = self.nodes[0].getvault(vaultId6) + assert_equal(vaultAfter["loanAmounts"], []) + [balanceDUSDAfter, _] = self.nodes[0].getaccount(addr_DFI_DUSD)[1].split('@') + [balanceDFIAfter, _] = self.nodes[0].getaccount(addr_DFI_DUSD)[0].split('@') + assert_equal(Decimal(balanceDUSDBefore) - Decimal(balanceDUSDAfter), Decimal('5.00022832')) + assert_equal(Decimal(balanceDFIBefore) - Decimal(balanceDFIAfter), Decimal('10')) + if __name__ == '__main__': PaybackDFILoanTest().main() diff --git a/test/functional/feature_loan_payback_dfi_v2.py b/test/functional/feature_loan_payback_dfi_v2.py index b1d8482103e..f234271783c 100755 --- a/test/functional/feature_loan_payback_dfi_v2.py +++ b/test/functional/feature_loan_payback_dfi_v2.py @@ -6,7 +6,7 @@ """Test Loan - payback loan dfi.""" from test_framework.test_framework import DefiTestFramework -from test_framework.util import assert_equal, assert_raises_rpc_error +from test_framework.util import assert_equal, assert_raises_rpc_error, connect_nodes from test_framework.authproxy import JSONRPCException import calendar @@ -16,17 +16,32 @@ class PaybackDFILoanTest (DefiTestFramework): def set_test_params(self): - self.num_nodes = 1 + self.FCR_HEIGHT = 800 + self.num_nodes = 2 + self.FINISHED_SETUP_BLOCK = 0 self.setup_clean_chain = True self.extra_args = [ - ['-txnotokens=0', '-amkheight=50', '-bayfrontheight=50', '-bayfrontgardensheight=1', '-eunosheight=50', - '-fortcanningheight=50', '-fortcanninghillheight=50', '-fortcanningroadheight=50', '-simulatemainnet', '-txindex=1', '-jellyfish_regtest=1'] + ['-txnotokens=0', '-amkheight=50', '-bayfrontheight=50', '-bayfrontgardensheight=1', '-eunosheight=50','-fortcanningheight=50', '-fortcanninghillheight=50', f'-fortcanningroadheight={self.FCR_HEIGHT}', '-simulatemainnet', '-txindex=1', '-jellyfish_regtest=1' ], + ['-txnotokens=0', '-amkheight=50', '-bayfrontheight=50', '-bayfrontgardensheight=1', '-eunosheight=50','-fortcanningheight=50', '-fortcanninghillheight=50', f'-fortcanningroadheight={self.FCR_HEIGHT}', '-simulatemainnet', '-txindex=1', '-jellyfish_regtest=1' ] ] self.symbolDFI = "DFI" self.symbolBTC = "BTC" self.symboldUSD = "DUSD" self.symbolTSLA = "TSLA" + def go_post_FCR(self): + self.nodes[0].generate(self.FCR_HEIGHT+1) + + def reset_chain(self): + # REVERTING + # mine blocks at node 1 + self.nodes[1].generate(20) + + connect_nodes(self.nodes[0], 1) + self.sync_blocks() + self.nodes[0].invalidateblock(self.nodes[0].getblockhash(1)) + self.nodes[0].generate(1) + def create_tokens(self): self.nodes[0].createtoken({ "symbol": self.symbolBTC, @@ -93,8 +108,10 @@ def setup_loan_tokens(self): 'interest': 1 }) self.nodes[0].generate(1) - self.nodes[0].minttokens("70000000@DUSD") + self.nodes[0].minttokens("70000100@DUSD") + self.nodes[0].generate(1) self.nodes[0].minttokens("500@BTC") + self.nodes[0].generate(1) self.nodes[0].minttokens("5000000000@TSLA") self.nodes[0].generate(1) self.iddUSD = list(self.nodes[0].gettoken(self.symboldUSD).keys())[0] @@ -106,10 +123,7 @@ def create_fill_addresses(self): # 20000000@DUSD # 1 DFI = 3.5 DUSD self.addr_pool_DFI_DUSD = self.nodes[0].getnewaddress("", "legacy") - toAmounts = { self.addr_pool_DFI_DUSD: "5000000@DFI"} - self.nodes[0].accounttoaccount(self.account0, toAmounts) - self.nodes[0].generate(1) - toAmounts = { self.addr_pool_DFI_DUSD: "20000000@DUSD"} + toAmounts = {self.addr_pool_DFI_DUSD: ["5000000@DFI", "20000000@DUSD"]} self.nodes[0].accounttoaccount(self.account0, toAmounts) self.nodes[0].generate(1) @@ -118,9 +132,7 @@ def create_fill_addresses(self): # 20000000@DUSD # 1 BTC = 50000 DUSD self.addr_pool_BTC_DUSD = self.nodes[0].getnewaddress("", "legacy") - toAmounts = {self.addr_pool_BTC_DUSD: "400@BTC"} - self.nodes[0].accounttoaccount(self.account0, toAmounts) - toAmounts = {self.addr_pool_BTC_DUSD: "20000000@DUSD"} + toAmounts = {self.addr_pool_BTC_DUSD: ["400@BTC", "20000000@DUSD"]} self.nodes[0].accounttoaccount(self.account0, toAmounts) self.nodes[0].generate(1) @@ -129,9 +141,7 @@ def create_fill_addresses(self): # 20000000@DUSD # 1 TSLA = 0.005 DUSD self.addr_pool_TSLA_DUSD = self.nodes[0].getnewaddress("", "legacy") - toAmounts = {self.addr_pool_TSLA_DUSD: "4000000000@TSLA"} - self.nodes[0].accounttoaccount(self.account0, toAmounts) - toAmounts = {self.addr_pool_TSLA_DUSD: "20000000@DUSD"} + toAmounts = {self.addr_pool_TSLA_DUSD: ["4000000000@TSLA", "20000000@DUSD"]} self.nodes[0].accounttoaccount(self.account0, toAmounts) self.nodes[0].generate(1) @@ -159,9 +169,10 @@ def create_fill_addresses(self): self.nodes[0].accounttoaccount(self.account0, toAmounts) self.nodes[0].generate(1) + # Check balances on each account account = self.nodes[0].getaccount(self.account0) - assert_equal(account, []) + assert_equal(account, ["100.00000000@DUSD"]) account = self.nodes[0].getaccount(self.addr_pool_DFI_DUSD) assert_equal(account, ['5000000.00000000@DFI', '20000000.00000000@DUSD']) account = self.nodes[0].getaccount(self.addr_pool_BTC_DUSD) @@ -242,8 +253,11 @@ def setup_loanschemes(self): self.nodes[0].createloanscheme(200, 1, 'LOAN200') self.nodes[0].generate(1) - def setup(self): - self.nodes[0].generate(150) + def setup(self, FCR=False): + self.nodes[0].generate(100) + if(FCR): + self.go_post_FCR() + self.account0 = self.nodes[0].get_genesis_keys().ownerAuthAddress self.create_tokens() self.setup_oracles() @@ -251,10 +265,11 @@ def setup(self): self.create_fill_addresses() self.setup_poolpairs() self.setup_loanschemes() + self.FINISHED_SETUP_BLOCK = self.nodes[0].getblockcount() def payback_DUSD_with_BTC(self): self.vaultId1 = self.nodes[0].createvault(self.account0, 'LOAN200') - self.nodes[0].generate(90) + self.nodes[0].generate(120) self.nodes[0].deposittovault(self.vaultId1, self.addr_DFI, "50@DFI") self.nodes[0].generate(1) @@ -266,6 +281,7 @@ def payback_DUSD_with_BTC(self): self.nodes[0].generate(1) # Should not be able to payback loan with BTC + errorString = '' try: self.nodes[0].paybackloan({ 'vaultId': self.vaultId1, @@ -278,6 +294,7 @@ def payback_DUSD_with_BTC(self): def payback_with_DFI_prior_to_atribute_activation(self): # Should not be able to payback loan before DFI payback enabled + errorString = '' try: self.nodes[0].paybackloan({ 'vaultId': self.vaultId1, @@ -455,6 +472,12 @@ def setgov_enable_dfi_payback_and_dfi_fee_pct(self): self.nodes[0].setgov({"ATTRIBUTES":{'v0/token/' + self.idTSLA + '/payback_dfi_fee_pct':'0.01'}}) self.nodes[0].generate(1) + self.nodes[0].setgov({"ATTRIBUTES":{'v0/token/' + self.idTSLA + '/payback_dfi':'true'}}) + self.nodes[0].generate(1) + + self.nodes[0].setgov({"ATTRIBUTES":{'v0/token/' + self.idTSLA + '/payback_dfi_fee_pct':'0.01'}}) + self.nodes[0].generate(1) + def setgov_enable_dTSLA_to_dBTC_payback(self): self.nodes[0].setgov({ "ATTRIBUTES":{ @@ -694,9 +717,125 @@ def payback_TSLA_with_1sat_dBTC(self): assert_equal(info['paybacktokens'], ['3750000.00000002@TSLA']) assert_equal(info['paybackburn'], old_info['paybackburn'] + Decimal('0.00012413')) - def run_test(self): - self.setup() + def multipayback_DUSD_with_DFI_and_DUSD(self): + self.vaultId5 = self.nodes[0].createvault(self.account0, 'LOAN200') + self.nodes[0].generate(1) + + self.nodes[0].deposittovault(self.vaultId5, self.addr_DFI, "100@DFI") + self.nodes[0].generate(1) + + self.nodes[0].generate(1) + # Create and fill addres with 10DFI + 70DUSD + self.nodes[0].utxostoaccount({self.account0: "11@" + self.symbolDFI}) + self.nodes[0].generate(1) + self.addr_DFI_DUSD = self.nodes[0].getnewaddress("", "legacy") + toAmounts = {self.addr_DFI_DUSD: ["11@DFI", "71@DUSD"]} + self.nodes[0].accounttoaccount(self.account0, toAmounts) + self.nodes[0].generate(1) + + self.nodes[0].takeloan({ + 'vaultId': self.vaultId5, + 'amounts': "100@" + self.symboldUSD + }) + self.nodes[0].generate(1) + + [balanceDFIBefore, _] = self.nodes[0].getaccount(self.addr_DFI_DUSD)[0].split('@') + [balanceDUSDBefore, _] = self.nodes[0].getaccount(self.addr_DFI_DUSD)[1].split('@') + assert_equal(balanceDUSDBefore, '71.00000000') + assert_equal(balanceDFIBefore, '11.00000000') + + self.nodes[0].paybackloan({ + 'vaultId': self.vaultId5, + 'from': self.addr_DFI_DUSD, + 'loans': [ + { + 'dToken': self.iddUSD, + 'amounts': ["70@DUSD", "10@DFI"] + } + ] + }) + #self.nodes[0].paybackloan({ + # 'vaultId': self.vaultId5, + # 'from': self.addr_DFI_DUSD, + # 'loans': [ + # { + # 'dToken': self.iddUSD, + # 'amounts': "70@DUSD" + # }, + # { + # 'dToken': self.iddUSD, + # 'amounts': "10@DFI" + # } + # ] + #}) + self.nodes[0].generate(1) + + vaultAfter = self.nodes[0].getvault(self.vaultId5) + assert_equal(vaultAfter["loanAmounts"], []) + [balanceDUSDAfter, _] = self.nodes[0].getaccount(self.addr_DFI_DUSD)[1].split('@') + [balanceDFIAfter, _] = self.nodes[0].getaccount(self.addr_DFI_DUSD)[0].split('@') + assert_equal(Decimal(balanceDUSDBefore) - Decimal(balanceDUSDAfter), Decimal('62.00000191')) # balanceAfter = 71DUSD - (100.00000191DUSD loan+interests - 38DUSD (40DFI - 5% fee) + assert_equal(Decimal(balanceDFIBefore) - Decimal(balanceDFIAfter), Decimal('10')) + + def multipayback_DUSD_with_DFI_and_DUSD_Pre_FCR(self): + self.vaultId6 = self.nodes[0].createvault(self.account0, 'LOAN200') + self.nodes[0].generate(1) + + self.nodes[0].deposittovault(self.vaultId6, self.addr_DFI, "100@DFI") + self.nodes[0].generate(1) + + self.nodes[0].generate(1) + # Create and fill addres with 10DFI + 70DUSD + self.nodes[0].utxostoaccount({self.account0: "11@" + self.symbolDFI}) + self.nodes[0].generate(1) + self.addr_DFI_DUSD = self.nodes[0].getnewaddress("", "legacy") + toAmounts = {self.addr_DFI_DUSD: ["11@DFI", "71@DUSD"]} + self.nodes[0].accounttoaccount(self.account0, toAmounts) + self.nodes[0].generate(1) + + self.nodes[0].takeloan({ + 'vaultId': self.vaultId6, + 'amounts': "100@" + self.symboldUSD + }) + self.nodes[0].generate(1) + + [balanceDFIBefore, _] = self.nodes[0].getaccount(self.addr_DFI_DUSD)[0].split('@') + [balanceDUSDBefore, _] = self.nodes[0].getaccount(self.addr_DFI_DUSD)[1].split('@') + assert_equal(balanceDUSDBefore, '71.00000000') + assert_equal(balanceDFIBefore, '11.00000000') + errorString = '' + try: + self.nodes[0].paybackloan({ + 'vaultId': self.vaultId6, + 'from': self.addr_DFI_DUSD, + 'amounts': ["70@DUSD", "10@DFI"] + }) + except JSONRPCException as e: + errorString = e.error['message'] + assert("Payback of loan via DFI token is not currently active" in errorString) + self.nodes[0].generate(1) + + self.nodes[0].setgov({"ATTRIBUTES":{'v0/token/' + self.iddUSD + '/payback_dfi':'true'}}) + self.nodes[0].generate(1) + + self.nodes[0].paybackloan({ + 'vaultId': self.vaultId6, + 'from': self.addr_DFI_DUSD, + 'amounts': ["70@DUSD", "10@DFI"] + }) + self.nodes[0].generate(1) + + vaultAfter = self.nodes[0].getvault(self.vaultId6) + assert_equal(vaultAfter["loanAmounts"], []) + [balanceDUSDAfter, _] = self.nodes[0].getaccount(self.addr_DFI_DUSD)[1].split('@') + [balanceDFIAfter, _] = self.nodes[0].getaccount(self.addr_DFI_DUSD)[0].split('@') + assert_equal(Decimal(balanceDUSDBefore) - Decimal(balanceDUSDAfter), Decimal('60.40000571')) + assert_equal(Decimal(balanceDFIBefore) - Decimal(balanceDFIAfter), Decimal('10')) + + + def run_test(self): + self.setup(FCR=True) self.payback_DUSD_with_BTC() self.payback_with_DFI_prior_to_atribute_activation() self.setgov_attribute_to_false_and_payback() @@ -720,5 +859,11 @@ def run_test(self): self.payback_dUSD_with_dUSD() self.payback_TSLA_with_1sat_dBTC() + self.multipayback_DUSD_with_DFI_and_DUSD() + + self.reset_chain() + self.setup(FCR=False) + self.multipayback_DUSD_with_DFI_and_DUSD_Pre_FCR() + if __name__ == '__main__': PaybackDFILoanTest().main() diff --git a/test/functional/feature_poolswap_mainnet.py b/test/functional/feature_poolswap_mainnet.py index 0810c733255..88e247a0d47 100755 --- a/test/functional/feature_poolswap_mainnet.py +++ b/test/functional/feature_poolswap_mainnet.py @@ -88,6 +88,16 @@ def create_pool_pairs(self): }, []) self.nodes[0].generate(1) + def add_1satoshi_liquidity_empty_pool(self): + errorString='' # remove [pylint E0601] + try: + self.nodes[0].addpoolliquidity({ + self.account_gs: ["0.0000001@" + self.symbol_key_GOLD, "0.0000001@" + self.symbol_key_SILVER] + }, self.account_gs, []) + except JSONRPCException as e: + errorString = e.error['message'] + assert('liquidity too low' in errorString) + def add_liquidity(self): self.nodes[0].addpoolliquidity({ self.account_gs: ["5000000@" + self.symbol_key_GOLD, "500000@" + self.symbol_key_SILVER] @@ -97,13 +107,25 @@ def add_liquidity(self): }, self.account_sd, []) self.nodes[0].generate(1) + def add_1satoshi_liquidity_non_empty_pool(self): + errorString='' # remove [pylint E0601] + try: + self.nodes[0].addpoolliquidity({ + self.account_gs: ["0.00000001@" + self.symbol_key_GOLD, "0.00000001@" + self.symbol_key_SILVER] + }, self.account_gs, []) + except JSONRPCException as e: + errorString = e.error['message'] + assert('amounts too low, zero liquidity' in errorString) + def setup(self): self.nodes[0].generate(self.FC_HEIGHT) self.create_tokens() self.mint_tokens(100000000) self.create_pool_pairs() + self.add_1satoshi_liquidity_empty_pool() self.add_liquidity() + self.add_1satoshi_liquidity_non_empty_pool() def test_swap_with_wrong_amounts(self): from_address = self.account_gs @@ -111,6 +133,7 @@ def test_swap_with_wrong_amounts(self): to_address = self.nodes[0].getnewaddress("") assert_equal(from_account[1], '45000000.00000000@GOLD#128') # try swap negative amount + errorString='' # remove [pylint E0601] try: self.nodes[0].poolswap({ "from": self.account_gs,