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 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 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 CanWithdrawNeo(): print("[can withdraw Neo]") tx = GetScriptContainer() context = GetContext() for output in tx.Outputs: shash = GetScriptHash(output) output_asset_id = GetAssetId(output) if output_asset_id == NEO_ASSET_ID: print("Asset is Neo") else: return False return True saved_timestamp = Get(context, "timestamp") Notify("saved_timestamp: ", saved_timestamp) current_timestamp = GetHeight() Notify("current_timestamp: ", current_timestamp) return current_timestamp > (saved_timestamp + 4)
def isChallenged(self, ID: bytes): ctx = GetContext() keyFrontRunnerSetTimestamp = concat(b'FrontRunnerSetTimestamp', ID) frontRunnerSetTimestamp = Get(ctx, keyFrontRunnerSetTimestamp) isChallenged = (frontRunnerSetTimestamp != 0) return isChallenged
def _TimeHasExpired(): context = GetContext() currentHeight = GetHeight() print("got current height") currentHeader = GetHeader(currentHeight) print("got current block...") endTime = Get(context, 'endTime') return endTime < currentHeader.Timestamp
def GetCorrectOracleCountForInstance(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_correct_count) context = GetContext() v = Get(context, key) return v
def CreateNewGame(client_hash, game_type): if isGameTypeLive(game_type): return "Game is Already Live" else: key = concat(key_prefix_game_type, game_type) context = GetContext() Put(context, key, client_hash) return "Success"
def isGameTypeLive(game_type): key = concat(key_prefix_game_type, game_type) context = GetContext() v = Get(context, key) if v == 0: return False else: return True
def Register(domain, owner): context = GetContext() occupy = Get(context, domain) if occupy != None: return False Put(context, domain, owner) Push('Register', domain, owner) return True
def DeletePendingWithdrawal(account): context = GetContext() account_pending = concat(account, 'pending') Delete(context, account_pending) return True
def HasVINHold(txid, index): context = GetContext() hold_id = concat(txid, index) item = Get(context, hold_id) return item
def GetPrediction(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_prediction) context = GetContext() v = Get(context, key) return v
def challengeOutcome(self, ID: bytes, newOutcome: int, challenger: bytes, contractAddress: bytes): ctx = GetContext() #check witness ischallenger = CheckWitness(challenger) if not ischallenger: #print('Must be challenger to challenge') return False #check if outcome is set isSet = self.isOutcomeSet(ID) if not isSet: #print('Outcome is not set yet') return False #check if is already challenged isChallenged = self.isChallenged(ID) if isChallenged: #print('Oracle is already be challenged') return False #check if challengePeriod over isChallengePeriodOver = self.isChallengePeriodOver(ID) if isChallengePeriodOver: #print('Challenge period is over') return False challengeAmount = self.getChallengeAmount(ID) #transfer challengeAmount to this contract account address #deposit isok = self.deposit(challenger, contractAddress, challengeAmount) if not isok: #print('Deposit Failed') return False keyOutcomeAmountT = concat(challenger, ID) keyOutcomeAmount = concat(newOutcome, keyOutcomeAmountT) Put(ctx, keyOutcomeAmount, challengeAmount) keyTotalOutcomeAmounts = concat(newOutcome, ID) Put(ctx, keyTotalOutcomeAmounts, challengeAmount) keyTotalAmount = concat(b'TotalAmount', ID) totalAmount = Get(ctx, keyTotalAmount) totalAmount = totalAmount + challengeAmount Put(ctx, keyTotalAmount, totalAmount) keyFrontRunner = concat(b'FrontRunner', ID) Put(ctx, keyFrontRunner, newOutcome) now = self.now() keyFrontRunnerSetTimestamp = concat(b'FrontRunnerSetTimestamp', ID) Put(ctx, keyFrontRunnerSetTimestamp, now) return True
def CheckHasPendingWithdrawal(account): context = GetContext() account_has_pending = concat(account, 'pending') pending = Get(context, account_has_pending) return pending
def Delete(domain): context = GetContext() occupy = Get(context, domain) if occupy != None: return False # Put(context, domain, owner) Push('Delete', domain) return True
def PlaceVINHold(account, txid, index): context = GetContext() hold_id = concat(index, txid) Put(context, hold_id, account) return True
def GetPendingWithdrawal(account): context = GetContext() account_pending = concat(account, 'pending') result = Get(context, account_pending) return result
def get_url_info(code): """ Returns all the information available for the considered code. """ context = GetContext() contextkey_for_url = get_contextkey_for_url(code) contextkey_for_sender = get_contextkey_for_sender(code) url = Get(context, contextkey_for_url) sender = Get(context, contextkey_for_sender) result = [url, sender] return result
def get_circulation(): """ Returns the amount of tokens in circulation. :return: int: The total number of tokens that are in circulation. """ context = GetContext() return Get(context,in_circulation_key)
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
def Query(domain): context = GetContext() owner = Get(context, domain) Push('query', domain) if owner != None: return False return owner
def CreateNewGame(client_hash, game_type): if client_hash.isGameTypeLive(game_type): return "Game is Already Live" else: client_hash=Game(client_hash,Game_rules.starting_timestamp) key = concat(key_prefix_game_type,game_type) context = GetContext() Put(context, key, client_hash) return "Success"
def RegisterPrediction(game_type, instance_ts, oracle, prediction): k1 = concat(key_prefix_game_type, game_type) k2 = concat(key_prefix_game_instance, instance_ts) k12 = concat(k1, k2) k3 = concat(key_prefix_game_instance_oracle, oracle) k123 = concat(k12, k3) key = concat(k123, key_prefix_game_instance_prediction) context = GetContext() Put(context, key, prediction)
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
def do_fetch(): """ Fetch current list value from storage. :return: the stored list value :rtype: list """ context = GetContext() list_bytes = Get(context, KEY) return deserialize_bytearray(list_bytes)
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
def GetOraclePrediction(game_type, instance_ts, oracle): k1 = concat(key_prefix_game_type, game_type) k2 = concat(key_prefix_game_instance, instance_ts) k12 = concat(k1, k2) k3 = concat(key_prefix_game_instance_oracle, oracle) k123 = concat(k12, k3) key = concat(k123, key_prefix_game_instance_prediction) context = GetContext() v = Get(context, key) return v
def GetOracleAtIndexN(game_type, instance_ts, index): k1 = concat(key_prefix_game_type, game_type) k2 = concat(key_prefix_game_instance, instance_ts) k3 = concat(key_prefix_game_instance_index, index) k12 = concat(k1, k2) key = concat(k12, k3) # This registers the Oracle in the nth slot context = GetContext() v = Get(context, key) return v
def RegisterOracle(game_type, instance_ts, oracle, slot_n): k1 = concat(key_prefix_game_type, game_type) k2 = concat(key_prefix_game_instance, instance_ts) k3 = concat(key_prefix_game_instance_index, slot_n) k12 = concat(k1, k2) key = concat(k12, k3) # This registers the Oracle in the nth slot context = GetContext() Put(context, key, oracle) k4 = concat(key_prefix_game_instance_oracle, oracle) key = concat(k12, k4) # This registers the Oracle in the Game Instance context = GetContext() Put(context, key, 1) # This updates the counter key = concat(k12, key_prefix_game_instance_count) context = GetContext() Put(context, key, slot_n) return True