Beispiel #1
0
def DeleteAgreement(agreement_key):
    """
    Method for the dApp owner to delete claimed or refunded agreements

    :param agreement_key: agreement_key
    :type agreement_key: str

    :return: whether the deletion succeeded
    :rtype: bool
    """

    if not CheckWitness(OWNER):
        Log("Must be owner to delete an agreement")
        return False

    context = GetContext
    agreement_data = Get(context, agreement_key)
    status = agreement_data[12]

    if status == 'claimed':
        Delete(context, agreement_key)
        DispatchDeleteAgreementEvent(agreement_key)

    elif status == 'refunded':
        Delete(context, agreement_key)
        DispatchDeleteAgreementEvent(agreement_key)

    return False
Beispiel #2
0
def DeleteMilestone(milestone_key):
    """
    Method for the dApp owner to delete claimed or refunded agreements

    :param milestone_key: milestone_key
    :type milestone_key: str

    :return: whether the deletion succeeded
    :rtype: bool
    """
    if not CheckWitness(OWNER):
        Notify("Must be owner to delete an agreement")
        return False

    context = GetContext
    milestone_data_serialized = Get(context, milestone_key)
    milestone_data = deserialize_bytearray(milestone_data_serialized)
    status = milestone_data[11]

    if status == 'reviewed':
        Delete(context, milestone_key)
        DispatchDeleteMilestoneEvent(milestone_key)
        return True

    elif status == 'refunded':
        Delete(context, milestone_key)
        DispatchDeleteMilestoneEvent(milestone_key)
        return True

    return False
Beispiel #3
0
def remove_offer(marketplace, offer_id):
    """
    Helper method to remove an offer that exists on a marketplace.

    :param marketplace:str The name of the marketplace to access.
    :param offer_id:int The id of the offer to remove.
    :return:
        bool: Whether the offer was removed.
    """
    context = GetContext()

    # Concatenate an offer key for the specified marketplace.
    marketplace_offers_key = concat(offers_key, marketplace)

    # Get all the available offers of a marketplace.
    offers_s = get_all_offers(marketplace)
    offers = deserialize_bytearray(offers_s)

    # Iterate through the offers, we must currently remove by index.
    # TODO: Remove manually searching for the index once list method "indexOf" is added.
    current_index = 0
    for offer in offers:
        # If the offer equals the offer_id remove at current_index.
        if offer == offer_id:
            offers.remove(current_index)
            # Replace the list of offers currently in storage with the modified list.
            offers_s = serialize_array(offers)
            Delete(context, marketplace_offers_key)
            Put(context, marketplace_offers_key, offers_s)
            # Delete the offer from the storage.
            Delete(context, offer_id)
            return True
        current_index += 1

    return False
Beispiel #4
0
def Unregister(user):
    """
    Unregister a user from the KRYPTON network

    :param user: the public address of the user
    :type user: str

    :return: whether the registration was successful
    :rtype: bool
    """

    # Check if the user is validating the transaction
    if not CheckWitness(user):
        Log('FORBIDDEN')
        return False

    context = GetContext()
    uuid = Get(context, user)

    if not (uuid == 0):
        # Remove registration
        Delete(context, user)
        Delete(context, uuid)
        Log('UNREGISTER_SUCCESS')
        return True

    # No registration found
    Log('UNREGISTER_FAILED')
    return False
Beispiel #5
0
def Register(user, provider, uuid):
    """
    Register a user with a provider and use the provided UUID as
    authorization nonce

    :param user: the public address of the user
    :type user: str

    :param provider: the public address of the provider
    :type provider: str

    :param uuid: the UUID supplied by provider and confirmed by user
    :type uuid: str

    :return: whether the registration was successful
    :rtype: bool
    """

    # Check if the UUID format is validating
    if not (CheckUUID(uuid)):
        return False

    # Check if the user is validating the transaction
    if not CheckWitness(user):
        Log('FORBIDDEN')
        return False

    context = GetContext()
    address = Get(context, provider)

    # Check if provider exists
    if (address == 0):
        Log('PROVIDER_ABSENT')
        return False

    address = Get(context, user)

    # Remove old registration before inserting new registration
    if not (address == 0):
        Delete(context, user)
        Delete(context, uuid)
        Log('UNREGISTER_SUCCESS')

    # Store new registration
    Put(context, user, uuid)
    Put(context, uuid, provider)
    Log('REGISTER_SUCCESS')
    return True
