forked from DeFiCh/ain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_token_lock.py
executable file
·330 lines (267 loc) · 12.3 KB
/
feature_token_lock.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/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 Token Lock."""
from test_framework.test_framework import DefiTestFramework
from test_framework.util import assert_equal, assert_raises_rpc_error
from decimal import Decimal
import time
class TokenLockTest(DefiTestFramework):
def set_test_params(self):
self.num_nodes = 1
self.setup_clean_chain = True
self.extra_args = [['-txnotokens=0', '-amkheight=1', '-bayfrontheight=1', '-eunosheight=1', '-fortcanningheight=1', '-fortcanninghillheight=1', '-fortcanningroadheight=1', '-fortcanningcrunchheight=200', '-subsidytest=1']]
def run_test(self):
self.nodes[0].generate(150)
# Set up oracles and tokens
self.setup_test()
# Test pool lock
self.pool_lock()
# Test oracle lock
self.oracle_lock()
# Test vault lock
self.vault_lock()
# Test token lock
self.token_lock()
def setup_test(self):
# Store address
self.address = self.nodes[0].get_genesis_keys().ownerAuthAddress
# Set token symbols
self.symbolDFI = 'DFI'
self.symbolDUSD = 'DUSD'
self.symbolTSLA = 'TSLA'
self.symbolGOOGL = 'GOOGL'
self.symbolTD = 'TSLA-DUSD'
# Setup oracle
oracle_address = self.nodes[0].getnewaddress("", "legacy")
price_feeds = [
{"currency": "USD", "token": self.symbolDFI},
{"currency": "USD", "token": self.symbolTSLA},
{"currency": "USD", "token": self.symbolGOOGL},
]
self.oracle_id = self.nodes[0].appointoracle(oracle_address, price_feeds, 10)
self.nodes[0].generate(1)
# Create Oracle prices
self.price_dfi = 5
self.price_tsla = 870
self.price_googl = 2850
# Set Oracle data
self.oracle_prices = [
{"currency": "USD", "tokenAmount": f'{self.price_dfi}@{self.symbolDFI}'},
{"currency": "USD", "tokenAmount": f'{self.price_tsla}@{self.symbolTSLA}'},
{"currency": "USD", "tokenAmount": f'{self.price_googl}@{self.symbolGOOGL}'},
]
self.nodes[0].setoracledata(self.oracle_id, int(time.time()), self.oracle_prices)
self.nodes[0].generate(10)
# Setup loan tokens
self.nodes[0].setloantoken({
'symbol': self.symbolDUSD,
'name': self.symbolDUSD,
'fixedIntervalPriceId': f'{self.symbolDUSD}/USD',
'mintable': True,
'interest': 0})
self.nodes[0].setloantoken({
'symbol': self.symbolTSLA,
'name': self.symbolTSLA,
'fixedIntervalPriceId': f'{self.symbolTSLA}/USD',
'mintable': True,
'interest': 1})
self.nodes[0].setloantoken({
'symbol': self.symbolGOOGL,
'name': self.symbolGOOGL,
'fixedIntervalPriceId': f'{self.symbolGOOGL}/USD',
'mintable': True,
'interest': 1})
self.nodes[0].generate(1)
# Set collateral token
self.nodes[0].setcollateraltoken({
'token': self.symbolDUSD,
'factor': 1,
'fixedIntervalPriceId': f'{self.symbolDUSD}/USD'
})
# Set token ids
self.idDUSD = list(self.nodes[0].gettoken(self.symbolDUSD).keys())[0]
self.idTSLA = list(self.nodes[0].gettoken(self.symbolTSLA).keys())[0]
self.idGOOGL = list(self.nodes[0].gettoken(self.symbolGOOGL).keys())[0]
# Mint tokens
self.nodes[0].minttokens([f'1000000@{self.idDUSD}'])
self.nodes[0].minttokens([f'1000000@{self.idTSLA}'])
self.nodes[0].minttokens([f'1000000@{self.idGOOGL}'])
self.nodes[0].generate(1)
# Tokenise DFI
self.nodes[0].utxostoaccount({self.address: f'2000@{self.symbolDFI}'})
self.nodes[0].generate(1)
# Create pool
self.nodes[0].createpoolpair({
"tokenA": self.symbolTSLA,
"tokenB": self.symbolDUSD,
"commission": 0.01,
"status": True,
"ownerAddress": self.address,
"pairSymbol": self.symbolTD,
})
self.nodes[0].createpoolpair({
"tokenA": self.symbolDFI,
"tokenB": self.symbolDUSD,
"commission": 0.01,
"status": True,
"ownerAddress": self.address,
})
self.nodes[0].generate(1)
# Add liquidity
self.nodes[0].addpoolliquidity({
self.address: [f'870000@{self.symbolTSLA}', f'1000@{self.symbolDUSD}']
}, self.address)
self.nodes[0].addpoolliquidity({
self.address: [f'1000@{self.symbolDFI}', f'5000@{self.symbolDUSD}']
}, self.address)
self.nodes[0].generate(1)
# Create loan schemes
self.nodes[0].createloanscheme(100, 0.01, 'LOAN100')
self.nodes[0].generate(1)
# Create vault
self.vault = self.nodes[0].createvault(self.address)
self.nodes[0].generate(1)
# Fund vault
self.nodes[0].deposittovault(self.vault, self.address, f'100000@{self.symbolDUSD}')
self.nodes[0].generate(1)
def pool_lock(self):
# Move to fork block
self.nodes[0].generate(200 - self.nodes[0].getblockcount())
# Enable token lock
self.nodes[0].setgov({"ATTRIBUTES":{f'v0/locks/token/{self.idTSLA}':'true'}})
self.nodes[0].generate(1)
# Try test pool swap in both directions
assert_raises_rpc_error(-32600, "Pool currently disabled due to locked token", self.nodes[0].poolswap, {
"from": self.address,
"tokenFrom": self.symbolTSLA,
"amountFrom": 1,
"to": self.address,
"tokenTo": self.symbolDUSD
})
assert_raises_rpc_error(-32600, "Pool currently disabled due to locked token", self.nodes[0].poolswap, {
"from": self.address,
"tokenFrom": self.symbolDUSD,
"amountFrom": 1,
"to": self.address,
"tokenTo": self.symbolTSLA
})
# Try pool swap in both directions
assert_raises_rpc_error(-32600, "Pool currently disabled due to locked token", self.nodes[0].poolswap, {
"from": self.address,
"tokenFrom": self.symbolTSLA,
"amountFrom": 1,
"to": self.address,
"tokenTo": self.symbolDUSD
})
assert_raises_rpc_error(-32600, "Pool currently disabled due to locked token", self.nodes[0].poolswap, {
"from": self.address,
"tokenFrom": self.symbolDUSD,
"amountFrom": 1,
"to": self.address,
"tokenTo": self.symbolTSLA
})
# Disable token lock
self.nodes[0].setgov({"ATTRIBUTES":{f'v0/locks/token/{self.idTSLA}':'false'}})
self.nodes[0].generate(1)
# Test pool swap should now work
self.nodes[0].testpoolswap({
"from": self.address,
"tokenFrom": self.symbolDUSD,
"amountFrom": 1,
"to": self.address,
"tokenTo": self.symbolTSLA
})
# Pool swap should now work
self.nodes[0].poolswap({
"from": self.address,
"tokenFrom": self.symbolDUSD,
"amountFrom": 1,
"to": self.address,
"tokenTo": self.symbolTSLA
})
self.nodes[0].clearmempool()
def oracle_lock(self):
# Set Oracle data
self.nodes[0].setoracledata(self.oracle_id, int(time.time()), self.oracle_prices)
self.nodes[0].generate(10)
# Check output before lock
result = self.nodes[0].getfixedintervalprice(f'{self.symbolTSLA}/USD')
assert_equal(result['fixedIntervalPriceId'], f'{self.symbolTSLA}/USD')
assert_equal(result['isLive'], True)
# Enable token lock
self.nodes[0].setgov({"ATTRIBUTES":{f'v0/locks/token/{self.idTSLA}':'true'}})
self.nodes[0].generate(1)
# Check price feed disabled
assert_raises_rpc_error(-5, "Fixed interval price currently disabled due to locked token", self.nodes[0].getfixedintervalprice, f'{self.symbolTSLA}/USD')
# Disable token lock
self.nodes[0].setgov({"ATTRIBUTES":{f'v0/locks/token/{self.idTSLA}':'false'}})
self.nodes[0].generate(1)
# Set Oracle data
self.nodes[0].setoracledata(self.oracle_id, int(time.time()), self.oracle_prices)
self.nodes[0].generate(10)
# Check output before lock
result = self.nodes[0].getfixedintervalprice(f'{self.symbolTSLA}/USD')
assert_equal(result['fixedIntervalPriceId'], f'{self.symbolTSLA}/USD')
assert_equal(result['isLive'], True)
def vault_lock(self):
# Take loan
self.nodes[0].takeloan({'vaultId': self.vault, 'amounts': f'1@{self.symbolTSLA}'})
self.nodes[0].generate(1)
# Enable token lock
self.nodes[0].setgov({"ATTRIBUTES":{f'v0/locks/token/{self.idTSLA}':'true'}})
self.nodes[0].generate(1)
# Try and take loan while token in vault locked
assert_raises_rpc_error(-32600, "Cannot take loan while any of the asset's price in the vault is not live", self.nodes[0].takeloan, {'vaultId': self.vault, 'amounts': f'1@{self.symbolTSLA}'})
assert_raises_rpc_error(-32600, "Cannot take loan while any of the asset's price in the vault is not live", self.nodes[0].takeloan, {'vaultId': self.vault, 'amounts': f'1@{self.symbolGOOGL}'})
# Vault amounts should be -1 while token locked
result = self.nodes[0].getvault(self.vault)
assert_equal(result['collateralValue'], -1)
assert_equal(result['loanValue'], -1)
assert_equal(result['interestValue'], -1)
assert_equal(result['informativeRatio'], -1)
assert_equal(result['collateralRatio'], -1)
result = self.nodes[0].getvault(self.vault, True)
assert_equal(result['collateralValue'], -1)
assert_equal(result['loanValue'], -1)
assert_equal(result['interestValue'], -1)
assert_equal(result['informativeRatio'], -1)
assert_equal(result['collateralRatio'], -1)
assert_equal(result['interestPerBlockValue'], -1)
assert_equal(result['interestsPerBlock'], [])
# Deposit to vault should fail
assert_raises_rpc_error(-32600, "Fixed interval price currently disabled due to locked token", self.nodes[0].deposittovault, self.vault, self.address, f'100000@{self.symbolDUSD}')
# Payback loan with native token failed due to no pool to swap interest to DFI
assert_raises_rpc_error(-32600, "Pool currently disabled due to locked token", self.nodes[0].paybackloan, {'vaultId': self.vault, 'from': self.address, 'amounts': f'1@{self.symbolTSLA}'})
# Disable token lock
self.nodes[0].setgov({"ATTRIBUTES":{f'v0/locks/token/{self.idTSLA}':'false'}})
self.nodes[0].generate(1)
# Set Oracle data
self.nodes[0].setoracledata(self.oracle_id, int(time.time()), self.oracle_prices)
self.nodes[0].generate(10)
# Vault amounts should now be restored
result = self.nodes[0].getvault(self.vault)
assert_equal(result['collateralValue'], Decimal('100000.00000000'))
assert_equal(result['loanValue'], Decimal('870.00217500'))
assert_equal(result['interestValue'], Decimal('0.00217500'))
assert_equal(result['informativeRatio'], Decimal('11494.22413800'))
assert_equal(result['collateralRatio'], 11494)
def token_lock(self):
# Enable token lock
self.nodes[0].setgov({"ATTRIBUTES":{f'v0/locks/token/{self.idTSLA}':'true'}})
self.nodes[0].generate(1)
# Try and update token
assert_raises_rpc_error(-32600, "Cannot update token during lock", self.nodes[0].updatetoken, self.idTSLA, {'name':'Tesla'})
# Disable token lock
self.nodes[0].setgov({"ATTRIBUTES":{f'v0/locks/token/{self.idTSLA}':'false'}})
self.nodes[0].generate(1)
# Try same update
self.nodes[0].updatetoken(self.idTSLA, {'name':'Tesla'})
self.nodes[0].generate(1)
# Verify results
result = self.nodes[0].gettoken(self.idTSLA)[self.idTSLA]
assert_equal(result['name'], 'Tesla')
if __name__ == '__main__':
TokenLockTest().main()