Beispiel #1
0
def post_bounties_guid_assertions(guid):
    account = g.chain.w3.toChecksumAddress(g.eth_address)
    base_nonce = int(
        request.args.get('base_nonce',
                         g.chain.w3.eth.getTransactionCount(account)))

    body = request.get_json()
    try:
        _post_bounties_guid_assertions_schema(body)
    except fastjsonschema.JsonSchemaException as e:
        return failure('Invalid JSON: ' + e.message, 400)

    bid = [int(b) for b in body['bid']]
    mask = bool_list_to_int(body['mask'])
    verdict_count = len([m for m in body['mask'] if m])

    commitment = body.get('commitment')
    verdicts = body.get('verdicts')

    if commitment is None and verdicts is None:
        return failure('Require verdicts and bid_portions or a commitment',
                       400)

    if not bid or len(bid) != verdict_count:
        return failure(
            'bid_portions must be equal in length to the number of true mask values',
            400)

    max_bid = eth.assertion_bid_max(g.chain.bounty_registry.contract)
    min_bid = eth.assertion_bid_min(g.chain.bounty_registry.contract)
    if any((b for b in bid if max_bid < b < min_bid)):
        return failure('Invalid assertion bid', 400)

    approve_amount = sum(bid) + eth.assertion_fee(
        g.chain.bounty_registry.contract)

    nonce = None
    if commitment is None:
        nonce, commitment = calculate_commitment(account,
                                                 bool_list_to_int(verdicts))
    else:
        commitment = int(commitment)

    ret = {
        'transactions': [
            build_transaction(
                g.chain.nectar_token.contract.functions.approve(
                    g.chain.bounty_registry.contract.address, approve_amount),
                base_nonce),
            build_transaction(
                g.chain.bounty_registry.contract.functions.postAssertion(
                    guid.int, bid, mask, commitment), base_nonce + 1),
        ]
    }

    if nonce is not None:
        # Pass generated nonce onto user in response, used for reveal
        ret['nonce'] = nonce

    return success(ret)
Beispiel #2
0
def post_arbiter_staking_deposit():
    account = g.chain.w3.toChecksumAddress(g.eth_address)
    base_nonce = int(
        request.args.get('base_nonce',
                         g.chain.w3.eth.getTransactionCount(account)))

    body = request.get_json()
    try:
        _post_arbiter_staking_deposit_schema(body)
    except fastjsonschema.JsonSchemaException as e:
        return failure('Invalid JSON: ' + e.message, 400)

    amount = int(body['amount'])

    total = g.chain.arbiter_staking.contract.functions.balanceOf(
        account).call()

    if amount + total >= eth.staking_total_max(
            g.chain.arbiter_staking.contract):
        return failure('Total stake above allowable maximum.', 400)

    transactions = [
        build_transaction(
            g.chain.nectar_token.contract.functions.approve(
                g.chain.arbiter_staking.contract.address, amount), base_nonce),
        build_transaction(
            g.chain.arbiter_staking.contract.functions.deposit(amount),
            base_nonce + 1),
    ]

    return success({'transactions': transactions})
Beispiel #3
0
def post_bounties():
    config = app.config['POLYSWARMD']
    session = app.config['REQUESTS_SESSION']
    account = g.chain.w3.toChecksumAddress(g.eth_address)
    base_nonce = int(
        request.args.get('base_nonce',
                         g.chain.w3.eth.getTransactionCount(account)))

    body = request.get_json()
    try:
        _post_bounties_schema(body)
    except fastjsonschema.JsonSchemaException as e:
        return failure('Invalid JSON: ' + e.message, 400)

    guid = uuid.uuid4()
    artifact_type = ArtifactType.from_string(body['artifact_type'])
    amount = int(body['amount'])
    artifact_uri = body['uri']
    duration_blocks = body['duration']
    metadata = body.get('metadata', '')

    try:
        arts = config.artifact.client.ls(artifact_uri, session)
    except HTTPError as e:
        return failure(e.response.content, e.response.status_code)
    except ArtifactException:
        logger.exception('Failed to ls given artifact uri')
        return failure(f'Failed to check artifact uri', 500)

    if amount < eth.bounty_amount_min(
            g.chain.bounty_registry.contract) * len(arts):
        return failure('Invalid bounty amount', 400)

    if metadata and not config.artifact.client.check_uri(metadata):
        return failure('Invalid bounty metadata URI (should be IPFS hash)',
                       400)

    num_artifacts = len(arts)
    bloom = calculate_bloom(arts)

    approve_amount = amount + eth.bounty_fee(g.chain.bounty_registry.contract)

    transactions = [
        build_transaction(
            g.chain.nectar_token.contract.functions.approve(
                g.chain.bounty_registry.contract.address, approve_amount),
            base_nonce),
        build_transaction(
            g.chain.bounty_registry.contract.functions.postBounty(
                guid.int, artifact_type.value, amount, artifact_uri,
                num_artifacts, duration_blocks, bloom, metadata),
            base_nonce + 1),
    ]

    return success({'transactions': transactions})
