-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_airdrop.py
365 lines (266 loc) · 10.6 KB
/
test_airdrop.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import json
import peewee as pw
import airdrop as ad
from models import BaseModel, Config, Recipient, Tx
# Note.
# All of addresses are valid the for binance testnet (97).
# But no one transaction will happend due to using pytest monkeypatch in
# the methods send (tranfer) and get receipts.
DBFILE = "db_test.sqlite"
def delete_tables():
BaseModel._meta.database.init(DBFILE)
Config.drop_table()
Recipient.drop_table()
Tx.drop_table()
def test_create_empty_db():
BaseModel._meta.database.init(DBFILE)
db = pw.SqliteDatabase(DBFILE)
db.connect()
db.create_tables([Config, Recipient, Tx])
assert db.table_exists('config')
assert db.table_exists('recipient')
assert db.table_exists('tx')
assert db.get_tables() == ['config', 'recipient', 'tx']
assert len(Config.select()) == 0
assert len(Recipient.select()) == 0
assert len(Tx.select()) == 0
delete_tables()
db.close()
def test_init_command():
db = pw.SqliteDatabase(DBFILE)
BaseModel._meta.database.init(DBFILE)
ad.initialize()
assert db.get_tables() == ['config', 'recipient', 'tx']
config = Config.get(1)
assert config.address == "0x0000000000000000000000000000000000000000"
assert config.private_key == "0000000000000000000000000000000000000000000000000000000000000000"
assert config.gas_price == 10000000000
assert config.web3_node == "https://data-seed-prebsc-2-s1.binance.org:8545/"
assert config.token == "0x0000000000000000000000000000000000000000"
assert config.current_nonce == 0
assert config.eth_balance == '0'
assert config.token_balance == '0'
delete_tables()
db.close()
def test_import_command(monkeypatch):
db = pw.SqliteDatabase(DBFILE)
BaseModel._meta.database.init(DBFILE)
db.connect()
ad.initialize()
monkeypatch.setattr('builtins.input',
lambda _: "a181ad022696f68244129bc35559d9fe28005d5289fca5961d3ce91dc29d13b3")
ad.import_key()
config = Config.get(1)
assert config.address == "0xB0718e1085E1E34537ff9fdAeeC5Ec1AfFe1872c"
assert config.private_key == "a181ad022696f68244129bc35559d9fe28005d5289fca5961d3ce91dc29d13b3"
# check again
assert config.gas_price == 10000000000
assert config.web3_node == "https://data-seed-prebsc-2-s1.binance.org:8545/"
assert config.token == "0x0000000000000000000000000000000000000000"
assert config.current_nonce == 0
assert config.eth_balance == '0'
assert config.token_balance == '0'
db.close()
def test_token_command():
db = pw.SqliteDatabase(DBFILE)
BaseModel._meta.database.init(DBFILE)
db.connect()
ad.set_token("0x688ce8a97d5f1193261DB2271f542193D1dFd866")
config = Config.get(1)
assert config.token == "0x688ce8a97d5f1193261DB2271f542193D1dFd866"
db.close()
def test_token_command_with_wrong_address():
db = pw.SqliteDatabase(DBFILE)
BaseModel._meta.database.init(DBFILE)
db.connect()
token_before = Config.get(1).token
ad.set_token("0x688ce8a97d5f1193261DB2271f542193D83742323234ac1dFd86693849")
assert Config.get(1).token == token_before
ad.set_token("0xce8a97d5f1193261D")
assert Config.get(1).token == token_before
ad.set_token("12e8a97d5f1193261D")
assert Config.get(1).token == token_before
ad.set_token(12345678901234567890)
assert Config.get(1).token == token_before
db.close()
def test_web3_command():
db = pw.SqliteDatabase(DBFILE)
BaseModel._meta.database.init(DBFILE)
db.connect()
ad.set_node_address("https://data-seed-prebsc-1-s3.binance.org:8545/")
config = Config.get(1)
assert config.web3_node == "https://data-seed-prebsc-1-s3.binance.org:8545/"
db.close()
def test_web3_command_dont_change_to_wrong_address():
db = pw.SqliteDatabase(DBFILE)
BaseModel._meta.database.init(DBFILE)
db.connect()
wen3_before = Config.get(1).web3_node
ad.set_node_address("https://")
assert Config.get(1).web3_node == wen3_before
ad.set_node_address("https://google.com/")
assert Config.get(1).web3_node == wen3_before
ad.set_node_address("abcdefg")
assert Config.get(1).web3_node == wen3_before
db.close()
def test_update_command():
db = pw.SqliteDatabase(DBFILE)
BaseModel._meta.database.init(DBFILE)
db.connect()
config = Config.get(1)
# before update
assert config.current_nonce == 0
assert int(config.eth_balance) == 0
assert int(config.token_balance) == 0
assert config.token == "0x688ce8a97d5f1193261DB2271f542193D1dFd866"
assert config.address == "0xB0718e1085E1E34537ff9fdAeeC5Ec1AfFe1872c"
assert config.private_key == "a181ad022696f68244129bc35559d9fe28005d5289fca5961d3ce91dc29d13b3"
# updated
ad.update_data()
config = Config.get(1)
assert config.current_nonce > 0
assert int(config.eth_balance) > 0
assert int(config.token_balance) > 0
db.close()
def test_gasprice_command():
db = pw.SqliteDatabase(DBFILE)
BaseModel._meta.database.init(DBFILE)
db.connect()
# In 'init' project's default gasprice is 10000000000
assert Config.get(1).gas_price == 10000000000
# Let's switch gas price to 20000000002
ad.set_gas_price(20000000002)
assert Config.get(1).gas_price == 20000000002
# Let's switch gas price to 30000000003
ad.set_gas_price("30000000003")
assert Config.get(1).gas_price == 30000000003
# Switching back
ad.set_gas_price(10000000000)
assert Config.get(1).gas_price == 10000000000
db.close()
def test_gasprice_command_with_wrong_value():
db = pw.SqliteDatabase(DBFILE)
BaseModel._meta.database.init(DBFILE)
db.connect()
gas_before = Config.get(1).gas_price
ad.set_gas_price(" wrong value ")
assert Config.get(1).gas_price == gas_before
ad.set_gas_price("3434340430d")
assert Config.get(1).gas_price == gas_before
ad.set_gas_price("0.0000001")
assert Config.get(1).gas_price == gas_before
db.close()
def test_add_command():
db = pw.SqliteDatabase(DBFILE)
BaseModel._meta.database.init(DBFILE)
db.connect()
adr1, amount1 = ("0x754a2bAe5b5eEE723409A1d0013377927Fd5F539", 1.234567890123456789)
adr2, amount2 = ("0x754a2bAe5b5eEE723409A1d0013377927Fd5F539", 0.123456789012345678)
adr3, amount3 = ("0x754a2bAe5b5eEE723409A1d0013377927Fd5F539", 0.012345678901234567)
# before
assert len(Recipient.select()) == 0
assert len(Tx.select()) == 0
# add one
ad.add_recepient(adr1, amount1)
assert len(Recipient.select()) == 1
recipient1 = Recipient.get(1)
assert recipient1.address == adr1
assert float(recipient1.amount) == amount1
assert len(Tx.select()) == 1
tx1 = Tx.get(1)
assert tx1.raw_tx == ""
assert tx1.signed_tx == b''
assert tx1.tx_hash == b''
assert tx1.tx_receipt == ""
assert tx1.nonce == None
assert tx1.recipient.address == "0x754a2bAe5b5eEE723409A1d0013377927Fd5F539"
assert tx1.status == 'NEW'
# add two more
ad.add_recepient(adr2, amount2)
ad.add_recepient(adr3, amount3)
assert len(Recipient.select()) == 3
recipient2 = Recipient.get(2)
recipient3 = Recipient.get(3)
assert recipient2.address == adr2
assert float(recipient2.amount) == amount2
assert recipient3.address == adr3
assert float(recipient3.amount) == amount3
assert len(Tx.select()) == 3
assert Tx.get(2).status == 'NEW'
assert Tx.get(3).status == 'NEW'
assert bool(Tx.get(2).signed_tx) == False
db.close()
def test_sign_command():
db = pw.SqliteDatabase(DBFILE)
BaseModel._meta.database.init(DBFILE)
db.connect()
nonce_before = Config.get(1).current_nonce
ad.sign()
tx1, tx2, tx3 = Tx.select()
assert tx1.status == 'SIGNED'
assert tx2.status == 'SIGNED'
assert tx3.status == 'SIGNED'
assert tx1.nonce == nonce_before
assert tx2.nonce == nonce_before + 1
assert tx3.nonce == nonce_before + 2
assert json.loads(tx1.raw_tx.replace("\'", "\""))['from'] == Config.get(1).address
assert json.loads(tx2.raw_tx.replace("\'", "\""))['from'] == Config.get(1).address
assert json.loads(tx3.raw_tx.replace("\'", "\""))['from'] == Config.get(1).address
assert json.loads(tx1.raw_tx.replace("\'", "\""))['to'] == Config.get(1).token
assert json.loads(tx2.raw_tx.replace("\'", "\""))['to'] == Config.get(1).token
assert json.loads(tx3.raw_tx.replace("\'", "\""))['to'] == Config.get(1).token
assert bool(json.loads(tx1.raw_tx.replace("\'", "\""))['data']) == True
assert bool(json.loads(tx2.raw_tx.replace("\'", "\""))['data']) == True
assert bool(json.loads(tx3.raw_tx.replace("\'", "\""))['data']) == True
assert bool(tx1.signed_tx) == True
assert bool(tx2.signed_tx) == True
assert bool(tx3.signed_tx) == True
db.close()
def test_send_command(monkeypatch):
db = pw.SqliteDatabase(DBFILE)
BaseModel._meta.database.init(DBFILE)
db.connect()
monkeypatch.setattr('airdrop.send_raw_tx',
lambda _, __: bytes.fromhex('593ebcf5700420b1'))
monkeypatch.setattr('airdrop.get_tx_receipt',
lambda _, __: {'blockNumber': 100, 'status': 1})
ad.send()
tx = Tx.get(1)
assert tx.tx_hash == bytes.fromhex('593ebcf5700420b1')
assert tx.status == 'MINED'
assert tx.tx_receipt == "{'blockNumber': 100, 'status': 1}"
db.close()
# db cleanup
delete_tables()
def test_dont_update_without_token(monkeypatch):
db = pw.SqliteDatabase(DBFILE)
BaseModel._meta.database.init(DBFILE)
ad.initialize()
monkeypatch.setattr('builtins.input',
lambda _: "a181ad022696f68244129bc35559d9fe28005d5289fca5961d3ce91dc29d13b3")
ad.import_key()
nonce_before = Config.get(1).current_nonce
token_balance_before = Config.get(1).token_balance
ad.update_data()
assert nonce_before == Config.get(1).current_nonce
assert token_balance_before == Config.get(1).token_balance
db.close()
delete_tables()
def test_add_with_wrong_inputs_dont_adds_data_to_db():
db = pw.SqliteDatabase(DBFILE)
BaseModel._meta.database.init(DBFILE)
ad.initialize()
adr1, amount1 = ("0x0000000000000000000000000000hd1100000000", 1.2345)
adr2, amount2 = ("0x754a2bAe5b5eEE723409A1d0013377927Fd5F539", "")
adr3, amount3 = ("0x754a2bAe5b5eEE723409A1d0013377927Fd5F539", "0.00000000000000000001")
# before
assert len(Recipient.select()) == 0
assert len(Tx.select()) == 0
ad.add_recepient(adr1, amount1)
ad.add_recepient(adr2, amount2)
ad.add_recepient(adr3, amount3)
# after
assert len(Recipient.select()) == 0
assert len(Tx.select()) == 0
db.close()
delete_tables()