Exemple #1
0
def Deploy(provider, location):
    """
    Deploy a new SIP provider to the KRYPTON network

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

    :param location: the (DNS SRV) location of the outbound proxy
    :type location: str

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

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

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

    # Deploy the provider
    if (address == 0):
        Put(context, provider, location)
        Log('DEPLOY_SUCCESS')
        return True

    Log('DEPLOY_FAILED')
    return False
Exemple #2
0
def Deploy():
    """
    This is used to distribute the initial tokens to the owner

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

    """

    if not CheckWitness(OWNER):
        Log("Must be owner to deploy")
        return False

    context = GetContext()
    has_deployed = Get(context, 'initialized')

    if has_deployed == 0:

        # do deploy logic
        Put(context, 'initialized', 1)
        Put(context, OWNER, TOTAL_SUPPLY)

        return True

    Log('Could not deploy')
    return False
def buy_product(buyer_address , p_hash):
    if not CheckWitness(buyer_address):
        Log('FORBIDDEN')
        return False

    Log('CAN_NOT_BUY_THE_PRODECT')
    return False
Exemple #4
0
def Main(alpha, beta: str) -> int:
    """Entry point for the smart contract.

    Args:
        alpha (str):
            UUID used as the first part of the key for Storage.Put().
        beta (str):
            UUID used as the second part of the key for Storage.Put().

    Return:
        (int): status code representing if execution was a success.
    """

    if not IsUUID(alpha):
        Log("'alpha' parameter has an invalid RFC UUID format")
        Log(alpha)
        return 101

    if not IsUUID(beta):
        Log("'beta' parameter has an invalid RFC UUID format")
        Log(beta)
        return 102

    context = GetContext()
    storageKey = GenerateStorageKey(alpha, beta)
    
    transactionHash = GetTransactionHash()
    if len(transactionHash) == 0:
        Log("Transaction hash has a length of 0")
        return 103

    Put(context, storageKey, transactionHash)

    return 200
Exemple #5
0
def JudgeInstance(game_type, instance_ts):
    if isGameInstanceJudged(game_type, instance_ts):
        return "Already Judged"
    # Separate Winners from Losers
    correct_prediction = GetPrediction(game_type, instance_ts)
    n_oracles_for_instance = GetOracleCountForInstance(game_type, instance_ts)
    n_correct = 0
    total_bounty = 0

    index = 0
    while index < n_oracles_for_instance:
        index = index + 1
        oracle = GetOracleAtIndexN(game_type, instance_ts, index)
        oracle_prediction = GetOraclePrediction(game_type, instance_ts, oracle)
        if oracle_prediction == correct_prediction:
            # Add to Winners
            # collateral is moved from locked into available
            UnlockCollateral(oracle)
            n_correct = n_correct + 1
        else:
            # Add to Losers
            # Both Available and Locked Balance is removed and added to Winner collection
            oracle_available_balance = GetOracleBalance(oracle)
            oracle_locked_balance = GetOracleLockedBalance(oracle)

            total_bounty = total_bounty + oracle_available_balance + oracle_locked_balance
            WipeOutBalances(oracle)

    if n_correct == 0:
        return "Nothing correct"

    bounty_per_correct_oracle = total_bounty // n_correct
    owner_bounty = total_bounty % n_correct
    AddBountyForOwner(owner_bounty)

    Log("n_correct")
    Log(n_correct)

    SetCorrectOracleCountForInstance(game_type, instance_ts, n_correct)

    # Loop again
    index = 0
    while index < n_oracles_for_instance:
        index = index + 1
        oracle = GetOracleAtIndexN(game_type, instance_ts, index)
        oracle_prediction = GetOraclePrediction(game_type, instance_ts, oracle)
        if oracle_prediction == correct_prediction:
            oracle_available_balance = GetOracleBalance(oracle)
            oracle_available_balance = oracle_available_balance + bounty_per_correct_oracle
            UpdateAvailableBalance(oracle, oracle_available_balance)

    sep = "SEPARATOR"
    notification = concat(instance_ts, sep)
    notification = concat(notification, n_correct)
    notification = concat(notification, sep)
    notification = concat(notification, correct_prediction)
    Notify(notification)
    # Set Game to be Judged (no more judging allowed)
    SetGameInstanceJudged(game_type, instance_ts)
    return True
def Main(operation, args):
    Log(args)

    trigger = GetTrigger()

    storage = StorageAPI()

    arg_error = 'Incorrect Arg Length'

    if trigger == Verification():
        # todo maybe implement later
        return True

    elif trigger == Application():
        if operation != None:
            if operation == "balance":
                if len(args) == 1:
                    account = args[0]
                    balance = storage.get(account)
                    return balance
                Log(arg_error)
                return arg_error
            if operation == "accrue":
                if len(args) == 2:
                    account = args[0],
                    gain = args[1]
                    new_balance = storage.get(account) + gain
                    return storage.put(account, new_balance)
                Log(arg_error)
            return arg_error
        return False
Exemple #7
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
Exemple #8
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
Exemple #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
Exemple #10
0
def GetCurrentMax(game_type, instance_ts):
    k1 = concat(key_prefix_game_type, game_type)
    k2 = concat(key_prefix_game_instance, instance_ts)
    k12 = concat(k1, k2)
    key = concat(k12, key_prefix_game_instance_max)
    context = GetContext()
    v = Get(context, key)
    Log(key)
    Log(v)
    return v
Exemple #11
0
def GetKey(key):
    msg = concat("Get key: ", key)
    Log(msg)

    #get key
    context = GetContext()
    data = Get(context, key)
    msg = concat("Data found: ", data)
    Log(msg)
    return data
Exemple #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
Exemple #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
Exemple #14
0
def ResultNotice(agreement_key, weather_param, oracle_cost):
    """
    Method to signal resulte by oracle

    :param agreement_key: the key of the agreement
    :type agreement_key: bytearray

    :param weather_param: weather parameter that the contract is depending on
    :type weather_param: int

    :param oracle_cost: costs made by the oracle to do this assignment
    :type oracle_cost: int

    :return: whether a pay out to the customer is done
    :rtype: bool
    """

    # Check if the method is triggered by the oracle for this agreement
    context = GetContext()
    agreement_data = Get(context, agreement_key)
    oracle = agreement_data[8]

    if not CheckWitness(oracle):
        Log("Must be oracle to notice results")
        return False

    timestamp = agreement_data[3]
    utc_offset = agreement_data[4]
    status = agreement_data[12]

    if not status == 'initialized':
        Log("Contract has incorrect status to do a result notice")
        return False

    agreement_data[12] = 'result-noticed'
    agreement_data[13] = weather_param
    agreement_data[14] = oracle_cost

    # Get timestamp of current block
    currentHeight = GetHeight()
    currentBlock = GetHeader(currentHeight)
    current_time = currentBlock.Timestamp

    Put(context, agreement_key, agreement_data)

    timezone_timestamp = timestamp + (3600 * utc_offset)
    timezone_current_time = current_time + (3600 * utc_offset)

    if timezone_current_time < timezone_timestamp:
        Log("Datetime of result notice is lower than agreed datetime")
        return False
    else:
        DispatchResultNoticeEvent(agreement_key, weather_param, oracle_cost)
        return True
Exemple #15
0
def DoTransferFrom(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:
        return False

    context = GetContext()

    allowance_key = concat(t_from, t_to)

    available_to_to_addr = Get(context, allowance_key)

    if available_to_to_addr < amount:
        Log("Insufficient funds approved")
        return False

    from_balance = Get(context, t_from)

    if from_balance < amount:
        Log("Insufficient tokens in from balance")
        return False

    to_balance = Get(context, t_to)

    # calculate the new balances
    new_from_balance = from_balance - amount
    new_to_balance = to_balance + amount
    new_allowance = available_to_to_addr - amount

    # persist the new balances
    Put(context, allowance_key, new_allowance)
    Put(context, t_to, new_to_balance)
    Put(context, t_from, new_from_balance)

    Log("transfer complete")

    # dispatch transfer event
    OnTransfer(t_from, t_to, amount)

    return True
def do_current_timestamp() -> bytearray:
    Log('do_current_timestamp triggered.')
    current_height = GetHeight()
    Log('current_height:')
    Log(current_height)
    current_block = GetHeader(current_height)
    # Log('current_block:')
    # Log(current_block)
    timestamp = current_block.Timestamp
    Log('timestamp:')
    Log(timestamp)  # Example b'\xc1\xc7|Z'
    return timestamp
def do_get_timestamp(args: list) -> bytearray:
    Log('do_get_timestamp triggered.')
    if len(args) > 0:
        height = args[0]
        Log('height:')
        Log(height)
        header = GetHeader(height)
        timestamp = header.Timestamp
        Log('timestamp:')
        Log(timestamp)
        return timestamp
    Notify('invalid argument length')
    return False
def do_get_next_consensus(args: list) -> bytearray:
    Log('do_get_next_consensus triggered.')
    if len(args) > 0:
        height = args[0]
        Log('height:')
        Log(height)
        header = GetHeader(height)
        next_consensus = header.NextConsensus
        Log('next_consensus:')
        Log(next_consensus)
        return next_consensus
    Notify('invalid argument length')
    return False
def do_get_block_hash(args: list) -> bytearray:
    Log('do_get_block_hash triggered.')
    if len(args) > 0:
        height = args[0]
        Log('height:')
        Log(height)
        header = GetHeader(height)
        # version = GetVersion(header)
        hash_val = GetHash(header)
        Log('hash_val:')
        Log(hash_val)
        return hash_val
    Notify('invalid argument length')
    return False
Exemple #20
0
def own_area(owner_address, lat, lon):
    if not CheckWitness(owner_address):
        Log('FORBIDDEN')
        return False

    address = Get(context, owner_address)
    location = [lat, lon]

    if (address == 0):
        Put(context, owner_address, location)
        Log('owned_success')
        return True

    return False
Exemple #21
0
def Deploy(dapp_name, oracle, time_margin, min_time, max_time):
    """
    Method for the dApp owner initiate settings in storage

    :param dapp_name: name of the dapp
    :type dapp_name: str

    :param oracle: oracle that is used
    :type oracle: bytearray

    :param time_margin: time margin in seconds
    :type time_margin: int

    :param min_time: minimum time until the datetime of the event in seconds
    :type min_time: int

    :param max_time: max_time until the datetime of the event in seconds
    :type max_time: int

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

    if not CheckWitness(OWNER):
        Log("Must be owner to deploy dApp")
        return False

    context = GetContext()
    Put(context, 'dapp_name', dapp_name)
    Put(context, 'oracle', oracle)

    if time_margin < 0:
        Log("time_margin must be positive")
        return False

    Put(context, 'time_margin', time_margin)

    if min_time < 3600 + time_margin:
        Log("min_time must be greater than 3600 + time_margin")
        return False

    Put(context, 'min_time', min_time)

    if max_time <= (min_time + time_margin):
        Log("max_time must be greather than min_time + time_margin")
        return False

    Put(context, 'max_time', max_time)

    return True
