File: test_payment_channel.py

package info (click to toggle)
python-autobahn 23.1.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,424 kB
  • sloc: python: 38,620; javascript: 2,705; makefile: 899; ansic: 373; sh: 63
file content (73 lines) | stat: -rw-r--r-- 2,868 bytes parent folder | download | duplicates (2)
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
import os
import sys
import binascii
import argparse

import web3

from autobahn import xbr

from test_accounts import addr_owner, addr_edith_consumer, addr_frank_consumer, markets, hl


def main(accounts):
    for market in markets:
        for actor in market['actors']:
            if actor['type'] == xbr.ActorType.CONSUMER:

                amount = 100 * 10**18

                result = xbr.xbrtoken.functions.approve(xbr.xbrnetwork.address, amount).transact({'from': actor['addr'], 'gas': 1000000})
                if not result:
                    print('Failed to allow transfer of tokens for payment channel!', result)
                else:
                    print('Allowed transfer of {} XBR from {} to {} for opening a payment channel'.format(amount, actor['addr'], xbr.xbrnetwork.address))

                    # openPaymentChannel (bytes16 marketId, address consumerDelegate, uint256 amount) public returns (address paymentChannel)
                    txn = xbr.xbrnetwork.functions.openPaymentChannel(market['id'], actor['delegate'], amount).transact({'from': actor['addr'], 'gas': 1000000})
                    receipt = w3.eth.getTransactionReceipt(txn)

                    # bytes16 marketId, address sender, address delegate, address receiver, address channel
                    args = xbr.xbrnetwork.events.PaymentChannelCreated().processReceipt(receipt)
                    args = args[0].args

                    marketId = args['marketId']
                    sender = args['sender']
                    delegate = args['delegate']
                    recipient = args['receiver']
                    channel = args['channel']

                    print('Actor {} opened payment channel {} in market {} with inital deposit of {}, delegate {} and recipient {}!'.format(actor['addr'], hl(channel), market['id'], amount, hl(delegate), recipient))


if __name__ == '__main__':
    print('using web3.py v{}'.format(web3.__version__))

    parser = argparse.ArgumentParser()

    parser.add_argument('--gateway',
                        dest='gateway',
                        type=str,
                        default=None,
                        help='Ethereum HTTP gateway URL or None for auto-select (default: -, means let web3 auto-select).')

    args = parser.parse_args()

    if args.gateway:
        w3 = web3.Web3(web3.Web3.HTTPProvider(args.gateway))
    else:
        # using automatic provider detection:
        from web3.auto import w3

    # check we are connected, and check network ID
    if not w3.isConnected():
        print('could not connect to Web3/Ethereum at: {}'.format(args.gateway or 'auto'))
        sys.exit(1)
    else:
        print('connected via provider "{}"'.format(args.gateway or 'auto'))

    # set new provider on XBR library
    xbr.setProvider(w3)

    # now enter main ..
    main(w3.eth.accounts)