Beispiel #4
0
def post_uri(guid):
    offer_channel = channel_to_dict(
        g.chain.offer_registry.contract.functions.guidToChannel(
            guid.int).call())
    msig_address = offer_channel['msig_address']
    offer_msig = g.chain.offer_multi_sig.bind(msig_address)
    account = g.chain.w3.toChecksumAddress(g.eth_address)
    base_nonce = int(
        request.args.get('base_nonce',
                         g.chain.w3.eth.getTransactionCount(account)))

    body = request.get_json()

    try:
        _post_uri_schema(body)
    except fastjsonschema.JsonSchemaException as e:
        return failure('Invalid JSON: ' + e.message)

    websocket_uri = body['websocketUri']

    transactions = [
        build_transaction(
            offer_msig.functions.setCommunicationUri(
                g.chain.w3.toHex(text=websocket_uri)), base_nonce),
    ]

    return success({'transactions': transactions})
Beispiel #5
0
def post_challange(guid):
    offer_channel = channel_to_dict(
        g.chain.offer_registry.contract.functions.guidToChannel(
            guid.int).call())
    msig_address = offer_channel['msig_address']
    offer_msig = g.chain.offer_multi_sig.bind(msig_address)
    account = g.chain.w3.toChecksumAddress(g.eth_address)
    base_nonce = int(
        request.args.get('base_nonce',
                         g.chain.w3.eth.getTransactionCount(account)))

    body = request.get_json()

    try:
        _post_challange_schema(body)
    except fastjsonschema.JsonSchemaException as e:
        return failure('Invalid JSON: ' + e.message)

    state = g.chain.w3.toBytes(hexstr=body['state'])
    v = body['v']
    r = list(map(lambda s: g.chain.w3.toBytes(hexstr=s), body['r']))
    s = list(map(lambda s: g.chain.w3.toBytes(hexstr=s), body['s']))

    transactions = [
        build_transaction(offer_msig.functions.challengeSettle(state, v, r, s),
                          base_nonce),
    ]

    return success({'transactions': transactions})
Beispiel #6
0
def post_create_offer_channel():
    account = g.chain.w3.toChecksumAddress(g.eth_address)
    base_nonce = int(
        request.args.get('base_nonce',
                         g.chain.w3.eth.getTransactionCount(account)))

    body = request.get_json()

    try:
        _post_create_offer_channel_schema(body)
    except fastjsonschema.JsonSchemaException as e:
        return failure('Invalid JSON: ' + e.message)

    guid = uuid.uuid4()
    ambassador = g.chain.w3.toChecksumAddress(body['ambassador'])
    expert = g.chain.w3.toChecksumAddress(body['expert'])
    settlement_period_length = body['settlementPeriodLength']

    transactions = [
        build_transaction(
            g.chain.offer_registry.contract.functions.initializeOfferChannel(
                guid.int, ambassador, expert, settlement_period_length),
            base_nonce),
    ]

    return success({'transactions': transactions})
Beispiel #7
0
def post_join(guid):
    offer_channel = channel_to_dict(
        g.chain.offer_registry.contract.functions.guidToChannel(
            guid.int).call())
    msig_address = offer_channel['msig_address']
    offer_msig = g.chain.offer_multi_sig.bind(msig_address)
    account = g.chain.w3.toChecksumAddress(g.eth_address)
    base_nonce = int(
        request.args.get('base_nonce',
                         g.chain.w3.eth.getTransactionCount(account)))

    body = request.get_json()

    try:
        _post_join_schema(body)
    except fastjsonschema.JsonSchemaException as e:
        return failure('Invalid JSON: ' + e.message)

    state = body['state']
    v = body['v']
    r = body['r']
    s = body['s']

    transactions = [
        build_transaction(
            offer_msig.functions.joinAgreement(state, v, to_padded_hex(r),
                                               to_padded_hex(s)), base_nonce),
    ]

    return success({'transactions': transactions})