Exemple #22
0
def CreateNewGameInstance(client_hash, game_type, instance_ts):
    if isGameInstanceLive(game_type, instance_ts):
        return "Game Instance is Already Live"
    else:
        k1 = concat(key_prefix_game_type, game_type)
        k2 = concat(key_prefix_game_instance, instance_ts)
        key = concat(k1, k2)
        context = GetContext()
        Log(key)
        Put(context, key, client_hash)
        key_hash = concat(key, client_hash)
        Log(key_hash)
        #Notify(key_hash)
    return "Success"
Exemple #23
0
def CheckTimestamp(timestamp_normalised):
    # Checks if TS() > T_n + deadline
    height = GetHeight()
    hdr = GetHeader(height)
    ts = GetTimestamp(hdr)
    Log(ts)
    Log(timestamp_normalised)
    nd = ts - deadline
    Log(nd)
    diff = nd - timestamp_normalised
    Log(diff)
    if nd > timestamp_normalised:
        return True
    return False
Exemple #24
0
def DoApprove(t_owner, t_spender, amount):
    """

    Method by which the owner of an address can approve another address
    ( the spender ) to spend an amount

    :param t_owner: Owner of tokens
    :type bytearray

    :param t_spender: Requestor of tokens
    :type bytearray

    :param amount: Amount requested to be spent by Requestor on behalf of owner
    :type bytearray

    :return: success of the operation
    :rtype: bool

    """

    owner_is_sender = CheckWitness(t_owner)

    if not owner_is_sender:
        Log("Incorrect permission")
        return False

    context = GetContext()

    from_balance = Get(context, t_owner)

    # cannot approve an amount that is
    # currently greater than the from balance
    if from_balance >= amount:

        approval_key = concat(t_owner, t_spender)

        current_approved_balance = Get(context, approval_key)

        new_approved_balance = current_approved_balance + amount

        Put(context, approval_key, new_approved_balance)

        Log("Approved")

        OnApprove(t_owner, t_spender, amount)

        return True

    return False
