forked from DeFiCh/ain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_poolswap_mainnet.py
executable file
·270 lines (242 loc) · 10.8 KB
/
feature_poolswap_mainnet.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
#!/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's RPC.
- verify basic token's creation, destruction, revert, collateral locking
"""
from test_framework.test_framework import DefiTestFramework
from test_framework.authproxy import JSONRPCException
from test_framework.util import assert_equal
from decimal import Decimal
class PoolPairTest (DefiTestFramework):
def set_test_params(self):
self.FC_HEIGHT = 170
self.num_nodes = 1
self.setup_clean_chain = True
self.extra_args = [
['-txnotokens=0', '-amkheight=50', '-bayfrontheight=50', '-bayfrontgardensheight=0', '-dakotaheight=160', '-fortcanningheight=163', '-fortcanninghillheight='+str(self.FC_HEIGHT), '-simulatemainnet', '-jellyfish_regtest=1']
]
def create_tokens(self):
self.symbolGOLD = "GOLD"
self.symbolSILVER = "SILVER"
self.symbolDOGE = "DOGE"
self.account0 = self.nodes[0].get_genesis_keys().ownerAuthAddress
self.nodes[0].createtoken({
"symbol": self.symbolGOLD,
"name": "Gold token",
"collateralAddress": self.account0
})
self.nodes[0].generate(1)
self.nodes[0].createtoken({
"symbol": self.symbolSILVER,
"name": "Silver token",
"collateralAddress": self.account0
})
self.nodes[0].generate(1)
self.nodes[0].createtoken({
"symbol": self.symbolDOGE,
"name": "DOGE token",
"collateralAddress": self.account0
})
self.nodes[0].generate(1)
self.symbol_key_GOLD = "GOLD#" + str(self.get_id_token(self.symbolGOLD))
self.symbol_key_SILVER = "SILVER#" + str(self.get_id_token(self.symbolSILVER))
self.symbol_key_DOGE = "DOGE#" + str(self.get_id_token(self.symbolDOGE))
def mint_tokens(self, amount=1000):
self.nodes[0].utxostoaccount({self.account0: "199999900@DFI"})
self.nodes[0].generate(1)
self.nodes[0].minttokens(str(amount) + "@" + self.symbol_key_GOLD)
self.nodes[0].minttokens(str(amount) + "@" + self.symbol_key_SILVER)
self.nodes[0].minttokens(str(amount) + "@" + self.symbol_key_DOGE)
self.account_gs = self.nodes[0].getnewaddress("")
self.account_sd = self.nodes[0].getnewaddress("")
self.nodes[0].generate(1)
self.nodes[0].accounttoaccount(self.account0, {self.account_gs: "50000000@" + self.symbol_key_GOLD})
self.nodes[0].accounttoaccount(self.account0, {self.account_gs: "50000000@" + self.symbol_key_SILVER})
self.nodes[0].generate(1)
self.nodes[0].accounttoaccount(self.account0, {self.account_sd: "50000000@" + self.symbol_key_SILVER})
self.nodes[0].accounttoaccount(self.account0, {self.account_sd: "50000000@" + self.symbol_key_DOGE})
self.nodes[0].generate(1)
def create_pool_pairs(self):
owner = self.nodes[0].getnewaddress("", "legacy")
self.nodes[0].createpoolpair({
"tokenA": self.symbol_key_GOLD,
"tokenB": self.symbol_key_SILVER,
"commission": 0.01,
"status": True,
"ownerAddress": owner,
"pairSymbol": "GS",
}, [])
self.nodes[0].generate(1)
self.nodes[0].createpoolpair({
"tokenA": self.symbol_key_SILVER,
"tokenB": self.symbol_key_DOGE,
"commission": 0.05,
"status": True,
"ownerAddress": owner,
"pairSymbol": "DS",
}, [])
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]
}, self.account_gs, [])
self.nodes[0].addpoolliquidity({
self.account_sd: ["100000@" + self.symbol_key_DOGE, "1000000@" + self.symbol_key_SILVER]
}, 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
from_account = self.nodes[0].getaccount(from_address)
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,
"tokenFrom": self.symbol_key_GOLD,
"amountFrom": Decimal('-0.00000001'),
"to": to_address,
"tokenTo": self.symbol_key_SILVER,
},[])
except JSONRPCException as e:
errorString = e.error['message']
assert('Amount out of range' in errorString)
#try swap too small amount
try:
self.nodes[0].poolswap({
"from": self.account_gs,
"tokenFrom": self.symbol_key_GOLD,
"amountFrom": Decimal('0.000000001'),
"to": to_address,
"tokenTo": self.symbol_key_SILVER,
},[])
except JSONRPCException as e:
errorString = e.error['message']
assert('Invalid amount' in errorString)
def test_simple_swap_1Satoshi(self):
from_address = self.account_gs
from_account = self.nodes[0].getaccount(from_address)
to_address = self.nodes[0].getnewaddress("")
assert_equal(from_account[1], '45000000.00000000@GOLD#128')
self.nodes[0].poolswap({
"from": self.account_gs,
"tokenFrom": self.symbol_key_GOLD,
"amountFrom": 0.00000001,
"to": to_address,
"tokenTo": self.symbol_key_SILVER,
},[])
self.nodes[0].generate(1)
from_account = self.nodes[0].getaccount(from_address)
to_account = self.nodes[0].getaccount(to_address)
assert_equal(from_account[1], '44999999.99999999@GOLD#128')
assert_equal(to_account, [])
def test_200_simple_swaps_1Satoshi(self):
from_address = self.account_gs
from_account = self.nodes[0].getaccount(from_address)
to_address = self.nodes[0].getnewaddress("")
assert_equal(from_account[1], '44999999.99999999@GOLD#128')
for _ in range(200):
self.nodes[0].poolswap({
"from": self.account_gs,
"tokenFrom": self.symbol_key_GOLD,
"amountFrom": 0.00000001,
"to": to_address,
"tokenTo": self.symbol_key_SILVER,
},[])
self.nodes[0].generate(1)
from_account = self.nodes[0].getaccount(from_address)
to_account = self.nodes[0].getaccount(to_address)
assert_equal(from_account[1], '44999999.99999799@GOLD#128')
assert_equal(to_account, [])
def test_compositeswap_1Satoshi(self):
from_address = self.account_gs
from_account = self.nodes[0].getaccount(from_address)
to_address = self.nodes[0].getnewaddress("")
assert_equal(from_account[1], '44999999.99999799@GOLD#128')
testPoolSwapRes = self.nodes[0].testpoolswap({
"from": from_address,
"tokenFrom": self.symbol_key_GOLD,
"amountFrom": 0.00000001,
"to": to_address,
"tokenTo": self.symbol_key_DOGE,
}, "auto", True)
assert_equal(testPoolSwapRes["amount"], '0.00000000@130')
assert_equal(len(testPoolSwapRes["pools"]), 2)
self.nodes[0].compositeswap({
"from": self.account_gs,
"tokenFrom": self.symbol_key_GOLD,
"amountFrom": 0.00000001,
"to": to_address,
"tokenTo": self.symbol_key_DOGE,
},[])
self.nodes[0].generate(1)
from_account = self.nodes[0].getaccount(from_address)
to_account = self.nodes[0].getaccount(to_address)
assert_equal(from_account[1], '44999999.99999798@GOLD#128')
assert_equal(to_account, [])
def test_200_compositeswaps_1Satoshi(self):
from_address = self.account_gs
from_account = self.nodes[0].getaccount(from_address)
to_address = self.nodes[0].getnewaddress("")
assert_equal(from_account[1], '44999999.99999798@GOLD#128')
for _ in range(200):
testPoolSwapRes = self.nodes[0].testpoolswap({
"from": from_address,
"tokenFrom": self.symbol_key_GOLD,
"amountFrom": 0.00000001,
"to": to_address,
"tokenTo": self.symbol_key_DOGE,
}, "auto", True)
assert_equal(testPoolSwapRes["amount"], '0.00000000@130')
assert_equal(len(testPoolSwapRes["pools"]), 2)
self.nodes[0].compositeswap({
"from": self.account_gs,
"tokenFrom": self.symbol_key_GOLD,
"amountFrom": 0.00000001,
"to": to_address,
"tokenTo": self.symbol_key_DOGE,
},[])
self.nodes[0].generate(1)
from_account = self.nodes[0].getaccount(from_address)
to_account = self.nodes[0].getaccount(to_address)
assert_equal(from_account[1], '44999999.99999598@GOLD#128')
assert_equal(to_account, [])
def run_test(self):
self.setup()
self.test_swap_with_wrong_amounts()
self.test_simple_swap_1Satoshi()
self.test_200_simple_swaps_1Satoshi()
self.test_compositeswap_1Satoshi()
self.test_200_compositeswaps_1Satoshi()
if __name__ == '__main__':
PoolPairTest ().main ()