Beispiel #8
0
def post_arbiter_staking_withdrawal():
    account = g.chain.w3.toChecksumAddress(g.eth_address)
    base_nonce = int(
        request.args.get('base_nonce',
                         g.chain.w3.eth.getTransactionCount(account)))
    body = request.get_json()
    try:
        _post_arbiter_staking_withdrawal_schema(body)
    except fastjsonschema.JsonSchemaException as e:
        return failure('Invalid JSON: ' + e.message, 400)

    amount = int(body['amount'])

    available = g.chain.arbiter_staking.contract.functions.withdrawableBalanceOf(
        account).call()

    if amount > available:
        return failure('Exceeds withdrawal eligible %s' % available, 400)

    transactions = [
        build_transaction(
            g.chain.arbiter_staking.contract.functions.withdraw(amount),
            base_nonce),
    ]

    return success({'transactions': transactions})
Beispiel #9
0
def post_bounties_guid_settle(guid):
    account = g.chain.w3.toChecksumAddress(g.eth_address)
    base_nonce = int(
        request.args.get('base_nonce',
                         g.chain.w3.eth.getTransactionCount(account)))

    transactions = [
        build_transaction(
            g.chain.bounty_registry.contract.functions.settleBounty(guid.int),
            base_nonce)
    ]

    return success({'transactions': transactions})
Beispiel #10
0
def post_cancel(guid):
    offer_channel = channel_to_dict(
        g.chain.offer_registry.contract.functions.guidToChannel(
            guid.int).call())
    msig_address = offer_channel['msig_address']
    offer_msig = g.chain.offer_multi_sig.bind(msig_address)
    account = g.chain.w3.toChecksumAddress(g.eth_address)
    base_nonce = int(
        request.args.get('base_nonce',
                         g.chain.w3.eth.getTransactionCount(account)))

    transactions = [
        build_transaction(offer_msig.functions.cancel(), base_nonce),
    ]

    return success({'transactions': transactions})
Beispiel #11
0
def post_bounties_guid_vote(guid):
    account = g.chain.w3.toChecksumAddress(g.eth_address)
    base_nonce = int(
        request.args.get('base_nonce',
                         g.chain.w3.eth.getTransactionCount(account)))

    body = request.get_json()
    try:
        _post_bounties_guid_vote_schema(body)
    except fastjsonschema.JsonSchemaException as e:
        return failure('Invalid JSON: ' + e.message, 400)

    votes = bool_list_to_int(body['votes'])
    valid_bloom = bool(body['valid_bloom'])

    transactions = [
        build_transaction(
            g.chain.bounty_registry.contract.functions.voteOnBounty(
                guid.int, votes, valid_bloom), base_nonce),
    ]
    return success({'transactions': transactions})
Beispiel #12
0
def send_funds_from():
    # Grab correct versions by chain type
    account = g.chain.w3.toChecksumAddress(g.eth_address)
    base_nonce = int(request.args.get('base_nonce', g.chain.w3.eth.getTransactionCount(account)))
    erc20_relay_address = g.chain.w3.toChecksumAddress(g.chain.erc20_relay.address)

    body = request.get_json()
    try:
        _send_funds_from_schema(body)
    except fastjsonschema.JsonSchemaException as e:
        return failure('Invalid JSON: ' + e.message, 400)

    amount = int(body['amount'])

    transactions = [
        build_transaction(
            g.chain.nectar_token.contract.functions.transfer(erc20_relay_address, amount), base_nonce
        ),
    ]

    return success({'transactions': transactions})
Beispiel #13
0
def post_bounties_guid_assertions_id_reveal(guid, id_):
    account = g.chain.w3.toChecksumAddress(g.eth_address)
    base_nonce = int(
        request.args.get('base_nonce',
                         g.chain.w3.eth.getTransactionCount(account)))

    body = request.get_json()
    try:
        _post_bounties_guid_assertions_id_reveal_schema(body)
    except fastjsonschema.JsonSchemaException as e:
        return failure('Invalid JSON: ' + e.message, 400)

    nonce = int(body['nonce'])
    verdicts = bool_list_to_int(body['verdicts'])
    metadata = body['metadata']

    transactions = [
        build_transaction(
            g.chain.bounty_registry.contract.functions.revealAssertion(
                guid.int, id_, nonce, verdicts, metadata), base_nonce),
    ]
    return success({'transactions': transactions})