Beispiel #6
0
def Undeploy(provider):
    """
    Undeploy a provider from the KRYPTON network

    :param provider: the address of the SIP provider wallet
    :type provider: str

    :return: whether the deploy was successful
    :rtype: bool
    """

    if not CheckWitness(provider):
        Log('FORBIDDEN')
        return False

    context = GetContext()
    address = Get(context, provider)

    # Remove deployment
    if not (address == 0):
        Delete(context, provider)
        Log('UNDEPLOY_SUCCESS')
        return True

    # No deployment found
    Log('UNDEPLOY_FAILED')
    return False
Beispiel #7
0
def DeletePendingWithdrawal(account):

    context = GetContext()
    account_pending = concat(account, 'pending')

    Delete(context, account_pending)

    return True
Beispiel #8
0
def DoTransfer(t_from, t_to, amount):
    """
    Method to transfer NEP5 tokens of a specified amount from one account to another

    :param t_from: the address to transfer from
    :type t_from: bytearray

    :param t_to: the address to transfer to
    :type t_to: bytearray

    :param amount: the amount of NEP5 tokens to transfer
    :type amount: int

    :return: whether the transfer was successful
    :rtype: bool

    """
    if amount <= 0:
        print("cannot transfer zero or less")
        return False

    from_is_sender = CheckWitness(t_from)

    if from_is_sender:

        if t_from == t_to:
            return True

        context = GetContext()

        from_val = Get(context, t_from)

        if from_val < amount:
            print("Insufficient funds")
            return False

        if from_val == amount:
            print("Removing all funds!")
            Delete(context, t_from)

        else:
            difference = from_val - amount
            Put(context, t_from, difference)

        to_value = Get(context, t_to)

        to_total = to_value + amount

        Put(context, t_to, to_total)

        OnTransfer(t_from, t_to, amount)

        return True
    else:
        print("from address is not the tx sender")

    return False
Beispiel #9
0
def RefundPromo(buyer, promo_id):
    """
    Refund all of buyer's purchased tickets for specified promo

    Args:
        buyer (str): buyer's public key
        promo_id (str): promo unique id

    Returns:
        (bool): True if successfully refunded
    """

    #
    # Checks for if args are valid and refund conditions are met
    #

    promo = get_promo_storage_keys(promo_id)

    promo_exists = IsPromoExist(promo_id)
    expired = IsPromoExpired(promo_id)

    context = GetContext()
    min_count = Get(context, promo.min_count_key)
    purchased_count = Get(context, promo.purchased_count_key)
    count_met = purchased_count >= min_count

    if promo_exists:
        # Cannot issue refund if minimum number of tickets has been sold past deadline
        if expired and count_met:
            Log('Refund no longer allowed, promo refund deadline has passed and the minimum number of tickets has been sold'
                )
            return False

    buyer_key = concat(promo_id, buyer)
    refund_quantity = Get(context, buyer_key)
    if not refund_quantity:
        Log('No purchases found using given public key and promo_id')
        return False

    #
    # Refund tickets
    #

    Delete(context, buyer_key)

    price_per_person = Get(context, promo.price_per_person_key)

    refund_amount = refund_quantity * price_per_person
    refund_address = GetCallingScriptHash()
    OnRefund(refund_address, refund_amount)

    # update purchased_count
    purchased_count -= refund_quantity
    Put(context, promo.purchased_count_key, purchased_count)

    return True
Beispiel #10
0
def do_delete():
    """
    Delete the storage and reset back to its default state.

    :return: indication success execution of the command
    :rtype: bool
    """
    context = GetContext()
    Delete(context, KEY)
    return True
Beispiel #11
0
def deleteOperation(key):
    print('delete attempt')

    if key == None:
        print('no key to delete')
        return False

    Delete(GetContext, key)
    print('delete successful')
    return True
