Exemple #1
0
def withdraw(stream_id: int, amount: int) -> bool:
    """
    Withdraw funds from contract to recipient. Can be triggered by
    either recipient or sender

    Args:
        stream_id (int): Stream ID
        amount (int): Amount to withdraw

    Returns:
        bool: Success or failure
    """
    stream = loadStream(stream_id)
    recipient = base64_decode(cast(str, stream['recipient']))
    sender = base64_decode(cast(str, stream['sender']))
    requester = ""
    if check_witness(recipient):
        print("Recipient requesting withdrawal")
        requester = cast(str, stream['recipient'])
    elif check_witness(sender):
        # TODO: should sender be able to request an advance to recipient?
        print("Sender requesting withdrawal to recipient")
        requester = cast(str, stream['sender'])
    else:
        print("Must be sender or recipient to withdraw")
        abort()

    remaining = cast(int, stream['remaining'])
    available = getAmountAvailableForWithdrawal(stream)

    assert amount > 0, 'nothing to do'
    assert available >= amount, 'withdrawal amount exceeds available funds'

    stream['remaining'] = remaining - amount

    call_contract(GAS, 'transfer',
                  [executing_script_hash, recipient, amount, None])

    if cast(int, stream['remaining']) == 0:
        deleteStream(stream)
        on_complete(stream_id)
    else:
        put(b'streams/' + stream_id.to_bytes(), json_serialize(stream))

    on_withdraw(stream_id, requester, amount)
    return True
Exemple #2
0
def saveStream(stream: Dict[str, Any]):
    """
    Save a string to storage and trigger on_create event

    Args:
        stream (Dict[str, Any]): Stream object
    """
    b_id = cast(bytes, stream['id'])
    i_id = cast(int, stream['id'])
    sender_key = cast(bytes, stream['sender']) + b':' + b_id
    recipient_key = cast(bytes, stream['recipient']) + b':' + b_id

    stream_json = cast(str, json_serialize(stream))

    put(b'streams/' + b_id, stream_json)
    put(b'bysender/' + sender_key, i_id)
    put(b'byrecipient/' + recipient_key, i_id)

    on_create(stream_json)
Exemple #3
0
def create_index(domain: bytes, index: Index, key: bytes, pointer: int) -> bool:
    """
    Creates a new index in the datastore for a domain.

    :param domain: the scope that the index exists within (this is similar to a table)
    :param index: the index definition
    :param key: the key to store the data under
    :param pointer: the write pointer used to store the data

    :return: A boolean indicating whether the operation was successful.
    """
    index_values = get_index(domain, index, key)

    # Enforce unique indices.
    assert not (index.get_unique() and len(index_values) > 0), 'Exception: Unique index exists.'

    # Append the new domain-global pointer to the index-key entry and update storage.
    index_values.append(pointer)
    set_variable_prefixed_value(domain, index.get_key(), key, json_serialize(index_values))

    return True
def main() -> str:
    return json_serialize(True)
Exemple #5
0
def main(value: Any) -> Any:
    serialized = json.json_serialize(value)
    return json.json_deserialize(serialized)
def main() -> str:
    return json_serialize('unit test')
def main() -> bytes:
    return json_serialize(True)
def main() -> bytes:
    return json_serialize('unit test')
Exemple #9
0
def main(item: Any) -> str:
    return json_serialize(item)
def main(item: Any) -> bytes:
    return json_serialize(item)