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
def TransferFromPool(addr_to, amount): print("********TransferFromPool()") if amount <= 0: return False if len(addr_to) != 20: return False # only the admin can transfer from pool if not CheckWitness(ADMIN): print("No privilege!") return False context = GetContext() balance_pool = Get(context, "balance/pool") if balance_pool < amount: print("Insufficient balance in pool!") return False balance_pool -= amount Put(context, "balance/pool", balance_pool) key_to = concat("balance/", addr_to) balance_to = Get(context, key_to) + amount Put(context, key_to, balance_to) OnTransfer(0, addr_to, amount) return True
def Transfer(addr_from, addr_to, amount): print("********Transfer()") if amount <= 0: print("Invalid amount!") return False if len(addr_to) != 20: print("Invalid addr_to!") return False if not CheckWitness(addr_from): print("No privilege!") return False context = GetContext() key_from = concat("balance/", addr_from) balance_from = Get(context, key_from) if balance_from < amount: print("Insufficient balance!") return False key_to = concat("balance/", addr_to) balance_to = Get(context, key_to) balance_from -= amount balance_to += amount Put(context, key_from, balance_from) Put(context, key_to, balance_to) OnTransfer(addr_from, addr_to, amount) return True
def do_approve(self, storage: StorageAPI, t_owner, t_spender, amount): if not CheckWitness(t_owner): print("Incorrect permission") return False if amount < 0: print("Negative amount") return False from_balance = storage.get(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) if amount == 0: storage.delete(approval_key) else: storage.put(approval_key, amount) OnApprove(t_owner, t_spender, amount) return True return False
def Main(operation, args): trigger = GetTrigger() if trigger == Verification(): if CheckWitness(owner): return True return False storage = StorageAPI() questions = Questions() questions.owner = owner if operation == 'get_clue': addr = get_addr(clue_gas_req) cluenum = args[0] return questions.get_clue(storage, cluenum, addr) elif operation == 'set_clue': cluenum = args[0] clueval = args[1] return questions.set_clue(storage, cluenum, clueval) elif operation == 'set_answer': cluenum = args[0] answer = args[1] return questions.set_answer(storage, cluenum, answer) elif operation == 'submit_answer': addr = get_addr(answer_gas_req) if len(args) < 2: return b'Not enough arguments' cluenum = args[0] answer = args[1] return questions.submit_answer(storage, cluenum, answer, addr) elif operation == 'total_questions': return total_q elif operation == 'progress': addr = get_addr(progress_gas_req) key = concat('progress_', addr) return storage.getitem(key) elif operation == 'total_winners': return storage.getitem('total_winners') elif operation == 'first_place': return storage.getitem('first_place') elif operation == 'second_place': return storage.getitem('second_place') elif operation == 'third_place': return storage.getitem('third_place') return b'Method not found'
def remove_item(marketplace, address, item_id): """ Remove an item from an address on a marketplace. :param marketplace:str The name of the marketplace to access. :param address:str The address to remove the item from. :param item_id:int The id of the item to remove from the address. :return: bool: Whether the item is removed. """ # Check marketplace permissions. owner = marketplace_owner(marketplace) if not CheckWitness(owner): print("Operation Forbidden: Only the owner of this marketplace may invoke the operation - remove_item") return False # Get the address's inventory. inventory_s = get_inventory(marketplace, address) inventory = deserialize_bytearray(inventory_s) current_index = 0 # TODO: Remove manually searching for the index once list method "indexOf" is added. for item in inventory: # If the player has the item, we can remove the item at the current index, # save the modified inventory to storage and return True. if item == item_id: inventory.remove(current_index) inventory_s = serialize_array(inventory) save_inventory(marketplace, address, inventory_s) return True current_index += 1 return False
def transfer_item(marketplace, address_from, address_to, item_id): """ Transfer an item from an address, to an address on a marketplace. :param marketplace:str The name of the marketplace to access. :param address_to:str The address receiving the item. :param address_from:str The address sending the item. :param item_id:int The id of the item being sent. :return: bool:Whether the transfer of the item was successful. """ # Check marketplace permissions. owner = marketplace_owner(marketplace) if not CheckWitness(owner): print("Operation Forbidden: Only the owner of this marketplace may invoke the operation - transfer_item") return False # If the item is being transferred to the same address, don't waste gas and return True. if address_from == address_to: return True # If the removal of the item from the address sending is successful, give the item to the address receiving. if remove_item(marketplace, address_from, item_id): # Give the item to the address receiving and return True. args = [marketplace, address_to, item_id] give_items(args) return True return False
def Deploy(): """ Method for the NEP5 Token owner to use in order to deploy an initial amount of tokens to their own address :return: whether the deploy was successful :rtype: bool """ print("deploying!") isowner = CheckWitness(OWNER) if isowner: print("ok to deploy") context = GetContext() total = Get(context, 'totalSupply') if len(total) == 0: Log("WILL DEPLOY!") Put(context, OWNER, PRE_ICO_CAP) Put(context, "totalSupply", PRE_ICO_CAP) OnTransfer(0, OWNER, PRE_ICO_CAP) return True else: print("ALREADY DEPLOYED, wont do it again") print("only owner can deploy") return False
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
def cancel_change_owner(token: Token): """ Cancel a pending ownership transfer request :param token: Token The token to cancel the ownership transfer for :return: bool: Whether the operation was successful """ storage = StorageAPI() new_owner = storage.get(token.new_owner_key) if not new_owner: print( "Can't cancel_change_owner unless an owner change is already pending" ) return False owner = storage.get(token.owner_key) if not CheckWitness(owner): print("Must be owner to cancel change_owner") return False # delete the new owner to cancel the transfer. storage.delete(token.new_owner_key) return True
def PostGeolocation(addr, geolocation, timestamp): print("********PostGeolocation") if len(addr) != 20: print("Invalid address!") return False if not CheckWitness(addr): print("No privilege!") return False context = GetContext() b = Get(context, "block/NRC") key = concat(b, "/cnt") cnt = Get(context, key) + 1 Put(context, key, cnt) key = Concat4(b, "/", cnt, "/ts") Put(context, key, timestamp) key = Concat4(b, "/", cnt, "/addr") Put(context, key, addr) key = Concat4(b, "/", cnt, "/geo") Put(context, key, geolocation) return True
def ValidateOwner(self): isOwner = CheckWitness(self.OWNER) if isOwner == False: Log("Sender is not the owner") return False return True
def Main(operation, args): """ Main for SC :param operation: The function performed :type operation: str :param args: list of arguments args[0] always sender hash :param type: str """ user = args[0] authorized = CheckWitness(user) if not authorized: Notify("Not authorized") return False if operation != None: # Requires: user, postHash, category if operation == 'submitPost' and len(args) == 3: submitPost(args) # Requires: user, postHash, category - optionally more categories # if operation == 'addPostToCategory' and len(args) >= 3: # addPostToCategory(args) # Requires: user, userName if operation == 'manageUser' and len(args) == 3: manageUser(args) return False
def do_approve(self, storage: StorageAPI, t_owner, t_spender, amount): if not CheckWitness(t_owner): print("Incorrect permission") return False from_balance = storage.get(t_owner) # Kann keinen Betrag bestätigen der momentan # größer ist als der von "from balance" if from_balance >= amount: approval_key = concat(t_owner, t_spender) current_approved_balance = storage.get(approval_key) new_approved_balance = current_approved_balance + amount storage.put(approval_key, new_approved_balance) OnApprove(t_owner, t_spender, amount) return True return False
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
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
def do_approve(self, storage: StorageAPI, t_owner, t_spender, amount): if not CheckWitness(t_owner): print("Incorrect permission") return False from_balance = storage.get(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 = storage.get(approval_key) new_approved_balance = current_approved_balance + amount storage.put(approval_key, new_approved_balance) OnApprove(t_owner, t_spender, amount) return True return False
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 Main(a, b, c, d): """ :param a: :param b: :param c: :param d: :return: """ print("Executing Four Param Verification code") trigger = GetTrigger() if trigger == Verification(): print("Running verification") is_owner = CheckWitness(OWNER) Notify(is_owner) return is_owner print("runnig app routine") result = a + b - c + d Notify(result) return result
def buy_product(buyer_address , p_hash): if not CheckWitness(buyer_address): Log('FORBIDDEN') return False Log('CAN_NOT_BUY_THE_PRODECT') return False
def kyc_deregister(self, args, token: Token): """ :param args:list a list of addresses to deregister :param token: Token A token object with your ICO settings :return: int: The number of addresses deregistered from KYC """ ok_count = 0 storage = StorageAPI() owner = storage.get(token.owner_key) if CheckWitness(owner): for address in args: if len(address) == 20: kyc_storage_key = concat(self.kyc_key, address) storage.delete(kyc_storage_key) OnKYCDeregister(address) ok_count += 1 return ok_count
def Main(arr): """ :return: Whether the invoker is defined as owner of this contract :rtype: bool """ is_owner = CheckWitness(OWNER) # Built-in CheckWitness() function able to determine whether the contract invoker matches to the provided address in byte array return is_owner
def kyc_register(self, args, token:Token): """ :param args:List Eine Liste der Adressen die zu registrieren sind :param token:Token Ein Token Objekt mit Ihren ICO Einstellungen :return: int: Die Nummer der Adressen die mit KYC registriert werden """ ok_count = 0 if CheckWitness(token.owner): for address in args: if len(address) == 20: storage = StorageAPI() kyc_storage_key = concat(self.kyc_key, address) storage.put(kyc_storage_key, True) OnKYCRegister(address) ok_count += 1 return ok_count
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
def kyc_register(self, args, token: Token): """ :param args:list a list of addresses to register :param token:Token A token object with your ICO settings :return: int: The number of addresses to register for KYC """ ok_count = 0 if CheckWitness(token.owner): for address in args: if len(address) == 20: storage = StorageAPI() kyc_storage_key = concat(self.kyc_key, address) storage.put(kyc_storage_key, True) OnKYCRegister(address) ok_count += 1 return ok_count
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
def Main(): Owner = b'031a6c6fbbdf02ca351745fa86b9ba5a9452d785ac4f7fc2b7548ca2a46c4fcf4a' result = CheckWitness(Owner) if result: print("Owner is caller") return True return False
def set_owner(context, to): previous = owner(context) if not previous: previous = to if not CheckWitness(previous): return "Only current owner can transfer ownership" Put(context, "owner", to) return True
def plot_transfer(context, x, y, to): key = plot_owner_key(x, y) owner = plot_owner(context, x, y) if owner and not CheckWitness(owner): return "You are not allowed to transfer this plot" Put(context, key, to) OnPlotTransfer(owner, to, x, y) return True
def Deploy(): print('checking if its the owner') isowner = CheckWitness(OWNER) if isowner: print('it is the owner') time = SetTime() return time return 0