This repository has been archived by the owner on May 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest_sdk.py
732 lines (579 loc) · 26.3 KB
/
test_sdk.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
from decimal import Decimal
import pytest
import threading
from time import sleep
from stellar_base.asset import Asset
from stellar_base.keypair import Keypair
from stellar_base.utils import XdrLengthError
import kin
def test_sdk_not_configured(setup):
sdk = kin.SDK(horizon_endpoint_uri=setup.horizon_endpoint_uri, network=setup.network)
with pytest.raises(kin.SdkError, match='address not configured'):
sdk.get_address()
with pytest.raises(kin.SdkError, match='address not configured'):
sdk.get_native_balance()
with pytest.raises(kin.SdkError, match='address not configured'):
sdk.get_kin_balance()
with pytest.raises(kin.SdkError, match='address not configured'):
sdk.create_account('address')
with pytest.raises(kin.SdkError, match='address not configured'):
sdk.monitor_kin_payments(None)
with pytest.raises(kin.SdkError, match='address not configured'):
sdk._trust_asset(Asset('TMP', 'tmp'))
with pytest.raises(kin.SdkError, match='address not configured'):
sdk._send_asset(Asset('TMP', 'tmp'), 'address', 1)
def test_sdk_create_fail(setup, helpers, test_sdk):
with pytest.raises(ValueError, match='invalid secret key: bad'):
kin.SDK(secret_key='bad',
horizon_endpoint_uri=setup.horizon_endpoint_uri, network=setup.network, kin_asset=setup.test_asset)
keypair = Keypair.random()
secret_key = keypair.seed()
address = keypair.address().decode()
with pytest.raises(ValueError, match='invalid channel key: bad'):
kin.SDK(secret_key=secret_key, channel_secret_keys=['bad'],
horizon_endpoint_uri=setup.horizon_endpoint_uri, network=setup.network, kin_asset=setup.test_asset)
# wallet account does not exist
with pytest.raises(kin.AccountNotFoundError):
kin.SDK(secret_key=secret_key,
horizon_endpoint_uri=setup.horizon_endpoint_uri, network=setup.network, kin_asset=setup.test_asset)
helpers.fund_account(setup, address)
# wallet account exists but not yet activated
with pytest.raises(kin.AccountNotActivatedError):
kin.SDK(secret_key=secret_key,
horizon_endpoint_uri=setup.horizon_endpoint_uri, network=setup.network, kin_asset=setup.test_asset)
helpers.trust_asset(setup, secret_key)
channel_keypair = Keypair.random()
channel_secret_key = channel_keypair.seed()
# channel account does not exist
with pytest.raises(kin.AccountNotFoundError):
kin.SDK(secret_key=secret_key, channel_secret_keys=[channel_secret_key],
horizon_endpoint_uri=setup.horizon_endpoint_uri, network=setup.network, kin_asset=setup.test_asset)
# bad Horizon endpoint
with pytest.raises(kin.NetworkError):
kin.SDK(secret_key=secret_key,
horizon_endpoint_uri='bad', network=setup.network, kin_asset=setup.test_asset)
# no Horizon on endpoint
with pytest.raises(kin.NetworkError):
kin.SDK(secret_key=secret_key,
horizon_endpoint_uri='http://localhost:666', network=setup.network, kin_asset=setup.test_asset)
def test_sdk_create_success(setup, test_sdk):
# test defaults
from stellar_base.horizon import HORIZON_LIVE, HORIZON_TEST
sdk = kin.SDK()
assert sdk.horizon.horizon_uri == HORIZON_LIVE
sdk = kin.SDK(network='TESTNET')
assert sdk.horizon.horizon_uri == HORIZON_TEST
# test test_sdk fixture
assert test_sdk.horizon
assert test_sdk.horizon.horizon_uri == setup.horizon_endpoint_uri
assert test_sdk.network == setup.network
assert test_sdk.base_keypair.verifying_key == setup.sdk_keypair.verifying_key
assert test_sdk.base_keypair.signing_key == setup.sdk_keypair.signing_key
assert test_sdk.channel_manager
def test_get_status(setup, test_sdk):
# bad Horizon endpoint
sdk = kin.SDK(horizon_endpoint_uri='bad')
status = sdk.get_status()
assert status['horizon']
assert status['horizon']['online'] is False
assert status['horizon']['error'].startswith("Invalid URL 'bad': No schema supplied")
# no Horizon on endpoint
sdk = kin.SDK(horizon_endpoint_uri='http://localhost:666')
status = sdk.get_status()
assert status['horizon']
assert status['horizon']['online'] is False
assert status['horizon']['error'].find('Connection refused') > 0
# success
status = test_sdk.get_status()
assert status['network'] == setup.network
assert status['address'] == setup.sdk_keypair.address().decode()
assert status['kin_asset']
assert status['kin_asset']['code'] == setup.test_asset.code
assert status['kin_asset']['issuer'] == setup.test_asset.issuer
assert status['horizon']
assert status['horizon']['uri'] == setup.horizon_endpoint_uri
assert status['horizon']['online']
assert status['horizon']['error'] is None
assert status['channels']
assert status['channels']['all'] == 1
assert status['channels']['free'] == 1
assert status['transport']
assert status['transport']['pool_size'] == sdk.horizon.pool_size
assert status['transport']['num_retries'] == sdk.horizon.num_retries
assert status['transport']['request_timeout'] == sdk.horizon.request_timeout
assert status['transport']['retry_statuses'] == sdk.horizon.status_forcelist
assert status['transport']['backoff_factor'] == sdk.horizon.backoff_factor
def test_get_address(setup, test_sdk):
assert test_sdk.get_address() == setup.sdk_keypair.address().decode()
def test_get_native_balance(test_sdk):
assert test_sdk.get_native_balance() > 9999
def test_check_account_exists(setup, test_sdk):
with pytest.raises(ValueError, match='invalid address: bad'):
test_sdk.check_account_exists('bad')
keypair = Keypair.random()
address = keypair.address().decode()
assert not test_sdk.check_account_exists(address)
address = setup.issuer_keypair.address().decode()
assert test_sdk.check_account_exists(address)
def test_create_account(test_sdk):
keypair = Keypair.random()
address = keypair.address().decode()
with pytest.raises(ValueError, match='invalid address: bad'):
test_sdk.create_account('bad')
# underfunded
with pytest.raises(kin.LowBalanceError) as exc_info:
test_sdk.create_account(address, starting_balance=1000000)
assert exc_info.value.error_code == kin.CreateAccountResultCode.UNDERFUNDED
# successful
starting_balance = 100
tx_hash = test_sdk.create_account(address, starting_balance=starting_balance, memo_text='foobar')
assert tx_hash
assert test_sdk.check_account_exists(address)
assert test_sdk.get_account_native_balance(address) == starting_balance
# test get_transaction_data for this transaction
sleep(1)
tx_data = test_sdk.get_transaction_data(tx_hash)
assert tx_data
assert tx_data.hash == tx_hash
assert tx_data.source_account == test_sdk.get_address()
assert tx_data.created_at
assert tx_data.source_account_sequence
assert tx_data.fee_paid == 100
assert tx_data.memo_type == 'text'
assert tx_data.memo == 'foobar'
assert len(tx_data.signatures) == 1
assert len(tx_data.operations) == 1
op = tx_data.operations[0]
assert op.id
assert op.type == 'create_account'
assert op.asset_code is None
assert op.asset_type is None
assert op.asset_issuer is None
assert op.trustor is None
assert op.trustee is None
assert op.limit is None
assert op.from_address is None
assert op.to_address is None
assert op.amount is None
with pytest.raises(kin.AccountExistsError) as exc_info:
test_sdk.create_account(address)
assert exc_info.value.error_code == kin.CreateAccountResultCode.ACCOUNT_EXISTS
def test_get_account_kin_balance_fail(test_sdk, setup):
with pytest.raises(ValueError, match='invalid address: bad'):
test_sdk.get_account_kin_balance('bad')
keypair = Keypair.random()
address = keypair.address().decode()
with pytest.raises(ValueError, match='invalid asset issuer: bad'):
test_sdk._get_account_asset_balance(address, Asset('TMP', 'bad'))
# account not created yet
with pytest.raises(kin.AccountNotFoundError) as exc_info:
test_sdk.get_account_kin_balance(address)
assert test_sdk.create_account(address, starting_balance=10)
with pytest.raises(kin.AccountNotActivatedError) as exc_info:
test_sdk.get_account_kin_balance(address)
def test_send_native(test_sdk):
with pytest.raises(ValueError, match='invalid address: bad'):
test_sdk.send_native('bad', 100)
keypair = Keypair.random()
address = keypair.address().decode()
with pytest.raises(ValueError, match='amount must be positive'):
test_sdk.send_native(address, 0)
# account does not exist yet
with pytest.raises(kin.AccountNotFoundError) as exc_info:
test_sdk.send_native(address, 100)
assert exc_info.value.error_code == kin.PaymentResultCode.NO_DESTINATION
assert test_sdk.create_account(address, starting_balance=100)
# check underfunded
with pytest.raises(kin.LowBalanceError) as exc_info:
test_sdk.send_native(address, 1000000)
assert exc_info.value.error_code == kin.PaymentResultCode.UNDERFUNDED
# send and check the resulting balance
tx_hash = test_sdk.send_native(address, 10.123, memo_text='foobar')
assert tx_hash
assert test_sdk.get_account_native_balance(address) == Decimal('110.123')
# test get_transaction_data for this transaction
sleep(1)
tx_data = test_sdk.get_transaction_data(tx_hash)
assert tx_data
assert tx_data.hash == tx_hash
assert tx_data.source_account == test_sdk.get_address()
assert tx_data.created_at
assert tx_data.source_account_sequence
assert tx_data.fee_paid == 100
assert tx_data.memo_type == 'text'
assert tx_data.memo == 'foobar'
assert len(tx_data.signatures) == 1
assert len(tx_data.operations) == 1
op = tx_data.operations[0]
assert op.id
assert op.type == 'payment'
assert op.asset_code is None
assert op.asset_type == 'native'
assert op.asset_issuer is None
assert op.trustor is None
assert op.trustee is None
assert op.limit is None
assert op.from_address == test_sdk.get_address()
assert op.to_address == address
assert op.amount == Decimal('10.123')
# check several payments in a row
tx_hash1 = test_sdk.send_native(address, 1)
assert tx_hash1
tx_hash2 = test_sdk.send_native(address, 1)
assert tx_hash2
tx_hash3 = test_sdk.send_native(address, 1)
assert tx_hash3
sleep(1)
tx_data = test_sdk.get_transaction_data(tx_hash1)
assert tx_data.hash == tx_hash1
tx_data = test_sdk.get_transaction_data(tx_hash2)
assert tx_data.hash == tx_hash2
tx_data = test_sdk.get_transaction_data(tx_hash3)
assert tx_data.hash == tx_hash3
def test_trust_asset(setup, test_sdk, helpers):
# failures
with pytest.raises(Exception, match='Issuer cannot be null'):
test_sdk._trust_asset(Asset(''))
with pytest.raises(XdrLengthError, match='Asset code must be 12 characters at max.'):
test_sdk._trust_asset(Asset('abcdefghijklmnopqr'))
with pytest.raises(Exception, match='Issuer cannot be null'):
test_sdk._trust_asset(Asset('TMP'))
with pytest.raises(ValueError, match='invalid asset issuer: tmp'):
test_sdk._trust_asset(Asset('TMP', 'tmp'))
address = Keypair.random().address().decode()
with pytest.raises(kin.RequestError) as exc_info:
test_sdk._trust_asset(Asset('TMP', address))
assert exc_info.value.error_code == kin.ChangeTrustResultCode.NO_ISSUER
# success
tx_hash = test_sdk._trust_asset(setup.test_asset, limit=1000, memo_text='foobar')
assert tx_hash
assert test_sdk._check_asset_trusted(test_sdk.get_address(), setup.test_asset)
# TODO: check asset limit
# test get_transaction_data for this transaction
sleep(1)
tx_data = test_sdk.get_transaction_data(tx_hash)
assert tx_data
assert tx_data.hash == tx_hash
assert tx_data.source_account == test_sdk.get_address()
assert tx_data.created_at
assert tx_data.source_account_sequence
assert tx_data.fee_paid == 100
assert tx_data.memo_type == 'text'
assert tx_data.memo == 'foobar'
assert len(tx_data.signatures) == 1
assert len(tx_data.operations) == 1
op = tx_data.operations[0]
assert op.id
# assert op.created_at
# assert op.transaction_hash == tx_hash
assert op.type == 'change_trust'
assert op.asset_code == setup.test_asset.code
assert op.asset_type == 'credit_alphanum4'
assert op.asset_issuer == setup.test_asset.issuer
assert op.trustor == test_sdk.get_address()
assert op.trustee == setup.test_asset.issuer
assert op.limit == Decimal('1000')
assert op.from_address is None
assert op.to_address is None
assert op.amount is None
# finally, fund the sdk account with asset
assert helpers.fund_asset(setup, test_sdk.get_address(), 1000)
assert test_sdk.get_account_kin_balance(test_sdk.get_address()) == Decimal('1000')
def test_check_account_activated(setup, test_sdk, helpers):
with pytest.raises(ValueError, match='invalid address: bad'):
test_sdk.check_account_activated('bad')
keypair = Keypair.random()
address = keypair.address().decode()
with pytest.raises(ValueError, match='invalid asset issuer: bad'):
test_sdk._check_asset_trusted(address, Asset('TMP', 'bad'))
with pytest.raises(kin.AccountNotFoundError):
test_sdk.check_account_activated(address)
assert test_sdk.create_account(address, starting_balance=100)
assert not test_sdk.check_account_activated(address)
assert helpers.trust_asset(setup, keypair.seed())
assert test_sdk.check_account_activated(address)
def test_send_kin(setup, test_sdk, helpers):
with pytest.raises(ValueError, match='invalid address: bad'):
test_sdk.send_kin('bad', 10)
keypair = Keypair.random()
address = keypair.address().decode()
with pytest.raises(ValueError, match='amount must be positive'):
test_sdk.send_kin(address, 0)
with pytest.raises(ValueError, match='invalid asset issuer: bad'):
test_sdk._send_asset(Asset('TMP', 'bad'), address, 10)
# account does not exist yet
with pytest.raises(kin.AccountNotFoundError) as exc_info:
test_sdk.send_kin(address, 10)
assert exc_info.value.error_code == kin.PaymentResultCode.NO_DESTINATION
assert test_sdk.create_account(address, starting_balance=100)
# no trustline yet
with pytest.raises(kin.AccountNotActivatedError) as exc_info:
test_sdk.send_kin(address, 10)
assert exc_info.value.error_code == kin.PaymentResultCode.NO_TRUST
# add trustline from the newly created account to the kin issuer
assert helpers.trust_asset(setup, keypair.seed())
# send and check the resulting balance
tx_hash = test_sdk.send_kin(address, 10.123, memo_text='foobar')
assert tx_hash
assert test_sdk.get_account_kin_balance(address) == Decimal('10.123')
# test get_transaction_data for this transaction
sleep(1)
tx_data = test_sdk.get_transaction_data(tx_hash)
assert tx_data
assert tx_data.hash == tx_hash
assert tx_data.source_account == test_sdk.get_address()
assert tx_data.created_at
assert tx_data.source_account_sequence
assert tx_data.fee_paid == 100
assert tx_data.memo_type == 'text'
assert tx_data.memo == 'foobar'
assert len(tx_data.signatures) == 1
assert len(tx_data.operations) == 1
op = tx_data.operations[0]
assert op.id
assert op.type == 'payment'
assert op.asset_code == setup.test_asset.code
assert op.asset_type == 'credit_alphanum4'
assert op.asset_issuer == setup.test_asset.issuer
assert op.trustor is None
assert op.trustee is None
assert op.limit is None
assert op.from_address == test_sdk.get_address()
assert op.to_address == address
assert op.amount == Decimal('10.123')
def test_get_account_data(setup, test_sdk):
with pytest.raises(ValueError, match='invalid address: bad'):
test_sdk.get_account_data('bad')
address = Keypair.random().address().decode()
with pytest.raises(kin.AccountNotFoundError):
test_sdk.get_account_data(address)
acc_data = test_sdk.get_account_data(test_sdk.get_address())
assert acc_data
assert acc_data.id == test_sdk.get_address()
assert acc_data.sequence
assert acc_data.data == {}
assert acc_data.thresholds
assert acc_data.thresholds.low_threshold == 0
assert acc_data.thresholds.medium_threshold == 0
assert acc_data.thresholds.high_threshold == 0
assert acc_data.flags
assert not acc_data.flags.auth_revocable
assert not acc_data.flags.auth_required
assert len(acc_data.balances) == 2
asset_balance = acc_data.balances[0]
native_balance = acc_data.balances[1]
assert asset_balance.balance > 900
assert asset_balance.limit == Decimal('1000')
assert asset_balance.asset_type == 'credit_alphanum4'
assert asset_balance.asset_code == setup.test_asset.code
assert asset_balance.asset_issuer == setup.test_asset.issuer
assert native_balance.balance > 9000
assert native_balance.asset_type == 'native'
# just to increase test coverage
assert str(acc_data)
def test_get_transaction_data_fail(test_sdk):
with pytest.raises(ValueError, match='invalid transaction hash: bad'):
test_sdk.get_transaction_data('bad')
with pytest.raises(kin.ResourceNotFoundError):
test_sdk.get_transaction_data('deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef')
def test_monitor_accounts_transactions_fail(setup, test_sdk):
with pytest.raises(ValueError, match='invalid asset issuer: bad'):
test_sdk._monitor_accounts_asset_transactions(Asset('TMP', 'bad'), None, None)
with pytest.raises(ValueError, match='no addresses to monitor'):
test_sdk.monitor_accounts_transactions([], None)
with pytest.raises(ValueError, match='invalid address: bad'):
test_sdk.monitor_accounts_transactions(['bad'], None)
keypair = Keypair.random()
address = keypair.address().decode()
with pytest.raises(kin.AccountNotFoundError):
test_sdk.monitor_accounts_transactions([address], None)
with pytest.raises(kin.AccountNotFoundError):
test_sdk.monitor_accounts_transactions([address], None)
def test_monitor_accounts_transactions(setup, test_sdk, helpers):
keypair = Keypair.random()
address = keypair.address().decode()
tx_hash1 = test_sdk.create_account(address, starting_balance=100, memo_text='create')
assert tx_hash1
ev = threading.Event()
tx_datas = []
def account_tx_callback(addr, tx_data):
assert addr == address
tx_datas.append(tx_data)
if len(tx_datas) == 4: # create/trust/send_asset/send_native
ev.set()
# start monitoring
sleep(1)
test_sdk.monitor_accounts_transactions([address], account_tx_callback)
tx_hash2 = helpers.trust_asset(setup, keypair.seed(), memo_text='trust')
assert tx_hash2
tx_hash3 = test_sdk.send_kin(address, 10, memo_text='send asset')
assert tx_hash3
tx_hash4 = test_sdk.send_native(address, 1, memo_text='send native')
assert tx_hash4
# wait until callback gets them all
assert ev.wait(3)
# check collected transactions
assert tx_datas[0].hash == tx_hash1
assert tx_datas[0].source_account == test_sdk.get_address()
assert tx_datas[0].memo == 'create'
assert tx_datas[0].operations[0].type == 'create_account'
assert tx_datas[1].hash == tx_hash2
assert tx_datas[1].source_account == address
assert tx_datas[1].memo == 'trust'
assert tx_datas[1].operations[0].type == 'change_trust'
assert tx_datas[2].hash == tx_hash3
assert tx_datas[2].source_account == test_sdk.get_address()
assert tx_datas[2].memo == 'send asset'
assert tx_datas[2].operations[0].type == 'payment'
assert tx_datas[2].operations[0].asset_code == setup.test_asset.code
assert tx_datas[3].hash == tx_hash4
assert tx_datas[3].source_account == test_sdk.get_address()
assert tx_datas[3].memo == 'send native'
assert tx_datas[3].operations[0].type == 'payment'
assert tx_datas[3].operations[0].asset_type == 'native'
def test_monitor_accounts_kin_payments_single(setup, test_sdk, helpers):
keypair = Keypair.random()
address = keypair.address().decode()
assert test_sdk.create_account(address, starting_balance=100, memo_text='create')
assert helpers.trust_asset(setup, keypair.seed(), memo_text='trust')
ev = threading.Event()
tx_datas = []
def account_tx_callback(addr, tx_data):
assert addr == address
tx_datas.append(tx_data)
if len(tx_datas) == 2:
ev.set()
# start monitoring
sleep(1)
test_sdk.monitor_accounts_kin_payments([address], account_tx_callback)
# pay from sdk to the account
tx_hash1 = test_sdk.send_kin(address, 10)
assert tx_hash1
# pay from the account back to the sdk
tx_hash2 = helpers.send_asset(setup, keypair.seed(), test_sdk.get_address(), 10)
assert tx_hash2
# wait until the callback gets them all
assert ev.wait(3)
# check collected transactions
assert tx_datas[0].hash == tx_hash1
op_data = tx_datas[0].operations[0]
assert op_data.type == 'payment'
assert op_data.asset_code == setup.test_asset.code
assert op_data.asset_issuer == setup.test_asset.issuer
assert op_data.from_address == test_sdk.get_address()
assert op_data.to_address == address
assert op_data.amount == Decimal('10')
assert tx_datas[1].hash == tx_hash2
op_data = tx_datas[1].operations[0]
assert op_data.type == 'payment'
assert op_data.asset_code == setup.test_asset.code
assert op_data.asset_issuer == setup.test_asset.issuer
assert op_data.from_address == address
assert op_data.to_address == test_sdk.get_address()
assert op_data.amount == Decimal('10')
def test_monitor_accounts_kin_payments_multiple(setup, test_sdk, helpers):
keypair1 = Keypair.random()
address1 = keypair1.address().decode()
keypair2 = Keypair.random()
address2 = keypair2.address().decode()
assert test_sdk.create_account(address1, starting_balance=100)
assert test_sdk.create_account(address2, starting_balance=100)
assert helpers.trust_asset(setup, keypair1.seed())
assert helpers.trust_asset(setup, keypair2.seed())
ev1 = threading.Event()
ev2 = threading.Event()
tx_datas1 = []
tx_datas2 = []
def account_tx_callback(addr, tx_data):
assert addr == address1 or addr == address2
op_data = tx_data.operations[0]
if op_data.to_address == address1:
tx_datas1.append(tx_data)
ev1.set()
elif op_data.to_address == address2:
tx_datas2.append(tx_data)
ev2.set()
# start monitoring
sleep(1)
test_sdk.monitor_accounts_kin_payments([address1, address2], account_tx_callback)
# send payments
tx_hash12 = test_sdk.send_kin(address1, 10)
assert tx_hash12
tx_hash22 = test_sdk.send_kin(address2, 10)
assert tx_hash22
# wait until callback gets them all
assert ev1.wait(3)
assert ev2.wait(3)
# check collected operations
assert tx_datas1[0].hash == tx_hash12
op_data = tx_datas1[0].operations[0]
assert op_data.type == 'payment'
assert op_data.asset_code == setup.test_asset.code
assert op_data.asset_issuer == setup.test_asset.issuer
assert op_data.from_address == test_sdk.get_address()
assert op_data.to_address == address1
assert op_data.amount == Decimal('10')
assert tx_datas2[0].hash == tx_hash22
op_data = tx_datas2[0].operations[0]
assert op_data.type == 'payment'
assert op_data.asset_code == setup.test_asset.code
assert op_data.asset_issuer == setup.test_asset.issuer
assert op_data.from_address == test_sdk.get_address()
assert op_data.to_address == address2
assert op_data.amount == Decimal('10')
def test_channels(setup, helpers):
# prepare channel accounts
channel_keypairs = [Keypair.random(), Keypair.random(), Keypair.random(), Keypair.random()]
channel_keys = [channel_keypair.seed() for channel_keypair in channel_keypairs]
channel_addresses = [channel_keypair.address().decode() for channel_keypair in channel_keypairs]
for channel_address in channel_addresses:
helpers.fund_account(setup, channel_address)
# init sdk with these channels
sdk = kin.SDK(secret_key=setup.sdk_keypair.seed(),channel_secret_keys=channel_keys,
horizon_endpoint_uri=setup.horizon_endpoint_uri, network=setup.network, kin_asset=setup.test_asset)
assert sdk
assert sdk.channel_manager
assert sdk.channel_manager.channel_builders.qsize() == len(channel_keypairs)
thread_ex = []
def channel_worker(thread_ex_holder):
try:
# create an account using a channel
address = Keypair.random().address().decode()
tx_hash1 = sdk.create_account(address, starting_balance=100)
assert tx_hash1
# send lumens
tx_hash2 = sdk.send_native(address, 1)
assert tx_hash2
# send more lumens
tx_hash3 = sdk.send_native(address, 1)
assert tx_hash3
sleep(1)
# check transactions
tx_data = sdk.get_transaction_data(tx_hash1)
assert tx_data
# transaction envelope source is some channel account
assert tx_data.source_account in channel_addresses
# operation source is the base account
assert tx_data.operations[0].source_account == sdk.get_address()
tx_data = sdk.get_transaction_data(tx_hash2)
assert tx_data
assert tx_data.source_account in channel_addresses
assert tx_data.operations[0].source_account == sdk.get_address()
tx_data = sdk.get_transaction_data(tx_hash3)
assert tx_data
assert tx_data.source_account in channel_addresses
assert tx_data.operations[0].source_account == sdk.get_address()
except Exception as e:
thread_ex_holder.append(e)
# now issue parallel transactions
threads = []
for channel_keypair in channel_keypairs:
t = threading.Thread(target=channel_worker, args=(thread_ex,))
threads.append(t)
for t in threads:
t.start()
# wait for all to finish
for t in threads:
t.join()
# check thread errors
assert not thread_ex