Exemple #25
0
def Main(operation, args):
    """
    :param operation: str The name of the operation to perform
    :param args: list A list of arguments along with the operation
    """

    if operation == 'GetVersion':
        # very simple method to test the contract
        Log("Invoked GetVersion")
        return BZSVERSION

    # Am I who I say I am? args[0] musth be the wallet hash code
    ontid = args[0]
    authorized = CheckWitness(ontid)
    if not authorized:
        Log("Not Authorized")
        return 'Not Authorized'
    Log(ontid)

    # storage operations
    if operation == 'Store':
        if len(args) < 4:
            return 'Invalid arguments'
        category = args[1]
        id = args[2]
        value = args[3]
        put(category, id, value)
        #Notify(['Store:', category, id])
        return 'OK'

    if operation == 'Retrieve':
        if len(args) < 3:
            return 'Invalid arguments'
        category = args[1]
        id = args[2]
        data = get(category, id)
        #Notify(['Retrieve:', category, id])
        return data

    if operation == 'Delete':
        if len(args) < 3:
            return 'Invalid arguments'
        category = args[1]
        id = args[2]
        delete(category, id)
        #Notify(['Delete:', category, id])
        return 'OK'

    return 'Invalid operation'
Exemple #26
0
def ClaimFunds(promo_id):
    """
    Creator of promo can claim funds from promo_id if the min_count and
    expiration conditions are met. Funds can only be claimed if wallet's public
    key used to invoke matches the public key used in create.

    Args:
        promo_id (str): promo unique id

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

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

    promo_exists = IsPromoExist(promo_id)
    if not promo_exists:
        Log('Promo not found')
        return False

    expired = IsPromoExpired(promo_id)
    if not expired:
        Log('Promo not over yet! Cannot claim funds yet')
        return False

    promo = get_promo_storage_keys(promo_id)

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

    if purchased_count < min_count:
        Log('Not enough tickets were sold by deadline, buyers can claim refund'
            )
        return False

    #
    # Claim funds
    #

    price_per_person = Get(context, promo.price_per_person_key)

    funds_amount = purchased_count * price_per_person
    claim_address = GetCallingScriptHash()
    OnClaim(claim_address, funds_amount)

    return True
Exemple #27
0
def QueryUser(query, user):
    """
    Query a user on the KRYPTON network

    :param query: type of query is UUID or provider
    :type query: str

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

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

    context = GetContext()

    uuid = Get(context, user)

    if (uuid == 0):
        Log('QUERY_USER_FAILED')
        return False

    if (query == "uuid"):
        Notify(uuid)
        return True

    provider = Get(context, uuid)

    if (provider == 0):
        Log('QUERY_PROVIDER_FAILED')
        return False

    if (query == "provider"):
        Notify(provider)
        return True

    location = Get(context, provider)

    if (location == 0):
        Log('QUERY_LOCATION_FAILED')
        return False

    if (query == "location"):
        Notify(location)
        return True

    # User or data not found
    Log('QUERY_FAILED')
    return False
def own_area(owner_address, lat, lon):
    if not CheckWitness(owner_address):
        Log('ALREADY')
        return False
    context = GetContext()
    address = Get(context, owner_address)
    obj = [lat, lon]
    location = _data_packing(obj)

    if (address == 0):
        Put(context, owner_address, location)
        Log('owned_success')
        return True

    return False
Exemple #29
0
def CheckTiming(timestamp_normalised):
    # Check T_n relative to current TS()
    height = GetHeight()
    hdr = GetHeader(height)
    ts = GetTimestamp(hdr)
    Log(ts)
    t_n_plus_one = timestamp_normalised + Game_rules.timestep
    Log(t_n_plus_one)
    Log(timestamp_normalised)
    if ts > t_n_plus_one:
        return 1 # expired
    elif ts < timestamp_normalised:
        return 2 # too early to submit, ignore
    else:
        return 0 # all good
Exemple #30
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