示例#1
0
def test_exchange(test_client, user_with_reserve_balance,
                  initialised_blockchain_network, from_token, to_token,
                  from_amount, status_code):
    from_token_obj = initialised_blockchain_network[from_token]
    to_token_obj = initialised_blockchain_network[to_token]

    response = test_client.post(
        '/api/v1/me/exchange/',
        headers=dict(
            Authorization=get_complete_auth_token(user_with_reserve_balance),
            Accept='application/json'),
        json={
            'from_token_id': from_token_obj.id,
            'to_token_id': to_token_obj.id,
            'from_amount': from_amount
        })

    assert response.status_code == status_code
    if status_code == 200 and will_func_test_blockchain():
        task_uuid = response.json['data']['exchange']['blockchain_task_uuid']
        time.sleep(
            1)  # Have to wait til after_request for task to be submitted
        result = bt.await_task_success(task_uuid,
                                       timeout=config.SYNCRONOUS_TASK_TIMEOUT *
                                       12)
        assert result['status'] == 'SUCCESS'
示例#2
0
def deploy_cic_token(post_data, creating_org=None):

    name = post_data['name']
    symbol = post_data['symbol']
    decimals = post_data.get('decimals', 18)
    issue_amount_wei = int(post_data['issue_amount_wei'])
    reserve_deposit_wei = int(post_data['reserve_deposit_wei'])
    exchange_contract_id = post_data['exchange_contract_id']
    reserve_ratio_ppm = post_data.get('reserve_ratio_ppm', 250000)

    if creating_org:
        deploying_address = creating_org.primary_blockchain_address
    else:
        deploying_address = g.user.primary_blockchain_address


    if not exchange_contract_id:
        response_object = {
            'message': 'Must supply exchange contract id if deploying smart token contract'
        }

        return response_object, 400

    exchange_contract = ExchangeContract.query.get(exchange_contract_id)

    if not exchange_contract:
        response_object = {
            'message': 'Exchange contract not found for id {}'.format(exchange_contract_id)
        }

        return response_object, 400

    balance_wei = bt.get_wallet_balance(deploying_address, exchange_contract.reserve_token)

    if balance_wei < reserve_deposit_wei:

        load_amount = int((reserve_deposit_wei - balance_wei) / 1e16)

        master_org = Organisation.master_organisation()

        print(f'Insufficient reserve funds (balance in wei: {balance_wei}), loading')

        if master_org.org_level_transfer_account.balance < load_amount:
            response_object = {
                'message': f'Insufficient reserve funds for both deploying account  ({balance_wei} wei), '
                           f'and master ({master_org.org_level_transfer_account.balance * 1e16} wei)'
            }

            return response_object, 400

        load_task_uuid = bt.make_token_transfer(
            signing_address=master_org.primary_blockchain_address,
            token=exchange_contract.reserve_token,
            from_address=master_org.primary_blockchain_address,
            to_address=deploying_address,
            amount=load_amount
        )

        try:
            bt.await_task_success(load_task_uuid)
        except TimeoutError:
            response_object = {
                'message': f'Insufficient reserve funds (balance in wei: {balance_wei}), and could not load from master'
            }

            return response_object, 400

        master_org.org_level_transfer_account.balance -= load_amount

    token = Token(name=name, symbol=symbol, token_type=TokenType.LIQUID)
    db.session.add(token)
    db.session.flush()

    deploy_data = dict(
        deploying_address=deploying_address,
        name=name, symbol=symbol, decimals=decimals,
        reserve_deposit_wei=reserve_deposit_wei,
        issue_amount_wei=issue_amount_wei,
        contract_registry_address=exchange_contract.contract_registry_blockchain_address,
        reserve_token_address=exchange_contract.reserve_token.address,
        reserve_ratio_ppm=reserve_ratio_ppm
    )

    @copy_current_request_context
    def deploy(_deploy_data, _token_id, _exchange_contract_id, _creating_org_id=None):
        smart_token_result = bt.deploy_smart_token(**_deploy_data)

        address = smart_token_result['smart_token_address']
        subexchange_address = smart_token_result['subexchange_address']

        _token = Token.query.get(_token_id)
        _token.address = address

        _exchange_contract = ExchangeContract.query.get(_exchange_contract_id)
        _exchange_contract.add_token(_token, subexchange_address, reserve_ratio_ppm)

        if _creating_org_id:

            _creating_org = Organisation.query.get(_creating_org_id)
            _creating_org.bind_token(_token)
            _creating_org.org_level_transfer_account.balance = int(_deploy_data['issue_amount_wei'] / 1e16)

            bal = bt.get_wallet_balance(_creating_org.primary_blockchain_address, _token)

            print(f'Balance is {bal}')

        db.session.commit()

    if creating_org:
        creating_org_id = creating_org.id
    else:
        creating_org_id = None

    t = threading.Thread(target=deploy,
                         args=(deploy_data, token.id, exchange_contract_id, creating_org_id))
    t.daemon = True
    t.start()

    response_object = {
        'message': 'success',
        'data': {
            'token_id': token.id
        }
    }

    return response_object, 201