Beispiel #12
0
def DoTransfer(t_from, t_to, amount):
    """
    Method to transfer NEP5 tokens of a specified amount from one account to another

    :param t_from: the address to transfer from
    :type t_from: bytearray

    :param t_to: the address to transfer to
    :type t_to: bytearray

    :param amount: the amount of NEP5 tokens to transfer
    :type amount: int

    :return: whether the transfer was successful
    :rtype: bool

    """
    if amount <= 0:
        Log("Cannot transfer negative amount")
        return False

    from_is_sender = CheckWitness(t_from)

    if not from_is_sender:
        Log("Not owner of funds to be transferred")
        return False

    if t_from == t_to:
        Log("Sending funds to self")
        return True

    context = GetContext()

    from_val = Get(context, t_from)

    if from_val < amount:
        Log("Insufficient funds to transfer")
        return False

    if from_val == amount:
        Delete(context, t_from)

    else:
        difference = from_val - amount
        Put(context, t_from, difference)

    to_value = Get(context, t_to)

    to_total = to_value + amount

    Put(context, t_to, to_total)

    OnTransfer(t_from, t_to, amount)

    return True
Beispiel #13
0
def DoTransfer(sender, receiver, amount):
    """
    Method to transfer tokens from one account to another

    :param sender: the address to transfer from
    :type sender: bytearray

    :param receiver: the address to transfer to
    :type receiver: bytearray

    :param amount: the amount of tokens to transfer
    :type amount: int

    :return: whether the transfer was successful
    :rtype: bool

    """

    if amount <= 0:
        Log("Cannot transfer negative amount")
        return False

    from_is_sender = CheckWitness(sender)

    if not from_is_sender:
        Log("Not owner of funds to be transferred")
        return False

    if sender == receiver:
        Log("Sending funds to self")
        return True

    context = GetContext()
    from_val = Get(context, sender)

    if from_val < amount:
        Log("Insufficient funds to transfer")
        return False

    if from_val == amount:
        Delete(context, sender)

    else:
        difference = from_val - amount
        Put(context, sender, difference)

    to_value = Get(context, receiver)

    to_total = to_value + amount

    Put(context, receiver, to_total)
    DispatchTransferEvent(sender, receiver, amount)

    return True
Beispiel #14
0
def put_cert(domain, cert):
    curr_domains = Get(GetContext, "all_domains")
    if curr_domains:
        Delete(GetContext, "all_domains")
        curr_domains = concat(curr_domains, ",")
        new_curr_domains = concat(curr_domains, domain)
        Put(GetContext, "all_domains", new_curr_domains)
    else:
        Put(GetContext, "all_domains", domain)
    Put(GetContext, domain, cert)
    return "ok" 
Beispiel #15
0
def transfer_token(address_from, address_to, amount):
    """
    Transfer the specified amount of LOOT from an address, to an address.

    :param address_from:str The address sending the LOOT.
    :param address_to:str The address receiving the LOOT.
    :param amount:int The amount of LOOT being sent.
    :return:
        bool: Whether the transfer of LOOT was successful.
    """
    context = GetContext()

    # The amount being transferred must be > 0.
    if amount <= 0:
        return False

    # If the address is sending LOOT to itself, save on gas and return True.
    if address_from == address_to:
        return True

    # If the balance of the address sending the LOOT does not have enough, return False.
    balance_from = balance_of(address_from)
    if balance_from < amount:
        return False

    # Subtract the amount from the address sending the LOOT and save it to storage.
    balance_from -= amount
    Delete(context, address_from)
    Put(context, address_from, balance_from)

    # Add the LOOT to the address receiving the tokens and save it to storage.
    balance_to = balance_of(address_to)
    balance_to += amount
    Delete(context, address_to)
    Put(context, address_to, balance_to)

    # Dispatch the transfer event.
    # This event causes the "buy_offer" method to not work with the API.
    # OnTransfer(address_from, address_to, amount)

    return True
Beispiel #16
0
def Review(milestone_key, review_score):
    """
    Method to signal result by SC owner or oracle

    :param milestone_key: the key of the milestone
    :type milestone_key: bytearray

    :param review_score: score that the reviewer assigned to this milestone
    :type review_score: int

    :return: whether a pay out to the assignee is done
    :rtype: bool
    """
    # Check if the method is triggered by the SC owner or oracle
    context = GetContext()
    milestone_data_serialized = Get(context, milestone_key)
    milestone_data = deserialize_bytearray(milestone_data_serialized)
    oracle = milestone_data[6]

    if not CheckWitness(OWNER) and not CheckWitness(oracle):
        Notify("Must be SC owner or oracle to submit review")
        return False

    status = milestone_data[11]

    if not status == 'initialized':
        Notify("Contract has incorrect status to do a review")
        return False

    elif status == 'refunded':
        Notify("Contract is already refunded")
        return False

    milestone_data[11] = 'reviewed'
    milestone_data[12] = review_score
    assignee = milestone_data[2]
    threshold = milestone_data[10]
    pay_out = milestone_data[7]

    # Update storage
    Delete(context, milestone_key)
    milestone_data_serialized = serialize_array(milestone_data)
    Put(context, milestone_key, milestone_data_serialized)
    DispatchReviewEvent(milestone_key, review_score)

    if review_score >= threshold:
        Notify("Review score was above threshold, processing pay out")
        DoTransfer(OWNER, assignee, pay_out)
        DispatchTransferEvent(OWNER, assignee, pay_out)

    return True
