forked from FeatherCoin/Feathercoin
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathomni_freedexspec.py
330 lines (264 loc) · 14.5 KB
/
omni_freedexspec.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
330
#!/usr/bin/env python3
# Copyright (c) 2017-2018 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test DEx spec using free DEx."""
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal
class OmniFreeDExSpec(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 1
self.setup_clean_chain = True
self.extra_args = [['-omniactivationallowsender=any']]
def run_test(self):
self.log.info("test dex spec using free dex")
# Create varaibles that will remain unchanged
stdBlockSpan = 10
stdCommitFee = "0.00010000"
actionNew = 1
actionUpdate = 2
actionCancel = 3
# Preparing some mature Bitcoins
coinbase_address = self.nodes[0].getnewaddress()
self.nodes[0].generatetoaddress(110, coinbase_address)
# Obtaining a master address to work with
address = self.nodes[0].getnewaddress()
# Funding the address with some testnet BTC for fees
self.nodes[0].sendtoaddress(address, 10)
self.nodes[0].generatetoaddress(1, coinbase_address)
# Activating Free DEx...
activation_block = self.nodes[0].getblockcount() + 8
txid = self.nodes[0].omni_sendactivation(address, 15, activation_block, 0)
self.nodes[0].generatetoaddress(1, coinbase_address)
# Checking the transaction was valid...
result = self.nodes[0].omni_gettransaction(txid)
assert_equal(result['valid'], True)
# Mining 10 blocks to forward past the activation block
self.nodes[0].generatetoaddress(10, coinbase_address)
# Checking the activation went live as expected...
featureid = self.nodes[0].omni_getactivations()['completedactivations']
freeDEx = False
for ids in featureid:
if ids['featureid'] == 15:
freeDEx = True
assert_equal(freeDEx, True)
# Create a fixed property
txid = self.nodes[0].omni_sendissuancefixed(address, 1, 2, 0, "Test Category", "Test Subcategory", "TST", "http://www.omnilayer.org", "", "100000")
self.nodes[0].generatetoaddress(1, coinbase_address)
# Checking the transaction was valid...
result = self.nodes[0].omni_gettransaction(txid)
assert_equal(result['valid'], True)
# Get currency ID
currencyOffered = result['propertyid']
# A new sell offer can be created with action = 1 (new)
activeOffersAtTheStart = self.nodes[0].omni_getactivedexsells()
startBTC = 0.1
startMSC = "2.5"
amountOffered = "1.00000000"
desiredBTC = "0.20000000"
# Created funded address for test
fundedAddress = self.nodes[0].getnewaddress()
txid = self.nodes[0].omni_send(address, fundedAddress, currencyOffered, startMSC)
self.nodes[0].sendtoaddress(fundedAddress, startBTC)
self.nodes[0].generatetoaddress(1, coinbase_address)
# Checking the transaction was valid...
result = self.nodes[0].omni_gettransaction(txid)
assert_equal(result['valid'], True)
# creating an offer with action = 1
txid = self.nodes[0].omni_senddexsell(fundedAddress, currencyOffered, amountOffered, desiredBTC, stdBlockSpan, stdCommitFee, actionNew)
self.nodes[0].generatetoaddress(1, coinbase_address)
# Checking the transaction was valid...
result = self.nodes[0].omni_gettransaction(txid)
assert_equal(result['valid'], True)
# Get and check the offer
offerTx = self.nodes[0].omni_gettransaction(txid)
assert_equal(offerTx['txid'], txid)
assert_equal(offerTx['sendingaddress'], fundedAddress)
assert_equal(offerTx['version'], 1)
assert_equal(offerTx['type_int'], 20)
assert_equal(offerTx['type'], "DEx Sell Offer")
assert_equal(offerTx['propertyid'], currencyOffered)
assert_equal(offerTx['divisible'], True)
assert_equal(offerTx['amount'], amountOffered)
assert_equal(offerTx['feathercoindesired'], desiredBTC)
assert_equal(offerTx['timelimit'], stdBlockSpan)
assert_equal(offerTx['feerequired'], stdCommitFee)
assert_equal(offerTx['action'], "new")
assert_equal(offerTx['valid'], True)
assert_equal(offerTx['confirmations'], 1)
# A new offer is created on the distributed exchange
assert_equal(len(self.nodes[0].omni_getactivedexsells()), len(activeOffersAtTheStart) + 1)
# Offering more tokens than available.
# This will fail in OmniFeather due to FEATURE_DEXMATH being hard coded, the same is not true for Omnicore itself.
# Created new funded address for test
fundedAddress = self.nodes[0].getnewaddress()
txid = self.nodes[0].omni_send(address, fundedAddress, currencyOffered, startMSC)
self.nodes[0].sendtoaddress(fundedAddress, startBTC)
self.nodes[0].generatetoaddress(1, coinbase_address)
# Checking the transaction was valid...
result = self.nodes[0].omni_gettransaction(txid)
assert_equal(result['valid'], True)
# Setup variables
amountAvailableAtStart = self.nodes[0].omni_getbalance(fundedAddress, currencyOffered)['balance']
amountOffered = float(amountAvailableAtStart) + 100
desiredBTC = "50.00000000"
# the amount offered for sale exceeds the sending address's available balance
payload = self.nodes[0].omni_createpayload_dexsell(currencyOffered, str(amountOffered), desiredBTC, stdBlockSpan, stdCommitFee, actionNew)
txid = self.nodes[0].omni_sendrawtx(fundedAddress, payload)
self.nodes[0].generatetoaddress(1, coinbase_address)
# Checking the transaction was invalid...
result = self.nodes[0].omni_gettransaction(txid)
assert_equal(result['valid'], False)
assert_equal(result['invalidreason'], "Sender has insufficient balance")
# The amount offered for sale is reserved from the available balance
# Setup varaibles
startBTC = 0.1
startMSC = "100"
amountOffered = "90.00000000"
desiredBTC = "45.00000000"
# Created new funded address for test
fundedAddress = self.nodes[0].getnewaddress()
txid = self.nodes[0].omni_send(address, fundedAddress, currencyOffered, startMSC)
self.nodes[0].sendtoaddress(fundedAddress, startBTC)
self.nodes[0].generatetoaddress(1, coinbase_address)
# Checking the transaction was valid...
result = self.nodes[0].omni_gettransaction(txid)
assert_equal(result['valid'], True)
# Get balance to compare later
balanceAtStart = self.nodes[0].omni_getbalance(fundedAddress, currencyOffered)
# an amount is offered for sale
txid = self.nodes[0].omni_senddexsell(fundedAddress, currencyOffered, amountOffered, desiredBTC, stdBlockSpan, stdCommitFee, actionNew)
self.nodes[0].generatetoaddress(1, coinbase_address)
# Checking the transaction was valid...
result = self.nodes[0].omni_gettransaction(txid)
assert_equal(result['valid'], True)
# Get and check the offer
offerTx = self.nodes[0].omni_gettransaction(txid)
offerAmount = offerTx['amount']
balanceNow = self.nodes[0].omni_getbalance(fundedAddress, currencyOffered)
assert_equal(float(balanceNow['balance']), float(balanceAtStart['balance']) - float(offerAmount))
assert_equal(float(balanceNow['reserved']), float(balanceAtStart['reserved']) + float(offerAmount))
# There can be only one active offer that accepts BTC
# Setup varaibles
startBTC = 0.1
startMSC = "2.5"
firstOfferMSC = "1"
firstOfferBTC = "0.2"
secondOfferMSC = "1.5"
secondOfferBTC = "0.3"
# Created new funded address for test
fundedAddress = self.nodes[0].getnewaddress()
txid = self.nodes[0].omni_send(address, fundedAddress, currencyOffered, startMSC)
self.nodes[0].sendtoaddress(fundedAddress, startBTC)
self.nodes[0].generatetoaddress(1, coinbase_address)
# Checking the transaction was valid...
result = self.nodes[0].omni_gettransaction(txid)
assert_equal(result['valid'], True)
# there is already an active offer accepting BTC
txid = self.nodes[0].omni_senddexsell(fundedAddress, currencyOffered, firstOfferMSC, firstOfferBTC, stdBlockSpan, stdCommitFee, actionNew)
self.nodes[0].generatetoaddress(1, coinbase_address)
# Checking the transaction was valid...
result = self.nodes[0].omni_gettransaction(txid)
assert_equal(result['valid'], True)
# and another offer accepting BTC is made
payload = self.nodes[0].omni_createpayload_dexsell(currencyOffered, secondOfferMSC, secondOfferBTC, stdBlockSpan, stdCommitFee, actionNew)
txid = self.nodes[0].omni_sendrawtx(fundedAddress, payload)
self.nodes[0].generatetoaddress(1, coinbase_address)
# Checking the transaction was valid...
result = self.nodes[0].omni_gettransaction(txid)
assert_equal(result['valid'], False)
# An offer can be updated with action = 2 (update), and cancelled with action = 3 (cancel)
# Setup varaibles
startBTC = 0.1
startMSC = "1.0"
offeredMSC = "0.50000000"
desiredBTC = "0.5"
updatedMSC = "1.00000000"
updatedBTC = "2.0"
# Created new funded address for test
fundedAddress = self.nodes[0].getnewaddress()
txid = self.nodes[0].omni_send(address, fundedAddress, currencyOffered, startMSC)
self.nodes[0].sendtoaddress(fundedAddress, startBTC)
self.nodes[0].generatetoaddress(1, coinbase_address)
# Checking the transaction was valid...
result = self.nodes[0].omni_gettransaction(txid)
assert_equal(result['valid'], True)
# Get balance to compare later
balanceAtStart = self.nodes[0].omni_getbalance(fundedAddress, currencyOffered)
# Get offers
offersAtStart = self.nodes[0].omni_getactivedexsells()
# creating an offer with action 1
txid = self.nodes[0].omni_senddexsell(fundedAddress, currencyOffered, offeredMSC, desiredBTC, stdBlockSpan, stdCommitFee, actionNew)
self.nodes[0].generatetoaddress(1, coinbase_address)
# Checking the transaction was valid...
result = self.nodes[0].omni_gettransaction(txid)
balanceNow = self.nodes[0].omni_getbalance(fundedAddress, currencyOffered)
assert_equal(result['valid'], True)
assert_equal(result['amount'], offeredMSC)
assert_equal(float(balanceNow['balance']), float(balanceAtStart['balance']) - float(offeredMSC))
assert_equal(float(balanceNow['reserved']), float(balanceAtStart['reserved']) + float(offeredMSC))
assert_equal(len(self.nodes[0].omni_getactivedexsells()), len(offersAtStart) + 1)
# updating an offer with action = 2
payload = self.nodes[0].omni_createpayload_dexsell(currencyOffered, updatedMSC, updatedBTC, stdBlockSpan, stdCommitFee, actionUpdate)
txid = self.nodes[0].omni_sendrawtx(fundedAddress, payload)
self.nodes[0].generatetoaddress(1, coinbase_address)
# Checking the transaction was valid...
result = self.nodes[0].omni_gettransaction(txid)
balanceNow = self.nodes[0].omni_getbalance(fundedAddress, currencyOffered)
assert_equal(result['valid'], True)
assert_equal(result['action'], "update")
assert_equal(result['amount'], updatedMSC)
assert_equal(float(balanceNow['balance']), float(balanceAtStart['balance']) - float(updatedMSC))
assert_equal(float(balanceNow['reserved']), float(balanceAtStart['reserved']) + float(updatedMSC))
# cancelling an offer with action = 3
txid = self.nodes[0].omni_senddexsell(fundedAddress, currencyOffered, "0", "0", 0, "0", actionCancel)
self.nodes[0].generatetoaddress(1, coinbase_address)
# Checking the transaction was valid...
result = self.nodes[0].omni_gettransaction(txid)
balanceNow = self.nodes[0].omni_getbalance(fundedAddress, currencyOffered)
assert_equal(result['valid'], True)
assert_equal(result['action'], "cancel")
assert_equal(balanceNow['balance'], balanceAtStart['balance'])
assert_equal(balanceNow['reserved'], balanceAtStart['reserved'])
assert_equal(len(self.nodes[0].omni_getactivedexsells()), len(offersAtStart))
# An offer can be accepted with an accept transaction of type 22
# Setup varaibles
startBTC = 0.1
startMSC = "0.1"
offeredMSC = "0.05000000"
desiredBTC = "0.07"
# Created new funded address for test
actorA = self.nodes[0].getnewaddress()
txid = self.nodes[0].omni_send(address, actorA, currencyOffered, startMSC)
self.nodes[0].sendtoaddress(actorA, startBTC)
self.nodes[0].generatetoaddress(1, coinbase_address)
# Checking the transaction was valid...
result = self.nodes[0].omni_gettransaction(txid)
assert_equal(result['valid'], True)
# A offers MSC
txid = self.nodes[0].omni_senddexsell(actorA, currencyOffered, offeredMSC, desiredBTC, stdBlockSpan, stdCommitFee, actionNew)
self.nodes[0].generatetoaddress(1, coinbase_address)
# Checking the transaction was valid...
result = self.nodes[0].omni_gettransaction(txid)
assert_equal(result['valid'], True)
actorB = self.nodes[0].getnewaddress()
self.nodes[0].sendtoaddress(actorB, startBTC)
self.nodes[0].generatetoaddress(1, coinbase_address)
# B accepts the offer
txid = self.nodes[0].omni_senddexaccept(actorB, actorA, currencyOffered, offeredMSC, False)
self.nodes[0].generatetoaddress(1, coinbase_address)
# Checking the transaction was valid...
result = self.nodes[0].omni_gettransaction(txid)
assert_equal(result['valid'], True)
assert_equal(result['txid'], txid)
assert_equal(result['sendingaddress'], actorB)
assert_equal(result['referenceaddress'], actorA)
assert_equal(result['version'], 0)
assert_equal(result['type_int'], 22)
assert_equal(result['type'], "DEx Accept Offer")
assert_equal(result['propertyid'], currencyOffered)
assert_equal(result['divisible'], True)
assert_equal(result['amount'], offeredMSC)
assert_equal(result['confirmations'], 1)
if __name__ == '__main__':
OmniFreeDExSpec().main()