示例#3
0
def test_force_third_party_transaction_sync():
    if will_func_test_blockchain():
        task_uuid = bt.force_third_party_transaction_sync()
        bt.await_task_success(
            task_uuid,
            timeout=config.CHAINS['ETHEREUM']['SYNCRONOUS_TASK_TIMEOUT'] * 48)
示例#4
0
def run_setup():
    app = create_app()
    ctx = app.app_context()
    ctx.push()

    # To simplify creation, we set the flask context to show all model data
    g.show_all = True

    master_organisation = Organisation.master_organisation()
    if master_organisation is None:
        print('Creating master organisation')
        master_organisation = Organisation(is_master=True)
        db.session.add(master_organisation)

    master_system_address = master_organisation.system_blockchain_address

    load_account(master_system_address, int(1e18))
    reserve_token = get_or_create_reserve_token(master_system_address, 'AUD Token', 'AUD')

    master_organisation.token = reserve_token

    print('Creating organisation')
    new_organisation = get_or_create_organisation('org1', reserve_token)
    load_account(new_organisation.system_blockchain_address, int(1e18))

    print('Creating admin user')
    amount_to_load = 1000
    admin_user = get_or_create_admin_user('*****@*****.**', 'TestPassword', new_organisation)
    admin_transfer_account = admin_user.transfer_account
    load_account(admin_transfer_account.blockchain_address, int(20e18))
    send_eth_task = bt.send_eth(
        signing_address=admin_transfer_account.blockchain_address,
        recipient_address=reserve_token.address,
        amount_wei=amount_to_load * int(1e16))

    bt.await_task_success(send_eth_task)

    admin_user.transfer_account.balance = amount_to_load

    print('Creating Transfer Usage')
    usages = list(map(
        get_or_create_transfer_usage,
        ['Broken Pencils',
         'Off Milk',
         'Stuxnet',
         'Used Playing Cards',
         '09 F9',
         'Junk Mail',
         'Cutlery',
         'Leaked Private Keys',
         'Parking Infringements',
         'Betamax Movies',
         'Hyperallergenic Soap',
         'Dioxygen Difluoride',
         'Hunter2'
         ]))

    print('Create a list of users with a different business usage id ')
    user_list = create_users_different_transer_usage(15, new_organisation)

    print('Making Bulk Transfers')
    seed_transfers(user_list, admin_user, reserve_token)

    print('Deploying Smart Token')
    smart_token, exchange_contract, registry_address = create_or_get_reserve_token(
        deploying_address=admin_transfer_account.blockchain_address,
        reserve_token=reserve_token,
        reserve_deposit_wei=5e17,
        issue_amount_wei=5e17,
        name='CIC1', symbol='CIC1', reserve_ratio_ppm=250000
    )

    print('Making exchanges')
    make_exchange(
        user=admin_user,
        from_token=reserve_token,
        to_token=smart_token,
        from_amount=2
    )

    create_transfer(
        amount=10,
        sender_user=admin_user,
        recipient_user=user_list[0],
        token=reserve_token
    )

    make_exchange(
        user=user_list[0],
        from_token=reserve_token,
        to_token=smart_token,
        from_amount=10
    )

    create_transfer(
        amount=2,
        sender_user=user_list[0],
        recipient_user=user_list[1],
        token=smart_token
    )

    db.session.commit()
    ctx.pop()