forked from DeFiCh/ain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_loan.py
executable file
·296 lines (247 loc) · 12.9 KB
/
feature_loan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/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."""
from test_framework.authproxy import JSONRPCException
from test_framework.test_framework import DefiTestFramework
from test_framework.util import assert_equal
import calendar
import time
from decimal import Decimal
class LoanTest (DefiTestFramework):
def set_test_params(self):
self.num_nodes = 2
self.setup_clean_chain = True
self.extra_args = [
['-txnotokens=0', '-amkheight=1', '-bayfrontheight=1', '-bayfrontgardensheight=1', '-eunosheight=1', '-txindex=1', '-fortcanningheight=1'],
['-txnotokens=0', '-amkheight=1', '-bayfrontheight=1', '-bayfrontgardensheight=1', '-eunosheight=1', '-txindex=1', '-fortcanningheight=1']
]
def run_test(self):
self.nodes[0].generate(300)
self.nodes[0].createtoken({
"symbol": "BTC",
"name": "BTC token",
"isDAT": True,
"collateralAddress": self.nodes[0].get_genesis_keys().ownerAuthAddress
})
self.nodes[0].createtoken({
"symbol": "USD",
"name": "USD token",
"isDAT": True,
"collateralAddress": self.nodes[0].get_genesis_keys().ownerAuthAddress
})
self.nodes[0].generate(1)
# mint BTC
self.nodes[0].minttokens("2000@BTC")
self.nodes[0].generate(1)
account = self.nodes[0].get_genesis_keys().ownerAuthAddress
# transfer DFI
self.nodes[0].utxostoaccount({account: "2000@DFI"})
self.nodes[0].generate(1)
# setup oracle
oracle_address1 = self.nodes[0].getnewaddress("", "legacy")
price_feeds = [{"currency": "USD", "token": "DFI"}, {"currency": "USD", "token": "BTC"}, {"currency": "USD", "token": "TSLA"}]
oracle_id1 = self.nodes[0].appointoracle(oracle_address1, price_feeds, 10)
self.nodes[0].generate(1)
oracle_address2 = self.nodes[0].getnewaddress("", "legacy")
oracle_id2 = self.nodes[0].appointoracle(oracle_address2, price_feeds, 10)
self.nodes[0].generate(1)
# feed oracle
oracle1_prices = [{"currency": "USD", "tokenAmount": "10@TSLA"}, {"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)
oracle2_prices = [{"currency": "USD", "tokenAmount": "15@TSLA"}, {"currency": "USD", "tokenAmount": "15@DFI"}, {"currency": "USD", "tokenAmount": "15@BTC"}]
timestamp = calendar.timegm(time.gmtime())
self.nodes[0].setoracledata(oracle_id2, timestamp, oracle2_prices)
self.nodes[0].generate(1)
# set DFI an BTC as collateral tokens
self.nodes[0].setcollateraltoken({
'token': "DFI",
'factor': 1,
'fixedIntervalPriceId': "DFI/USD"})
self.nodes[0].generate(1)
self.nodes[0].setcollateraltoken({
'token': "BTC",
'factor': 1,
'fixedIntervalPriceId': "BTC/USD"})
self.nodes[0].generate(1)
# Create loan schemes
self.nodes[0].createloanscheme(200, 1, 'LOAN0001')
self.nodes[0].generate(1)
self.nodes[0].createloanscheme(150, 0.5, 'LOAN0002')
self.nodes[0].generate(1)
# Create vault
vaultId1 = self.nodes[0].createvault(account, '') # default loan scheme
self.nodes[0].generate(5)
# deposit DFI and BTC to vault1
self.nodes[0].deposittovault(vaultId1, account, '1000@DFI')
self.nodes[0].generate(1)
self.nodes[0].deposittovault(vaultId1, account, '1000@BTC')
self.nodes[0].generate(1)
# set TSLA as loan token
self.nodes[0].setloantoken({
'symbol': "TSLA",
'name': "Tesla Token",
'fixedIntervalPriceId': "TSLA/USD",
'mintable': True,
'interest': 1})
self.nodes[0].generate(6)
# take loan
self.nodes[0].takeloan({
'vaultId': vaultId1,
'amounts': "1000@TSLA"})
self.nodes[0].generate(1)
accountBal = self.nodes[0].getaccount(account)
assert_equal(accountBal, ['1000.00000000@DFI', '1000.00000000@BTC', '1000.00000000@TSLA'])
self.nodes[0].generate(60) # ~10 hours
vault1 = self.nodes[0].getvault(vaultId1)
interests = self.nodes[0].getinterest(
vault1['loanSchemeId'],
'TSLA'
)
totalLoanAmount = (1000+interests[0]['totalInterest']) # Initial loan taken
assert_equal(Decimal(vault1['loanAmounts'][0].split("@")[0]), totalLoanAmount)
# Trigger liquidation updating price in oracle
oracle1_prices = [{"currency": "USD", "tokenAmount": "20@TSLA"}]
oracle2_prices = [{"currency": "USD", "tokenAmount": "30@TSLA"}]
timestamp = calendar.timegm(time.gmtime())
self.nodes[0].setoracledata(oracle_id1, timestamp, oracle1_prices)
self.nodes[0].setoracledata(oracle_id2, timestamp, oracle2_prices)
self.nodes[0].generate(36)
# Auction tests
auctionlist = self.nodes[0].listauctions()
assert_equal(len(auctionlist[0]['batches']), 3)
vault1 = self.nodes[0].getvault(vaultId1)
getloaninfo = self.nodes[0].getloaninfo()
assert_equal(getloaninfo['totals']['openAuctions'], 3)
# Fail auction bid
try:
self.nodes[0].placeauctionbid(vaultId1, 0, account, "410@TSLA")
except JSONRPCException as e:
errorString = e.error['message']
assert("First bid should include liquidation penalty of 5%" in errorString)
self.nodes[0].minttokens("1000@TSLA")
self.nodes[0].generate(1)
self.nodes[0].placeauctionbid(vaultId1, 0, account, "550@TSLA")
self.nodes[0].generate(1)
batches = self.nodes[0].listauctions()[0]['batches']
highestBid = batches[0]['highestBid']
assert_equal(highestBid['owner'], account)
assert_equal(highestBid['amount'], '550.00000000@TSLA')
accountBal = self.nodes[0].getaccount(account)
assert_equal(accountBal, ['1000.00000000@DFI', '1000.00000000@BTC', '1450.00000000@TSLA'])
# new account for new bidder
self.nodes[0].generate(1)
account2 = self.nodes[0].getnewaddress()
self.nodes[0].accounttoaccount(account, {account2: "1000@TSLA"} )
self.nodes[0].generate(1)
# Fail auction bid less that 1% higher
try:
self.nodes[0].placeauctionbid(vaultId1, 0, account2, "555@TSLA") # just under 1%
except JSONRPCException as e:
errorString = e.error['message']
assert("Bid override should be at least 1% higher than current one" in errorString)
self.nodes[0].placeauctionbid(vaultId1, 0, account2, "555.5@TSLA") # above 1%
self.nodes[0].generate(1)
# check balances are right after greater bid
account2Bal = self.nodes[0].getaccount(account2)
accountBal = self.nodes[0].getaccount(account)
assert_equal(accountBal, ['1000.00000000@DFI', '1000.00000000@BTC', '1000.00000000@TSLA'])
assert_equal(account2Bal, ['444.50000000@TSLA'])
# let auction end and check account balances
self.nodes[0].generate(8)
account2Bal = self.nodes[0].getaccount(account2)
accountBal = self.nodes[0].getaccount(account)
vault1 = self.nodes[0].getvault(vaultId1)
assert_equal(vault1['state'], "inLiquidation")
assert_equal(vault1['liquidationHeight'], 469)
assert_equal(vault1['liquidationPenalty'], Decimal('5.00000000'))
assert_equal(vault1['batchCount'], 2)
assert_equal(accountBal, ['1000.00000000@DFI', '1000.00000000@BTC', '1000.00000000@TSLA'])
# auction winner account has now first batch collaterals
assert_equal(account2Bal, ['400.00000000@DFI', '400.00000000@BTC', '444.50000000@TSLA'])
auction = self.nodes[0].listauctionhistory()[0]
assert_equal(auction['winner'], account2)
assert_equal(auction['blockHeight'], 432)
assert_equal(auction['vaultId'], vaultId1)
assert_equal(auction['batchIndex'], 0)
assert_equal(auction['auctionBid'], "555.50000000@TSLA")
assert_equal(auction['auctionWon'].sort(), ['400.00000000@DFI', '400.00000000@BTC'].sort())
# check that still auction due to 1 batch without bid
auctionlist = self.nodes[0].listauctions()
assert_equal(len(auctionlist[0]['batches']), 2)
self.nodes[0].placeauctionbid(vaultId1, 0, account, "515@TSLA") # above 5% and leave vault with some loan to exit liquidation state
self.nodes[0].generate(40) # let auction end
auction = self.nodes[0].listauctionhistory(account)[0]
assert_equal(auction['winner'], account)
assert_equal(auction['blockHeight'], 469)
assert_equal(auction['vaultId'], vaultId1)
assert_equal(auction['batchIndex'], 0)
assert_equal(auction['auctionBid'], "515.00000000@TSLA")
assert_equal(auction['auctionWon'].sort(), ['399.99999600@DFI', '399.99999600@DFI'].sort())
self.nodes[0].placeauctionbid(vaultId1, 0, account, "259@TSLA")
self.nodes[0].generate(40) # let auction end
auctions = self.nodes[0].listauctionhistory()
assert_equal(len(auctions), 3)
assert_equal(auctions[0]['winner'], account)
assert_equal(auctions[1]['winner'], account)
assert_equal(auctions[2]['winner'], account2)
assert_equal(auctions[0]['blockHeight'], 506)
assert_equal(auctions[1]['blockHeight'], 469)
assert_equal(auctions[2]['blockHeight'], 432)
assert_equal(auctions[0]['auctionBid'], "259.00000000@TSLA")
assert_equal(auctions[1]['auctionBid'], "515.00000000@TSLA")
assert_equal(auctions[2]['auctionBid'], "555.50000000@TSLA")
auctionsV = self.nodes[0].listauctionhistory(vaultId1)
assert_equal(len(auctionsV), 3)
assert_equal(auctions, auctionsV)
vault1 = self.nodes[0].getvault(vaultId1)
accountBal = self.nodes[0].getaccount(account)
assert_equal(vault1['state'], "active")
assert_equal(accountBal, ['1600.00000000@DFI', '1600.00000000@BTC', '226.00000000@TSLA'])
self.nodes[0].deposittovault(vaultId1, account, '600@DFI')
self.nodes[0].generate(1)
vault1 = self.nodes[0].getvault(vaultId1)
assert_equal(vault1['collateralAmounts'], ['600.00000000@DFI'])
# Trigger liquidation updating price in oracle
oracle1_prices = [{"currency": "USD", "tokenAmount": "2@TSLA"}]
oracle2_prices = [{"currency": "USD", "tokenAmount": "3@TSLA"}]
timestamp = calendar.timegm(time.gmtime())
self.nodes[0].setoracledata(oracle_id1, timestamp, oracle1_prices)
self.nodes[0].setoracledata(oracle_id2, timestamp, oracle2_prices)
self.nodes[0].generate(36)
# take loan
self.nodes[0].takeloan({
'vaultId': vaultId1,
'amounts': "1000@TSLA"
})
self.nodes[0].generate(1)
# Reset price in oracle
oracle1_prices = [{"currency": "USD", "tokenAmount": "200@TSLA"}]
oracle2_prices = [{"currency": "USD", "tokenAmount": "300@TSLA"}]
timestamp = calendar.timegm(time.gmtime())
self.nodes[0].setoracledata(oracle_id1, timestamp, oracle1_prices)
self.nodes[0].setoracledata(oracle_id2, timestamp, oracle2_prices)
self.nodes[0].generate(36)
# Not enought tokens on account
try:
self.nodes[0].placeauctionbid(vaultId1, 0, "*", "1600@TSLA")
except JSONRPCException as e:
errorString = e.error['message']
assert("Not enough tokens on account, call sendtokenstoaddress to increase it." in errorString)
newAddress = self.nodes[0].getnewaddress("")
self.nodes[0].sendtokenstoaddress({}, {newAddress: "1600@TSLA"}) # newAddress has now the highest amount of TSLA token
self.nodes[0].generate(1)
self.nodes[0].placeauctionbid(vaultId1, 0, "*", "1600@TSLA") # Autoselect address with highest amount of TSLA token
self.nodes[0].generate(40) # let auction end
auction = self.nodes[0].listauctionhistory(newAddress)[0]
assert_equal(auction['winner'], newAddress)
assert_equal(auction['blockHeight'], 600)
assert_equal(auction['vaultId'], vaultId1)
assert_equal(auction['batchIndex'], 0)
assert_equal(auction['auctionBid'], "1600.00000000@TSLA")
if __name__ == '__main__':
LoanTest().main()