From 710ff5a832be758bf14133dab2fedec4ba8f6aae Mon Sep 17 00:00:00 2001 From: jouzo Date: Mon, 31 Jan 2022 11:12:57 +0100 Subject: [PATCH 1/5] Add getinterest output test --- test/functional/feature_loan_get_interest.py | 148 +++++++++++++++++++ test/functional/feature_loan_payback_dfi.py | 6 +- test/functional/test_runner.py | 1 + 3 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 test/functional/feature_loan_get_interest.py diff --git a/test/functional/feature_loan_get_interest.py b/test/functional/feature_loan_get_interest.py new file mode 100644 index 00000000000..c452966dd11 --- /dev/null +++ b/test/functional/feature_loan_get_interest.py @@ -0,0 +1,148 @@ +#!/usr/bin/env python3 +# Copyright (c) 2014-2019 The Bitcoin Core developers +# Copyright (c) DeFi Blockchain Developers +# Distributed under the MIT software license, see the accompanying +# file LICENSE or http://www.opensource.org/licenses/mit-license.php. +"""Test Loan - payback loan.""" + +from test_framework.test_framework import DefiTestFramework +from test_framework.util import assert_equal + +import calendar +import time +from decimal import Decimal + +class LoanGetInterestTest (DefiTestFramework): + symbolDFI = "DFI" + symbolBTC = "BTC" + symboldUSD = "DUSD" + + def set_test_params(self): + self.num_nodes = 1 + self.setup_clean_chain = True + self.extra_args = [ + ['-txnotokens=0', '-amkheight=50', '-bayfrontheight=50', '-bayfrontgardensheight=1', + '-fortcanningheight=50', '-fortcanninghillheight=50', '-eunosheight=50', '-txindex=1'] + ] + + def setup(self): + self.nodes[0].generate(150) + + self.account0 = self.nodes[0].get_genesis_keys().ownerAuthAddress + + self.nodes[0].createtoken({ + "symbol": self.symbolBTC, + "name": "BTC token", + "isDAT": True, + "collateralAddress": self.account0 + }) + + self.nodes[0].generate(1) + + idDFI = list(self.nodes[0].gettoken(self.symbolDFI).keys())[0] + idBTC = list(self.nodes[0].gettoken(self.symbolBTC).keys())[0] + + self.nodes[0].utxostoaccount({self.account0: "1000@" + self.symbolDFI}) + + oracle_address1 = self.nodes[0].getnewaddress("", "legacy") + price_feeds1 = [ + {"currency": "USD", "token": "DFI"}, + {"currency": "USD", "token": "BTC"}, + ] + oracle_id1 = self.nodes[0].appointoracle( + oracle_address1, price_feeds1, 10) + self.nodes[0].generate(1) + + # feed oracle + oracle1_prices = [ + {"currency": "USD", "tokenAmount": "10@DFI"}, + {"currency": "USD", "tokenAmount": "10@BTC"} + ] + timestamp = calendar.timegm(time.gmtime()) + self.nodes[0].setoracledata(oracle_id1, timestamp, oracle1_prices) + self.nodes[0].generate(1) + + self.nodes[0].setcollateraltoken({ + 'token': idDFI, + 'factor': 1, + 'fixedIntervalPriceId': "DFI/USD" + }) + + self.nodes[0].setcollateraltoken({ + 'token': idBTC, + 'factor': 1, + 'fixedIntervalPriceId': "BTC/USD"}) + + self.nodes[0].generate(1) + + self.nodes[0].setloantoken({ + 'symbol': self.symboldUSD, + 'name': "DUSD stable token", + 'fixedIntervalPriceId': "DUSD/USD", + 'mintable': True, + 'interest': 1 + }) + self.nodes[0].generate(1) + + self.nodes[0].createloanscheme(150, 100, 'LOAN150') + + self.nodes[0].generate(5) + + iddUSD = list(self.nodes[0].gettoken(self.symboldUSD).keys())[0] + + self.nodes[0].minttokens("300@" + self.symboldUSD) + self.nodes[0].generate(1) + + poolOwner = self.nodes[0].getnewaddress("", "legacy") + # create pool DUSD-DFI + self.nodes[0].createpoolpair({ + "tokenA": iddUSD, + "tokenB": idDFI, + "commission": Decimal('0.000'), + "status": True, + "ownerAddress": poolOwner, + "pairSymbol": "DUSD-DFI", + }) + self.nodes[0].generate(1) + + self.nodes[0].addpoolliquidity( + {self.account0: ["30@" + self.symbolDFI, "300@" + self.symboldUSD]}, self.account0) + self.nodes[0].generate(1) + + def run_test(self): + self.setup() + + vaultId = self.nodes[0].createvault(self.account0, 'LOAN150') + self.nodes[0].generate(1) + + self.nodes[0].deposittovault(vaultId, self.account0, "400@DFI") + self.nodes[0].generate(1) + + self.nodes[0].takeloan({ + 'vaultId': vaultId, + 'amounts': "2000@" + self.symboldUSD + }) + self.nodes[0].generate(1) + + self.nodes[0].generate(25) # Accrue interest + + getInterest = self.nodes[0].getinterest("LOAN150", "DUSD") + assert_equal(getInterest[0]['totalInterest'], Decimal('0.99923876')) + assert_equal(getInterest[0]['interestPerBlock'], Decimal('0.03843226')) + + self.nodes[0].deposittovault(vaultId, self.account0, "100@DFI") + self.nodes[0].generate(1) + + self.nodes[0].takeloan({ + 'vaultId': vaultId, + 'amounts': "500@" + self.symboldUSD + }) + self.nodes[0].generate(1) + + getInterest = self.nodes[0].getinterest("LOAN150", "DUSD") + assert_equal(getInterest[0]['totalInterest'], Decimal('1.08571134')) + assert_equal(getInterest[0]['interestPerBlock'], Decimal('0.04804032')) + + +if __name__ == '__main__': + LoanGetInterestTest().main() diff --git a/test/functional/feature_loan_payback_dfi.py b/test/functional/feature_loan_payback_dfi.py index bcb193ab37a..4d85026a916 100755 --- a/test/functional/feature_loan_payback_dfi.py +++ b/test/functional/feature_loan_payback_dfi.py @@ -3,7 +3,7 @@ # Copyright (c) DeFi Blockchain Developers # Distributed under the MIT software license, see the accompanying # file LICENSE or http://www.opensource.org/licenses/mit-license.php. -"""Test Loan - payback loan.""" +"""Test Loan - payback loan dfi.""" from test_framework.test_framework import DefiTestFramework from test_framework.util import assert_equal, assert_raises_rpc_error @@ -13,7 +13,7 @@ from decimal import Decimal, ROUND_UP -class PaybackLoanTest (DefiTestFramework): +class PaybackDFILoanTest (DefiTestFramework): def set_test_params(self): self.num_nodes = 1 self.setup_clean_chain = True @@ -287,4 +287,4 @@ def run_test(self): if __name__ == '__main__': - PaybackLoanTest().main() + PaybackDFILoanTest().main() diff --git a/test/functional/test_runner.py b/test/functional/test_runner.py index 383e5520fb7..4db7114cc6c 100755 --- a/test/functional/test_runner.py +++ b/test/functional/test_runner.py @@ -164,6 +164,7 @@ 'feature_loan_setloantoken.py', 'feature_loan_basics.py', 'feature_loan_payback_dfi.py', + 'feature_loan_get_interest.py', 'feature_loan_listauctions.py', 'feature_loan_auctions.py', 'feature_loan_dusd_as_collateral.py', From 34d44d3eba84666b59c9883f01347ea7bbcdaaa6 Mon Sep 17 00:00:00 2001 From: jouzo Date: Mon, 31 Jan 2022 11:17:19 +0100 Subject: [PATCH 2/5] Adds FCM height and adjust calculations --- test/functional/feature_loan_get_interest.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/functional/feature_loan_get_interest.py b/test/functional/feature_loan_get_interest.py index c452966dd11..45f7227f36a 100644 --- a/test/functional/feature_loan_get_interest.py +++ b/test/functional/feature_loan_get_interest.py @@ -22,7 +22,7 @@ def set_test_params(self): self.setup_clean_chain = True self.extra_args = [ ['-txnotokens=0', '-amkheight=50', '-bayfrontheight=50', '-bayfrontgardensheight=1', - '-fortcanningheight=50', '-fortcanninghillheight=50', '-eunosheight=50', '-txindex=1'] + '-fortcanningheight=50', '-fortcanningmuseumheight=50', '-fortcanninghillheight=50', '-eunosheight=50', '-txindex=1'] ] def setup(self): @@ -127,8 +127,8 @@ def run_test(self): self.nodes[0].generate(25) # Accrue interest getInterest = self.nodes[0].getinterest("LOAN150", "DUSD") - assert_equal(getInterest[0]['totalInterest'], Decimal('0.99923876')) - assert_equal(getInterest[0]['interestPerBlock'], Decimal('0.03843226')) + assert_equal(getInterest[0]['totalInterest'], Decimal('0.99923902')) + assert_equal(getInterest[0]['interestPerBlock'], Decimal('0.03843227')) self.nodes[0].deposittovault(vaultId, self.account0, "100@DFI") self.nodes[0].generate(1) @@ -140,8 +140,8 @@ def run_test(self): self.nodes[0].generate(1) getInterest = self.nodes[0].getinterest("LOAN150", "DUSD") - assert_equal(getInterest[0]['totalInterest'], Decimal('1.08571134')) - assert_equal(getInterest[0]['interestPerBlock'], Decimal('0.04804032')) + assert_equal(getInterest[0]['totalInterest'], Decimal('1.08571163')) + assert_equal(getInterest[0]['interestPerBlock'], Decimal('0.04804034')) if __name__ == '__main__': From 2b2fa74b26917d51cdd396e4c463dc3a685b153c Mon Sep 17 00:00:00 2001 From: jouzo Date: Mon, 31 Jan 2022 11:38:00 +0100 Subject: [PATCH 3/5] Add calculation post FCM and post FCH --- test/functional/feature_loan_get_interest.py | 32 +++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/test/functional/feature_loan_get_interest.py b/test/functional/feature_loan_get_interest.py index 45f7227f36a..ea65f93f12d 100644 --- a/test/functional/feature_loan_get_interest.py +++ b/test/functional/feature_loan_get_interest.py @@ -22,7 +22,7 @@ def set_test_params(self): self.setup_clean_chain = True self.extra_args = [ ['-txnotokens=0', '-amkheight=50', '-bayfrontheight=50', '-bayfrontgardensheight=1', - '-fortcanningheight=50', '-fortcanningmuseumheight=50', '-fortcanninghillheight=50', '-eunosheight=50', '-txindex=1'] + '-fortcanningheight=50', '-fortcanningmuseumheight=200', '-fortcanninghillheight=250', '-eunosheight=50', '-txindex=1'] ] def setup(self): @@ -127,8 +127,8 @@ def run_test(self): self.nodes[0].generate(25) # Accrue interest getInterest = self.nodes[0].getinterest("LOAN150", "DUSD") - assert_equal(getInterest[0]['totalInterest'], Decimal('0.99923902')) - assert_equal(getInterest[0]['interestPerBlock'], Decimal('0.03843227')) + assert_equal(getInterest[0]['totalInterest'], Decimal('0.99923876')) + assert_equal(getInterest[0]['interestPerBlock'], Decimal('0.03843226')) self.nodes[0].deposittovault(vaultId, self.account0, "100@DFI") self.nodes[0].generate(1) @@ -140,9 +140,31 @@ def run_test(self): self.nodes[0].generate(1) getInterest = self.nodes[0].getinterest("LOAN150", "DUSD") - assert_equal(getInterest[0]['totalInterest'], Decimal('1.08571163')) - assert_equal(getInterest[0]['interestPerBlock'], Decimal('0.04804034')) + assert_equal(getInterest[0]['totalInterest'], Decimal('1.08571134')) + assert_equal(getInterest[0]['interestPerBlock'], Decimal('0.04804032')) + self.nodes[0].generate(10) # Activate FCM + + self.nodes[0].takeloan({ + 'vaultId': vaultId, + 'amounts': "500@" + self.symboldUSD + }) + self.nodes[0].generate(1) + + getInterest = self.nodes[0].getinterest("LOAN150", "DUSD") + assert_equal(getInterest[0]['totalInterest'], Decimal('1.62376293')) + assert_equal(getInterest[0]['interestPerBlock'], Decimal('0.05764839')) + + self.nodes[0].generate(50) # Activate FCH + self.nodes[0].takeloan({ + 'vaultId': vaultId, + 'amounts': "300@" + self.symboldUSD + }) + self.nodes[0].generate(1) + + getInterest = self.nodes[0].getinterest("LOAN150", "DUSD") + assert_equal(getInterest[0]['totalInterest'], Decimal('4.56959566')) + assert_equal(getInterest[0]['interestPerBlock'], Decimal('0.06341323')) if __name__ == '__main__': LoanGetInterestTest().main() From a47fae211b23c9d88ded09ef69a2fbc8a17f2b14 Mon Sep 17 00:00:00 2001 From: jouzo Date: Mon, 31 Jan 2022 12:36:43 +0100 Subject: [PATCH 4/5] Test takeloan of 1sat in loop --- test/functional/feature_loan_get_interest.py | 37 +++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/test/functional/feature_loan_get_interest.py b/test/functional/feature_loan_get_interest.py index ea65f93f12d..f9531572e42 100644 --- a/test/functional/feature_loan_get_interest.py +++ b/test/functional/feature_loan_get_interest.py @@ -22,7 +22,7 @@ def set_test_params(self): self.setup_clean_chain = True self.extra_args = [ ['-txnotokens=0', '-amkheight=50', '-bayfrontheight=50', '-bayfrontgardensheight=1', - '-fortcanningheight=50', '-fortcanningmuseumheight=200', '-fortcanninghillheight=250', '-eunosheight=50', '-txindex=1'] + '-fortcanningheight=50', '-fortcanningmuseumheight=200', '-fortcanningparkheight=270', '-fortcanninghillheight=300', '-eunosheight=50', '-txindex=1'] ] def setup(self): @@ -55,7 +55,7 @@ def setup(self): # feed oracle oracle1_prices = [ - {"currency": "USD", "tokenAmount": "10@DFI"}, + {"currency": "USD", "tokenAmount": "100000@DFI"}, {"currency": "USD", "tokenAmount": "10@BTC"} ] timestamp = calendar.timegm(time.gmtime()) @@ -147,24 +147,43 @@ def run_test(self): self.nodes[0].takeloan({ 'vaultId': vaultId, - 'amounts': "500@" + self.symboldUSD + 'amounts': "500.2@" + self.symboldUSD + }) + self.nodes[0].generate(1) + + getInterest = self.nodes[0].getinterest("LOAN150", "DUSD") + assert_equal(getInterest[0]['totalInterest'], Decimal('1.62376677')) + assert_equal(getInterest[0]['interestPerBlock'], Decimal('0.05765223')) + + self.nodes[0].takeloan({ + 'vaultId': vaultId, + 'amounts': "0.00000001@" + self.symboldUSD }) self.nodes[0].generate(1) getInterest = self.nodes[0].getinterest("LOAN150", "DUSD") - assert_equal(getInterest[0]['totalInterest'], Decimal('1.62376293')) - assert_equal(getInterest[0]['interestPerBlock'], Decimal('0.05764839')) + print("getInterest", getInterest) + assert_equal(getInterest[0]['totalInterest'], Decimal('1.68141901')) + assert_equal(getInterest[0]['interestPerBlock'], Decimal('0.05765224')) - self.nodes[0].generate(50) # Activate FCH self.nodes[0].takeloan({ 'vaultId': vaultId, - 'amounts': "300@" + self.symboldUSD + 'amounts': "50000.00000001@" + self.symboldUSD }) self.nodes[0].generate(1) getInterest = self.nodes[0].getinterest("LOAN150", "DUSD") - assert_equal(getInterest[0]['totalInterest'], Decimal('4.56959566')) - assert_equal(getInterest[0]['interestPerBlock'], Decimal('0.06341323')) + assert_equal(getInterest[0]['totalInterest'], Decimal('2.69987797')) + assert_equal(getInterest[0]['interestPerBlock'], Decimal('1.01845896')) + + for i in range(1, 50): + self.nodes[0].takeloan({ + 'vaultId': vaultId, + 'amounts': "0.00000001@" + self.symboldUSD + }) + self.nodes[0].generate(1) + newInterest = self.nodes[0].getinterest("LOAN150", "DUSD") + assert_equal(newInterest[0]['totalInterest'], Decimal('2.69987797') + newInterest[0]['interestPerBlock'] * i) if __name__ == '__main__': LoanGetInterestTest().main() From 2fbc769507ac54fe2f54f734a9271e47854f9cdd Mon Sep 17 00:00:00 2001 From: jouzo Date: Mon, 31 Jan 2022 12:39:20 +0100 Subject: [PATCH 5/5] Update test description --- test/functional/feature_loan_get_interest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/functional/feature_loan_get_interest.py b/test/functional/feature_loan_get_interest.py index f9531572e42..724baa3cf16 100644 --- a/test/functional/feature_loan_get_interest.py +++ b/test/functional/feature_loan_get_interest.py @@ -3,7 +3,7 @@ # Copyright (c) DeFi Blockchain Developers # Distributed under the MIT software license, see the accompanying # file LICENSE or http://www.opensource.org/licenses/mit-license.php. -"""Test Loan - payback loan.""" +"""Test Loan - get interest calculation.""" from test_framework.test_framework import DefiTestFramework from test_framework.util import assert_equal