Beispiel #17
0
def spend(context, amount, src, dest, tag):
    context = GetContext()
    org_tag = concat(src, tag)
    total = Get(context, org_tag)
    if not total:
        return False
    if amount > total:
        return False
    Delete(context, org_tag)
    new_total = total - amount
    Put(context, org_tag, new_total)
    receiver = GetExecutingScriptHash()
    OnSpend(dest, amount, receiver)
    return True
Beispiel #18
0
def SetPendingWithdrawal(account, value):

    context = GetContext()

    account_has_pending = concat(account, 'pending')

    if value == 0:

        Delete(context, account_has_pending)

    else:

        Put(context, account_has_pending, value)

    return True
Beispiel #19
0
def DeleteRenter(renter_wallet_address):
    msg = concat("DeleteRenter: ", renter_wallet_address)
    Notify(msg)

    context = GetContext()
    owner = Get(context, renter_wallet_address)
    if not owner:
        Notify("Domain is not yet registered")
        return False

    if not CheckWitness(owner):
        Notify("Sender is not the owner, cannot transfer")
        return False

    Delete(context, renter_wallet_address)
    return True
Beispiel #20
0
def DeleteDomain(domain_name):
    msg = concat("DeleteDomain: ", domain_name)
    Notify(msg)

    context = GetContext()
    owner = Get(context, domain_name)
    if not owner:
        Notify("Domain is not yet registered")
        return False

    if not CheckWitness(owner):
        Notify("Sender is not the owner, cannot transfer")
        return False

    Delete(context, domain_name)
    return True
Beispiel #21
0
def Refund(milestone_key, fee_refund):
    """
    Method for the contract owner to refund payments

    :param milestone_key: milestone_key
    :type milestone_key: bytearray

    :param fee_refund: fee_refund
    :type fee_refund: bool

    :return: whether the refund was successful
    :rtype: bool

    """
    if not CheckWitness(OWNER):
        Notify("Must be owner to do a refund to all")
        return False

    context = GetContext()
    milestone_data_serialized = Get(context, milestone_key)
    milestone_data = deserialize_bytearray(milestone_data_serialized)
    customer = milestone_data[1]
    status = milestone_data[11]
    pay_out = milestone_data[7]
    fee = milestone_data[9]

    if status == 'refunded':
        Notify("A refund already took place")
        return False

    # Perform refund
    if fee_refund:
        refund_amount = pay_out + fee
    else:
        refund_amount = pay_out

    DoTransfer(OWNER, customer, refund_amount)
    DispatchTransferEvent(OWNER, customer, refund_amount)

    milestone_data[11] = 'refunded'
    milestone_data_serialized = serialize_array(milestone_data)
    Delete(context, milestone_key)
    Put(context, milestone_key, milestone_data_serialized)
    DispatchRefundEvent(milestone_key)

    return True
Beispiel #22
0
def DeleteBuyer(Buyer_name):
    msg = concat("DeleteBuyer: ", Buyer_name)
    Notify(msg)

    context = GetContext()
    owner = Get(context, Buyer_name)
    if not owner:
        Notify("Buyer is not yet registered")
        return False

    if not CheckWitness(owner):
        Notify(
            "This person is not the owner of the name, the Buyer cannot be deleted"
        )
        return False

    Delete(context, Buyer_name)
    return True
Beispiel #23
0
def DeleteFarm(Farm_name):
    msg = concat("DeleteFarm: ", Farm_name)
    Notify(msg)

    context = GetContext()
    owner = Get(context, Farm_name)
    if not owner:
        Notify("Farm is not yet registered")
        return False

    if not CheckWitness(owner):
        Notify(
            "This person is not the owner of the farm, the farm cannot be deleted"
        )
        return False

    Delete(context, Farm_name)
    return True
