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

Test/payback v2 multiple error #1183

Merged
merged 6 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions test/functional/feature_loan_payback_dfi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
187 changes: 166 additions & 21 deletions test/functional/feature_loan_payback_dfi_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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]
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -242,19 +253,23 @@ 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()
self.setup_loan_tokens()
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)
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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":{
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Loading