Beispiel #1
0
def observe_address_history_to(address, track):
    if is_valid_address(address):
        if track:
            _get_os().track_address(address, "historyto")
        else:
            _get_os().untrack_address(address, "historyto")
    else:
        raise AccountDoesNotExistsException()
Beispiel #2
0
def _get_from_history(address, take, to_or_from, after_hash=None):
    if not is_valid_address(address):
        raise AccountDoesNotExistsException()

    take = int(take)

    all_operations = []

    # normalize address (given address can contain id or name)
    address_split = split_unique_address(address)
    address = create_unique_address(address_split["account_id"], address_split["customer_id"])

    afterTimestamp = datetime.fromtimestamp(0)

    filter_dict = {to_or_from: address_split["account_id"]}
    if address_split["customer_id"]:
        filter_dict.update({"customer_id": address_split["customer_id"]})

    for operation in _get_os().get_operations_completed(
            filter_by=filter_dict):
        # deposit, thus from
        add_op = {
            "timestamp": operation.get("timestamp", None),
            "fromAddress": get_from_address(operation),
            "toAddress": get_to_address(operation),
            "assetId": operation["amount_asset_id"],
            "amount": str(operation["amount_value"]),
            "hash": operation["chain_identifier"]
        }
        if (to_or_from == "to" and add_op["toAddress"] == address) or\
                (to_or_from == "from" and add_op["fromAddress"] == address):
            all_operations.append(add_op)
            if operation["chain_identifier"] == after_hash and add_op["timestamp"]:
                afterTimestamp = utils.string_to_date(add_op["timestamp"])

    older = [op for op in all_operations if
             op["timestamp"] and afterTimestamp <=
             utils.string_to_date(op["timestamp"])]
    older.sort(key=lambda x: utils.string_to_date(x["timestamp"]))

    max_end = max(take, len(older))

    return older[0:max_end]
Beispiel #3
0
def get_block_explorer_url(address):
    if is_valid_address(address):
        return ["http://bitshares-explorer.io/#/accounts/" + split_unique_address(address)["account_id"]]
    else:
        raise AccountDoesNotExistsException()
Beispiel #4
0
def build_transaction(incidentId, fromAddress, fromMemoWif, toAddress, asset_id,
                      amount, includeFee, bitshares_instance=None):
    """ Builds a transaction (without signature)

        :param guid incidentId: Lykke unique operation ID
        :param str fromAddress: Source address
        :param str toAddress: Destination address
        :param str assetId: Asset ID to transfer
        :param str amount: Amount to transfer. Integer as string, aligned to the asset
                accuracy. Actual value can be calculated as x = amount / (10 ^
                asset.Accuracy)
        :param bool includeFee: Flag, which indicates, that the fee should
                be included in the specified amount
    """

    def obtain_raw_tx():
#         if old_operation is None:
        _memo = memo.encrypt(memo_plain)
        _expiration = Config.get("bitshares", "transaction_expiration_in_sec", 60 * 60 * 24)  # 24 hours
#         else:
#             memo_encrypted = memo.encrypt(memo_plain)

        op = operations.Transfer(**{
            "fee": {
                "amount": 0,
                "asset_id": "1.3.0"
            },  # will be replaced
            "from": from_account["id"],
            "to": to_account["id"],
            "amount": amount.json(),
            "memo": _memo,
            "prefix": bitshares_instance.prefix
        })

        tx = TransactionBuilder(
            bitshares_instance=bitshares_instance
        )
        tx.appendOps(op)
        tx.set_expiration(_expiration)

        # Build the transaction, obtain fee to be paid
        tx.constructTx()
        return tx.json()

    operation_formatter.validate_incident_id(incidentId)

    if not is_valid_address(fromAddress):
        raise AccountDoesNotExistsException()

    if not is_valid_address(toAddress):
        raise AccountDoesNotExistsException()

#     # check if this was already built
#     old_operation = None
#     try:
#         old_operation = _get_os().get_operation(incidentId)
#     except OperationNotFoundException:
#         pass

    # Decode addresses
    from_address = split_unique_address(fromAddress)
    to_address = split_unique_address(toAddress)

    # obtain chain accounts from addresses
    from_account = Account(
        from_address["account_id"],
        bitshares_instance=bitshares_instance)
    to_account = Account(
        to_address["account_id"],
        bitshares_instance=bitshares_instance)

    memo_plain = create_memo(from_address, to_address, incidentId)

    try:
        # Construct amount
        amount = Amount(
            {
                "amount": amount,
                "asset_id": asset_id
            },
            bitshares_instance=bitshares_instance
        )
    except AssetDoesNotExistsException:
        raise AssetNotFoundException()

    # encrypt memo
    # TODO this is a hack. python-bitshares issue is opened, once resolve, fix
    if not fromMemoWif:
        if from_address["account_id"] == Config.get("bitshares", "exchange_account_id"):
            fromMemoWif = Config.get("bitshares", "exchange_account_memo_key")

    if fromMemoWif:
        bitshares_instance.wallet.setKeys(fromMemoWif)

    # memo key of the account must be known!
    if not from_account["options"]["memo_key"] in Wallet.keys:
            raise MemoMatchingFailedException()

    memo = Memo(
        from_account=from_account,
        to_account=to_account,
        bitshares_instance=bitshares_instance
    )

    try:
        tx = obtain_raw_tx()
    except MissingKeyError:
        raise MemoMatchingFailedException()

    fee = Amount(tx["operations"][0][1]["fee"],
                 bitshares_instance=bitshares_instance)

    # virtual internal transfers always do full amount
    if includeFee and from_account != to_account:
        # Reduce fee from amount to transfer
        amount -= fee
        tx = obtain_raw_tx()

    # Add additional/optional information
    #   - add incident_id as fallback for internal database
    #   - add decoded memo to avoid double decoding
    tx.update({
        "incident_id": incidentId,
        "decoded_memo": memo_plain,
    })

    if bitshares_instance.prefix != "BTS":
        tx["prefix"] = bitshares_instance.prefix

    return {"transactionContext": json.dumps(tx)}
Beispiel #5
0
def unobserve_address(address):
    if is_valid_address(address):
        _get_os().untrack_address(address, "balance")
    else:
        raise AccountDoesNotExistsException()