Beispiel #24
0
def save_inventory(marketplace, address, inventory):
    """
    Helper method for inventory operations, saves a serialized list of items to storage.

    :param marketplace:str The name of the marketplace to access.
    :param address:str The address of the inventory to save.
    :param inventory:bytearray A serialized list containing the items an address owns.
    :return:
        bool: Whether the operation completed.
    """
    context = GetContext()

    # Concatenate the specific storage key, delete the old storage
    # and add the updated inventory into storage.
    inventory_marketplace_key = concat(inventory_key, marketplace)
    storage_key = concat(inventory_marketplace_key, address)
    Delete(context, storage_key)
    Put(context, storage_key, inventory)

    return True
Beispiel #25
0
def increment_cln(product_id):
    """
    :param product_id:
    :return: bool
    """
    if not is_owner(product_id):
        return False

    product = init_product(product_id)

    # increment the cln
    current_cln = product.cln
    new_cln = current_cln + 1
    all_product_data = get_all_product(product)

    all_product_data[7] = new_cln

    Delete(GetContext, product_id)
    Put(GetContext, product_id, all_product_data)
    return True
Beispiel #26
0
def Main(operation, args):
    if operation == 'Query':
        domain = args[0]
        return Query(domain)

    if operation == 'Register':
        domain = args[0]
        owner = args[1]
        return Register(domain, owner)

    if operation == 'Transfer':
        domain = args[0]
        to = args[1]
        return Transfer(domain, to)

    if operation == 'Delete':
        domain = args[0]
        return Delete(domain)

    return False
Beispiel #27
0
def DeletePromo(promo_id):
    """
    Delete promo identified by promo_id

    Args:
        promo_id (str): promo unique id

    Returns:
        (bool): True if promo deleted successfully
    """

    expired = IsPromoExpired(promo_id)
    if expired:
        Log('Promo has already finished, can no longer delete it!')
        return False

    context = GetContext()
    Delete(context, promo_id)  # delete promo_exists

    return True
Beispiel #28
0
def testAll():
    Put(GetContext, 'original', 1)

    amount = Get(GetContext, 'original')
    amount += 20

    Put(GetContext, 'original', amount)
    Put(GetContext, 'copy', amount)

    Delete(GetContext, 'copy')

    copy = Get(GetContext, 'copy')
    original = Get(GetContext, 'original')

    Put(GetContext, 'storeString', 'this is a string')
    storeString = Get(GetContext, 'storeString')

    test = [original, copy, amount, storeString]
    Notify(test)

    return original
Beispiel #29
0
def donate(context, dest_tag, dest_tag_users, dest_tag_src, src, amount):
    total = Get(context, dest_tag)
    # This destination + tag already have donations
    if total:
        new_total = total + amount
        Put(context, dest_tag, new_total)
        donors = Get(context, dest_tag_users)
        # Donor has already donated previously
        if contains(donors, src):
            amount_donated = Get(context, dest_tag_src)
            amount_donated = amount_donated + amount
        # New donor, need to add to list
        else:
            donors = concat(donors, ",")
            donors = concat(donors, src)
            Delete(context, dest_tag_users)
            Put(context, dest_tag_users, donors)
            Put(context, dest_tag_src, amount)
    # First donation to this destination + tag
    else:
        Put(context, dest_tag, amount)
        Put(context, dest_tag_users, src)
        Put(context, dest_tag_src, amount)
Beispiel #30
0
def transfer_license(license_id, new_owner):
    """
    transfer license from one user to the other

    :param license_id:
    :param new_owner:
    :return: bool
    """
    if not is_owner(license_id):
        return False

    license = init_license(license_id)

    product_id = license.product_id
    product = init_product(product_id)
    transferable = product.trans

    # check if the license is still valid
    license_expiration_height = license.expir
    height = GetHeight()

    if height > license_expiration_height:
        print("This license has already expired!")
        return False

    if not transferable:
        print("This product does not support transfers")
        return False

    Delete(GetContext, license_id)

    all_license_data = get_all_license_data(license)

    all_license_data[0] = new_owner
    Put(GetContext, license_id, all_license_data)
    return True