def main(accounts): # XBR tokens to transfer amount = 1000 # raw amount of XBR tokens (taking into account decimals) raw_amount = amount * 10**18 for acct in [addr_alice_market, addr_bob_market, addr_charlie_provider, addr_donald_provider, addr_edith_consumer, addr_frank_consumer]: success = xbr.xbrToken.functions.transfer(acct, raw_amount).transact({'from': addr_owner, 'gas': 100000}) if success: print('Transferred {} XBR to {}'.format(hl(amount), hl(acct))) else: print('Failed to transfer tokens!')
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))
def main(accounts): print('\nTest accounts:') for acct in accounts: balance_eth = w3.eth.getBalance(acct) balance_xbr = xbr.xbrtoken.functions.balanceOf(acct).call() print(' balances of {}: {:>30} ETH, {:>30} XBR'.format( hl(acct), balance_eth, balance_xbr)) print('\nXBR contracts:') for acct in [xbr.xbrtoken.address, xbr.xbrnetwork.address]: balance_eth = w3.eth.getBalance(acct) balance_xbr = xbr.xbrtoken.functions.balanceOf(acct).call() print(' balances of {}: {:>30} ETH, {:>30} XBR'.format( hl(acct), balance_eth, balance_xbr)) print()
def main(accounts): for acct in [addr_alice_market, addr_bob_market, addr_charlie_provider, addr_donald_provider, addr_edith_consumer, addr_frank_consumer]: level = xbr.xbrNetwork.functions.getMemberLevel(acct).call() if not level: eula = 'QmU7Gizbre17x6V2VR1Q2GJEjz6m8S1bXmBtVxS2vmvb81' profile = '' xbr.xbrNetwork.functions.register(eula, profile).transact({'from': acct, 'gas': 200000}) print('New member {} registered in the XBR Network (eula={}, profile={})'.format(hl(acct), eula, profile)) else: eula = xbr.xbrNetwork.functions.getMemberEula(acct).call() profile = xbr.xbrNetwork.functions.getMemberProfile(acct).call() print('{} is already a member (level={}, eula={}, profile={})'.format(hl(acct), hl(level), eula, profile))
def main(accounts): for market in markets: owner = xbr.xbrNetwork.functions.getMarketOwner(market['id']).call() if owner != '0x0000000000000000000000000000000000000000': if owner != market['owner']: print('Market {} already exists, but has wrong owner!! Expected {}, but owner is {}'.format(market['id'], market['owner'], owner)) else: print('Market {} already exists and has expected owner {}'.format(hl(market['id']), owner)) else: xbr.xbrNetwork.functions.createMarket( market['id'], market['terms'], market['meta'], market['maker'], market['providerSecurity'], market['consumerSecurity'], market['marketFee']).transact({'from': market['owner'], 'gas': 1000000}) print('Market {} created with owner!'.format(hl(market['id']), market['owner'])) for actor in market['actors']: atype = xbr.xbrNetwork.functions.getMarketActorType(market['id'], actor['addr']).call() if atype: if atype != actor['type']: print('Account {} is already actor in the market, but has wrong actor type! Expected {}, but got {}.'.format(actor['addr'], actor['type'], atype)) else: print('Account {} is already actor in the market and has correct actor type {}'.format(actor['addr'], atype)) else: result = xbr.xbrToken.functions.approve(xbr.xbrNetwork.address, actor['security']).transact({'from': actor['addr'], 'gas': 1000000}) if not result: print('Failed to allow transfer of tokens for market security!', result) else: print('Allowed transfer of {} XBR from {} to {} as security for joining market'.format(actor['security'], actor['addr'], xbr.xbrNetwork.address)) security = xbr.xbrNetwork.functions.joinMarket(market['id'], actor['type']).transact({'from': actor['addr'], 'gas': 1000000}) print('Actor {} joined market {} as actor type {} with security {}!'.format(hl(actor['addr']), market['id'], hl(actor['type']), security))
def main(accounts): for acct in [ addr_alice_market, addr_bob_market, addr_charlie_provider, addr_donald_provider, addr_edith_consumer, addr_frank_consumer ]: level = xbr.xbrnetwork.functions.getMemberLevel(acct).call() if not level: eula = 'QmU7Gizbre17x6V2VR1Q2GJEjz6m8S1bXmBtVxS2vmvb81' profile = '' xbr.xbrnetwork.functions.register(eula, profile).transact({ 'from': acct, 'gas': 200000 }) print( 'New member {} registered in the XBR Network (eula={}, profile={})' .format(hl(acct), eula, profile)) else: eula = xbr.xbrnetwork.functions.getMemberEula(acct).call() profile = xbr.xbrnetwork.functions.getMemberProfile(acct).call() print('{} is already a member (level={}, eula={}, profile={})'. format(hl(acct), hl(level), eula, profile)) DomainStatus_NULL = 0 DomainStatus_ACTIVE = 1 DomainStatus_CLOSED = 2 NodeType_NULL = 0 NodeType_MASTER = 1 NodeType_CORE = 2 NodeType_EDGE = 3 NodeLicense_NULL = 0 NodeLicense_INFINITE = 1 NodeLicense_FREE = 2 _domain_id = "0x9d9827822252fbe721d45224c7db7cac" _domain_key = "0xfeb083ce587a4ea72681d7db776452b05aaf58dc778534a6938313e4c85912f0" _license = "" _terms = "" _meta = "" status = xbr.xbrnetwork.functions.getDomainStatus(_domain_id).call() if status == DomainStatus_NULL: print('Domain does not exist - creating ..') xbr.xbrnetwork.functions.createDomain(_domain_id, _domain_key, _license, _terms, _meta).transact({ 'from': acct, 'gas': 200000 }) print('Domain created') elif status == DomainStatus_ACTIVE: print('Domain already exists') elif status == DomainStatus_CLOSED: print('FATAL: domain is closed') sys.exit(1) _pubkey = '0xf4050a787994fcca715103a83532785d79eaff3e42d18fd3f667fa2bb4af439e' _node_id = '0x4570160dd5be4726b2a785499609d6ab' _nodeType = NodeType_MASTER _nodeLicense = NodeLicense_FREE _config = '' node = xbr.xbrnetwork.functions.getNodeByKey(_pubkey).call() if node == b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00': print('Node not yet paired - pairing ..') xbr.xbrnetwork.functions.pairNode(_node_id, _domain_id, _nodeType, _nodeLicense, _pubkey, _config).transact({ 'from': acct, 'gas': 200000 }) print('Node paired!') else: print('Node already paired')